From 74177d79c9e80616edce2336cd487f9e01c2db08 Mon Sep 17 00:00:00 2001 From: Aiden Storey Date: Sun, 7 Apr 2024 22:13:10 -0400 Subject: [PATCH] Fix multiline array/dictionary match statements Currently array and dictionary expressions cannot be spread over multiple lines in match statements. Adding mutliline push/pop while parsing the pattern for bracket and brace enables the ability for these to be multiline. This enables more complex patterns to be matched without exceeding line limits. Fixes #90372 --- modules/gdscript/gdscript_parser.cpp | 34 +++++++++++-------- .../scripts/parser/features/match_array.gd | 33 ++++++++++++++++++ .../scripts/parser/features/match_array.out | 6 ++++ .../parser/features/match_dictionary.gd | 21 ++++++++++++ .../parser/features/match_dictionary.out | 3 ++ 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 modules/gdscript/tests/scripts/parser/features/match_array.gd create mode 100644 modules/gdscript/tests/scripts/parser/features/match_array.out diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 173bff575ad..bb5d6770c66 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -2270,28 +2270,31 @@ GDScriptParser::PatternNode *GDScriptParser::parse_match_pattern(PatternNode *p_ break; case GDScriptTokenizer::Token::BRACKET_OPEN: { // Array. + push_multiline(true); advance(); pattern->pattern_type = PatternNode::PT_ARRAY; - - if (!check(GDScriptTokenizer::Token::BRACKET_CLOSE)) { - do { - PatternNode *sub_pattern = parse_match_pattern(p_root_pattern != nullptr ? p_root_pattern : pattern); - if (sub_pattern == nullptr) { - continue; - } - if (pattern->rest_used) { - push_error(R"(The ".." pattern must be the last element in the pattern array.)"); - } else if (sub_pattern->pattern_type == PatternNode::PT_REST) { - pattern->rest_used = true; - } - pattern->array.push_back(sub_pattern); - } while (match(GDScriptTokenizer::Token::COMMA)); - } + do { + if (is_at_end() || check(GDScriptTokenizer::Token::BRACKET_CLOSE)) { + break; + } + PatternNode *sub_pattern = parse_match_pattern(p_root_pattern != nullptr ? p_root_pattern : pattern); + if (sub_pattern == nullptr) { + continue; + } + if (pattern->rest_used) { + push_error(R"(The ".." pattern must be the last element in the pattern array.)"); + } else if (sub_pattern->pattern_type == PatternNode::PT_REST) { + pattern->rest_used = true; + } + pattern->array.push_back(sub_pattern); + } while (match(GDScriptTokenizer::Token::COMMA)); consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected "]" to close the array pattern.)"); + pop_multiline(); break; } case GDScriptTokenizer::Token::BRACE_OPEN: { // Dictionary. + push_multiline(true); advance(); pattern->pattern_type = PatternNode::PT_DICTIONARY; do { @@ -2334,6 +2337,7 @@ GDScriptParser::PatternNode *GDScriptParser::parse_match_pattern(PatternNode *p_ } } while (match(GDScriptTokenizer::Token::COMMA)); consume(GDScriptTokenizer::Token::BRACE_CLOSE, R"(Expected "}" to close the dictionary pattern.)"); + pop_multiline(); break; } default: { diff --git a/modules/gdscript/tests/scripts/parser/features/match_array.gd b/modules/gdscript/tests/scripts/parser/features/match_array.gd new file mode 100644 index 00000000000..9103092cb41 --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/features/match_array.gd @@ -0,0 +1,33 @@ +func foo(x): + match x: + ["value1"]: + print('["value1"]') + ["value1", "value2"]: + print('["value1", "value2"]') + +func bar(x): + match x: + [ + "value1" + ]: + print('multiline ["value1"]') + [ + "value1", + "value2", + ]: + print('multiline ["value1", "value2",]') + [ + "value1", + [ + "value2", + .., + ], + ]: + print('multiline ["value1", ["value2", ..,],]') + +func test(): + foo(["value1"]) + foo(["value1", "value2"]) + bar(["value1"]) + bar(["value1", "value2"]) + bar(["value1", ["value2", "value3"]]) diff --git a/modules/gdscript/tests/scripts/parser/features/match_array.out b/modules/gdscript/tests/scripts/parser/features/match_array.out new file mode 100644 index 00000000000..d0111f07b18 --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/features/match_array.out @@ -0,0 +1,6 @@ +GDTEST_OK +["value1"] +["value1", "value2"] +multiline ["value1"] +multiline ["value1", "value2",] +multiline ["value1", ["value2", ..,],] diff --git a/modules/gdscript/tests/scripts/parser/features/match_dictionary.gd b/modules/gdscript/tests/scripts/parser/features/match_dictionary.gd index 75857fb8ff9..7685622e5ab 100644 --- a/modules/gdscript/tests/scripts/parser/features/match_dictionary.gd +++ b/modules/gdscript/tests/scripts/parser/features/match_dictionary.gd @@ -26,6 +26,24 @@ func bar(x): _: print("wildcard") +func baz(x): + match x: + { + "key1": "value1" + }: + print('multiline {"key1": "value1"}') + { + "key2": "value2", + }: + print('multiline {"key2": "value2",}') + { + "key3": { + "key1", + .., + }, + }: + print('multiline {"key3": {"key1", ..,},}') + func test(): foo({"key1": "value1", "key2": "value2"}) foo({"key1": "value1", "key2": ""}) @@ -41,3 +59,6 @@ func test(): bar({1: "1"}) bar({2: "2"}) bar({3: "3"}) + baz({"key1": "value1"}) + baz({"key2": "value2"}) + baz({"key3": {"key1": "value1", "key2": "value2"}}) diff --git a/modules/gdscript/tests/scripts/parser/features/match_dictionary.out b/modules/gdscript/tests/scripts/parser/features/match_dictionary.out index 4dee886927c..f9adcbd3316 100644 --- a/modules/gdscript/tests/scripts/parser/features/match_dictionary.out +++ b/modules/gdscript/tests/scripts/parser/features/match_dictionary.out @@ -13,3 +13,6 @@ wildcard 1 2 wildcard +multiline {"key1": "value1"} +multiline {"key2": "value2",} +multiline {"key3": {"key1", ..,},}