From d8be5d7b0f8f187df4d4a86245660a8482ebb238 Mon Sep 17 00:00:00 2001 From: bog Date: Mon, 1 Apr 2024 14:13:36 +0200 Subject: [PATCH] :sparkles: comments. --- lib/include/lexer.h | 2 ++ lib/src/lexer.c | 31 +++++++++++++++++++++++++++++-- lib/src/parser.c | 3 +-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/include/lexer.h b/lib/include/lexer.h index 1d80b90..815ba80 100644 --- a/lib/include/lexer.h +++ b/lib/include/lexer.h @@ -20,7 +20,9 @@ struct lexer void lexer_init(struct lexer* self, char const* source); void lexer_free(struct lexer* self); +bool lexer_is_at_end(struct lexer* self); void lexer_skip_spaces(struct lexer* self); +void lexer_skip_comments(struct lexer* self); bool lexer_is_sep(struct lexer* self, size_t index); bool lexer_next_is(struct lexer* self, TokenKind kind); diff --git a/lib/src/lexer.c b/lib/src/lexer.c index 8c6e2a9..1931ece 100644 --- a/lib/src/lexer.c +++ b/lib/src/lexer.c @@ -31,6 +31,13 @@ void lexer_free(struct lexer* self) } } +bool lexer_is_at_end(struct lexer* self) +{ + assert(self); + lexer_skip_comments(self); + return self->context.cursor >= self->len; +} + void lexer_skip_spaces(struct lexer* self) { assert(self); @@ -47,6 +54,25 @@ void lexer_skip_spaces(struct lexer* self) } } +void lexer_skip_comments(struct lexer* self) +{ + assert(self); + + lexer_skip_spaces(self); + + while (self->context.cursor < self->len + && self->source[self->context.cursor] == '#') + { + while (self->context.cursor < self->len + && self->source[self->context.cursor] != '\n') + { + self->context.cursor++; + } + + lexer_skip_spaces(self); + } +} + bool lexer_is_sep(struct lexer* self, size_t index) { if (index >= self->len) { return true; } @@ -98,13 +124,13 @@ struct token* lexer_try_new_next(struct lexer* self) { assert(self); + lexer_skip_comments(self); + if (!errors_ok()) { return NULL; } - lexer_skip_spaces(self); - struct token* tok = NULL; if ( (tok=lexer_try_scan_int(self)) ) @@ -128,6 +154,7 @@ struct token* lexer_try_new_next(struct lexer* self) SK_SCAN_KEYWORD("or", TOKEN_OR, NULL); SK_SCAN_KEYWORD("not", TOKEN_NOT, NULL); + if (self->context.cursor < self->len) { struct str str; diff --git a/lib/src/parser.c b/lib/src/parser.c index 0a6b6b1..230b951 100644 --- a/lib/src/parser.c +++ b/lib/src/parser.c @@ -50,10 +50,9 @@ struct node* parser_try_root(struct parser* self) node_init(node, NODE_ROOT, tok); - while (true) + while (!lexer_is_at_end(&self->lexer)) { struct node* expr = SK_TRY(parser_try_expr); - if (!expr) { break; } node_push_new_child(node, expr); }