save commit
This commit is contained in:
79
src/parser/lexer.c
Normal file
79
src/parser/lexer.c
Normal file
@@ -0,0 +1,79 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lexer.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/09 18:56:41 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/09 20:42:50 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "core.h"
|
||||
#include "parser.h"
|
||||
|
||||
static t_token *tokenize(const char *line, size_t *start);
|
||||
static t_token_type get_token_type(const char *str);
|
||||
|
||||
t_list *lex(
|
||||
const char *line
|
||||
) {
|
||||
t_list *tokens;
|
||||
t_token *token;
|
||||
size_t i;
|
||||
|
||||
tokens = NULL;
|
||||
i = 0;
|
||||
while (line[i] != '\0')
|
||||
{
|
||||
// ignore spaces
|
||||
while (ft_isspace(line[i]))
|
||||
i++;
|
||||
// create token
|
||||
token = tokenize(line, &i);
|
||||
// add token to list
|
||||
if (token != NULL)
|
||||
ft_lstadd_back(&tokens, ft_lstnew(token));
|
||||
}
|
||||
return (tokens);
|
||||
}
|
||||
|
||||
static t_token *tokenize(const char *line, size_t *start) {
|
||||
t_token *token;
|
||||
t_token_type type;
|
||||
|
||||
token = NULL;
|
||||
if (line == NULL || line[*start] == '\0')
|
||||
return (NULL);
|
||||
type = get_token_type(line + *start);
|
||||
(void)type;
|
||||
// if (type != TOKEN_WORD)
|
||||
// token = token_new(type, NULL);
|
||||
// else
|
||||
// token = read_word(line, start);
|
||||
// if (token == NULL)
|
||||
// (*start) += ft_strlen(token->value);
|
||||
return (token);
|
||||
}
|
||||
|
||||
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 (ft_strcmp(str, tokens[i].key) == 0)
|
||||
return ((t_token_type)tokens[i].value);
|
||||
i++;
|
||||
}
|
||||
return (TOKEN_WORD);
|
||||
}
|
||||
Reference in New Issue
Block a user