refactor: lexer

This commit is contained in:
Quinten Mennen 2025-02-27 16:18:57 +01:00
parent 4cdc9c3530
commit c2f062c671

View File

@ -11,7 +11,6 @@
/* ************************************************************************** */
#include "minishell.h"
#include "typedef.h"
static t_token_type get_word_type(char c)
{
@ -23,6 +22,21 @@ static t_token_type get_word_type(char c)
return (T_WORD);
}
static t_token *process_word(t_minishell *msh, t_lexer *lexer, int pos)
{
t_token_type word_type;
t_token *token;
char *word;
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);
}
/**
* @brief Retrieves the next token from the lexer.
*
@ -45,32 +59,23 @@ static t_token_type get_word_type(char c)
t_token *ft_token_next(t_minishell *msh, t_lexer *lexer)
{
t_token *token;
t_token_type word_type;
char *word;
int current_pos;
char c;
token = NULL;
while (ft_isspace(lexer->current_char))
lexer_readchar(lexer);
current_pos = lexer->pos;
if (lexer->current_char == '\0')
c = lexer->current_char;
if (c == '\0')
token = token_new(msh, T_EOF, NULL, current_pos);
else if (lexer->current_char == '<' || lexer->current_char == '>'
|| lexer->current_char == '|')
else if (c == '<' || c == '>'
|| c == '|')
token = token_parse(msh, lexer);
else if (ft_isprint(lexer->current_char))
{
word_type = get_word_type(lexer->current_char);
word = lexer_readword(msh, lexer);
if (!word)
return (token_new(msh, T_ERROR, &lexer->current_char, current_pos));
token = token_new(msh, word_type, word, current_pos);
free_safe(msh, (void **)&word);
}
token = process_word(msh, lexer, current_pos);
else
{
token = token_new(msh, T_ERROR, NULL, current_pos);
printf("token->type: %d\n", token->type);
}
c = lexer->current_char;
return (token);
}