diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 6c46f51..ff7f351 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -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); diff --git a/src/lexer/lexer_utils.c b/src/lexer/lexer_utils.c index d80134a..e4a9bab 100644 --- a/src/lexer/lexer_utils.c +++ b/src/lexer/lexer_utils.c @@ -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] != '<') diff --git a/src/lexer/lexer_utils.h b/src/lexer/lexer_utils.h index aa8941a..6173485 100644 --- a/src/lexer/lexer_utils.h +++ b/src/lexer/lexer_utils.h @@ -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].