From 1d9981e5879260d9f158f5b3d9e13b12df43c68e Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Mon, 1 May 2023 01:57:20 +0200 Subject: 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 Reviewed-by: Carlos Maniero --- src/lexer.c | 14 ++++++++++++++ src/lexer.h | 3 +++ 2 files changed, 17 insertions(+) (limited to 'src') diff --git a/src/lexer.c b/src/lexer.c index 3730620..f937170 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -217,6 +217,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) { 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); -- cgit v1.2.3