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);