From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1 3/4] lexer: add token lookahead capability
Date: Wed, 28 Feb 2024 20:04:20 +0100 [thread overview]
Message-ID: <20240228190956.78191-4-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240228190956.78191-1-johnny@johnnyrichard.com>
In order to skip line breaks (LF) we have to be able to spy the next
token without consume it.
This patch also adds a function to **lexer_peek_next** token, which is
equivalent to **lexer_look_ahead(n = 1)**.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/lexer.c | 22 ++++++++++++++++++++++
src/lexer.h | 6 ++++++
2 files changed, 28 insertions(+)
diff --git a/src/lexer.c b/src/lexer.c
index b107762..c7756a6 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -233,3 +233,25 @@ lexer_str_to_token_kind(string_view_t text)
return TOKEN_IDENTIFIER;
}
+
+void
+lexer_peek_next(lexer_t *lexer, token_t *token)
+{
+ lexer_lookahead(lexer, token, 1);
+}
+
+void
+lexer_lookahead(lexer_t *lexer, token_t *token, size_t n)
+{
+ size_t previous_offset = lexer->offset;
+ size_t previous_row = lexer->row;
+ size_t previous_bol = lexer->bol;
+
+ for (size_t i = 0; i < n; ++i) {
+ lexer_next_token(lexer, token);
+ }
+
+ lexer->offset = previous_offset;
+ lexer->row = previous_row;
+ lexer->bol = previous_bol;
+}
diff --git a/src/lexer.h b/src/lexer.h
index 8c09e02..729c957 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -68,6 +68,12 @@ lexer_init(lexer_t *lexer, string_view_t source);
void
lexer_next_token(lexer_t *lexer, token_t *token);
+void
+lexer_peek_next(lexer_t *lexer, token_t *token);
+
+void
+lexer_lookahead(lexer_t *lexer, token_t *token, size_t n);
+
char *
token_kind_to_cstr(token_kind_t kind);
--
2.43.2
next prev parent reply other threads:[~2024-02-28 18:10 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-28 19:04 [PATCH olang v1 0/4] create initial syntax analysis logic Johnny Richard
2024-02-28 19:04 ` [PATCH olang v1 1/4] string_view: add string view formatter for printf fmt Johnny Richard
2024-02-28 19:04 ` [PATCH olang v1 2/4] string_view: add string view conversion to uint32_t Johnny Richard
2024-02-29 15:16 ` Carlos Maniero
2024-02-29 22:40 ` Johnny Richard
2024-02-28 19:04 ` Johnny Richard [this message]
2024-02-28 19:04 ` [PATCH olang v1 4/4] parser: create simplified parser for tiny AST Johnny Richard
2024-02-28 18:11 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-03-01 3:34 ` [PATCH olang v1 4/4] parser: create simplified parser for tiny AST Carlos Maniero
2024-03-01 22:23 ` Johnny Richard
2024-03-01 22:33 ` [PATCH olang v1 0/4] create initial syntax analysis logic Johnny Richard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240228190956.78191-4-johnny@johnnyrichard.com \
--to=johnny@johnnyrichard.com \
--cc=~johnnyrichard/olang-devel@lists.sr.ht \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.johnnyrichard.com/olang.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox