fix(lexer): $# and backslash implemented (partial version)

This commit is contained in:
matteo 2026-01-23 20:11:21 +01:00
parent 0c80663d7c
commit e853249fbf
3 changed files with 15 additions and 11 deletions

View file

@ -100,10 +100,10 @@ struct token *peek_token(struct lexer_context *ctx)
if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL)
{
if (is_special_char(stream[i]))
if (is_special_char(stream, i))
{
if (i == 0) // where we create spe_char token
i++;
i += len_op_sepchar(stream, i);
break;
}
if (isblank(stream[i]))
@ -159,10 +159,7 @@ struct token *pop_token(struct lexer_context *ctx)
if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL)
{
// TODO call here a function
// it must check if is a spe char or an operator
// and sets i accordingly.
if (is_special_char(stream[i]))
if (is_special_char(stream, i))
{
if (i == 0) // where we create spe_char token
i += len_op_sepchar(stream, i);

View file

@ -178,12 +178,19 @@ static bool is_end_of_line(char c)
return c == EOF || c == '\n';
}
bool is_special_char(char c)
bool is_special_char(char *stream, ssize_t i)
{
char c = stream[i];
if (c == EOF)
return true;
if (i > 0 && c == '#' && stream[i - 1] == '$')
return false; // the edge case of $#
if (i > 0 && stream[i - 1] == '\\')
return false; // TODO handle backslash better
// this doesnt work, ex : echo \\#comment
// (need to count the previous consequtive backslashes)
char special_chars[] = "\n'\"`;#|&\\(){}<>*";
char special_chars[] = "\n'\"`;#|&(){}<>*";
return strchr(special_chars, c) != NULL;
}
@ -247,7 +254,7 @@ void stream_init(struct lexer_context *ctx)
ssize_t len_op_sepchar(char *stream, ssize_t i)
{
if (!is_special_char(stream[i]))
if (!is_special_char(stream, i))
return -1; // should never happen
if (stream[i] != '>' && stream[i] != '<')

View file

@ -80,10 +80,10 @@ struct token
char *data;
};
/* @return: true if a special character from the grammar was found,
/* @return: true if a special character from the grammar was found at stream[i],
* false otherwise.
*/
bool is_special_char(char c);
bool is_special_char(char *stream, ssize_t i);
/* @brief: return a newly allocated token, with the corresponding type.
* The data contains [size] char, starting from [begin].