fixed: printing symbol name instead of "TOKEN_TYPE_SYMBOL"
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 31 Aug 2023 19:22:13 +0000 (14:22 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 31 Aug 2023 19:22:13 +0000 (14:22 -0500)
compiler/include/lex.h
compiler/src/lex.c
compiler/src/parser.c

index bc9b0cf3882102ca04625cfef1d45a12e2195d7b..9a0638a3427a09500fa98df53fdf31885b957a82 100644 (file)
@@ -112,7 +112,8 @@ typedef struct OnyxTokenizer {
     bh_arr(OnyxToken) tokens;
 } OnyxTokenizer;
 
-const char* token_name(TokenType tkn_type);
+const char *token_type_name(TokenType tkn_type);
+const char* token_name(OnyxToken *tkn);
 void token_toggle_end(OnyxToken* tkn);
 OnyxToken* onyx_get_token(OnyxTokenizer* tokenizer);
 OnyxTokenizer onyx_tokenizer_create(bh_allocator allocator, bh_file_contents *fc);
index 40d88478be972646f3fc5eae2000cb28de94e17a..5853b5c95a91ba7dc68ae371a0a06d368331d756 100644 (file)
@@ -119,7 +119,7 @@ static inline b32 token_lit(OnyxTokenizer* tokenizer, OnyxToken* tk, char* lit,
     return 1;
 }
 
-const char* token_name(TokenType tkn_type) {
+const char *token_type_name(TokenType tkn_type) {
     if (tkn_type < Token_Type_Ascii_End) {
         return bh_aprintf(global_scratch_allocator, "%c", (char) tkn_type);
     } else {
@@ -127,6 +127,16 @@ const char* token_name(TokenType tkn_type) {
     }
 }
 
+const char* token_name(OnyxToken * tkn) {
+    TokenType tkn_type = tkn->type;
+
+    if (tkn_type == Token_Type_Symbol) {
+        return bh_aprintf(global_scratch_allocator, "%b", tkn->text, tkn->length);
+    }
+
+    return token_type_name(tkn_type);
+}
+
 void token_toggle_end(OnyxToken* tkn) {
     static char backup = 0;
     char tmp = tkn->text[tkn->length];
index 6b39d538790443826d1bd25a7693725789757969..82d4d7082d1f8a806260e228406184dfb9c8f49a 100644 (file)
@@ -127,7 +127,7 @@ static OnyxToken* expect_token(OnyxParser* parser, TokenType token_type) {
     consume_token(parser);
 
     if (token->type != token_type) {
-        onyx_report_error(token->pos, Error_Critical, "expected token '%s', got '%s'.", token_name(token_type), token_name(token->type));
+        onyx_report_error(token->pos, Error_Critical, "expected token '%s', got '%s'.", token_type_name(token_type), token_name(token));
         parser->hit_unexpected_token = 1;
         // :LinearTokenDependent
         parser->curr = &parser->tokenizer->tokens[bh_arr_length(parser->tokenizer->tokens) - 1];
@@ -620,7 +620,7 @@ static AstTyped* parse_factor(OnyxParser* parser) {
             }
 
             if (parser->curr->type != '{') {
-                onyx_report_error(parser->curr->pos, Error_Critical, "Expected '{' after 'do', got '%s'.", token_name(parser->curr->type));
+                onyx_report_error(parser->curr->pos, Error_Critical, "Expected '{' after 'do', got '%s'.", token_name(parser->curr));
                 retval = NULL;
                 break;
             }
@@ -906,7 +906,7 @@ static AstTyped* parse_factor(OnyxParser* parser) {
 
         default:
         no_match:
-            onyx_report_error(parser->curr->pos, Error_Critical, "Unexpected token '%s'.", token_name(parser->curr->type));
+            onyx_report_error(parser->curr->pos, Error_Critical, "Unexpected token '%s'.", token_name(parser->curr));
             return NULL;
     }
 
@@ -3742,7 +3742,7 @@ static void parse_top_level_statement(OnyxParser* parser) {
 
                 if (symbol_token->type > Token_Type_Keyword_Start && symbol_token->type < Token_Type_Keyword_End) {
                     onyx_report_error(directive_token->pos, Error_Critical, "Did you mean the keyword, '%s'?",
-                        token_name(symbol_token->type));
+                        token_name(symbol_token));
                 }
 
                 return;