Skip to content

Commit 1af3f76

Browse files
stereotype441Commit Queue
authored and
Commit Queue
committed
Patterns parsing: fix handling of builtin identifiers and pseudo-keywords.
Bug: #50035 Change-Id: I95f2b7d59a54d0d5e1975bd8cd1dce88db03fa51 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/269580 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Jens Johansen <[email protected]>
1 parent 5ea3ef2 commit 1af3f76

File tree

32 files changed

+1772
-2
lines changed

32 files changed

+1772
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9304,7 +9304,7 @@ class Parser {
93049304
// TODO(paulberry): Make sure OTHER_IDENTIFIER is handled
93059305
// TODO(paulberry): Technically `dynamic` is valid for
93069306
// `typeIdentifier`--file an issue
9307-
if (isValidNonRecordTypeReference(next)) {
9307+
if (next.isIdentifier) {
93089308
Token beforeFirstIdentifier = token;
93099309
Token firstIdentifier = token = next;
93109310
next = token.next!;
@@ -9313,7 +9313,7 @@ class Parser {
93139313
if (optional('.', next)) {
93149314
dot = token = next;
93159315
next = token.next!;
9316-
if (isValidNonRecordTypeReference(next)) {
9316+
if (next.isIdentifier) {
93179317
secondIdentifier = token = next;
93189318
// TODO(paulberry): grammar specifies
93199319
// `typeIdentifier | qualifiedName`, but that permits `a.b.c`,

pkg/analyzer/test/generated/patterns_parser_test.dart

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,32 @@ RecordPattern
956956
''');
957957
}
958958

959+
test_constant_identifier_doublyPrefixed_builtin() {
960+
_parse('''
961+
void f(x) {
962+
const y = abstract.as.get; // verify that this works
963+
switch (x) {
964+
case abstract.as.get:
965+
break;
966+
}
967+
}
968+
''');
969+
var node = findNode.switchPatternCase('case abstract.as.get').pattern;
970+
assertParsedNodeText(node, '''
971+
ConstantPattern
972+
expression: PropertyAccess
973+
target: PrefixedIdentifier
974+
prefix: SimpleIdentifier
975+
token: abstract
976+
period: .
977+
identifier: SimpleIdentifier
978+
token: as
979+
operator: .
980+
propertyName: SimpleIdentifier
981+
token: get
982+
''');
983+
}
984+
959985
test_constant_identifier_doublyPrefixed_insideCase() {
960986
_parse('''
961987
void f(x) {
@@ -1089,6 +1115,54 @@ PostfixPattern
10891115
''');
10901116
}
10911117

1118+
test_constant_identifier_doublyPrefixed_pseudoKeyword() {
1119+
_parse('''
1120+
void f(x) {
1121+
const y = show.hide.when; // verify that this works
1122+
switch (x) {
1123+
case show.hide.when:
1124+
break;
1125+
}
1126+
}
1127+
''');
1128+
var node = findNode.switchPatternCase('case show.hide.when').pattern;
1129+
assertParsedNodeText(node, '''
1130+
ConstantPattern
1131+
expression: PropertyAccess
1132+
target: PrefixedIdentifier
1133+
prefix: SimpleIdentifier
1134+
token: show
1135+
period: .
1136+
identifier: SimpleIdentifier
1137+
token: hide
1138+
operator: .
1139+
propertyName: SimpleIdentifier
1140+
token: when
1141+
''');
1142+
}
1143+
1144+
test_constant_identifier_prefixed_builtin() {
1145+
_parse('''
1146+
void f(x) {
1147+
const y = abstract.as; // verify that this works
1148+
switch (x) {
1149+
case abstract.as:
1150+
break;
1151+
}
1152+
}
1153+
''');
1154+
var node = findNode.switchPatternCase('case abstract.as').pattern;
1155+
assertParsedNodeText(node, '''
1156+
ConstantPattern
1157+
expression: PrefixedIdentifier
1158+
prefix: SimpleIdentifier
1159+
token: abstract
1160+
period: .
1161+
identifier: SimpleIdentifier
1162+
token: as
1163+
''');
1164+
}
1165+
10921166
test_constant_identifier_prefixed_insideCase() {
10931167
_parse('''
10941168
void f(x) {
@@ -1202,6 +1276,28 @@ PostfixPattern
12021276
''');
12031277
}
12041278

1279+
test_constant_identifier_prefixed_pseudoKeyword() {
1280+
_parse('''
1281+
void f(x) {
1282+
const y = show.hide; // verify that this works
1283+
switch (x) {
1284+
case show.hide:
1285+
break;
1286+
}
1287+
}
1288+
''');
1289+
var node = findNode.switchPatternCase('case show.hide').pattern;
1290+
assertParsedNodeText(node, '''
1291+
ConstantPattern
1292+
expression: PrefixedIdentifier
1293+
prefix: SimpleIdentifier
1294+
token: show
1295+
period: .
1296+
identifier: SimpleIdentifier
1297+
token: hide
1298+
''');
1299+
}
1300+
12051301
test_constant_identifier_prefixedWithUnderscore_insideCase() {
12061302
// We need to make sure the `_` isn't misinterpreted as a wildcard pattern
12071303
_parse('''
@@ -1224,6 +1320,24 @@ ConstantPattern
12241320
''');
12251321
}
12261322

1323+
test_constant_identifier_unprefixed_builtin() {
1324+
_parse('''
1325+
void f(x) {
1326+
const y = abstract; // verify that this works
1327+
switch (x) {
1328+
case abstract:
1329+
break;
1330+
}
1331+
}
1332+
''');
1333+
var node = findNode.switchPatternCase('case abstract').pattern;
1334+
assertParsedNodeText(node, '''
1335+
ConstantPattern
1336+
expression: SimpleIdentifier
1337+
token: abstract
1338+
''');
1339+
}
1340+
12271341
test_constant_identifier_unprefixed_insideCase() {
12281342
_parse('''
12291343
void f(x) {
@@ -1322,6 +1436,24 @@ PostfixPattern
13221436
''');
13231437
}
13241438

1439+
test_constant_identifier_unprefixed_pseudoKeyword() {
1440+
_parse('''
1441+
void f(x) {
1442+
const y = show; // verify that this works
1443+
switch (x) {
1444+
case show:
1445+
break;
1446+
}
1447+
}
1448+
''');
1449+
var node = findNode.switchPatternCase('case show').pattern;
1450+
assertParsedNodeText(node, '''
1451+
ConstantPattern
1452+
expression: SimpleIdentifier
1453+
token: show
1454+
''');
1455+
}
1456+
13251457
test_constant_list_typed_empty_insideCase() {
13261458
_parse('''
13271459
void f(x) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
void f(x) {
2+
const y = abstract.as.get; // verify that this works
3+
switch (x) {
4+
case abstract.as.get:
5+
break;
6+
}
7+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
beginCompilationUnit(void)
2+
beginMetadataStar(void)
3+
endMetadataStar(0)
4+
beginTopLevelMember(void)
5+
beginTopLevelMethod(, null, null)
6+
handleVoidKeyword(void)
7+
handleIdentifier(f, topLevelFunctionDeclaration)
8+
handleNoTypeVariables(()
9+
beginFormalParameters((, MemberKind.TopLevelMethod)
10+
beginMetadataStar(x)
11+
endMetadataStar(0)
12+
beginFormalParameter(x, MemberKind.TopLevelMethod, null, null, null)
13+
handleNoType(()
14+
handleIdentifier(x, formalParameterDeclaration)
15+
handleFormalParameterWithoutValue())
16+
endFormalParameter(null, null, null, x, null, null, FormalParameterKind.requiredPositional, MemberKind.TopLevelMethod)
17+
endFormalParameters(1, (, ), MemberKind.TopLevelMethod)
18+
handleAsyncModifier(null, null)
19+
beginBlockFunctionBody({)
20+
beginMetadataStar(const)
21+
endMetadataStar(0)
22+
handleNoType(const)
23+
beginVariablesDeclaration(y, null, const)
24+
handleIdentifier(y, localVariableDeclaration)
25+
beginInitializedIdentifier(y)
26+
beginVariableInitializer(=)
27+
handleIdentifier(abstract, expression)
28+
handleNoTypeArguments(.)
29+
handleNoArguments(.)
30+
handleSend(abstract, .)
31+
handleIdentifier(as, expressionContinuation)
32+
handleNoTypeArguments(.)
33+
handleNoArguments(.)
34+
handleSend(as, .)
35+
handleEndingBinaryExpression(.)
36+
handleIdentifier(get, expressionContinuation)
37+
handleNoTypeArguments(;)
38+
handleNoArguments(;)
39+
handleSend(get, ;)
40+
handleEndingBinaryExpression(.)
41+
endVariableInitializer(=)
42+
endInitializedIdentifier(y)
43+
endVariablesDeclaration(1, ;)
44+
beginSwitchStatement(switch)
45+
handleIdentifier(x, expression)
46+
handleNoTypeArguments())
47+
handleNoArguments())
48+
handleSend(x, ))
49+
handleParenthesizedCondition((, null, null)
50+
beginSwitchBlock({)
51+
beginCaseExpression(case)
52+
handleIdentifier(abstract, expression)
53+
handleNoTypeArguments(.)
54+
handleNoArguments(.)
55+
handleSend(abstract, .)
56+
handleIdentifier(as, expressionContinuation)
57+
handleNoTypeArguments(.)
58+
handleNoArguments(.)
59+
handleSend(as, .)
60+
handleEndingBinaryExpression(.)
61+
handleIdentifier(get, expressionContinuation)
62+
handleNoTypeArguments(:)
63+
handleNoArguments(:)
64+
handleSend(get, :)
65+
handleEndingBinaryExpression(.)
66+
handleConstantPattern(null)
67+
endCaseExpression(case, null, :)
68+
beginSwitchCase(0, 1, case)
69+
handleBreakStatement(false, break, ;)
70+
endSwitchCase(0, 1, null, null, 1, case, })
71+
endSwitchBlock(1, {, })
72+
endSwitchStatement(switch, })
73+
endBlockFunctionBody(2, {, })
74+
endTopLevelMethod(void, null, })
75+
endTopLevelDeclaration()
76+
endCompilationUnit(1, )

0 commit comments

Comments
 (0)