diff options
author | Johnny Richard <johnny@johnnyrichard.com> | 2023-05-01 01:57:20 +0200 |
---|---|---|
committer | Carlos Maniero <carlos@maniero.me> | 2023-05-01 18:08:47 -0300 |
commit | 1d9981e5879260d9f158f5b3d9e13b12df43c68e (patch) | |
tree | 0c30a7b9210f7d47aa6ff259e2271651d3173321 /src | |
parent | 7ad1b0f3d676441ea14d7d261e730398875ae114 (diff) |
lexer: Peek next token
The only way to get the next token was by consuming it. So then, our
parser starts to become hard to understand, once sometimes we just
want to take a look on the next token to understand what should be the
next kind of expression.
This commit introduces a new function that will help us to improve our
parser implementation.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Reviewed-by: Carlos Maniero <carlos@maniero.me>
Diffstat (limited to 'src')
-rw-r--r-- | src/lexer.c | 14 | ||||
-rw-r--r-- | src/lexer.h | 3 |
2 files changed, 17 insertions, 0 deletions
diff --git a/src/lexer.c b/src/lexer.c index 3730620..f937170 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -218,6 +218,20 @@ lexer_load_file_contents(lexer_t *lexer) } void +lexer_peek_next_token(lexer_t *lexer, token_t *token) +{ + uint32_t cur = lexer->cur; + uint32_t row = lexer->row; + uint32_t bol = lexer->bol; + + lexer_next_token(lexer, token); + + lexer->cur = cur; + lexer->row = row; + lexer->bol = bol; +} + +void lexer_step_back_to(lexer_t *lexer, token_t *token) { lexer->cur = token->bol + token->col; diff --git a/src/lexer.h b/src/lexer.h index d4e84e1..0b9f2ad 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -85,6 +85,9 @@ lexer_drop_char(lexer_t *lexer); void lexer_step_back_to(lexer_t *lexer, token_t *token); +void +lexer_peek_next_token(lexer_t *lexer, token_t *token); + char * token_kind_to_str(token_kind_t kind); |