From 13fcb05e7b2cc30c9e5870ba65f4a116d95e75a8 Mon Sep 17 00:00:00 2001 From: JackErb Date: Sat, 30 Nov 2024 14:29:30 -0800 Subject: [PATCH] Improve GDScript "unexpected token in class body" parser error This parser error was misleading. Fixes: 1. Now points at correct line 2. For identifiers, prints out `Identifier "%s"` --- modules/gdscript/gdscript_parser.cpp | 2 +- modules/gdscript/gdscript_tokenizer.cpp | 9 +++++++++ modules/gdscript/gdscript_tokenizer.h | 1 + .../parser/errors/unexpected_token_in_class_body.gd | 7 +++++++ .../parser/errors/unexpected_token_in_class_body.out | 2 ++ 5 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 modules/gdscript/tests/scripts/parser/errors/unexpected_token_in_class_body.gd create mode 100644 modules/gdscript/tests/scripts/parser/errors/unexpected_token_in_class_body.out diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 12e71004dbd..d5112fa3082 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -1060,8 +1060,8 @@ void GDScriptParser::parse_class_body(bool p_is_multiline) { default: // Display a completion with identifiers. make_completion_context(COMPLETION_IDENTIFIER, nullptr); - push_error(vformat(R"(Unexpected "%s" in class body.)", current.get_name())); advance(); + push_error(vformat(R"(Unexpected %s in class body.)", previous.get_debug_name())); break; } if (token.type != GDScriptTokenizer::Token::STATIC) { diff --git a/modules/gdscript/gdscript_tokenizer.cpp b/modules/gdscript/gdscript_tokenizer.cpp index 404b61fb400..dd936cbe7ed 100644 --- a/modules/gdscript/gdscript_tokenizer.cpp +++ b/modules/gdscript/gdscript_tokenizer.cpp @@ -164,6 +164,15 @@ const char *GDScriptTokenizer::Token::get_name() const { return token_names[type]; } +String GDScriptTokenizer::Token::get_debug_name() const { + switch (type) { + case IDENTIFIER: + return vformat(R"(identifier "%s")", source); + default: + return vformat(R"("%s")", get_name()); + } +} + bool GDScriptTokenizer::Token::can_precede_bin_op() const { switch (type) { case IDENTIFIER: diff --git a/modules/gdscript/gdscript_tokenizer.h b/modules/gdscript/gdscript_tokenizer.h index 5d763751738..08e74179225 100644 --- a/modules/gdscript/gdscript_tokenizer.h +++ b/modules/gdscript/gdscript_tokenizer.h @@ -178,6 +178,7 @@ public: String source; const char *get_name() const; + String get_debug_name() const; bool can_precede_bin_op() const; bool is_identifier() const; bool is_node_name() const; diff --git a/modules/gdscript/tests/scripts/parser/errors/unexpected_token_in_class_body.gd b/modules/gdscript/tests/scripts/parser/errors/unexpected_token_in_class_body.gd new file mode 100644 index 00000000000..b036799d91f --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/errors/unexpected_token_in_class_body.gd @@ -0,0 +1,7 @@ +# GH-96792 + +var error +error = true + +func test(): + pass diff --git a/modules/gdscript/tests/scripts/parser/errors/unexpected_token_in_class_body.out b/modules/gdscript/tests/scripts/parser/errors/unexpected_token_in_class_body.out new file mode 100644 index 00000000000..f6eed886d12 --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/errors/unexpected_token_in_class_body.out @@ -0,0 +1,2 @@ +GDTEST_PARSER_ERROR +Unexpected identifier "error" in class body.