From f41317b598e59eefaa912f0d49fe2b1817573d88 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 3 Mar 2019 15:57:21 +0100 Subject: Cleanup, fix syntax def --- src/parser.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/parser.c b/src/parser.c index a3bd330..fb18685 100644 --- a/src/parser.c +++ b/src/parser.c @@ -84,17 +84,30 @@ static THROWABLE parser_parse_body_loop(Parser *self, Buffer *body_list, Token e } /* -LHS = ('const' TOK_IDENTIFIER '=') | - ('var' TOK_IDENTIFIER '='|';') +VAR_TYPE_DEF = ':' TOK_IDENTIFIER +*/ +static THROWABLE parser_parse_var_type_def(Parser *self, BufferView *type_name) { + bool match; + throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_COLON, &match)); + if(match) { + throw_if_error(tokenizer_accept(&self->tokenizer, TOK_IDENTIFIER)); + *type_name = self->tokenizer.value.identifier; + } + return PARSER_OK; +} + +/* +LHS = ('const' TOK_IDENTIFIER VAR_TYPE_DEF? '=') | + ('var' TOK_IDENTIFIER VAR_TYPE_DEF? '='|';') */ static THROWABLE parser_parse_lhs(Parser *self, LhsExpr **result, bool *assignment) { - bool isConst; + bool is_const; bool match; BufferView var_name; *result = NULL; - throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_CONST, &isConst)); - if(!isConst) { + throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_CONST, &is_const)); + if(!is_const) { bool isVar; throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_VAR, &isVar)); if(!isVar) @@ -105,13 +118,9 @@ static THROWABLE parser_parse_lhs(Parser *self, LhsExpr **result, bool *assignme var_name = self->tokenizer.value.identifier; amal_log_debug("var name: %.*s", (int)var_name.size, var_name.data); throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(LhsExpr), (void**)result)); - lhsexpr_init(*result, isConst, var_name); + lhsexpr_init(*result, is_const, var_name); - throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_COLON, &match)); - if(match) { - throw_if_error(tokenizer_accept(&self->tokenizer, TOK_IDENTIFIER)); - (*result)->type_name = self->tokenizer.value.identifier; - } + try(parser_parse_var_type_def(self, &(*result)->type_name)); throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_EQUALS, &match)); if(match) { @@ -121,7 +130,7 @@ static THROWABLE parser_parse_lhs(Parser *self, LhsExpr **result, bool *assignme *assignment = bool_false; throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_SEMICOLON, &match)); - if(match && isConst) { + if(match && is_const) { self->error = tokenizer_create_error(&self->tokenizer, "const variable declaration requires assignment (expected '=', got ';')"); throw(PARSER_UNEXPECTED_TOKEN); } -- cgit v1.2.3