From c7a4494fd97b7e80665cbd47ed96bc37bf5800e5 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 2 Aug 2024 15:29:00 +0200 Subject: Refactor parser * Fix norme for free_node.c * Uglify parser.c * Merge parser updates into refactor-parser * Outsource collect_assigns and collect_redirs * Make parse_cmd.c norme conform * Make free_tokens() norme conform * Make collect_assigns.c norme conform * Some refactoring of collect_redirs * Refactor collect_redirs--- src/collect_redirs.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/collect_redirs.c (limited to 'src/collect_redirs.c') diff --git a/src/collect_redirs.c b/src/collect_redirs.c new file mode 100644 index 0000000..76b08da --- /dev/null +++ b/src/collect_redirs.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collect_redirs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser next != NULL) + { + if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) + collect_redir(tokens, result, cur); + else if (cur->type == REDIR_TOKEN) + { + dbg("TODO: Add parsing errmsg"); + return (free(result), NULL); + } + else + cur = cur->next; + } + return (result); +} + +static void collect_redir(t_token **tokens, t_redirection *result, t_token *cur) +{ + set_redir(&result[is_output_redir(cur->content.redir_type)], + cur->content.redir_type, cur->next->content.string); + cur = cur->next; + free_token_and_connect(cur->previous); + if (cur->next != NULL) + { + if (cur->previous == NULL) + *tokens = cur->next; + cur = cur->next; + free_token_and_connect(cur->previous); + } + else + free_token(cur); +} + +static void set_redir(t_redirection *redir, int type, char *specifier) +{ + redir->type = type; + redir->specifier = specifier; +} + +static int is_output_redir(int i) +{ + if (i & (INPUT_FILE | INPUT_LIMITER)) + return (0); + else if (i & (OUTPUT_APPEND | OUTPUT_OVERRIDE)) + return (1); + else + { + panic(UNREACHABLE); + return (-1); + } +} -- cgit v1.2.3 From f53acc629b1b3e7f4097eef1e26841ef0b9b24c6 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Sun, 11 Aug 2024 12:15:40 +0200 Subject: Fix parser bugs --- Makefile | 2 +- src/collect_redirs.c | 13 +++++++++---- src/parse_cmd.c | 10 ++++------ src/tokenizer.c | 4 +++- 4 files changed, 17 insertions(+), 12 deletions(-) (limited to 'src/collect_redirs.c') diff --git a/Makefile b/Makefile index 3450e3c..d4778bf 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ HEADERS = -I include -I $(LIB_DIR)/libft VPATH := src SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ free_token.c new_node.c free_node.c tokenizer.c parser.c \ - parse_cmd.c print_ast.c + parse_cmd.c collect_assigns.c collect_redirs.c print_ast.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 76b08da..d50bec4 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,13 +6,13 @@ /* By: dkaiser next != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_redir(tokens, result, cur); + cur = collect_redir(tokens, result, cur); else if (cur->type == REDIR_TOKEN) { dbg("TODO: Add parsing errmsg"); @@ -43,7 +43,8 @@ t_redirection *collect_redirs(t_token **tokens) return (result); } -static void collect_redir(t_token **tokens, t_redirection *result, t_token *cur) +static t_token *collect_redir(t_token **tokens, t_redirection *result, + t_token *cur) { set_redir(&result[is_output_redir(cur->content.redir_type)], cur->content.redir_type, cur->next->content.string); @@ -57,7 +58,11 @@ static void collect_redir(t_token **tokens, t_redirection *result, t_token *cur) free_token_and_connect(cur->previous); } else + { free_token(cur); + return (NULL); + } + return (cur); } static void set_redir(t_redirection *redir, int type, char *specifier) diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 068a4c1..eb70f0d 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: dkaiser next; - } result = malloc(sizeof(char *) * (i + 1)); if (result == NULL) return (free_tokens(*tokens), NULL); @@ -46,10 +43,11 @@ static char **collect_args(t_token **tokens) i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { + if (cur->previous) + free_token(cur->previous); result[i] = cur->content.string; i++; cur = cur->next; - free_token(cur->previous); } result[i] = NULL; return (result); diff --git a/src/tokenizer.c b/src/tokenizer.c index 34685ac..efbf723 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/09 15:40:00 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 11:40:35 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -110,4 +110,6 @@ void tokenizer(char *s, t_token **token_list) pos = i + 1; } } + while ((*token_list)->previous) + *token_list = (*token_list)->previous; } -- cgit v1.2.3 From cf8754ca029817f092f5159b1c4bc3bbabec4217 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 17 Sep 2024 15:09:24 +0200 Subject: Add error messages --- src/collect_redirs.c | 4 ++-- src/parser.c | 3 ++- src/print_ast.c | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src/collect_redirs.c') diff --git a/src/collect_redirs.c b/src/collect_redirs.c index d50bec4..9beddf7 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: dkaiser type == REDIR_TOKEN) { - dbg("TODO: Add parsing errmsg"); + printf("Parsing error.\n"); return (free(result), NULL); } else diff --git a/src/parser.c b/src/parser.c index 4ad514d..6993a1f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,7 +6,7 @@ /* By: dkaiser type == CMD_NODE) print_cmd_node(ast, indent); else if (ast->type == PIPE_NODE) -- cgit v1.2.3 From e8891daeb2e9cf26dc6be1f4d3693734d7a9c5c1 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 17 Sep 2024 17:25:28 +0200 Subject: Fix redir ignored at end --- src/collect_redirs.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/collect_redirs.c') diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 9beddf7..79ae95c 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: dkaiser next; } + if (cur && cur->type == REDIR_TOKEN) + { + printf("Parsing error.\n"); + return (free(result), NULL); + } return (result); } -- cgit v1.2.3 From 1fec66236f1811a3eeac673d0002fe8d9d3d8835 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 17 Sep 2024 19:25:13 +0200 Subject: Fix error message printing --- src/collect_redirs.c | 16 +++++++++------- src/new_node.c | 4 +++- src/parser.c | 40 ++++++++++------------------------------ 3 files changed, 22 insertions(+), 38 deletions(-) (limited to 'src/collect_redirs.c') diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 79ae95c..60f197b 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: dkaiser next != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) + { + is_redir_only = 0; + if (cur->previous == NULL && cur->next->next == NULL) + is_redir_only = 1; cur = collect_redir(tokens, result, cur); + if (is_redir_only) + *tokens = NULL; + } else if (cur->type == REDIR_TOKEN) - { - printf("Parsing error.\n"); return (free(result), NULL); - } else cur = cur->next; } if (cur && cur->type == REDIR_TOKEN) - { - printf("Parsing error.\n"); return (free(result), NULL); - } return (result); } diff --git a/src/new_node.c b/src/new_node.c index 5c770aa..c58d291 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -6,7 +6,7 @@ /* By: dkaiser next = ft_lstnew(parse_statement(current_tokens)); - current = current->next; - } - if (current == NULL) - return (ft_lstclear(&result, free_node_wrapper), NULL); - current_tokens = split_at_first(&tokens, NEWLINE_TOKEN); - } - return (result); + if ((*tokens).type == PIPE_TOKEN) + result = NULL; + else + result = parse_statement(tokens); + if (result == NULL) + printf("Parsing error.\n"); + return (ft_lstnew(result)); } static t_node *parse_statement(t_token *tokens) @@ -53,7 +39,6 @@ static t_node *parse_statement(t_token *tokens) if (left_side_tokens == NULL) { free_tokens(tokens); - printf("Parsing error.\n"); return (NULL); } else if (tokens != NULL) @@ -97,8 +82,3 @@ static t_token *find_token_by_type(t_token *tokens, int type) } return (NULL); } - -static void free_node_wrapper(void *node) -{ - free_node((t_node *)node); -} -- cgit v1.2.3 From e7d58349800bd6bb0f139eec411bfd434f2c824f Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 17 Sep 2024 19:28:37 +0200 Subject: Make norminette happy and fry my brain. I just summoned some cosmic horror, but at least norminette doesn't complain anymore. --- src/collect_redirs.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/collect_redirs.c') diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 60f197b..8917c78 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: dkaiser type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) { - is_redir_only = 0; - if (cur->previous == NULL && cur->next->next == NULL) - is_redir_only = 1; cur = collect_redir(tokens, result, cur); - if (is_redir_only) - *tokens = NULL; + *tokens = (t_token *)(((unsigned long)*tokens) & (~0 + * (!cur->previous && !cur->next->next))); } else if (cur->type == REDIR_TOKEN) return (free(result), NULL); -- cgit v1.2.3 From 4765148b87b6c095aae1b32b023d5815356584c3 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 17 Sep 2024 19:49:29 +0200 Subject: Banish the cosmic horror --- src/collect_redirs.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src/collect_redirs.c') diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 8917c78..9ac1605 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: dkaiser next != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - { - cur = collect_redir(tokens, result, cur); - *tokens = (t_token *)(((unsigned long)*tokens) & (~0 - * (!cur->previous && !cur->next->next))); - } + collect_and_check_redir(tokens, result, &cur); else if (cur->type == REDIR_TOKEN) return (free(result), NULL); else @@ -46,6 +44,19 @@ t_redirection *collect_redirs(t_token **tokens) return (result); } +static void collect_and_check_redir(t_token **tokens, t_redirection *result, + t_token **cur) +{ + int is_redir_only; + + is_redir_only = 0; + if ((*cur)->previous == NULL && (*cur)->next->next == NULL) + is_redir_only = 1; + *cur = collect_redir(tokens, result, *cur); + if (is_redir_only) + *tokens = NULL; +} + static t_token *collect_redir(t_token **tokens, t_redirection *result, t_token *cur) { -- cgit v1.2.3