From ea17be0db03e45231fb019b020dd48b66847eb6d Mon Sep 17 00:00:00 2001 From: gazhonsepaskwa Date: Sat, 8 Feb 2025 14:39:31 +0100 Subject: [PATCH] quote correction --- srcs/parsing/tokenizer/tokenizer.c | 47 +++++++++++++++++++----- srcs/parsing/tokenizer/tokenizer_utils.c | 2 +- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/srcs/parsing/tokenizer/tokenizer.c b/srcs/parsing/tokenizer/tokenizer.c index 6cd4f5b..1c849bc 100644 --- a/srcs/parsing/tokenizer/tokenizer.c +++ b/srcs/parsing/tokenizer/tokenizer.c @@ -89,13 +89,12 @@ static int stick_quote_node(t_node *head, char q) it = head; while (it != NULL) { - if (it->val[0] == q && !ft_strchr(&it->val[1], q) - && find_quote_node(it->next, q)) + if (ft_strchr(it->val, q)) { - while (it->next->val[0] != q) + while (it->next && !ft_strchr(it->next->val, q)) if (!merge_with_next_node(it)) return (0); - if (!merge_with_next_node(it)) + if (it->next && !merge_with_next_node(it)) return (0); } it = it->next; @@ -120,6 +119,32 @@ void debug_token_list(t_node* lst, char *msg) } } +void del_void_nodes(t_node **head) +{ + t_node *cpy; + t_node *tmp; + + cpy = *head; + if (ft_strlen((*head)->val) == 0) + { + cpy = (*head)->next; + free ((*head)->val); + free (*head); + } + *head = cpy; + while (cpy) + { + if (cpy->next && ft_strlen(cpy->next->val) == 0) + { + tmp = cpy->next->next; + free(cpy->next->val); + free(cpy->next); + cpy->next = tmp; + } + cpy = cpy->next; + } +} + t_node *tokenize(char *str) { t_node *head; @@ -127,17 +152,19 @@ t_node *tokenize(char *str) head = tokenize_base(str); if (!head) return (NULL); - // debug_token_list(head, "tokenize_base"); - if (!trim_nodes(head)) - return (NULL); - // debug_token_list(head, "trim_nodes"); + debug_token_list(head, "tokenize_base"); if (!unstick_nodes(head)) return (NULL); - // debug_token_list(head, "unstick_nodes"); + debug_token_list(head, "unstick_nodes"); stick_quote_node(head, 39); stick_quote_node(head, '"'); - debug_token_list(head, "tokenizer"); + debug_token_list(head, "stick quote node"); + if (!trim_nodes(head)) + return (NULL); + debug_token_list(head, "trim_nodes"); set_token(head); + del_void_nodes(&head); + debug_token_list(head, "del_void_nodes"); if (syntax_error(head)) return (NULL); return (head); diff --git a/srcs/parsing/tokenizer/tokenizer_utils.c b/srcs/parsing/tokenizer/tokenizer_utils.c index 786bd56..63c4751 100644 --- a/srcs/parsing/tokenizer/tokenizer_utils.c +++ b/srcs/parsing/tokenizer/tokenizer_utils.c @@ -15,7 +15,7 @@ int is_meta(char c) { if (c == '&' || c == '|' || c == '<' || c == '>' || c == '(' || c == ')' - || c == '"' || c == 39 || c == ';' || c == '{' || c == '}' || c == '[' + || c == ';' || c == '{' || c == '}' || c == '[' || c == ']') return (1); return (0);