diff --git a/src/parser/lexer.c b/src/parser/lexer.c index bca5866..cf47b98 100644 --- a/src/parser/lexer.c +++ b/src/parser/lexer.c @@ -6,7 +6,7 @@ /* By: sede-san value); + if (type != TOKEN_WORD) + token = read_token(type, line, start); + else + token = read_word(line, start); return (token); } -static t_token_type get_token_type(const char *str) +static t_token_type get_token_type( + const char *str +) { - size_t i; - static const t_map_entry tokens[TOKENS_COUNT] = { - {PIPE_STR, (void *)TOKEN_PIPE}, - {REDIRECT_IN_STR, (void *)TOKEN_REDIRECT_IN}, - {REDIRECT_OUT_STR, (void *)TOKEN_REDIRECT_OUT}, - {APPEND_STR, (void *)TOKEN_APPEND}, - {HEREDOC_STR, (void *)TOKEN_HEREDOC} - }; - - i = 0; - while (i < TOKENS_COUNT) + if (str == NULL || str[0] == '\0') + return (TOKEN_WORD); + if (str[0] == '|') + return (TOKEN_PIPE); + if (str[0] == '<') { - if (ft_strcmp(str, tokens[i].key) == 0) - return ((t_token_type)tokens[i].value); - i++; + if (str[1] == '<') + return (TOKEN_HEREDOC); + return (TOKEN_REDIRECT_IN); + } + if (str[0] == '>') + { + if (str[1] == '>') + return (TOKEN_APPEND); + return (TOKEN_REDIRECT_OUT); } return (TOKEN_WORD); } + +static t_token *token_new( + t_token_type type, + const char *text) +{ + t_token *token; + + token = (t_token *)malloc(sizeof(t_token)); + if (token == NULL) + return (NULL); + ft_putendl("malloc"); + token->type = type; + token->value = text; + if (token->type == TOKEN_WORD && token->value == NULL) + { + free(token); + return (NULL); + } + return (token); +} + +static void token_clear( + t_token *token) +{ + if (token != NULL) + { + if (token->value != NULL) + { + free(token->value); + ft_putendl("free"); + } + free(token); + ft_putendl("free"); + } +} + +static t_token *read_token( + t_token_type type, + const char *line, + size_t *i) +{ + while (ft_isspace(line[*i]) || is_meta(line[*i])) + (*i)++; + return (token_new(type, NULL)); +} + +static t_token *read_word( + const char *line, + size_t *i) +{ + const size_t start = *i; + bool in_single_quote; + bool in_double_quote; + + in_single_quote = false; + in_double_quote = false; + while (line[*i] != '\0') + { + char c = line[*i]; + (void)c; + if (line[*i] == '\'' && !in_double_quote) + in_single_quote = !in_single_quote; + else if (line[*i] == '"' && !in_single_quote) + in_double_quote = !in_double_quote; + else if (!in_single_quote && !in_double_quote && (isspace(line[*i]) || is_meta(line[*i]))) + break; + (*i)++; + } + return (token_new(TOKEN_WORD, ft_substr(line, start, *i - start))); +} + +static inline bool is_meta(char c) +{ + return (c == '|' || c == '<' || c == '>'); +}