Godot Engine – Multi-platform 2D and 3D game engine
GDScript: Report the unexpected token in condition ":" errors
Chaganti-Reddy9adfd25b2ede
diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cppindex 4890a1814b2..0acab8423dd 100644--- a/modules/gdscript/gdscript_parser.cpp+++ b/modules/gdscript/gdscript_parser.cpp@@ -2361,7 +2361,9 @@ GDScriptParser::ForNode *GDScriptParser::parse_for() {push_error(R"(Expected iterable after "in".)");}- consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "for" condition.)");+ if (!match(GDScriptTokenizer::Token::COLON)) {+ push_error(vformat(R"(Expected ":" after "for" condition, found "%s" instead.)", current.get_name()), current);+ }// Save break/continue state.bool could_break = can_break;@@ -2398,7 +2400,9 @@ GDScriptParser::IfNode *GDScriptParser::parse_if(const String &p_token) {push_error(vformat(R"(Expected conditional expression after "%s".)", p_token));}- consume(GDScriptTokenizer::Token::COLON, vformat(R"(Expected ":" after "%s" condition.)", p_token));+ if (!match(GDScriptTokenizer::Token::COLON)) {+ push_error(vformat(R"(Expected ":" after "%s" condition, found "%s" instead.)", p_token, current.get_name()), current);+ }n_if->true_block = parse_suite(vformat(R"("%s" block)", p_token));n_if->true_block->parent_if = n_if;@@ -2756,7 +2760,9 @@ GDScriptParser::WhileNode *GDScriptParser::parse_while() {push_error(R"(Expected conditional expression after "while".)");}- consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "while" condition.)");+ if (!match(GDScriptTokenizer::Token::COLON)) {+ push_error(vformat(R"(Expected ":" after "while" condition, found "%s" instead.)", current.get_name()), current);+ }// Save break/continue state.bool could_break = can_break;diff --git a/modules/gdscript/tests/scripts/parser/errors/double_dot.gd b/modules/gdscript/tests/scripts/parser/errors/double_dot.gdnew file mode 100644index 00000000000..8d2ce618290--- /dev/null+++ b/modules/gdscript/tests/scripts/parser/errors/double_dot.gd@@ -0,0 +1,3 @@+func test():+ if [0]..size():+ passdiff --git a/modules/gdscript/tests/scripts/parser/errors/double_dot.out b/modules/gdscript/tests/scripts/parser/errors/double_dot.outnew file mode 100644index 00000000000..9346cc5ab42--- /dev/null+++ b/modules/gdscript/tests/scripts/parser/errors/double_dot.out@@ -0,0 +1,2 @@+GDTEST_PARSER_ERROR+Expected ":" after "if" condition, found ".." instead.diff --git a/modules/gdscript/tests/scripts/parser/errors/missing_colon.out b/modules/gdscript/tests/scripts/parser/errors/missing_colon.outindex 687b963bc86..2adad314193 100644--- a/modules/gdscript/tests/scripts/parser/errors/missing_colon.out+++ b/modules/gdscript/tests/scripts/parser/errors/missing_colon.out@@ -1,2 +1,2 @@GDTEST_PARSER_ERROR-Expected ":" after "if" condition.+Expected ":" after "if" condition, found "Newline" instead.