| 2303 | } |
| 2304 | |
| 2305 | GDScriptParser::MatchNode *GDScriptParser::parse_match() { |
| 2306 | MatchNode *match_node = alloc_node<MatchNode>(); |
| 2307 | |
| 2308 | match_node->test = parse_expression(false); |
| 2309 | if (match_node->test == nullptr) { |
| 2310 | push_error(R"(Expected expression to test after "match".)"); |
| 2311 | } |
| 2312 | |
| 2313 | consume(GDScriptTokenizer::Token::COLON, R"(Expected ":" after "match" expression.)"); |
| 2314 | consume(GDScriptTokenizer::Token::NEWLINE, R"(Expected a newline after "match" statement.)"); |
| 2315 | |
| 2316 | if (!consume(GDScriptTokenizer::Token::INDENT, R"(Expected an indented block after "match" statement.)")) { |
| 2317 | complete_extents(match_node); |
| 2318 | return match_node; |
| 2319 | } |
| 2320 | |
| 2321 | bool all_have_return = true; |
| 2322 | bool have_wildcard = false; |
| 2323 | |
| 2324 | List<AnnotationNode *> match_branch_annotation_stack; |
| 2325 | |
| 2326 | while (!check(GDScriptTokenizer::Token::DEDENT) && !is_at_end()) { |
| 2327 | if (match(GDScriptTokenizer::Token::PASS)) { |
| 2328 | consume(GDScriptTokenizer::Token::NEWLINE, R"(Expected newline after "pass".)"); |
| 2329 | continue; |
| 2330 | } |
| 2331 | |
| 2332 | if (match(GDScriptTokenizer::Token::ANNOTATION)) { |
| 2333 | AnnotationNode *annotation = parse_annotation(AnnotationInfo::STATEMENT); |
| 2334 | if (annotation == nullptr) { |
| 2335 | continue; |
| 2336 | } |
| 2337 | if (annotation->name != SNAME("@warning_ignore")) { |
| 2338 | push_error(vformat(R"(Annotation "%s" is not allowed in this level.)", annotation->name), annotation); |
| 2339 | continue; |
| 2340 | } |
| 2341 | match_branch_annotation_stack.push_back(annotation); |
| 2342 | continue; |
| 2343 | } |
| 2344 | |
| 2345 | MatchBranchNode *branch = parse_match_branch(); |
| 2346 | if (branch == nullptr) { |
| 2347 | advance(); |
| 2348 | continue; |
| 2349 | } |
| 2350 | |
| 2351 | for (AnnotationNode *annotation : match_branch_annotation_stack) { |
| 2352 | branch->annotations.push_back(annotation); |
| 2353 | } |
| 2354 | match_branch_annotation_stack.clear(); |
| 2355 | |
| 2356 | #ifdef DEBUG_ENABLED |
| 2357 | if (have_wildcard && !branch->patterns.is_empty()) { |
| 2358 | push_warning(branch->patterns[0], GDScriptWarning::UNREACHABLE_PATTERN); |
| 2359 | } |
| 2360 | #endif |
| 2361 | |
| 2362 | have_wildcard = have_wildcard || branch->has_wildcard; |