From 6b9e09fb55665d713a29829413f39100e704fd7b Mon Sep 17 00:00:00 2001 From: Quinten Mennen Date: Tue, 18 Mar 2025 16:22:03 +0100 Subject: [PATCH] feat (lexer): print syntax error --- inc/token.h | 3 ++- src/lexer/lexer_read_word.c | 27 +++++++++++++++++++++++++++ src/lexer/lexer_token_next.c | 16 +++++++--------- src/main.c | 24 ++++++++++++++++-------- src/token/token_validate_list.c | 30 ++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 src/token/token_validate_list.c diff --git a/inc/token.h b/inc/token.h index 51c7a15..dae5a76 100644 --- a/inc/token.h +++ b/inc/token.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/05 13:11:41 by qmennen #+# #+# */ -/* Updated: 2025/03/05 20:54:45 by qmennen ### ########.fr */ +/* Updated: 2025/03/18 16:14:51 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,5 +17,6 @@ t_token *token_from_list(t_list *list); t_list *token_list_index(t_list *tokens, int index); +t_token *token_validate_list(t_list *tokens); #endif diff --git a/src/lexer/lexer_read_word.c b/src/lexer/lexer_read_word.c index f1119a9..9b3ab0c 100644 --- a/src/lexer/lexer_read_word.c +++ b/src/lexer/lexer_read_word.c @@ -57,6 +57,31 @@ char *read_word(t_minishell *msh, t_lexer *lexer, int len) return (dest); } +int match_quotes(t_lexer *lexer) +{ + char c; + int i; + int sq; + int dq; + + sq = 0; + dq = 0; + i = lexer->pos; + c = lexer->current_char; + while (c && !ft_isspace(c) && !(c == '>' || c == '<' || c == '|')) + { + c = lexer->input[i]; + if (c == '\'') + sq++; + else if (c == '"') + dq++; + i++; + } + if (sq == 0 && dq == 0) + return (1); + return (dq % 2 == 0 && sq % 2 == 0); +} + char *lexer_readword(t_minishell *msh, t_lexer *lexer) { int len; @@ -68,6 +93,8 @@ char *lexer_readword(t_minishell *msh, t_lexer *lexer) qts = (c == '"' || c == '\''); if (qts) return (lexer_parse_quotes(msh, lexer)); + if (!match_quotes(lexer)) + return (NULL); len = calculate_word_len(lexer); word = read_word(msh, lexer, len); return (word); diff --git a/src/lexer/lexer_token_next.c b/src/lexer/lexer_token_next.c index 52deb97..53f24d6 100644 --- a/src/lexer/lexer_token_next.c +++ b/src/lexer/lexer_token_next.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* :::::::: */ -/* lexer_token_next.c :+: :+: */ -/* +:+ */ -/* By: qmennen +#+ */ -/* +#+ */ -/* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */ -/* Updated: 2025/02/26 16:14:10 by whaffman ######## odam.nl */ +/* ::: :::::::: */ +/* lexer_token_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */ +/* Updated: 2025/03/18 16:18:23 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,9 +31,7 @@ static t_token *process_word(t_minishell *msh, t_lexer *lexer, int pos) word_type = get_word_type(lexer->current_char); word = lexer_readword(msh, lexer); if (!word) - { return (token_new(msh, T_ERROR, &(lexer->current_char), pos)); - } token = token_new(msh, word_type, word, pos); free_safe(msh, (void **)&word); return (token); diff --git a/src/main.c b/src/main.c index e0220a6..6988c9d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,20 +1,20 @@ /* ************************************************************************** */ /* */ -/* :::::::: */ -/* main.c :+: :+: */ -/* +:+ */ -/* By: qmennen +#+ */ -/* +#+ */ -/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ -/* Updated: 2025/03/06 11:24:46 by whaffman ######## odam.nl */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ +/* Updated: 2025/03/18 16:20:18 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "minishell.h" static void main_loop(t_minishell *msh) { + t_token *error_token; while (TRUE) { msh->line = ft_prompt(msh); @@ -22,6 +22,14 @@ static void main_loop(t_minishell *msh) break ; msh->lexer = ft_lexer_new(msh); msh->tokens = ft_parse_input(msh); + error_token = token_validate_list(msh->tokens); + if (error_token) + { + printf(BOLD RED"minishell"RESET": syntax error near position %i\n", + (error_token->position + 1)); + free_minishell_line(msh); + continue; + } ft_lstiter(msh->tokens, token_print); msh->commands = parser_get_commands(msh); executor_execute_pipeline(msh); diff --git a/src/token/token_validate_list.c b/src/token/token_validate_list.c new file mode 100644 index 0000000..f82d780 --- /dev/null +++ b/src/token/token_validate_list.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* token_validate_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/18 16:08:44 by qmennen #+# #+# */ +/* Updated: 2025/03/18 16:13:41 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "token.h" + +t_token *token_validate_list(t_list *tokens) +{ + t_list *current; + t_token *token; + + current = tokens; + token = token_from_list(current); + while (current) + { + token = token_from_list(current); + if (token->type == T_ERROR) + return (token); + current = current->next; + } + return (NULL); +}