aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/parser.c33
1 files 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);
}