feat (lexer): print syntax error

This commit is contained in:
Quinten Mennen 2025-03-18 16:22:03 +01:00
parent df327180cb
commit 6b9e09fb55
5 changed files with 82 additions and 18 deletions

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View File

@ -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);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* lexer_token_next.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* 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 <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -1,20 +1,20 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* main.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */
/* Updated: 2025/03/06 11:24:46 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token_validate_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}