Skip to content

Commit 8eb0cd6

Browse files
Nigel-Ecmajskeet
authored andcommitted
Address #1285 & #1428
1285: - text incorrectly placed in §6.2.5 removed - text added to §12.14.1 to cover disambiguation is-type/is-pattern - the comment regarding “precedence at least as high" is covered under #1428, see HTML comment bullet below 1428: - The suggested grammar changes reduce to changing one rule, *constant_expression* - Inserted HTML comment [TODO] note in §12.14.1 regarding reviewing decision on “is pattern” New sample “Pattern precedence” to check relational_expression, and lambdas in conjunction with patterns, have correct precedence
1 parent a3b37c7 commit 8eb0cd6

10 files changed

Lines changed: 6705 additions & 8 deletions

File tree

standard/expressions.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,11 +4392,14 @@ equality_expression
43924392
;
43934393
```
43944394

4395-
> *Note*: Lookup for the right operand of the `is` operator must first test as a *type*, then as an *expression* which may span multiple tokens. In the case where the operand is an *expression*, the pattern expression must have precedence at least as high as *shift_expression*. *end note*
4396-
<!-- markdownlint-disable MD028 -->
4397-
4398-
<!-- markdownlint-enable MD028 -->
4399-
> *Note*: There is a grammar ambiguity between *type* and *constant_pattern* in a `relational_expression` on the right-hand-side of `is`; either might be a valid parse of a qualified identifier. In such a case, only if it fails to bind as a type (for compatibility with previous versions of the language), is it resolved to be the first thing found (which must be either a constant or a type). This ambiguity is only present on the right-hand side of such an expression. *end note*
4395+
<!--
4396+
[TODO] PR#1428 recommended changing *pattern* on the RHS of 'is' above to restrict constant_pattern to relational_pattern to
4397+
avoid precedence issues, this was not done. This might be required for a certain compiler but ANTLR grammar semantics does the
4398+
right thing, maybe not obviously for some, and so no change was made – making the change in the grammar is effectively harmless
4399+
as it does not effect the resulting parse at all, but the prose gets messier. For C#9 this decision should be checked to be
4400+
still valid after the new pattern changes.
4401+
-->
4402+
When recognising a *relational_expression* ([§12.14.1](expressions.md#12141-general)) if both the “*relational_expression* `is` *type*” and “*relational_expression* `is` *constant_pattern*” alternatives are applicable, and *type* resolves to an accessible type, then the “*relational_expression* `is` *type*” alternative shall be chosen.
44004403

44014404
The `is` operator is described in [§12.14.12](expressions.md#121412-the-is-operator) and the `as` operator is described in [§12.14.13](expressions.md#121413-the-as-operator).
44024405

@@ -7151,7 +7154,7 @@ A constant expression is an expression that shall be fully evaluated at compile-
71517154

71527155
```ANTLR
71537156
constant_expression
7154-
: expression
7157+
: conditional_expression
71557158
;
71567159
```
71577160

standard/lexical-structure.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ then the *type_argument_list* shall be retained as part of the disambiguated pro
134134
>
135135
> *end example*
136136
137-
When recognising a *relational_expression* ([§12.14.1](expressions.md#12141-general)) if both the “*relational_expression* `is` *type*” and “*relational_expression* `is` *pattern*” alternatives are applicable, and *type* resolves to an accessible type, then the “*relational_expression* `is` *type*” alternative shall be chosen.
138-
139137
## 6.3 Lexical analysis
140138
141139
### 6.3.1 General
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Sample: Pattern precedence
2+
3+
Samples showing pattern precedence in relational_expression & lambda_expression

tools/GrammarTesting/Tests/Parsing/Samples/v8/Pattern precedence/Reference/sample.gruntree.red.txt

Lines changed: 1992 additions & 0 deletions
Large diffs are not rendered by default.

tools/GrammarTesting/Tests/Parsing/Samples/v8/Pattern precedence/Reference/sample.stderr.txt

Whitespace-only changes.

tools/GrammarTesting/Tests/Parsing/Samples/v8/Pattern precedence/Reference/sample.svg

Lines changed: 4330 additions & 0 deletions
Loading
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
[@0,0:4='class',<'class'>,1:0]
2+
[@1,6:14='IsPattern',<Simple_Identifier>,1:6]
3+
[@2,16:16='{',<'{'>,2:0]
4+
[@3,21:24='void',<'void'>,3:3]
5+
[@4,26:34='IsPattern',<Simple_Identifier>,3:8]
6+
[@5,35:35='(',<'('>,3:17]
7+
[@6,36:36=')',<')'>,3:18]
8+
[@7,41:41='{',<'{'>,4:3]
9+
[@8,362:365='bool',<'bool'>,11:6]
10+
[@9,367:367='A',<Simple_Identifier>,11:11]
11+
[@10,369:369='=',<'='>,11:13]
12+
[@11,371:375='false',<'false'>,11:15]
13+
[@12,376:376=';',<';'>,11:20]
14+
[@13,384:388='const',<'const'>,12:6]
15+
[@14,390:393='bool',<'bool'>,12:12]
16+
[@15,395:395='B',<Simple_Identifier>,12:17]
17+
[@16,397:397='=',<'='>,12:19]
18+
[@17,399:402='true',<'true'>,12:21]
19+
[@18,403:403=';',<';'>,12:25]
20+
[@19,411:415='const',<'const'>,13:6]
21+
[@20,417:420='bool',<'bool'>,13:12]
22+
[@21,422:422='C',<Simple_Identifier>,13:17]
23+
[@22,424:424='=',<'='>,13:19]
24+
[@23,426:429='true',<'true'>,13:21]
25+
[@24,430:430=';',<';'>,13:25]
26+
[@25,438:440='int',<'int'>,14:6]
27+
[@26,442:442='X',<Simple_Identifier>,14:10]
28+
[@27,444:444='=',<'='>,14:12]
29+
[@28,446:446='0',<Integer_Literal>,14:14]
30+
[@29,447:447=';',<';'>,14:15]
31+
[@30,455:459='const',<'const'>,15:6]
32+
[@31,461:463='int',<'int'>,15:12]
33+
[@32,465:465='Y',<Simple_Identifier>,15:16]
34+
[@33,467:467='=',<'='>,15:18]
35+
[@34,469:470='42',<Integer_Literal>,15:20]
36+
[@35,471:471=';',<';'>,15:22]
37+
[@36,479:483='const',<'const'>,16:6]
38+
[@37,485:487='int',<'int'>,16:12]
39+
[@38,489:489='Z',<Simple_Identifier>,16:16]
40+
[@39,491:491='=',<'='>,16:18]
41+
[@40,493:494='24',<Integer_Literal>,16:20]
42+
[@41,495:495=';',<';'>,16:22]
43+
[@42,510:514='const',<'const'>,18:6]
44+
[@43,516:519='bool',<'bool'>,18:12]
45+
[@44,521:521='D',<Simple_Identifier>,18:17]
46+
[@45,523:523='=',<'='>,18:19]
47+
[@46,525:525='Y',<Simple_Identifier>,18:21]
48+
[@47,527:528='is',<'is'>,18:23]
49+
[@48,530:532='int',<'int'>,18:26]
50+
[@49,533:533=';',<';'>,18:29]
51+
[@50,542:545='bool',<'bool'>,20:6]
52+
[@51,547:547='N',<Simple_Identifier>,20:11]
53+
[@52,549:549='=',<'='>,20:13]
54+
[@53,551:551='A',<Simple_Identifier>,20:15]
55+
[@54,553:554='is',<'is'>,20:17]
56+
[@55,556:556='B',<Simple_Identifier>,20:20]
57+
[@56,558:559='is',<'is'>,20:22]
58+
[@57,561:561='C',<Simple_Identifier>,20:25]
59+
[@58,562:562=';',<';'>,20:26]
60+
[@59,598:601='bool',<'bool'>,21:6]
61+
[@60,603:603='O',<Simple_Identifier>,21:11]
62+
[@61,605:605='=',<'='>,21:13]
63+
[@62,607:607='A',<Simple_Identifier>,21:15]
64+
[@63,609:610='is',<'is'>,21:17]
65+
[@64,612:612='(',<'('>,21:20]
66+
[@65,613:613='Y',<Simple_Identifier>,21:21]
67+
[@66,615:616='is',<'is'>,21:23]
68+
[@67,618:620='int',<'int'>,21:26]
69+
[@68,621:621=')',<')'>,21:29]
70+
[@69,622:622=';',<';'>,21:30]
71+
[@70,637:640='bool',<'bool'>,23:6]
72+
[@71,642:642='P',<Simple_Identifier>,23:11]
73+
[@72,644:644='=',<'='>,23:13]
74+
[@73,646:646='X',<Simple_Identifier>,23:15]
75+
[@74,648:649='is',<'is'>,23:17]
76+
[@75,651:651='Y',<Simple_Identifier>,23:20]
77+
[@76,653:653='>',<'>'>,23:22]
78+
[@77,654:654='>',<'>'>,23:23]
79+
[@78,656:656='4',<Integer_Literal>,23:25]
80+
[@79,657:657=';',<';'>,23:26]
81+
[@80,693:696='bool',<'bool'>,24:6]
82+
[@81,698:698='Q',<Simple_Identifier>,24:11]
83+
[@82,700:700='=',<'='>,24:13]
84+
[@83,702:702='(',<'('>,24:15]
85+
[@84,703:703='X',<Simple_Identifier>,24:16]
86+
[@85,705:706='is',<'is'>,24:18]
87+
[@86,708:708='Y',<Simple_Identifier>,24:21]
88+
[@87,709:709=')',<')'>,24:22]
89+
[@88,711:711='>',<'>'>,24:24]
90+
[@89,712:712='>',<'>'>,24:25]
91+
[@90,714:714='4',<Integer_Literal>,24:27]
92+
[@91,715:715=';',<';'>,24:28]
93+
[@92,730:733='bool',<'bool'>,26:6]
94+
[@93,735:735='R',<Simple_Identifier>,26:11]
95+
[@94,737:737='=',<'='>,26:13]
96+
[@95,739:739='A',<Simple_Identifier>,26:15]
97+
[@96,741:742='is',<'is'>,26:17]
98+
[@97,744:744='B',<Simple_Identifier>,26:20]
99+
[@98,746:747='<=',<'<='>,26:22]
100+
[@99,749:749='C',<Simple_Identifier>,26:25]
101+
[@100,750:750=';',<';'>,26:26]
102+
[@101,786:789='bool',<'bool'>,27:6]
103+
[@102,791:791='S',<Simple_Identifier>,27:11]
104+
[@103,793:793='=',<'='>,27:13]
105+
[@104,795:795='A',<Simple_Identifier>,27:15]
106+
[@105,797:798='is',<'is'>,27:17]
107+
[@106,800:800='(',<'('>,27:20]
108+
[@107,801:801='Y',<Simple_Identifier>,27:21]
109+
[@108,803:804='<=',<'<='>,27:23]
110+
[@109,806:806='4',<Integer_Literal>,27:26]
111+
[@110,807:807=')',<')'>,27:27]
112+
[@111,808:808=';',<';'>,27:28]
113+
[@112,823:826='bool',<'bool'>,29:6]
114+
[@113,828:828='T',<Simple_Identifier>,29:11]
115+
[@114,830:830='=',<'='>,29:13]
116+
[@115,832:832='A',<Simple_Identifier>,29:15]
117+
[@116,834:835='is',<'is'>,29:17]
118+
[@117,837:837='B',<Simple_Identifier>,29:20]
119+
[@118,839:840='==',<'=='>,29:22]
120+
[@119,842:842='C',<Simple_Identifier>,29:25]
121+
[@120,843:843=';',<';'>,29:26]
122+
[@121,879:882='bool',<'bool'>,30:6]
123+
[@122,884:884='U',<Simple_Identifier>,30:11]
124+
[@123,886:886='=',<'='>,30:13]
125+
[@124,888:888='A',<Simple_Identifier>,30:15]
126+
[@125,890:891='is',<'is'>,30:17]
127+
[@126,893:893='(',<'('>,30:20]
128+
[@127,894:894='Y',<Simple_Identifier>,30:21]
129+
[@128,896:897='==',<'=='>,30:23]
130+
[@129,899:899='Z',<Simple_Identifier>,30:26]
131+
[@130,900:900=')',<')'>,30:27]
132+
[@131,901:901=';',<';'>,30:28]
133+
[@132,916:919='bool',<'bool'>,32:6]
134+
[@133,921:921='V',<Simple_Identifier>,32:11]
135+
[@134,923:923='=',<'='>,32:13]
136+
[@135,925:925='A',<Simple_Identifier>,32:15]
137+
[@136,927:928='is',<'is'>,32:17]
138+
[@137,930:930='B',<Simple_Identifier>,32:20]
139+
[@138,932:933='||',<'||'>,32:22]
140+
[@139,935:935='C',<Simple_Identifier>,32:25]
141+
[@140,936:936=';',<';'>,32:26]
142+
[@141,973:976='bool',<'bool'>,33:6]
143+
[@142,978:978='W',<Simple_Identifier>,33:11]
144+
[@143,980:980='=',<'='>,33:13]
145+
[@144,982:982='A',<Simple_Identifier>,33:15]
146+
[@145,984:985='is',<'is'>,33:17]
147+
[@146,987:987='(',<'('>,33:20]
148+
[@147,988:988='B',<Simple_Identifier>,33:21]
149+
[@148,990:991='||',<'||'>,33:23]
150+
[@149,993:993='C',<Simple_Identifier>,33:26]
151+
[@150,994:994=')',<')'>,33:27]
152+
[@151,995:995=';',<';'>,33:28]
153+
[@152,1043:1045='int',<'int'>,36:6]
154+
[@153,1046:1046='?',<'?'>,36:9]
155+
[@154,1048:1049='Yn',<Simple_Identifier>,36:11]
156+
[@155,1051:1051='=',<'='>,36:14]
157+
[@156,1053:1053='Y',<Simple_Identifier>,36:16]
158+
[@157,1054:1054=';',<';'>,36:17]
159+
[@158,1062:1064='int',<'int'>,37:6]
160+
[@159,1065:1065='?',<'?'>,37:9]
161+
[@160,1067:1068='Zn',<Simple_Identifier>,37:11]
162+
[@161,1070:1070='=',<'='>,37:14]
163+
[@162,1072:1075='null',<'null'>,37:16]
164+
[@163,1076:1076=';',<';'>,37:20]
165+
[@164,1084:1087='bool',<'bool'>,38:6]
166+
[@165,1089:1089='J',<Simple_Identifier>,38:11]
167+
[@166,1091:1091='=',<'='>,38:13]
168+
[@167,1093:1094='Yn',<Simple_Identifier>,38:15]
169+
[@168,1096:1097='is',<'is'>,38:18]
170+
[@169,1099:1101='int',<'int'>,38:21]
171+
[@170,1102:1102=';',<';'>,38:24]
172+
[@171,1133:1136='bool',<'bool'>,39:6]
173+
[@172,1138:1138='K',<Simple_Identifier>,39:11]
174+
[@173,1140:1140='=',<'='>,39:13]
175+
[@174,1142:1143='Zn',<Simple_Identifier>,39:15]
176+
[@175,1145:1146='is',<'is'>,39:18]
177+
[@176,1148:1150='int',<'int'>,39:21]
178+
[@177,1151:1151=';',<';'>,39:24]
179+
[@178,1183:1186='bool',<'bool'>,40:6]
180+
[@179,1188:1189='Jn',<Simple_Identifier>,40:11]
181+
[@180,1191:1191='=',<'='>,40:14]
182+
[@181,1193:1194='Yn',<Simple_Identifier>,40:16]
183+
[@182,1196:1197='is',<'is'>,40:19]
184+
[@183,1199:1201='int',<'int'>,40:22]
185+
[@184,1202:1202='?',<'?'>,40:25]
186+
[@185,1203:1203=';',<';'>,40:26]
187+
[@186,1232:1235='bool',<'bool'>,41:6]
188+
[@187,1237:1238='Kn',<Simple_Identifier>,41:11]
189+
[@188,1240:1240='=',<'='>,41:14]
190+
[@189,1242:1243='Zn',<Simple_Identifier>,41:16]
191+
[@190,1245:1246='is',<'is'>,41:19]
192+
[@191,1248:1250='int',<'int'>,41:22]
193+
[@192,1251:1251='?',<'?'>,41:25]
194+
[@193,1252:1252=';',<';'>,41:26]
195+
[@194,1282:1285='bool',<'bool'>,42:6]
196+
[@195,1287:1288='Ly',<Simple_Identifier>,42:11]
197+
[@196,1290:1290='=',<'='>,42:14]
198+
[@197,1292:1293='Yn',<Simple_Identifier>,42:16]
199+
[@198,1295:1296='is',<'is'>,42:19]
200+
[@199,1298:1298='7',<Integer_Literal>,42:22]
201+
[@200,1300:1300='*',<'*'>,42:24]
202+
[@201,1302:1302='6',<Integer_Literal>,42:26]
203+
[@202,1303:1303=';',<';'>,42:27]
204+
[@203,1331:1334='bool',<'bool'>,43:6]
205+
[@204,1336:1337='Lz',<Simple_Identifier>,43:11]
206+
[@205,1339:1339='=',<'='>,43:14]
207+
[@206,1341:1342='Zn',<Simple_Identifier>,43:16]
208+
[@207,1344:1345='is',<'is'>,43:19]
209+
[@208,1347:1347='7',<Integer_Literal>,43:22]
210+
[@209,1349:1349='*',<'*'>,43:24]
211+
[@210,1351:1351='6',<Integer_Literal>,43:26]
212+
[@211,1352:1352=';',<';'>,43:27]
213+
[@212,1381:1384='bool',<'bool'>,44:6]
214+
[@213,1386:1386='M',<Simple_Identifier>,44:11]
215+
[@214,1388:1388='=',<'='>,44:13]
216+
[@215,1390:1390='X',<Simple_Identifier>,44:15]
217+
[@216,1392:1393='is',<'is'>,44:17]
218+
[@217,1395:1397='int',<'int'>,44:20]
219+
[@218,1398:1398='?',<'?'>,44:23]
220+
[@219,1399:1399=';',<';'>,44:24]
221+
[@220,1430:1433='bool',<'bool'>,45:6]
222+
[@221,1435:1436='Mn',<Simple_Identifier>,45:11]
223+
[@222,1438:1438='=',<'='>,45:14]
224+
[@223,1440:1441='Zn',<Simple_Identifier>,45:16]
225+
[@224,1443:1444='is',<'is'>,45:19]
226+
[@225,1446:1449='null',<'null'>,45:22]
227+
[@226,1450:1450=';',<';'>,45:26]
228+
[@227,1486:1486='_',<'_'>,47:6]
229+
[@228,1488:1488='=',<'='>,47:8]
230+
[@229,1490:1490='A',<Simple_Identifier>,47:10]
231+
[@230,1492:1497='switch',<'switch'>,47:12]
232+
[@231,1505:1505='{',<'{'>,48:6]
233+
[@232,1516:1516='(',<'('>,49:9]
234+
[@233,1517:1517='a',<Simple_Identifier>,49:10]
235+
[@234,1519:1520='=>',<'=>'>,49:12]
236+
[@235,1522:1522='b',<Simple_Identifier>,49:15]
237+
[@236,1523:1523=')',<')'>,49:16]
238+
[@237,1525:1526='=>',<'=>'>,49:18]
239+
[@238,1528:1528='c',<Simple_Identifier>,49:21]
240+
[@239,1529:1529=',',<','>,49:22]
241+
[@240,1747:1747='a',<Simple_Identifier>,52:9]
242+
[@241,1749:1750='=>',<'=>'>,52:11]
243+
[@242,1752:1752='b',<Simple_Identifier>,52:14]
244+
[@243,1754:1755='=>',<'=>'>,52:16]
245+
[@244,1757:1757='c',<Simple_Identifier>,52:19]
246+
[@245,1758:1758=',',<','>,52:20]
247+
[@246,1825:1825='a',<Simple_Identifier>,53:9]
248+
[@247,1827:1828='=>',<'=>'>,53:11]
249+
[@248,1830:1830='(',<'('>,53:14]
250+
[@249,1831:1831='b',<Simple_Identifier>,53:15]
251+
[@250,1833:1834='=>',<'=>'>,53:17]
252+
[@251,1836:1836='c',<Simple_Identifier>,53:20]
253+
[@252,1837:1837=')',<')'>,53:21]
254+
[@253,1870:1870='}',<'}'>,54:6]
255+
[@254,1871:1871=';',<';'>,54:7]
256+
[@255,1886:1891='static',<'static'>,56:6]
257+
[@256,1893:1896='void',<'void'>,56:13]
258+
[@257,1898:1898='f',<Simple_Identifier>,56:18]
259+
[@258,1899:1899='(',<'('>,56:19]
260+
[@259,1900:1903='Func',<Simple_Identifier>,56:20]
261+
[@260,1904:1904='<',<'<'>,56:24]
262+
[@261,1905:1907='int',<'int'>,56:25]
263+
[@262,1908:1908=',',<','>,56:28]
264+
[@263,1910:1912='int',<'int'>,56:30]
265+
[@264,1913:1913='>',<'>'>,56:33]
266+
[@265,1915:1915='g',<Simple_Identifier>,56:35]
267+
[@266,1916:1916=')',<')'>,56:36]
268+
[@267,1924:1924='{',<'{'>,57:6]
269+
[@268,1936:1938='var',<'var'>,58:10]
270+
[@269,1940:1940='I',<Simple_Identifier>,58:14]
271+
[@270,1942:1942='=',<'='>,58:16]
272+
[@271,1944:1944='g',<Simple_Identifier>,58:18]
273+
[@272,1946:1951='switch',<'switch'>,58:20]
274+
[@273,1963:1963='{',<'{'>,59:10]
275+
[@274,1979:1982='null',<'null'>,60:14]
276+
[@275,1984:1985='=>',<'=>'>,60:19]
277+
[@276,1987:1987='0',<Integer_Literal>,60:22]
278+
[@277,1988:1988=',',<','>,60:23]
279+
[@278,2052:2054='var',<'var'>,61:14]
280+
[@279,2056:2056='k',<Simple_Identifier>,61:18]
281+
[@280,2058:2059='=>',<'=>'>,61:20]
282+
[@281,2061:2061='k',<Simple_Identifier>,61:23]
283+
[@282,2062:2062='(',<'('>,61:24]
284+
[@283,2063:2064='42',<Integer_Literal>,61:25]
285+
[@284,2065:2065=')',<')'>,61:27]
286+
[@285,2066:2066=',',<','>,61:28]
287+
[@286,2131:2131='(',<'('>,62:14]
288+
[@287,2132:2132='w',<Simple_Identifier>,62:15]
289+
[@288,2134:2135='=>',<'=>'>,62:17]
290+
[@289,2137:2137='x',<Simple_Identifier>,62:20]
291+
[@290,2138:2138=')',<')'>,62:21]
292+
[@291,2140:2141='=>',<'=>'>,62:23]
293+
[@292,2143:2143='g',<Simple_Identifier>,62:26]
294+
[@293,2144:2144='(',<'('>,62:27]
295+
[@294,2145:2146='42',<Integer_Literal>,62:28]
296+
[@295,2147:2147=')',<')'>,62:30]
297+
[@296,2217:2217='}',<'}'>,63:10]
298+
[@297,2218:2218=';',<';'>,63:11]
299+
[@298,2226:2226='}',<'}'>,64:6]
300+
[@299,2227:2227=';',<';'>,64:7]
301+
[@300,2232:2232='}',<'}'>,65:3]
302+
[@301,2234:2234='}',<'}'>,66:0]
303+
[@302,2235:2234='<EOF>',<EOF>,66:1]

0 commit comments

Comments
 (0)