Updated to support heredocs

This commit is contained in:
Maieul BOYER 2024-09-04 16:43:14 +00:00
parent 92823aff80
commit 980cae5597
5 changed files with 42 additions and 202 deletions

View file

@ -11,67 +11,34 @@
/* ************************************************************************** */
#include "app/node.h"
#include "ast/_from_node.h"
#include "ast/ast.h"
#include "gmr/symbols.h"
#include "gmr/field_identifiers.h"
#include "me/str/str.h"
#include "me/types.h"
#include "parser/api.h"
#include <stdio.h>
t_error build_sym_heredoc_body(t_parse_node self, t_const_str input, \
t_ast_node *out)
{
printf("This function is not done '%s'", __func__);
(void)(self);
(void)(input);
(void)(out);
return (ERROR);
}
t_error build_sym_heredoc_content(t_parse_node self, t_const_str input, \
t_ast_node *out)
{
printf("This function is not done '%s'", __func__);
(void)(self);
(void)(input);
(void)(out);
return (ERROR);
}
t_error build_sym_heredoc_end(t_parse_node self, t_const_str input, \
t_ast_node *out)
{
printf("This function is not done '%s'", __func__);
(void)(self);
(void)(input);
(void)(out);
return (ERROR);
}
t_error build_sym_heredoc_redirect(t_parse_node self, t_const_str input, \
t_ast_node *out)
{
printf("This function is not done '%s'", __func__);
(void)(self);
(void)(input);
(void)(out);
return (ERROR);
}
t_ast_node ret;
t_usize i;
t_error build_sym_heredoc_start(t_parse_node self, t_const_str input, \
t_ast_node *out)
{
printf("This function is not done '%s'", __func__);
(void)(self);
(void)(input);
(void)(out);
return (ERROR);
}
t_error build_sym_simple_heredoc_body(t_parse_node self, t_const_str input, \
t_ast_node *out)
{
printf("This function is not done '%s'", __func__);
(void)(self);
(void)(input);
(void)(out);
return (ERROR);
if (ts_node_symbol(self) != sym_heredoc_redirect || input == NULL || out == NULL)
return (ERROR);
ret = ast_alloc(AST_HEREDOC_REDIRECTION);
i = 0;
while (i < ts_node_child_count(self))
{
if (ts_node_field_id_for_child(self, i) == field_op)
ret->data.heredoc_redirection.op = _get_redirection_op(ts_node_child(self, i));
else if (ts_node_symbol(ts_node_child(self, i)) == sym_heredoc_start)
ret->data.heredoc_redirection.delimiter = _extract_str(ts_node_child(self, i), input);
else if (ts_node_symbol(ts_node_child(self, i)) == sym_heredoc_body)
ret->data.heredoc_redirection.content = _extract_str(ts_node_child(self, i), input);
i++;
}
return (*out = ret, NO_ERROR);
}

View file

@ -29,7 +29,7 @@ void ast_set_term(t_ast_node *node, t_ast_terminator_kind term)
t_ast_node val;
if (node == NULL)
return ((void)printf("node == NULL\n"));
return ;
val = *node;
ptr = &void_storage;
if (val->kind == AST_CASE)
@ -45,9 +45,6 @@ void ast_set_term(t_ast_node *node, t_ast_terminator_kind term)
if (val->kind == AST_SUBSHELL)
ptr = &val->data.subshell.term;
*ptr = term;
if (ptr == &void_storage)
printf("node wasn't a term capable node\n");
(void)(void_storage);
}
t_error build_sym_if_statement(\

View file

@ -19,58 +19,6 @@
#include "me/vec/vec_ast.h"
#include "parser/api.h"
#include <stdio.h>
/*
sym_arithmetic_binary_expression
sym_arithmetic_expansion
sym_arithmetic_literal
sym_arithmetic_parenthesized_expression
sym_arithmetic_postfix_expression
sym_arithmetic_ternary_expression
sym_arithmetic_unary_expression
sym_case_item
sym_case_statement
sym_command
sym_command_name
sym_command_substitution
sym_comment
sym_compound_statement
sym_concatenation
sym_do_group
sym_elif_clause
sym_else_clause
sym_expansion
sym_expansion_expression
sym_expansion_regex
sym_extglob_pattern
sym_file_descriptor
sym_file_redirect
sym_for_statement
sym_function_definition
sym_heredoc_body
sym_heredoc_content
sym_heredoc_end
sym_heredoc_redirect
sym_heredoc_start
sym_if_statement
sym_list
sym_negated_command
sym_number
sym_pipeline
sym_program
sym_raw_string
sym_redirected_statement
sym_regex
sym_simplex _expansion
sym_simple_heredoc_body
sym_string
sym_string_content
sym_subshell
sym_variable_assignment
sym_variable_assignments
sym_variable_name
sym_while_statement
sym_word
*/
/*
E_OP_NONE = 0, // ${var}
@ -193,79 +141,6 @@ t_error build_sym_list(t_parse_node self, t_const_str input, t_ast_node *out)
return (*out = ret, NO_ERROR);
}
/*
alias_sym_statements
alias_sym_terminator
sym___error_recovery
sym__arithmetic_expression
sym__bare_dollar
sym__case_item_last
sym__comment_word
sym__concat
sym__concatenation_in_expansion
sym__empty_value
sym__expansion_body
sym__expansion_word
sym__extglob_blob
sym__heredoc_body
sym__heredoc_body_beginning
sym__heredoc_expression
sym__heredoc_pipeline
sym__immediate_double_hash
sym__simple_heredoc_body
sym__statement_not_pipeline
sym__statements
sym_arithmetic_binary_expression
sym_arithmetic_expansion
sym_arithmetic_literal
sym_arithmetic_parenthesized_expression
sym_arithmetic_postfix_expression
sym_arithmetic_ternary_expression
sym_arithmetic_unary_expression
sym_case_item
sym_case_statement
sym_command
sym_command_name
sym_command_substitution
sym_comment
sym_compound_statement
sym_concatenation
sym_do_group
sym_elif_clause
sym_else_clause
sym_expansion
sym_expansion_expression
sym_expansion_regex
sym_extglob_pattern
sym_file_descriptor
sym_file_redirect
sym_for_statement
sym_function_definition
sym_heredoc_body
sym_heredoc_content
sym_heredoc_end
sym_heredoc_redirect
sym_heredoc_start
sym_if_statement
sym_list
sym_negated_command
sym_number
sym_pipeline
sym_program
sym_raw_string
sym_redirected_statement
sym_regex
sym_simple_expansion
sym_simple_heredoc_body
sym_string
sym_string_content
sym_subshell
sym_variable_assignment
sym_variable_assignments
sym_variable_name
sym_while_statement
sym_word
*/
t_error ast_from_node(\
t_parse_node node, t_const_str input, t_ast_node *out)
@ -321,16 +196,8 @@ t_error ast_from_node(\
return (build_sym_for_statement(node, input, out));
if (ts_node_symbol(node) == sym_function_definition)
return (build_sym_function_definition(node, input, out));
if (ts_node_symbol(node) == sym_heredoc_body)
return (build_sym_heredoc_body(node, input, out));
if (ts_node_symbol(node) == sym_heredoc_content)
return (build_sym_heredoc_content(node, input, out));
if (ts_node_symbol(node) == sym_heredoc_end)
return (build_sym_heredoc_end(node, input, out));
if (ts_node_symbol(node) == sym_heredoc_redirect)
return (build_sym_heredoc_redirect(node, input, out));
if (ts_node_symbol(node) == sym_heredoc_start)
return (build_sym_heredoc_start(node, input, out));
if (ts_node_symbol(node) == sym_if_statement)
return (build_sym_if_statement(node, input, out));
if (ts_node_symbol(node) == sym_list)
@ -351,8 +218,6 @@ t_error ast_from_node(\
return (build_sym_regex(node, input, out));
if (ts_node_symbol(node) == sym_simple_expansion)
return (build_sym_simple_expansion(node, input, out));
if (ts_node_symbol(node) == sym_simple_heredoc_body)
return (build_sym_simple_heredoc_body(node, input, out));
if (ts_node_symbol(node) == sym_string)
return (build_sym_string(node, input, out));
if (ts_node_symbol(node) == sym_string_content)

View file

@ -52,16 +52,19 @@ void _append_redirection(t_ast_node node, t_ast_node redirection)
(ast_free(redirection));
}
//// hello I am the SEGV police and I sized this code
//
// t_symbol symbol;
//
// symbol = ts_node_grammar_symbol(ts_node_child(node, 0));
// if (symbol == anon_sym_SEMI)
// return (AST_TERM_SEMI);
// if (symbol == anon_sym_SEMI_SEMI)
// return (AST_TERM_DOUBLE_SEMI);
// printf("unknown term symbol %d\n", symbol);
t_ast_terminator_kind _select_term(t_parse_node node)
{
t_symbol symbol;
symbol = ts_node_grammar_symbol(ts_node_child(node, 0));
if (symbol == anon_sym_SEMI)
return (AST_TERM_SEMI);
if (symbol == anon_sym_SEMI_SEMI)
return (AST_TERM_DOUBLE_SEMI);
printf("unknown term symbol %d\n", symbol);
(void)(node);
return (AST_TERM_NONE);
}

8
test_heredoc.sh Executable file
View file

@ -0,0 +1,8 @@
make
valgrind --leak-check=full --show-leak-kinds=none --track-origins=yes --track-fds=yes --trace-children=yes ./minishell <<<'cat <<EOF
bonjour je suis un heredoc
le saviez tu je dois finir par EOF mais qui est sur la ligne
donc par example si j ai cette ligne qui fini avec EOF
rien ne se passe
mais la ligne d en dessous fini le heredoc :D
EOF'