Skip to content

Commit 5923b88

Browse files
stereotype441Commit Queue
authored and
Commit Queue
committed
Parser: add support for wildcard patterns.
The bare identifier `_` is treated as a variable pattern wherever it appears, even though other bare identifiers are sometimes treated as constant patterns. Bug: #50035 Change-Id: I475ef627b6d0bf519a1972aa5ec683e2d032f02b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/263280 Reviewed-by: Jens Johansen <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 27e0e84 commit 5923b88

File tree

162 files changed

+6109
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+6109
-7
lines changed

pkg/_fe_analyzer_shared/lib/src/parser/parser_impl.dart

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9286,6 +9286,10 @@ class Parser {
92869286
token = parseExtractorPatternRest(token);
92879287
listener.handleExtractorPattern(firstIdentifier, dot, secondIdentifier);
92889288
return token;
9289+
} else if (firstIdentifier.lexeme == '_' && dot == null) {
9290+
// It's a wildcard pattern with no preceding type, so parse it as a
9291+
// variable pattern.
9292+
return parseVariablePattern(beforeFirstIdentifier, typeInfo: typeInfo);
92899293
}
92909294
// It's not an extractor pattern so parse it as an expression.
92919295
token = beforeFirstIdentifier;
@@ -9308,13 +9312,18 @@ class Parser {
93089312
token = typeInfo.parseType(token, this);
93099313
} else {
93109314
Token next = token.next!;
9311-
assert(optional('var', next) || optional('final', next));
9312-
token = keyword = next;
9313-
// TODO(paulberry): this accepts `var <type> name` as a variable pattern.
9314-
// We want to accept that for error recovery, but don't forget to report
9315-
// the appropriate error.
9316-
typeInfo = computeVariablePatternType(token);
9317-
token = typeInfo.parseType(token, this);
9315+
if (optional('var', next) || optional('final', next)) {
9316+
token = keyword = next;
9317+
// TODO(paulberry): this accepts `var <type> name` as a variable
9318+
// pattern. We want to accept that for error recovery, but don't forget
9319+
// to report the appropriate error.
9320+
typeInfo = computeVariablePatternType(token);
9321+
token = typeInfo.parseType(token, this);
9322+
} else {
9323+
// Bare wildcard pattern
9324+
assert(next.lexeme == '_');
9325+
listener.handleNoType(token);
9326+
}
93189327
}
93199328
Token next = token.next!;
93209329
if (next.isIdentifier) {

0 commit comments

Comments
 (0)