From 92955d56dde9394e1efaca565e553e99b0026b2d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 2 Nov 2024 14:39:59 +1100 Subject: [PATCH] Added a nice error message --- source/toy_lexer.h | 6 +++--- source/toy_parser.c | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/source/toy_lexer.h b/source/toy_lexer.h index 0b589de..30d3cae 100644 --- a/source/toy_lexer.h +++ b/source/toy_lexer.h @@ -21,8 +21,8 @@ typedef struct { TOY_API void Toy_bindLexer(Toy_Lexer* lexer, const char* source); TOY_API Toy_Token Toy_private_scanLexer(Toy_Lexer* lexer); -TOY_API void Toy_private_printToken(Toy_Token* token); //debugging -//util -#define TOY_BLANK_TOKEN() ((Toy_Token){TOY_TOKEN_NULL, 0, 0, NULL}) +TOY_API const char* Toy_private_findKeywordByType(const Toy_TokenType type); +TOY_API Toy_TokenType Toy_private_findTypeByKeyword(const char* keyword); +TOY_API void Toy_private_printToken(Toy_Token* token); diff --git a/source/toy_parser.c b/source/toy_parser.c index 25aaf2c..f3c43e1 100644 --- a/source/toy_parser.c +++ b/source/toy_parser.c @@ -570,7 +570,14 @@ static void parsePrecedence(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_A ParsingRule prefix = getParsingRule(parser->previous.type)->prefix; if (prefix == NULL) { - printError(parser, parser->previous, "Expected expression"); + //make a nice error message + if (Toy_private_findKeywordByType(parser->previous.type)) { + printError(parser, parser->previous, "Found reserved keyword instead"); + } + else { + printError(parser, parser->previous, "Expected expression"); + } + Toy_private_emitAstError(bucketHandle, rootHandle); return; } @@ -780,8 +787,8 @@ Toy_Ast* Toy_scanParser(Toy_Bucket** bucketHandle, Toy_Parser* parser) { void Toy_resetParser(Toy_Parser* parser) { parser->lexer = NULL; - parser->current = TOY_BLANK_TOKEN(); - parser->previous = TOY_BLANK_TOKEN(); + parser->current = ((Toy_Token){TOY_TOKEN_NULL, 0, 0, NULL}); + parser->previous = ((Toy_Token){TOY_TOKEN_NULL, 0, 0, NULL}); parser->error = false; parser->panic = false;