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; 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]. * 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) 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() // we already created the upcoming token during the previous call to peek()
if (ctx->current_token != NULL) if (ctx->current_token != NULL)
{ {
return ctx->current_token; 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) while (i < ctx->remaining_chars)
{ {
// true if we didn't encounter a quote of any type at stream[i] // 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) if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL) && lexing_mode == LEXER_NORMAL)
{ {
update_flags(stream, i, ctx); update_flags(stream, i, &info);
if (is_special_char(stream, i)) if (is_special_char(stream, i))
{ {
if (i == 0) // where we create spe_char token if (i == 0) // where we create spe_char token
@ -137,7 +144,7 @@ struct token *peek_token(struct lexer_context *ctx)
i++; 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 token is comment, we don't want it
if (tok->type == TOKEN_COMMENT) 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) 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) if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF)
{ {
// we reached end of input, frees all the token still allocated. // 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); free_token(&ctx->current_token);
return NULL; 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) 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) if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL) && lexing_mode == LEXER_NORMAL)
{ {
update_flags(stream, i, &info);
if (is_special_char(stream, i)) if (is_special_char(stream, i))
{ {
if (i == 0) // where we create spe_char token 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) // (this should never happen)
if (ctx->current_token == NULL) 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); 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 /* @brief: Sets the token to an IO number
* Also allocates the data and fills it. * 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; 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)); struct token *tok = calloc(1, sizeof(struct token));
if (tok == NULL) if (tok == NULL)
return NULL; return NULL;
if (only_digits) if (info->only_digits)
set_token_ION(tok, begin, size); set_token_ION(tok, begin, size);
else if (info->has_equal)
set_token_operator(tok, begin, size); set_token_assignment(tok, begin, size);
set_token_spechar(tok, begin, size); else
set_token_keyword(tok, begin, size); {
set_token_word(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);
}
return tok; return tok;
} }

View file

@ -10,13 +10,6 @@ struct lexer_context
char *end_previous_token; char *end_previous_token;
ssize_t remaining_chars; 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 *previous_token;
struct token *current_token; struct token *current_token;
}; };
@ -57,10 +50,6 @@ enum token_type
TOKEN_PIPE, TOKEN_PIPE,
TOKEN_NEGATION, TOKEN_NEGATION,
// TODO merge into one and use the data field
// (Too difficult to handle in the parser because of firsts)
// TOKEN_REDIRECTION
//
// Redirections // Redirections
TOKEN_REDIR_LEFT, TOKEN_REDIR_LEFT,
TOKEN_REDIR_RIGHT, TOKEN_REDIR_RIGHT,
@ -88,18 +77,29 @@ struct token
char *data; 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], /* @return: true if a special character from the grammar was found at stream[i],
* false otherwise. * false otherwise.
*/ */
bool is_special_char(char *stream, ssize_t i); bool is_special_char(char *stream, ssize_t i);
/* @brief: return a newly allocated token, with the type corresponding /* @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]. * The data contains [size] char, starting from [begin].
* *
* @return: NULL on error, a token otherwise. * @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 /* @brief: frees the token given in argument
*/ */

View file

@ -7,6 +7,7 @@
#include <string.h> #include <string.h>
#include "grammar_basic.h" #include "grammar_basic.h"
#include "grammar.h"
static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type) 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 ast *parse_prefix(struct lexer_context *ctx)
{ {
struct token *token = TOKEN_PEEK(); struct token *token = PEEK_TOKEN();
if (token->type == TOKEN_ASSIGNMENT_WORD) if (token->type == TOKEN_ASSIGNMENT_WORD)
{ {
token = TOKEN_POP(); token = POP_TOKEN();
return ast_create_assignment_word(token->data); return ast_create_assignment_word(token->data);
} }
else if (is_first(*token, RULE_REDIRECTION)) 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) if (prefix->type == AST_ASSIGNEMENT)
{ {
assignments = list_append(assignments, prefix) assignments = list_append(assignments, prefix);
} }
else if (prefix->type == AST_REDIR) 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. * Creates a new AST node representing a command.
*/ */
struct ast *ast_create_command(struct list *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. * @brief: frees the given ast_command and sets the pointer to NULL.