From 3b2efe45ce23f51e97a54b820b41f230b99bbab2 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 8 Jul 2024 16:35:30 +0200 Subject: Add most of the parser --- src/parser.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/parser.c (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..5bf82f6 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser next = ft_lstnew(parse_statement(current_tokens)); + current = current->next; + } + if (current == NULL) + { + // Free: ft_lstclear(&result, free_node); + return (NULL); + } + current_tokens = split_at_first(&tokens, NEWLINE_TOKEN); + } + return (result); +} + +static t_node *parse_statement(t_token *tokens) +{ + t_token *left_side_tokens; + + left_side_tokens = split_at_first(&tokens, PIPE_TOKEN); + if (tokens != NULL) + { + return (new_pipe_node(parse_cmd(left_side_tokens), + parse_statement(tokens))); + } + else + { + return (parse_cmd(left_side_tokens)); + } +} + +static t_node *parse_cmd(t_token *tokens) +{ + char **args; + t_assign **assigns; + t_redirection **redirs; + + redirs = collect_redirs(&tokens); + assigns = collect_assigns(&tokens); + args = collect_args(&tokens); + return (new_cmd_node(args, assigns, redirs)); +} + +t_token *split_at_first(t_token **tokens, int type) +{ + t_token *split; + t_token *result; + + split = find_token_by_type(*tokens, type); + if (split == NULL) + { + result = *tokens; + *tokens = NULL; + return (result); + } + result = *tokens; + *tokens = split->next; + free_token(split); + return (result); +} + +static t_token *find_token_by_type(t_token *tokens, int type) +{ + while (tokens != NULL) + { + if (tokens->type == type) + return (tokens); + tokens = tokens->next; + } + return (NULL); +} -- cgit v1.2.3 From 16ca09f57525cd9a4e7eb82ce70e4c1e0cb85a31 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 9 Jul 2024 12:56:09 +0200 Subject: Remove redundant funcs and add parse funcs to header and Makefile --- Makefile | 2 +- include/minishell.h | 6 +++++- src/free_node.c | 8 +------- src/parser.c | 15 +-------------- 4 files changed, 8 insertions(+), 23 deletions(-) (limited to 'src/parser.c') diff --git a/Makefile b/Makefile index 41551ec..8896ded 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,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 + free_token.c new_node.c free_node.c parser.c parse_cmd.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/minishell.h b/include/minishell.h index a528e4a..2411cc5 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: dkaiser content.assign.var); - free(node->content.assign.value); -} - static void free_pipe_node(t_node *node) { free_node(node->content.pipe.left); diff --git a/src/parser.c b/src/parser.c index 5bf82f6..66c6785 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,7 +6,7 @@ /* By: dkaiser # include -int init(void); -int init_signal_handling(void); +int init(void); +int init_signal_handling(void); -void repl(const char *prompt); +void repl(const char *prompt); -t_list *parse(t_token *tokens); -t_node *parse_cmd(t_token *tokens); +t_list *parse(t_token *tokens); +t_node *parse_cmd(t_token *tokens); +t_redirection *collect_redirs(t_token **tokens); +t_assign **collect_assigns(t_token **tokens); -void print_ast(t_node *ast); +void print_ast(t_node *ast); #endif diff --git a/src/collect_assigns.c b/src/collect_assigns.c new file mode 100644 index 0000000..da43503 --- /dev/null +++ b/src/collect_assigns.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collect_assigns.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser type == STRING_TOKEN + && !is_quote(cur->content.string[0]) && ft_strchr(cur->content.string, + '=') != NULL) + { + result[i++] = to_assign(cur->content.string); + if (cur->next != NULL) + { + cur = cur->next; + free_token(cur->previous); + } + else + free_token(cur); + } + *tokens = cur; + result[i] = NULL; + return (result); +} + +static t_assign *to_assign(char *str) +{ + t_assign *result; + char *split_pos; + + split_pos = ft_strchr(str, '='); + *split_pos = '\0'; + result = malloc(sizeof(t_assign)); + if (result == NULL) + { + return (NULL); + } + result->var = str; + result->value = split_pos + 1; + return (result); +} + +static int count_tokens(t_token *tokens) +{ + int len; + + len = 0; + while (tokens != NULL && tokens->type == STRING_TOKEN + && !is_quote(tokens->content.string[0]) + && ft_strchr(tokens->content.string, '=') != NULL) + { + len++; + tokens = tokens->next; + } + return (len); +} + +static int is_quote(char c) +{ + return (c == '"' || 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); + } +} diff --git a/src/free_node.c b/src/free_node.c index f8677d5..f387c0a 100644 --- a/src/free_node.c +++ b/src/free_node.c @@ -6,7 +6,7 @@ /* By: dkaiser content.cmd.args); free_assigns(node->content.cmd.assigns); - if (node->content.cmd.redirs[0].type != 0 && node->content.cmd.redirs[0].specifier != NULL) + if (node->content.cmd.redirs[0].type != 0 + && node->content.cmd.redirs[0].specifier != NULL) free(node->content.cmd.redirs[0].specifier); - if (node->content.cmd.redirs[1].type != 0 && node->content.cmd.redirs[0].specifier != NULL) + if (node->content.cmd.redirs[1].type != 0 + && node->content.cmd.redirs[0].specifier != NULL) free(node->content.cmd.redirs[1].specifier); } diff --git a/src/free_token.c b/src/free_token.c index 3c8ce1a..9b035ac 100644 --- a/src/free_token.c +++ b/src/free_token.c @@ -6,7 +6,7 @@ /* By: dkaiser next != NULL) { diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 8ed9785..068a4c1 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,18 +6,13 @@ /* By: dkaiser next != NULL) - { - if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - { - if (cur->content.redir_type & (INPUT_FILE | INPUT_LIMITER)) - idx = 0; - else if (cur->content.redir_type & (OUTPUT_APPEND | OUTPUT_OVERRIDE)) - idx = 1; - set_redir(&result[idx], 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); - } - } - else if (cur->type == REDIR_TOKEN) - { - // err Parsing error - free(result); - return (NULL); - } - else - { - cur = cur->next; - } - } - return (result); -} - -static t_assign **collect_assigns(t_token **tokens) -{ - t_token *cur; - t_assign **result; - int i; - - cur = *tokens; - i = 0; - while (cur != NULL && cur->type == STRING_TOKEN - && cur->content.string[0] != '"' && cur->content.string[0] != '\'' - && ft_strchr(cur->content.string, '=') != NULL) - { - i++; - cur = cur->next; - } - result = malloc(sizeof(t_assign *) * (i + 1)); - if (!result) - { - // free all tokens - return (NULL); - } - cur = *tokens; - i = 0; - while (cur != NULL && cur->type == STRING_TOKEN - && cur->content.string[0] != '"' && cur->content.string[0] != '\'' - && ft_strchr(cur->content.string, '=') != NULL) - { - result[i] = to_assign(cur->content.string); - i++; - if (cur->next != NULL) - { - cur = cur->next; - free_token(cur->previous); - } - else - { - free_token(cur); - } - } - *tokens = cur; - result[i] = NULL; - return (result); -} - -static t_assign *to_assign(char *str) -{ - t_assign *result; - char *split_pos; - - split_pos = ft_strchr(str, '='); - *split_pos = '\0'; - result = malloc(sizeof(t_assign)); - if (result == NULL) - { - return (NULL); - } - result->var = str; - result->value = split_pos + 1; - return (result); -} - static char **collect_args(t_token **tokens) { t_token *cur; @@ -159,26 +40,17 @@ static char **collect_args(t_token **tokens) cur = cur->next; } result = malloc(sizeof(char *) * (i + 1)); - if (!result) - { - // free all tokens; - return (NULL); - } + if (result == NULL) + return (free_tokens(*tokens), NULL); cur = *tokens; i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { result[i] = cur->content.string; - // free token i++; cur = cur->next; + free_token(cur->previous); } result[i] = NULL; return (result); } - -static void set_redir(t_redirection *redir, int type, char *specifier) -{ - redir->type = type; - redir->specifier = specifier; -} diff --git a/src/parser.c b/src/parser.c index 66c6785..f9484c9 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,7 +6,7 @@ /* By: dkaiser next; } if (current == NULL) - { - // Free: ft_lstclear(&result, free_node); - return (NULL); - } + return (ft_lstclear(&result, free_node_wrapper), NULL); current_tokens = split_at_first(&tokens, NEWLINE_TOKEN); } return (result); @@ -91,3 +88,8 @@ 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 2a3c0b5dd78c7305ef154c2579225dba81813ad2 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 17 Sep 2024 14:39:04 +0200 Subject: Fix pipe parsing error --- src/parser.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c index f9484c9..4ad514d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,11 +6,12 @@ /* By: dkaiser next; + if (result == split) + result = NULL; free_token(split); return (result); } -- 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/parser.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 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/parser.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