comments.

main
bog 2024-04-01 14:13:36 +02:00
parent 7a10a7f330
commit d8be5d7b0f
3 changed files with 32 additions and 4 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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);
}