fix(lexer): $# and backslash implemented (partial version)
This commit is contained in:
parent
0c80663d7c
commit
e853249fbf
3 changed files with 15 additions and 11 deletions
|
|
@ -100,10 +100,10 @@ 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)
|
||||||
{
|
{
|
||||||
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
|
||||||
i++;
|
i += len_op_sepchar(stream, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (isblank(stream[i]))
|
if (isblank(stream[i]))
|
||||||
|
|
@ -159,10 +159,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)
|
||||||
{
|
{
|
||||||
// TODO call here a function
|
if (is_special_char(stream, i))
|
||||||
// it must check if is a spe char or an operator
|
|
||||||
// and sets i accordingly.
|
|
||||||
if (is_special_char(stream[i]))
|
|
||||||
{
|
{
|
||||||
if (i == 0) // where we create spe_char token
|
if (i == 0) // where we create spe_char token
|
||||||
i += len_op_sepchar(stream, i);
|
i += len_op_sepchar(stream, i);
|
||||||
|
|
|
||||||
|
|
@ -178,12 +178,19 @@ static bool is_end_of_line(char c)
|
||||||
return c == EOF || c == '\n';
|
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)
|
if (c == EOF)
|
||||||
return true;
|
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;
|
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)
|
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
|
return -1; // should never happen
|
||||||
|
|
||||||
if (stream[i] != '>' && stream[i] != '<')
|
if (stream[i] != '>' && stream[i] != '<')
|
||||||
|
|
|
||||||
|
|
@ -80,10 +80,10 @@ struct token
|
||||||
char *data;
|
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.
|
* 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.
|
/* @brief: return a newly allocated token, with the corresponding type.
|
||||||
* The data contains [size] char, starting from [begin].
|
* The data contains [size] char, starting from [begin].
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue