From 71b6b2eabfadafcb987039f7b41f0ab79ab6f379 Mon Sep 17 00:00:00 2001 From: bog Date: Fri, 29 Mar 2024 10:04:13 +0100 Subject: [PATCH] :sparkles: comments using ';'. --- lib/lexer.c | 21 ++++++++++++++++++++- lib/lexer.h | 1 + lib/parser.c | 7 +++++-- lib/token.h | 3 ++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/lexer.c b/lib/lexer.c index ccdfa6d..198fe72 100644 --- a/lib/lexer.c +++ b/lib/lexer.c @@ -33,12 +33,13 @@ struct token* lexer_try_new_next(struct lexer* self) assert(self); struct token* tok = NULL; - if (!status_is_ok(self->status) > 0) + if (!status_is_ok(self->status)) { return NULL; } lexer_skip_spaces(self); + lexer_skip_comments(self); if ( (tok=lexer_try_new_text(self, TOKEN_OPAR, "(")) ) { @@ -147,6 +148,23 @@ void lexer_skip_spaces(struct lexer* self) } } +void lexer_skip_comments(struct lexer* self) +{ + assert(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); + } +} + struct token* lexer_try_new_int(struct lexer* self) { assert(self); @@ -527,5 +545,6 @@ bool lexer_end(struct lexer* self) { assert(self); lexer_skip_spaces(self); + lexer_skip_comments(self); return self->context.cursor >= self->len; } diff --git a/lib/lexer.h b/lib/lexer.h index bb8a901..f89c87d 100644 --- a/lib/lexer.h +++ b/lib/lexer.h @@ -30,6 +30,7 @@ void lexer_free(struct lexer* self); struct token* lexer_try_new_next(struct lexer* self); void lexer_skip(struct lexer* self, TokenKind kind); void lexer_skip_spaces(struct lexer* self); +void lexer_skip_comments(struct lexer* self); struct token* lexer_try_new_int(struct lexer* self); struct token* lexer_try_new_float(struct lexer* self); diff --git a/lib/parser.c b/lib/parser.c index b01027f..48fbdef 100644 --- a/lib/parser.c +++ b/lib/parser.c @@ -29,7 +29,7 @@ struct node* parser_try(struct parser* self, struct lex_context ctx = lexer_state(self->lexer); struct node* node = rule(self); - + if (node == NULL) { lexer_restore(self->lexer, ctx); @@ -47,8 +47,10 @@ struct node* parser_try_new_root(struct parser* self) while (!lexer_end(self->lexer)) { struct node* expr = MK_TRY(parser_try_new_expr); + if (!expr && !lexer_end(self->lexer)) { + printf("not at end: %c\n", self->lexer->source[self->lexer->context.cursor]); node_free(root); free(root); return NULL; @@ -117,7 +119,6 @@ struct node* parser_try_new_call(struct parser* self) } lexer_skip(self->lexer, TOKEN_CPAR); - return call; } @@ -184,6 +185,8 @@ struct node* parser_try_new_atom(struct parser* self) self->lexer->context.line); return node; } + + return NULL; } diff --git a/lib/token.h b/lib/token.h index 62aa117..453e3c6 100644 --- a/lib/token.h +++ b/lib/token.h @@ -7,7 +7,8 @@ G(TOKEN_INT), G(TOKEN_FLOAT), \ G(TOKEN_BOOL), G(TOKEN_STRING), \ G(TOKEN_SYMBOL), G(TOKEN_IDENT), \ -G(TOKEN_OPAR), G(TOKEN_CPAR) +G(TOKEN_OPAR), G(TOKEN_CPAR), \ +G(TOKEN_EOF) MK_ENUM_H(TokenKind, TOKEN_KIND);