comments using ';'.

main
bog 2024-03-29 10:04:13 +01:00
parent 8317634e06
commit 71b6b2eabf
4 changed files with 28 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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