feat(lexer + grammar): inch les redirections ca marche

This commit is contained in:
matteo 2026-01-29 18:21:44 +01:00
parent 28749a2992
commit 25079bfebf
6 changed files with 77 additions and 46 deletions

View file

@ -78,36 +78,43 @@ static bool update_lexing_mode(char *stream, ssize_t i,
return *lexing_mode != mode_before_update;
}
/* @brief: updates the flags only_digits and equal_count
/* @brief: updates the flags only_digits and has_equal.
* according to the character at stream[i].
*/
static void update_flags(char *stream, ssize_t i, struct lexer_context *ctx)
static void update_flags(char *stream, ssize_t i, struct token_info *info)
{
if (stream[i] == '=')
if (stream[i] == '=' && !info->has_equal)
{
ctx->equal_count++;
if (i == 0)
{
perror("Syntax error: word start with a '='");
return;
}
else
info->has_equal = true;
}
else if (!isdigit(stream[i]) && ctx->only_digits)
else if (!isdigit(stream[i]) && info->only_digits)
{
ctx->only_digits = false;
info->only_digits = false;
}
}
struct token *peek_token(struct lexer_context *ctx)
{
stream_init(ctx);
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
struct token_info info = {true, 0};
char *stream = ctx->end_previous_token;
ssize_t i = 0;
// we already created the upcoming token during the previous call to peek()
if (ctx->current_token != NULL)
{
return ctx->current_token;
}
stream_init(ctx);
char *stream = ctx->end_previous_token;
ssize_t i = 0;
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
while (i < ctx->remaining_chars)
{
// true if we didn't encounter a quote of any type at stream[i]
@ -115,7 +122,7 @@ struct token *peek_token(struct lexer_context *ctx)
if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL)
{
update_flags(stream, i, ctx);
update_flags(stream, i, &info);
if (is_special_char(stream, i))
{
if (i == 0) // where we create spe_char token
@ -137,7 +144,7 @@ struct token *peek_token(struct lexer_context *ctx)
i++;
}
struct token *tok = new_token(stream, i, ctx->only_digits, ctx->equal_count);
struct token *tok = new_token(stream, i, &info);
// if token is comment, we don't want it
if (tok->type == TOKEN_COMMENT)
@ -154,6 +161,15 @@ struct token *peek_token(struct lexer_context *ctx)
struct token *pop_token(struct lexer_context *ctx)
{
stream_init(ctx);
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
struct token_info info = {true, 0};
char *stream = ctx->end_previous_token;
ssize_t i = 0;
if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF)
{
// we reached end of input, frees all the token still allocated.
@ -161,12 +177,6 @@ struct token *pop_token(struct lexer_context *ctx)
free_token(&ctx->current_token);
return NULL;
}
stream_init(ctx);
char *stream = ctx->end_previous_token;
ssize_t i = 0;
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
while (i < ctx->remaining_chars)
{
@ -175,6 +185,7 @@ struct token *pop_token(struct lexer_context *ctx)
if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL)
{
update_flags(stream, i, &info);
if (is_special_char(stream, i))
{
if (i == 0) // where we create spe_char token
@ -200,7 +211,7 @@ struct token *pop_token(struct lexer_context *ctx)
// (this should never happen)
if (ctx->current_token == NULL)
{
ctx->current_token = new_token(stream, i, ctx->only_digits);
ctx->current_token = new_token(stream, i, &info);
}
save_state(stream, i, ctx);

View file

@ -146,6 +146,21 @@ static void set_token_word(struct token *tok, char *begin, ssize_t size)
}
}
/* @brief: Sets the token to an assignment_word
* Also allocates the data and fills it.
*/
static void set_token_assignment(struct token *tok, char *begin, ssize_t size)
{
if (tok->type == TOKEN_NULL && size != 0)
{
tok->type = TOKEN_ASSIGNMENT_WORD;
tok->data = calloc(size + 1, sizeof(char));
if (tok->data == NULL)
return;
strncpy(tok->data, begin, size);
}
}
/* @brief: Sets the token to an IO number
* Also allocates the data and fills it.
*/
@ -204,19 +219,23 @@ bool is_special_char(char *stream, ssize_t i)
return strchr(special_chars, c) != NULL;
}
struct token *new_token(char *begin, ssize_t size, bool only_digits)
struct token *new_token(char *begin, ssize_t size, struct token_info *info)
{
struct token *tok = calloc(1, sizeof(struct token));
if (tok == NULL)
return NULL;
if (only_digits)
if (info->only_digits)
set_token_ION(tok, begin, size);
set_token_operator(tok, begin, size);
set_token_spechar(tok, begin, size);
set_token_keyword(tok, begin, size);
set_token_word(tok, begin, size);
else if (info->has_equal)
set_token_assignment(tok, begin, size);
else
{
set_token_operator(tok, begin, size);
set_token_spechar(tok, begin, size);
set_token_keyword(tok, begin, size);
set_token_word(tok, begin, size);
}
return tok;
}

View file

@ -10,13 +10,6 @@ struct lexer_context
char *end_previous_token;
ssize_t remaining_chars;
// usefull to detect IO numbers.
// tells us if we only lexed digits in current token.
bool only_digits;
// usefull to detect assignments, and syntax errors with '='.
int equal_count;
struct token *previous_token;
struct token *current_token;
};
@ -57,10 +50,6 @@ enum token_type
TOKEN_PIPE,
TOKEN_NEGATION,
// TODO merge into one and use the data field
// (Too difficult to handle in the parser because of firsts)
// TOKEN_REDIRECTION
//
// Redirections
TOKEN_REDIR_LEFT,
TOKEN_REDIR_RIGHT,
@ -88,18 +77,29 @@ struct token
char *data;
};
// used to give info from lexing when creating a new token.
struct token_info
{
// usefull to detect IO numbers.
// tells us if we only lexed digits in current token.
bool only_digits;
// usefull to detect assignments, and syntax errors with '='.
bool has_equal;
};
/* @return: true if a special character from the grammar was found at stream[i],
* false otherwise.
*/
bool is_special_char(char *stream, ssize_t i);
/* @brief: return a newly allocated token, with the type corresponding
* to the context given in arguments.
* to the info given in arguments.
* The data contains [size] char, starting from [begin].
*
* @return: NULL on error, a token otherwise.
*/
struct token *new_token(char *begin, ssize_t size, bool only_digits, int equal_count);
struct token *new_token(char *begin, ssize_t size, struct token_info *info);
/* @brief: frees the token given in argument
*/

View file

@ -7,6 +7,7 @@
#include <string.h>
#include "grammar_basic.h"
#include "grammar.h"
static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type)
{
@ -66,10 +67,10 @@ struct ast *parse_redirection(struct lexer_context *ctx)
struct ast *parse_prefix(struct lexer_context *ctx)
{
struct token *token = TOKEN_PEEK();
struct token *token = PEEK_TOKEN();
if (token->type == TOKEN_ASSIGNMENT_WORD)
{
token = TOKEN_POP();
token = POP_TOKEN();
return ast_create_assignment_word(token->data);
}
else if (is_first(*token, RULE_REDIRECTION))

View file

@ -200,7 +200,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
}
if (prefix->type == AST_ASSIGNEMENT)
{
assignments = list_append(assignments, prefix)
assignments = list_append(assignments, prefix);
}
else if (prefix->type == AST_REDIR)
{

View file

@ -26,7 +26,7 @@ struct ast_command *ast_get_command(struct ast *node);
* Creates a new AST node representing a command.
*/
struct ast *ast_create_command(struct list *command,
struct list *redirections, struct ast_list *assignements);
struct list *redirections, struct list *assignements);
/*
* @brief: frees the given ast_command and sets the pointer to NULL.