fix: changed puts to perror + redirections in parser

This commit is contained in:
Matteo Flebus 2026-01-27 16:05:11 +01:00
parent 96626d9850
commit d52f603eec
5 changed files with 26 additions and 24 deletions

View file

@ -66,7 +66,7 @@ static bool init_firsts_map(void)
firsts_map = calloc(NUMBER_OF_RULES, sizeof(struct firsts_list)); firsts_map = calloc(NUMBER_OF_RULES, sizeof(struct firsts_list));
if (firsts_map == NULL) if (firsts_map == NULL)
{ {
puts("Internal error: couldn't create the firsts_map (is your memory " perror("Internal error: couldn't create the firsts_map (is your memory "
"full ?)"); "full ?)");
return false; return false;
} }
@ -161,7 +161,7 @@ struct firsts_list *first(enum rule rule)
{ {
if (firsts_map == NULL || firsts_map[rule].tokens == NULL) if (firsts_map == NULL || firsts_map[rule].tokens == NULL)
{ {
puts("Internal error: attempted to get the firsts of a rule without " perror("Internal error: attempted to get the firsts of a rule without "
"properly initializing the firsts map"); "properly initializing the firsts map");
return NULL; return NULL;
} }
@ -208,7 +208,7 @@ struct ast *parse_input(struct lexer_context *ctx)
return ast; return ast;
} }
puts("Syntax error: expected newline or EOF after list"); perror("Syntax error: expected newline or EOF after list");
ast_free(&ast); ast_free(&ast);
return NULL; return NULL;
} }

View file

@ -6,6 +6,8 @@
#include "grammar_basic.h" #include "grammar_basic.h"
bool
struct ast *parse_redirection(struct lexer_context *ctx) struct ast *parse_redirection(struct lexer_context *ctx)
{ {
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
@ -17,20 +19,20 @@ struct ast *parse_redirection(struct lexer_context *ctx)
token = PEEK_TOKEN(); token = PEEK_TOKEN();
} }
if (token->type != TOKEN_REDIRECTION) if (!is_token_redir(token))
{ {
puts("Syntax error: expected a redirection token but got something " perror("Syntax error: expected a redirection token but got something "
"else"); "else");
return NULL; return NULL;
} }
char *redir_op = strdup(token->data); // char *redir_op = strdup(token->data);
POP_TOKEN(); POP_TOKEN();
token = PEEK_TOKEN(); token = PEEK_TOKEN();
if (token->type != TOKEN_WORD) if (token->type != TOKEN_WORD)
{ {
puts("Syntax error: expected a word after redirection"); perror("Syntax error: expected a word after redirection");
free(redir_op); // free(redir_op);
return NULL; return NULL;
} }
char *target = strdup(token->data); char *target = strdup(token->data);

View file

@ -160,7 +160,7 @@ struct ast *parse_command(struct lexer_context *ctx)
} }
else else
{ {
puts("Syntax error: unexpected token"); perror("Syntax error: unexpected token");
return NULL; return NULL;
} }
} }
@ -173,7 +173,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
struct token *token = POP_TOKEN(); struct token *token = POP_TOKEN();
if (token->type != TOKEN_WORD) if (token->type != TOKEN_WORD)
{ {
puts("Expected a command but got a different token type"); perror("Expected a command but got a different token type");
return NULL; return NULL;
} }
char *command = strdup(token->data); char *command = strdup(token->data);
@ -202,12 +202,12 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
else if (ast_is_redir(element)) else if (ast_is_redir(element))
{ {
// TODO // TODO
puts("NOT IMPLEMENTED"); perror("NOT IMPLEMENTED");
return NULL; return NULL;
} }
else else
{ {
puts("Internal error: unexpected return value from parse_element " perror("Internal error: unexpected return value from parse_element "
"in parse_simple_command"); "in parse_simple_command");
list_deep_destroy(command_elements); list_deep_destroy(command_elements);
return NULL; return NULL;
@ -241,7 +241,7 @@ struct ast *parse_element(struct lexer_context *ctx)
} }
else else
{ {
puts("Syntax error: unexpected token at parse_element"); perror("Syntax error: unexpected token at parse_element");
return NULL; return NULL;
} }
} }
@ -257,7 +257,7 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
struct token *token = POP_TOKEN(); struct token *token = POP_TOKEN();
if (token->type != TOKEN_IF) if (token->type != TOKEN_IF)
{ {
puts("Internal error: expected a if rule but token has different " perror("Internal error: expected a if rule but token has different "
"type"); "type");
return NULL; return NULL;
} }
@ -270,7 +270,7 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
if (token->type != TOKEN_THEN) if (token->type != TOKEN_THEN)
{ {
ast_free(&condition_content); ast_free(&condition_content);
puts("Expected the 'then' keyword but token has different type"); perror("Expected the 'then' keyword but token has different type");
return NULL; return NULL;
} }
@ -299,7 +299,7 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
ast_free(&condition_content); ast_free(&condition_content);
ast_free(&then_content); ast_free(&then_content);
ast_free(&else_content); ast_free(&else_content);
puts("Expected the 'fi' keyword but token has different type"); perror("Expected the 'fi' keyword but token has different type");
return NULL; return NULL;
} }
@ -311,7 +311,7 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
ast_free(&condition_content); ast_free(&condition_content);
ast_free(&then_content); ast_free(&then_content);
ast_free(&else_content); ast_free(&else_content);
puts("Internal error: could not create a new AST (AST_IF)"); perror("Internal error: could not create a new AST (AST_IF)");
return NULL; return NULL;
} }
@ -396,7 +396,7 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
token = POP_TOKEN(); token = POP_TOKEN();
if (token->type != TOKEN_THEN) if (token->type != TOKEN_THEN)
{ {
puts("Expected the 'then' keyword but got a different token type"); perror("Expected the 'then' keyword but got a different token type");
return NULL; return NULL;
} }

View file

@ -14,7 +14,7 @@ bool parser_init(void)
{ {
if (state == PARSER_STATE_READY) if (state == PARSER_STATE_READY)
{ {
puts("Internal error: tried to initialize the parser module twice."); perror("Internal error: tried to initialize the parser module twice.");
return NULL; return NULL;
} }
int success = grammar_init(); int success = grammar_init();
@ -29,19 +29,19 @@ struct ast *get_ast(struct lexer_context *ctx)
{ {
if (ctx == NULL) if (ctx == NULL)
{ {
puts("Internal error: called parser with no lexer context (NULL " perror("Internal error: called parser with no lexer context (NULL "
"pointer). Aborting."); "pointer). Aborting.");
return NULL; return NULL;
} }
if (state == PARSER_STATE_NOT_INITIALIZED) if (state == PARSER_STATE_NOT_INITIALIZED)
{ {
puts("Internal error: attempted to call parser without initializing " perror("Internal error: attempted to call parser without initializing "
"it. Aborting."); "it. Aborting.");
return NULL; return NULL;
} }
if (state == PARSER_STATE_CLOSED) if (state == PARSER_STATE_CLOSED)
{ {
puts("Internal error: attempted to call parser after closing it. " perror("Internal error: attempted to call parser after closing it. "
"Aborting."); "Aborting.");
return NULL; return NULL;
} }

View file

@ -11,7 +11,7 @@ void ast_free(struct ast **node)
{ {
if (node == NULL || *node == NULL) if (node == NULL || *node == NULL)
{ {
puts( perror(
"WARNING: Internal error: failed to free AST node (NULL argument)"); "WARNING: Internal error: failed to free AST node (NULL argument)");
return; return;
} }
@ -45,7 +45,7 @@ void ast_free(struct ast **node)
break; break;
default: default:
puts("WARNING: Internal error: failed to free an AST node (Unknown " perror("WARNING: Internal error: failed to free an AST node (Unknown "
"type)"); "type)");
return; return;
} }