diff --git a/src/lexer/lexer_token_next.c b/src/lexer/lexer_token_next.c index aa57eae..99dfdea 100644 --- a/src/lexer/lexer_token_next.c +++ b/src/lexer/lexer_token_next.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */ -/* Updated: 2025/02/05 19:25:35 by qmennen ### ########.fr */ +/* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,22 +39,22 @@ t_token *ft_token_next(t_lexer *lexer) token = NULL; while (ft_isspace(lexer->current_char)) - ft_lexer_readchar(lexer); + lexer_readchar(lexer); current_pos = lexer->pos; if (lexer->current_char == '\0') - token = ft_token_new(T_EOF, NULL, current_pos); + token = token_new(T_EOF, NULL, current_pos); else if (lexer->current_char == '<' || lexer->current_char == '>' || lexer->current_char == '|') - token = ft_parse_token(lexer); + token = token_parse(lexer); else if (ft_isprint(lexer->current_char)) { - word = ft_lexer_readword(lexer); + word = lexer_readword(lexer); if (!word) - return (ft_token_new(T_ERROR, &lexer->current_char, current_pos)); - token = ft_token_new(T_WORD, word, current_pos); + return (token_new(T_ERROR, &lexer->current_char, current_pos)); + token = token_new(T_WORD, word, current_pos); free(word); } else - token = ft_token_new(T_ERROR, NULL, current_pos); + token = token_new(T_ERROR, NULL, current_pos); return (token); } diff --git a/src/token/token_parse.c b/src/token/token_parse.c index 5ce93aa..7a9d561 100644 --- a/src/token/token_parse.c +++ b/src/token/token_parse.c @@ -6,39 +6,69 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/05 19:10:17 by qmennen #+# #+# */ -/* Updated: 2025/02/11 17:38:51 by qmennen ### ########.fr */ +/* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_token *ft_parse_token(t_lexer *lexer) +static t_token_type token_from_char(char c, int is_double) { - t_token *token; + if (c == '<') + { + if (is_double) + return (T_HEREDOC); + return (T_REDIRECT_IN); + } + else if (c == '>') + { + if (is_double) + return (T_APPEND_OUT); + return (T_REDIRECT_OUT); + } + else if (c == '&' && is_double) + return (T_AND); + else if (c == '|') + { + if (is_double) + return (T_OR); + return (T_PIPE); + } + return (T_ERROR); +} - token = NULL; - if (lexer->current_char == '|') - { - token = ft_token_new(T_PIPE, "|", lexer->pos); - } - else if (lexer->current_char == '<' && lexer->input[lexer->pos + 1] == '<') - { - token = ft_token_new(T_HEREDOC, "<<", lexer->pos); - ft_lexer_readchar(lexer); - } - else if (lexer->current_char == '<') - { - token = ft_token_new(T_REDIRECT_IN, "<", lexer->pos); - } - else if (lexer->current_char == '>' && lexer->input[lexer->pos + 1] == '>') - { - token = ft_token_new(T_APPEND_OUT, ">>", lexer->pos); - ft_lexer_readchar(lexer); - } - else if (lexer->current_char == '>') - { - token = ft_token_new(T_REDIRECT_OUT, ">", lexer->pos); - } - ft_lexer_readchar(lexer); +static char *char_from_type(t_token_type type) +{ + if (type == T_HEREDOC) + return ("<<"); + else if (type == T_REDIRECT_IN) + return ("<"); + else if (type == T_APPEND_OUT) + return (">>"); + else if (type == T_REDIRECT_OUT) + return (">"); + else if (type == T_AND) + return ("&&"); + else if (type == T_OR) + return ("||"); + else if (type == T_PIPE) + return ("|"); + return (NULL); +} + +t_token *token_parse(t_lexer *lexer) +{ + int is_double; + char c; + t_token *token; + t_token_type type; + + c = lexer->current_char; + is_double = lexer->input[lexer->pos + 1] == c; + type = token_from_char(c, is_double); + token = token_new(type, char_from_type(type), lexer->pos); + if (is_double) + lexer_readchar(lexer); + lexer_readchar(lexer); return (token); }