Prechádzať zdrojové kódy

Merge pull request #99887 from JackErb/gdscript-parser-error-fix

Improve misleading `Unexpected "x" in class body.` GDScript parser error
Thaddeus Crews 8 mesiacov pred
rodič
commit
79dd5179e3

+ 1 - 1
modules/gdscript/gdscript_parser.cpp

@@ -1073,8 +1073,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) {

+ 9 - 0
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:

+ 1 - 0
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;

+ 7 - 0
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

+ 2 - 0
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.