C_formatter_42 from_node.c

This commit is contained in:
Maieul BOYER 2024-07-30 18:51:42 +02:00
parent 3d7b8f4cd2
commit 9233485003
No known key found for this signature in database

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/26 10:55:52 by rparodi #+# #+# */ /* Created: 2024/07/26 10:55:52 by rparodi #+# #+# */
/* Updated: 2024/07/30 18:20:33 by maiboyer ### ########.fr */ /* Updated: 2024/07/30 18:43:02 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,8 +18,8 @@
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_ast.h" #include "me/vec/vec_ast.h"
#include "parser/api.h" #include "parser/api.h"
#include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h>
/* /*
sym_arithmetic_binary_expression sym_arithmetic_binary_expression
@ -74,7 +74,7 @@ sym_while_statement
sym_word sym_word
*/ */
t_ast_node ast_alloc(t_ast_node_kind kind); t_ast_node ast_alloc(t_ast_node_kind kind);
void ast_set_term(t_ast_node *node, t_ast_terminator_kind term) void ast_set_term(t_ast_node *node, t_ast_terminator_kind term)
{ {
@ -137,8 +137,8 @@ void _append_redirection(t_ast_node node, t_ast_node redirection)
t_vec_ast *vec; t_vec_ast *vec;
vec = NULL; vec = NULL;
if (!(redirection->kind == AST_FILE_REDIRECTION || \ if (!(redirection->kind == AST_FILE_REDIRECTION
redirection->kind == AST_HEREDOC_REDIRECTION)) || redirection->kind == AST_HEREDOC_REDIRECTION))
return (ast_free(redirection)); return (ast_free(redirection));
if (node->kind == AST_CASE) if (node->kind == AST_CASE)
vec = &node->data.case_.suffixes_redirections; vec = &node->data.case_.suffixes_redirections;
@ -191,20 +191,20 @@ t_str _extract_str(t_parse_node self, t_const_str input)
return (result); return (result);
} }
/* /*
E_OP_NONE = 0, // ${var} E_OP_NONE = 0, // ${var}
E_OP_DEFAULT, // ${var-word} E_OP_DEFAULT, // ${var-word}
E_OP_ASSIGN_DEFAULT, // ${var=word} E_OP_ASSIGN_DEFAULT, // ${var=word}
E_OP_ERROR, // ${var?word} E_OP_ERROR, // ${var?word}
E_OP_ALTERNATE, // ${var+word} E_OP_ALTERNATE, // ${var+word}
E_OP_DEFAULT_COLON, // ${var:-word} E_OP_DEFAULT_COLON, // ${var:-word}
E_OP_ASSIGN_DEFAULT_COLON, // ${var:=word} E_OP_ASSIGN_DEFAULT_COLON, // ${var:=word}
E_OP_ERROR_COLON, // ${var:?word} E_OP_ERROR_COLON, // ${var:?word}
E_OP_ALTERNATE_COLON, // ${var:+word} E_OP_ALTERNATE_COLON, // ${var:+word}
E_OP_LENGTH, // ${#var} E_OP_LENGTH, // ${#var}
E_OP_SMALLEST_PREFIX, // ${var#pattern} E_OP_SMALLEST_PREFIX, // ${var#pattern}
E_OP_LARGEST_PREFIX, // ${var##pattern} E_OP_LARGEST_PREFIX, // ${var##pattern}
E_OP_SMALLEST_SUFFIX, // ${var%pattern} E_OP_SMALLEST_SUFFIX, // ${var%pattern}
E_OP_LARGEST_SUFFIX, // ${var%%pattern} E_OP_LARGEST_SUFFIX, // ${var%%pattern}
*/ */
t_ast_expansion_operator _extract_exp_op(t_parse_node self) t_ast_expansion_operator _extract_exp_op(t_parse_node self)
@ -292,60 +292,105 @@ t_ast_arithmetic_operator _parse_operator(t_parse_node self)
return (me_abort("invalid arithmetic operator"), 0); return (me_abort("invalid arithmetic operator"), 0);
} }
t_error ast_from_node(t_parse_node node, t_const_str input, t_ast_node *out); t_error ast_from_node(t_parse_node node, t_const_str input,
t_ast_node *out);
/* FUNCTION THAT ARE DONE */ /* FUNCTION THAT ARE DONE */
t_error build_sym_case_item(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_case_item(t_parse_node self,
t_error build_sym_case_statement(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_command(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_case_statement(t_parse_node self,
t_error build_sym_command_name(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_comment(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_command(t_parse_node self,
t_error build_sym_compound_statement(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_elif_clause(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_command_name(t_parse_node self,
t_error build_sym_else_clause(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_extglob_pattern(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_comment(t_parse_node self,
t_error build_sym_file_redirect(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_for_statement(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_compound_statement(t_parse_node self,
t_error build_sym_function_definition(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_if_statement(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_elif_clause(t_parse_node self,
t_error build_sym_list(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_negated_command(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_else_clause(t_parse_node self,
t_error build_sym_number(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_pipeline(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_extglob_pattern(t_parse_node self,
t_error build_sym_program(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_raw_string(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_file_redirect(t_parse_node self,
t_error build_sym_redirected_statement(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_regex(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_for_statement(t_parse_node self,
t_error build_sym_simple_expansion(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_string_content(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_function_definition(t_parse_node self,
t_error build_sym_subshell(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_variable_assignment(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_if_statement(t_parse_node self,
t_error build_sym_while_statement(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_word(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_list(t_parse_node self, t_const_str input,
t_error build_sym_expansion(t_parse_node self, t_const_str input, t_ast_node *out); t_ast_node *out);
t_error build_sym_command_substitution(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_negated_command(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_number(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_pipeline(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_program(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_raw_string(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_redirected_statement(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_regex(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_simple_expansion(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_string_content(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_subshell(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_variable_assignment(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_while_statement(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_word(t_parse_node self, t_const_str input,
t_ast_node *out);
t_error build_sym_expansion(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_command_substitution(t_parse_node self,
t_const_str input, t_ast_node *out);
/* FUNCTION DONE BUT BY RAPH */ /* FUNCTION DONE BUT BY RAPH */
t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_arithmetic_binary_expression(t_parse_node self,
t_error build_sym_arithmetic_literal(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_parenthesized_expression(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_arithmetic_literal(t_parse_node self,
t_error build_sym_arithmetic_postfix_expression(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_ternary_expression(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_arithmetic_parenthesized_expression(t_parse_node self,
t_error build_sym_arithmetic_unary_expression(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_expansion(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_arithmetic_postfix_expression(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_ternary_expression(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_unary_expression(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_expansion(t_parse_node self,
t_const_str input, t_ast_node *out);
/* FUNCTION THAT ARE NOT DONE */ /* FUNCTION THAT ARE NOT DONE */
// TODO: This is my homework, it'll need to be handled in a special way I feel... // TODO: This is my homework,
t_error build_sym_heredoc_redirect(t_parse_node self, t_const_str input, t_ast_node *out); // it'll need to be handled in a special way I feel...
t_error build_sym_simple_heredoc_body(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_heredoc_redirect(t_parse_node self,
t_error build_sym_heredoc_body(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_heredoc_content(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_simple_heredoc_body(t_parse_node self,
t_error build_sym_heredoc_end(t_parse_node self, t_const_str input, t_ast_node *out); t_const_str input, t_ast_node *out);
t_error build_sym_heredoc_start(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_heredoc_body(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_heredoc_content(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_heredoc_end(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_heredoc_start(t_parse_node self,
t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_arithmetic_binary_expression(t_parse_node self,
t_const_str input, t_ast_node *out)
{ {
t_usize i; t_usize i;
t_ast_node ret; t_ast_node ret;
@ -359,12 +404,15 @@ t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str in
while (i < ts_node_child_count(self)) while (i < ts_node_child_count(self))
{ {
if (ts_node_field_id_for_child(self, i) == field_lhs) if (ts_node_field_id_for_child(self, i) == field_lhs)
if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_binary.lhs)) if (ast_from_node(ts_node_child(self, i), input,
&ret->data.arithmetic_binary.lhs))
return (ERROR); return (ERROR);
if (ts_node_field_id_for_child(self, i) == field_op) if (ts_node_field_id_for_child(self, i) == field_op)
ret->data.arithmetic_binary.op = _parse_operator(ts_node_child(self, i)); ret->data.arithmetic_binary.op = _parse_operator(ts_node_child(self,
i));
if (ts_node_field_id_for_child(self, i) == field_rhs) if (ts_node_field_id_for_child(self, i) == field_rhs)
if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_binary.rhs)) if (ast_from_node(ts_node_child(self, i), input,
&ret->data.arithmetic_binary.rhs))
return (ERROR); return (ERROR);
i++; i++;
} }
@ -372,7 +420,8 @@ t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str in
} }
// RAPH // RAPH
t_error build_sym_arithmetic_literal(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_arithmetic_literal(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_usize i; t_usize i;
t_ast_node ret; t_ast_node ret;
@ -383,12 +432,14 @@ t_error build_sym_arithmetic_literal(t_parse_node self, t_const_str input, t_ast
return (ERROR); return (ERROR);
i = 0; i = 0;
ret = ast_alloc(AST_ARITHMETIC_LITTERAL); ret = ast_alloc(AST_ARITHMETIC_LITTERAL);
if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_literal.value)) if (ast_from_node(ts_node_child(self, i), input,
&ret->data.arithmetic_literal.value))
return (ERROR); return (ERROR);
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_arithmetic_parenthesized_expression(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_arithmetic_parenthesized_expression(t_parse_node self,
t_const_str input, t_ast_node *out)
{ {
if (out == NULL) if (out == NULL)
return (ERROR); return (ERROR);
@ -397,7 +448,8 @@ t_error build_sym_arithmetic_parenthesized_expression(t_parse_node self, t_const
return (ast_from_node(ts_node_child(self, 1), input, out)); return (ast_from_node(ts_node_child(self, 1), input, out));
} }
t_error build_sym_arithmetic_postfix_expression(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_arithmetic_postfix_expression(t_parse_node self,
t_const_str input, t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
@ -408,16 +460,19 @@ t_error build_sym_arithmetic_postfix_expression(t_parse_node self, t_const_str i
if (ts_node_child_count(self) != 2) if (ts_node_child_count(self) != 2)
return (ERROR); return (ERROR);
ret = ast_alloc(AST_ARITHMETIC_POSTFIX); ret = ast_alloc(AST_ARITHMETIC_POSTFIX);
if (ast_from_node(ts_node_child(self, 0), input, &ret->data.arithmetic_postfix.value)) if (ast_from_node(ts_node_child(self, 0), input,
return (ast_free(ret), ERROR); &ret->data.arithmetic_postfix.value))
return (ast_free(ret), ERROR);
if (ts_node_field_id_for_child(self, 1) == field_op) if (ts_node_field_id_for_child(self, 1) == field_op)
ret->data.arithmetic_postfix.op = _parse_operator(ts_node_child(self, 1)); ret->data.arithmetic_postfix.op = _parse_operator(ts_node_child(self,
1));
else else
return (ast_free(ret), ERROR); return (ast_free(ret), ERROR);
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_arithmetic_ternary_expression(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_arithmetic_ternary_expression(t_parse_node self,
t_const_str input, t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_usize i; t_usize i;
@ -433,20 +488,24 @@ t_error build_sym_arithmetic_ternary_expression(t_parse_node self, t_const_str i
while (i < ts_node_child_count(self)) while (i < ts_node_child_count(self))
{ {
if (ts_node_field_id_for_child(self, i) == field_cond) if (ts_node_field_id_for_child(self, i) == field_cond)
if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_ternary.condition)) if (ast_from_node(ts_node_child(self, i), input,
&ret->data.arithmetic_ternary.condition))
return (ast_free(ret), ERROR); return (ast_free(ret), ERROR);
if (ts_node_field_id_for_child(self, i) == field_then) if (ts_node_field_id_for_child(self, i) == field_then)
if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_ternary.then)) if (ast_from_node(ts_node_child(self, i), input,
&ret->data.arithmetic_ternary.then))
return (ast_free(ret), ERROR); return (ast_free(ret), ERROR);
if (ts_node_field_id_for_child(self, i) == field_else) if (ts_node_field_id_for_child(self, i) == field_else)
if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_ternary.else_)) if (ast_from_node(ts_node_child(self, i), input,
&ret->data.arithmetic_ternary.else_))
return (ast_free(ret), ERROR); return (ast_free(ret), ERROR);
i++; i++;
} }
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_arithmetic_unary_expression(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_arithmetic_unary_expression(t_parse_node self,
t_const_str input, t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
@ -458,13 +517,16 @@ t_error build_sym_arithmetic_unary_expression(t_parse_node self, t_const_str inp
return (ERROR); return (ERROR);
ret = ast_alloc(AST_ARITHMETIC_UNARY); ret = ast_alloc(AST_ARITHMETIC_UNARY);
if (ts_node_field_id_for_child(self, 0) == field_op) if (ts_node_field_id_for_child(self, 0) == field_op)
ret->data.arithmetic_unary.operator = _parse_operator(ts_node_child(self, 0)); ret->data.arithmetic_unary.operator= _parse_operator(ts_node_child(self,
if (ast_from_node(ts_node_child(self, 1), input, &ret->data.arithmetic_unary.value)) 0));
if (ast_from_node(ts_node_child(self, 1), input,
&ret->data.arithmetic_unary.value))
return (ast_free(ret), ERROR); return (ast_free(ret), ERROR);
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_arithmetic_expansion(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_arithmetic_expansion(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_usize i; t_usize i;
t_ast_node ret; t_ast_node ret;
@ -477,25 +539,32 @@ t_error build_sym_arithmetic_expansion(t_parse_node self, t_const_str input, t_a
i = 0; i = 0;
while (i < ts_node_child_count(self)) while (i < ts_node_child_count(self))
{ {
if (ts_node_field_id_for_child(self, i) == sym_arithmetic_binary_expression) if (ts_node_field_id_for_child(self,
i) == sym_arithmetic_binary_expression)
return (build_sym_arithmetic_binary_expression(self, input, out)); return (build_sym_arithmetic_binary_expression(self, input, out));
if (ts_node_field_id_for_child(self, i) == sym_arithmetic_literal) if (ts_node_field_id_for_child(self, i) == sym_arithmetic_literal)
return (build_sym_arithmetic_literal(self, input, out)); return (build_sym_arithmetic_literal(self, input, out));
if (ts_node_field_id_for_child(self, i) == sym_arithmetic_parenthesized_expression) if (ts_node_field_id_for_child(self,
return (build_sym_arithmetic_parenthesized_expression(self, input, out)); i) == sym_arithmetic_parenthesized_expression)
if (ts_node_field_id_for_child(self, i) == sym_arithmetic_postfix_expression) return (build_sym_arithmetic_parenthesized_expression(self, input,
out));
if (ts_node_field_id_for_child(self,
i) == sym_arithmetic_postfix_expression)
return (build_sym_arithmetic_postfix_expression(self, input, out)); return (build_sym_arithmetic_postfix_expression(self, input, out));
if (ts_node_field_id_for_child(self, i) == sym_arithmetic_ternary_expression) if (ts_node_field_id_for_child(self,
i) == sym_arithmetic_ternary_expression)
return (build_sym_arithmetic_ternary_expression(self, input, out)); return (build_sym_arithmetic_ternary_expression(self, input, out));
if (ts_node_field_id_for_child(self, i) == sym_arithmetic_unary_expression) if (ts_node_field_id_for_child(self,
i) == sym_arithmetic_unary_expression)
return (build_sym_arithmetic_unary_expression(self, input, out)); return (build_sym_arithmetic_unary_expression(self, input, out));
i++; i++;
} }
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
//PLUS RAPH // PLUS RAPH
t_error build_sym_command_substitution(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_command_substitution(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -514,8 +583,8 @@ t_error build_sym_command_substitution(t_parse_node self, t_const_str input, t_a
if (ts_node_symbol(ts_node_child(self, i)) == field_term) if (ts_node_symbol(ts_node_child(self, i)) == field_term)
{ {
if (ret->data.command_substitution.body.len != 0) if (ret->data.command_substitution.body.len != 0)
ast_set_term(&ret->data.command_substitution.body.buffer[ret->data.command_substitution.body.len - 1], ast_set_term(&ret->data.command_substitution.body.buffer[ret->data.command_substitution.body.len
_select_term(ts_node_child(self, i))); - 1], _select_term(ts_node_child(self, i)));
} }
else else
{ {
@ -528,7 +597,8 @@ t_error build_sym_command_substitution(t_parse_node self, t_const_str input, t_a
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_expansion(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_expansion(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -551,7 +621,8 @@ t_error build_sym_expansion(t_parse_node self, t_const_str input, t_ast_node *ou
if (ts_node_field_id_for_child(self, i) == field_len) if (ts_node_field_id_for_child(self, i) == field_len)
ret->data.expansion.len_operator = true; ret->data.expansion.len_operator = true;
if (ts_node_field_id_for_child(self, i) == field_name) if (ts_node_field_id_for_child(self, i) == field_name)
ret->data.expansion.var_name = _extract_str(ts_node_child(self, i), input); ret->data.expansion.var_name = _extract_str(ts_node_child(self, i),
input);
if (ts_node_field_id_for_child(self, i) == field_op) if (ts_node_field_id_for_child(self, i) == field_op)
ret->data.expansion.kind = _extract_exp_op(ts_node_child(self, i)); ret->data.expansion.kind = _extract_exp_op(ts_node_child(self, i));
if (ts_node_field_id_for_child(self, i) == field_args) if (ts_node_field_id_for_child(self, i) == field_args)
@ -565,7 +636,8 @@ t_error build_sym_expansion(t_parse_node self, t_const_str input, t_ast_node *ou
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_simple_expansion(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_simple_expansion(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_usize i; t_usize i;
@ -584,13 +656,15 @@ t_error build_sym_simple_expansion(t_parse_node self, t_const_str input, t_ast_n
{ {
if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true)) if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true))
continue ; continue ;
ret->data.expansion.var_name = _extract_str(ts_node_child(self, i), input); ret->data.expansion.var_name = _extract_str(ts_node_child(self, i),
input);
i++; i++;
} }
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_file_descriptor(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_file_descriptor(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
@ -624,7 +698,8 @@ t_error build_sym_number(t_parse_node self, t_const_str input, t_ast_node *out)
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_file_redirect(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_file_redirect(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -651,7 +726,8 @@ t_error build_sym_file_redirect(t_parse_node self, t_const_str input, t_ast_node
} }
if (ts_node_field_id_for_child(self, i) == field_op) if (ts_node_field_id_for_child(self, i) == field_op)
{ {
ret->data.file_redirection.op = _get_redirection_op(ts_node_child(self, i)); ret->data.file_redirection.op = _get_redirection_op(ts_node_child(self,
i));
} }
if (ts_node_field_id_for_child(self, i) == field_dest) if (ts_node_field_id_for_child(self, i) == field_dest)
{ {
@ -680,7 +756,8 @@ t_error build_sym_regex(t_parse_node self, t_const_str input, t_ast_node *out)
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_extglob_pattern(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_extglob_pattern(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
@ -696,7 +773,8 @@ t_error build_sym_extglob_pattern(t_parse_node self, t_const_str input, t_ast_no
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_function_definition(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_function_definition(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -717,7 +795,8 @@ t_error build_sym_function_definition(t_parse_node self, t_const_str input, t_as
continue ; continue ;
if (ts_node_field_id_for_child(self, i) == field_name) if (ts_node_field_id_for_child(self, i) == field_name)
{ {
ret->data.function_definition.name = _extract_str(ts_node_child(self, i), input); ret->data.function_definition.name = _extract_str(ts_node_child(self,
i), input);
} }
if (ts_node_field_id_for_child(self, i) == field_body) if (ts_node_field_id_for_child(self, i) == field_body)
{ {
@ -730,7 +809,8 @@ t_error build_sym_function_definition(t_parse_node self, t_const_str input, t_as
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_case_statement(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_case_statement(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -751,7 +831,8 @@ t_error build_sym_case_statement(t_parse_node self, t_const_str input, t_ast_nod
continue ; continue ;
if (ts_node_field_id_for_child(self, i) == field_value) if (ts_node_field_id_for_child(self, i) == field_value)
{ {
if (ast_from_node(ts_node_child(self, i), input, &ret->data.case_.word)) if (ast_from_node(ts_node_child(self, i), input,
&ret->data.case_.word))
return (ast_free(ret), ERROR); return (ast_free(ret), ERROR);
} }
if (ts_node_field_id_for_child(self, i) == field_cases) if (ts_node_field_id_for_child(self, i) == field_cases)
@ -765,7 +846,8 @@ t_error build_sym_case_statement(t_parse_node self, t_const_str input, t_ast_nod
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym__case_item_last(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym__case_item_last(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -801,7 +883,8 @@ t_error build_sym__case_item_last(t_parse_node self, t_const_str input, t_ast_no
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_case_item(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_case_item(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -837,7 +920,8 @@ t_error build_sym_case_item(t_parse_node self, t_const_str input, t_ast_node *ou
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_if_statement(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_if_statement(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -886,7 +970,8 @@ t_error build_sym_if_statement(t_parse_node self, t_const_str input, t_ast_node
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_elif_clause(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_elif_clause(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -923,7 +1008,8 @@ t_error build_sym_elif_clause(t_parse_node self, t_const_str input, t_ast_node *
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_else_clause(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_else_clause(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -954,7 +1040,8 @@ t_error build_sym_else_clause(t_parse_node self, t_const_str input, t_ast_node *
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_for_statement(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_for_statement(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -975,7 +1062,8 @@ t_error build_sym_for_statement(t_parse_node self, t_const_str input, t_ast_node
continue ; continue ;
if (ts_node_field_id_for_child(self, i) == field_var) if (ts_node_field_id_for_child(self, i) == field_var)
{ {
ret->data.for_.var_name = _extract_str(ts_node_child(self, i), input); ret->data.for_.var_name = _extract_str(ts_node_child(self, i),
input);
} }
if (ts_node_field_id_for_child(self, i) == field_value) if (ts_node_field_id_for_child(self, i) == field_value)
{ {
@ -994,7 +1082,8 @@ t_error build_sym_for_statement(t_parse_node self, t_const_str input, t_ast_node
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_pipeline(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_pipeline(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -1021,7 +1110,8 @@ t_error build_sym_pipeline(t_parse_node self, t_const_str input, t_ast_node *out
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_do_group(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_do_group(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -1039,15 +1129,18 @@ t_error build_sym_do_group(t_parse_node self, t_const_str input, t_ast_node *out
i = 0; i = 0;
while (i < ts_node_child_count(self)) while (i < ts_node_child_count(self))
{ {
if (ts_node_symbol(ts_node_child(self, i)) == anon_sym_do || ts_node_symbol(ts_node_child(self, i)) == anon_sym_done) if (ts_node_symbol(ts_node_child(self, i)) == anon_sym_do
|| ts_node_symbol(ts_node_child(self, i)) == anon_sym_done)
{ {
i++; i++;
continue ; continue ;
} }
if (ts_node_field_id_for_child(self, i) == field_term && ret->data.compound_statement.body.len != 0) if (ts_node_field_id_for_child(self, i) == field_term
&& ret->data.compound_statement.body.len != 0)
{ {
term = _select_term(ts_node_child(self, i)); term = _select_term(ts_node_child(self, i));
ast_set_term(&ret->data.compound_statement.body.buffer[ret->data.compound_statement.body.len - 1], term); ast_set_term(&ret->data.compound_statement.body.buffer[ret->data.compound_statement.body.len
- 1], term);
} }
else else
{ {
@ -1060,7 +1153,8 @@ t_error build_sym_do_group(t_parse_node self, t_const_str input, t_ast_node *out
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_subshell(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_subshell(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -1078,10 +1172,12 @@ t_error build_sym_subshell(t_parse_node self, t_const_str input, t_ast_node *out
i = 0; i = 0;
while (i < ts_node_child_count(self)) while (i < ts_node_child_count(self))
{ {
if (ts_node_field_id_for_child(self, i) == field_term && ret->data.subshell.body.len != 0) if (ts_node_field_id_for_child(self, i) == field_term
&& ret->data.subshell.body.len != 0)
{ {
term = _select_term(ts_node_child(self, i)); term = _select_term(ts_node_child(self, i));
ast_set_term(&ret->data.subshell.body.buffer[ret->data.subshell.body.len - 1], term); ast_set_term(&ret->data.subshell.body.buffer[ret->data.subshell.body.len
- 1], term);
} }
else else
{ {
@ -1096,7 +1192,8 @@ t_error build_sym_subshell(t_parse_node self, t_const_str input, t_ast_node *out
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_while_statement(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_while_statement(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -1126,7 +1223,8 @@ t_error build_sym_while_statement(t_parse_node self, t_const_str input, t_ast_no
if (ts_node_field_id_for_child(self, i) == field_term) if (ts_node_field_id_for_child(self, i) == field_term)
{ {
term = _select_term(ts_node_child(self, i)); term = _select_term(ts_node_child(self, i));
ast_set_term(&ret->data.while_.condition.buffer[ret->data.while_.condition.len - 1], term); ast_set_term(&ret->data.while_.condition.buffer[ret->data.while_.condition.len
- 1], term);
i++; i++;
continue ; continue ;
} }
@ -1143,7 +1241,8 @@ t_error build_sym_while_statement(t_parse_node self, t_const_str input, t_ast_no
// t_error buildw // t_error buildw
t_error build_sym_redirected_statement(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_redirected_statement(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret_tmp; t_ast_node ret_tmp;
t_ast_node ret; t_ast_node ret;
@ -1164,21 +1263,25 @@ t_error build_sym_redirected_statement(t_parse_node self, t_const_str input, t_a
{ {
if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true)) if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true))
continue ; continue ;
if (!(ts_node_symbol(ts_node_child(self, i)) == sym_file_redirect || \ if (!(ts_node_symbol(ts_node_child(self, i)) == sym_file_redirect
ts_node_symbol(ts_node_child(self, i)) == sym_heredoc_redirect)) || ts_node_symbol(ts_node_child(self,
i)) == sym_heredoc_redirect))
{ {
if (ast_from_node(ts_node_child(self, i++), input, &ret)) if (ast_from_node(ts_node_child(self, i++), input, &ret))
return (ast_free(ret_tmp), ERROR); return (ast_free(ret_tmp), ERROR);
continue ; continue ;
} }
if (ast_from_node(ts_node_child(self, i++), input, &tmp)) if (ast_from_node(ts_node_child(self, i++), input, &tmp))
return ((void) !((ret != ret_tmp) && (ast_free(ret_tmp), true)), ast_free(ret), ERROR); return ((void)!((ret != ret_tmp) && (ast_free(ret_tmp), true)),
ast_free(ret), ERROR);
_append_redirection(ret, tmp); _append_redirection(ret, tmp);
} }
return ((void) !((ret != ret_tmp) && (ast_free(ret_tmp), true)), *out = ret, NO_ERROR); return ((void)!((ret != ret_tmp) && (ast_free(ret_tmp), true)), *out = ret,
NO_ERROR);
} }
t_error build_sym_negated_command(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_negated_command(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
@ -1197,7 +1300,8 @@ t_error build_sym_negated_command(t_parse_node self, t_const_str input, t_ast_no
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_compound_statement(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_compound_statement(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp; t_ast_node tmp;
@ -1215,10 +1319,12 @@ t_error build_sym_compound_statement(t_parse_node self, t_const_str input, t_ast
i = 0; i = 0;
while (i < ts_node_child_count(self)) while (i < ts_node_child_count(self))
{ {
if (ts_node_field_id_for_child(self, i) == field_term && ret->data.compound_statement.body.len != 0) if (ts_node_field_id_for_child(self, i) == field_term
&& ret->data.compound_statement.body.len != 0)
{ {
term = _select_term(ts_node_child(self, i)); term = _select_term(ts_node_child(self, i));
ast_set_term(&ret->data.compound_statement.body.buffer[ret->data.compound_statement.body.len - 1], term); ast_set_term(&ret->data.compound_statement.body.buffer[ret->data.compound_statement.body.len
- 1], term);
} }
else else
{ {
@ -1246,7 +1352,8 @@ t_error build_sym_comment(t_parse_node self, t_const_str input, t_ast_node *out)
return (NO_ERROR); return (NO_ERROR);
} }
t_error build_sym_variable_assignment(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_variable_assignment(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_parse_node temp_ast; t_parse_node temp_ast;
@ -1268,7 +1375,8 @@ t_error build_sym_variable_assignment(t_parse_node self, t_const_str input, t_as
} }
if (ts_node_child_count(self) > 2) if (ts_node_child_count(self) > 2)
{ {
if (ast_from_node(ts_node_child(self, 2), input, &ret->data.variable_assignment.value)) if (ast_from_node(ts_node_child(self, 2), input,
&ret->data.variable_assignment.value))
return (ast_free(ret), ERROR); return (ast_free(ret), ERROR);
} }
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
@ -1302,7 +1410,8 @@ t_error build_sym_string(t_parse_node self, t_const_str input, t_ast_node *out)
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_concatenation(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_concatenation(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_usize i; t_usize i;
@ -1328,7 +1437,8 @@ t_error build_sym_concatenation(t_parse_node self, t_const_str input, t_ast_node
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_string_content(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_string_content(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_str temp_str; t_str temp_str;
@ -1345,7 +1455,8 @@ t_error build_sym_string_content(t_parse_node self, t_const_str input, t_ast_nod
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_raw_string(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_raw_string(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node temp; t_ast_node temp;
@ -1393,14 +1504,16 @@ t_error build_sym_program(t_parse_node self, t_const_str input, t_ast_node *out)
{ {
if (ret->data.program.body.len == 0 && (i++, true)) if (ret->data.program.body.len == 0 && (i++, true))
continue ; continue ;
ast_set_term(&ret->data.program.body.buffer[ret->data.program.body.len - 1], _select_term(ts_node_child(self, i))); ast_set_term(&ret->data.program.body.buffer[ret->data.program.body.len
- 1], _select_term(ts_node_child(self, i)));
} }
i++; i++;
} }
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_command_name(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_command_name(t_parse_node self, t_const_str input,
t_ast_node *out)
{ {
if (out == NULL) if (out == NULL)
return (ERROR); return (ERROR);
@ -1575,7 +1688,8 @@ t_error ast_from_node(t_parse_node node, t_const_str input, t_ast_node *out)
if (ts_node_symbol(node) == sym_arithmetic_literal) if (ts_node_symbol(node) == sym_arithmetic_literal)
return (build_sym_arithmetic_literal(node, input, out)); return (build_sym_arithmetic_literal(node, input, out));
if (ts_node_symbol(node) == sym_arithmetic_parenthesized_expression) if (ts_node_symbol(node) == sym_arithmetic_parenthesized_expression)
return (build_sym_arithmetic_parenthesized_expression(node, input, out)); return (build_sym_arithmetic_parenthesized_expression(node, input,
out));
if (ts_node_symbol(node) == sym_arithmetic_postfix_expression) if (ts_node_symbol(node) == sym_arithmetic_postfix_expression)
return (build_sym_arithmetic_postfix_expression(node, input, out)); return (build_sym_arithmetic_postfix_expression(node, input, out));
if (ts_node_symbol(node) == sym_arithmetic_ternary_expression) if (ts_node_symbol(node) == sym_arithmetic_ternary_expression)