even more cleanup in the parser
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 1 Feb 2021 05:26:48 +0000 (23:26 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 1 Feb 2021 05:26:48 +0000 (23:26 -0600)
bin/onyx
onyx.exe
src/onyxparser.c

index e0c7dc408118e76d6f9a05b3770e82e94e6ee40a..672ceec0aae42b55901d8c7ee2cd70a16c8d954f 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 5ee288b29298cd2072dcd501ad25474c5d6d7341..20936ed87db8cd1dc94548059f408a6d80fe4211 100644 (file)
Binary files a/onyx.exe and b/onyx.exe differ
index 524573c2cabe787bd7f34ba5871271f6672f658e..3e98425a513406c75327485e2696dbdbeac6fed4 100644 (file)
@@ -731,9 +731,8 @@ static AstTyped* parse_compound_expression(OnyxParser* parser, b32 assignment_al
         bh_arr_new(global_heap_allocator, compound->exprs, 2);
         bh_arr_push(compound->exprs, first);
 
-        while (parser->curr->type == ',') {
+        while (consume_token_if_next(parser, ',')) {
             if (parser->hit_unexpected_token) return (AstTyped *) compound;
-            consume_token(parser);
 
             AstTyped* expr = parse_expression(parser, 0);
             bh_arr_push(compound->exprs, expr);
@@ -841,10 +840,9 @@ static AstIfWhile* parse_if_stmt(OnyxParser* parser) {
     if (true_stmt != NULL)
         if_node->true_stmt = true_stmt;
 
-    while (parser->curr->type == Token_Type_Keyword_Elseif) {
+    while (consume_token_if_next(parser, Token_Type_Keyword_Elseif)) {
         if (parser->hit_unexpected_token) return root_if;
 
-        consume_token(parser);
         AstIfWhile* elseif_node = make_node(AstIfWhile, Ast_Kind_If);
 
         cond = parse_expression(parser, 1);
@@ -945,13 +943,12 @@ static AstSwitch* parse_switch_stmt(OnyxParser* parser) {
     switch_node->expr = parse_expression(parser, 1);
     expect_token(parser, '{');
 
-    while (parser->curr->type == Token_Type_Keyword_Case) {
+    while (consume_token_if_next(parser, Token_Type_Keyword_Case)) {
+        if (parser->hit_unexpected_token) return switch_node;
+
         bh_arr(AstTyped *) case_values = NULL;
         bh_arr_new(global_heap_allocator, case_values, 1);
 
-        expect_token(parser, Token_Type_Keyword_Case);
-        if (parser->hit_unexpected_token) return switch_node;
-
         if (parse_possible_directive(parser, "default")) {
             switch_node->default_case = parse_block(parser);
 
@@ -1191,10 +1188,7 @@ static AstNode* parse_jump_stmt(OnyxParser* parser, TokenType token_type, JumpTy
     jnode->jump  = jump_type;
 
     u64 count = 1;
-    while (parser->curr->type == token_type) {
-        consume_token(parser);
-        count++;
-    }
+    while (consume_token_if_next(parser, token_type)) count++;
     jnode->count = count;
 
     return (AstNode *) jnode;
@@ -1522,8 +1516,7 @@ static AstType* parse_type(OnyxParser* parser) {
 
                 *next_insertion = (AstType *) symbol_node;
 
-                while (parser->curr->type == '.') {
-                    consume_token(parser);
+                while (consume_token_if_next(parser, '.')) {
                     AstFieldAccess* field = make_node(AstFieldAccess, Ast_Kind_Field_Access);
                     field->token = expect_token(parser, Token_Type_Symbol);
                     field->expr  = (AstTyped *) *next_insertion;
@@ -2342,8 +2335,7 @@ void onyx_parser_free(OnyxParser* parser) {
 
 ParseResults onyx_parse(OnyxParser *parser) {
     // NOTE: Skip comments at the beginning of the file
-    while (parser->curr->type == Token_Type_Comment)
-        consume_token(parser);
+    while (consume_token_if_next(parser, Token_Type_Comment));
 
     parser->package = parse_package_name(parser)->package;
     parser->file_scope = scope_create(parser->allocator, parser->package->private_scope, parser->tokenizer->tokens[0].pos);