minishell/src/lexer/lexer_token_next.c
2025-02-18 17:03:11 +01:00

61 lines
2.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_token_next.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */
/* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/**
* @brief Retrieves the next token from the lexer.
*
* This function reads the next token from the lexer, skipping any whitespace
* characters. It handles different types of tokens such as end-of-file (EOF),
* special characters ('<', '>', '|'), printable characters, and errors.
*
* @param lexer A pointer to the lexer structure.
* @return A pointer to the newly created token.
*
* The function performs the following steps:
* 1. Skips any whitespace characters.
* 2. Checks the current character in the lexer:
* - If it is the end-of-file character ('\0'), creates an EOF token.
* - If it is a special character ('<', '>', '|'), parses
* the token accordingly.
* - If it is a printable character, reads the word and creates a word token.
* - Otherwise, creates an error token.
*/
t_token *ft_token_next(t_lexer *lexer)
{
t_token *token;
char *word;
int current_pos;
token = NULL;
while (ft_isspace(lexer->current_char))
lexer_readchar(lexer);
current_pos = lexer->pos;
if (lexer->current_char == '\0')
token = token_new(T_EOF, NULL, current_pos);
else if (lexer->current_char == '<' || lexer->current_char == '>'
|| lexer->current_char == '|')
token = token_parse(lexer);
else if (ft_isprint(lexer->current_char))
{
word = lexer_readword(lexer);
if (!word)
return (token_new(T_ERROR, &lexer->current_char, current_pos));
token = token_new(T_WORD, word, current_pos);
free(word);
}
else
token = token_new(T_ERROR, NULL, current_pos);
return (token);
}