@@ -1403,20 +1403,16 @@ class Parser {
1403
1403
1404
1404
/// Parse a record type similarly as a formal parameter list of a function.
1405
1405
///
1406
- /// TODO(jensj): Update this to fit any new updates to the spec.
1407
- /// E.g. having two recordTypeNamedField entries doesn't make sense.
1408
- ///
1409
1406
/// recordType ::= '(' recordTypeFields ',' recordTypeNamedFields ')'
1410
1407
/// | '(' recordTypeFields ','? ')'
1411
- /// | '(' recordTypeNamedFields ')'
1408
+ /// | '(' recordTypeNamedFields? ')'
1412
1409
///
1413
1410
/// recordTypeFields ::= recordTypeField ( ',' recordTypeField )*
1414
1411
/// recordTypeField ::= metadata type identifier?
1415
1412
///
1416
1413
/// recordTypeNamedFields ::= '{' recordTypeNamedField
1417
1414
/// ( ',' recordTypeNamedField )* ','? '}'
1418
- /// recordTypeNamedField ::= type identifier
1419
- /// recordTypeNamedField ::= metadata typedIdentifier
1415
+ /// recordTypeNamedField ::= metadata type identifier
1420
1416
Token parseRecordType (final Token start, Token token) {
1421
1417
token = token.next! ;
1422
1418
assert (optional ('(' , token));
@@ -1429,11 +1425,18 @@ class Parser {
1429
1425
int parameterCount = 0 ;
1430
1426
bool hasNamedFields = false ;
1431
1427
bool sawComma = false ;
1428
+ Token ? illegalTrailingComma;
1432
1429
while (true ) {
1433
1430
Token next = token.next! ;
1434
1431
if (optional (')' , next)) {
1435
1432
token = next;
1436
1433
break ;
1434
+ } else if (parameterCount == 0 &&
1435
+ optional (',' , next) &&
1436
+ optional (')' , next.next! )) {
1437
+ illegalTrailingComma = next;
1438
+ token = next.next! ;
1439
+ break ;
1437
1440
}
1438
1441
++ parameterCount;
1439
1442
String ? value = next.stringValue;
@@ -1478,7 +1481,11 @@ class Parser {
1478
1481
}
1479
1482
assert (optional (')' , token));
1480
1483
1481
- if (parameterCount == 1 && ! hasNamedFields && ! sawComma) {
1484
+ if (parameterCount == 0 && illegalTrailingComma != null ) {
1485
+ // Empty record type with a comma `(,)`.
1486
+ reportRecoverableError (illegalTrailingComma,
1487
+ codes.messageRecordTypeZeroFieldsButTrailingComma);
1488
+ } else if (parameterCount == 1 && ! hasNamedFields && ! sawComma) {
1482
1489
// Single non-named element without trailing comma.
1483
1490
reportRecoverableError (
1484
1491
token, codes.messageRecordTypeOnePositionalFieldNoTrailingComma);
@@ -6143,9 +6150,20 @@ class Parser {
6143
6150
int count = 0 ;
6144
6151
bool wasRecord = constKeywordForRecord != null ;
6145
6152
bool wasValidRecord = false ;
6153
+ Token ? illegalTrailingComma;
6146
6154
while (true ) {
6147
6155
Token next = token.next! ;
6148
- if ((count > 0 || wasRecord) && optional (')' , next)) {
6156
+ if (optional (')' , next)) {
6157
+ if (count == 0 ) {
6158
+ wasRecord = true ;
6159
+ }
6160
+ break ;
6161
+ } else if (count == 0 &&
6162
+ optional (',' , next) &&
6163
+ optional (')' , next.next! )) {
6164
+ illegalTrailingComma = next;
6165
+ wasRecord = true ;
6166
+ token = next;
6149
6167
break ;
6150
6168
}
6151
6169
Token ? colon = null ;
@@ -6179,8 +6197,10 @@ class Parser {
6179
6197
assert (wasRecord || count <= 1 );
6180
6198
6181
6199
if (wasRecord) {
6182
- if (count == 0 ) {
6183
- reportRecoverableError (token, codes.messageRecordLiteralEmpty);
6200
+ if (count == 0 && illegalTrailingComma != null ) {
6201
+ // Empty record literal with a comma `(,)`.
6202
+ reportRecoverableError (illegalTrailingComma,
6203
+ codes.messageRecordLiteralZeroFieldsWithTrailingComma);
6184
6204
} else if (count == 1 && ! wasValidRecord) {
6185
6205
reportRecoverableError (
6186
6206
token, codes.messageRecordLiteralOnePositionalFieldNoTrailingComma);
@@ -9484,9 +9504,7 @@ class Parser {
9484
9504
assert (wasRecord || count <= 1 );
9485
9505
9486
9506
if (wasRecord) {
9487
- if (count == 0 ) {
9488
- reportRecoverableError (token, codes.messageRecordLiteralEmpty);
9489
- } else if (count == 1 && ! wasValidRecord) {
9507
+ if (count == 1 && ! wasValidRecord) {
9490
9508
reportRecoverableError (
9491
9509
token, codes.messageRecordLiteralOnePositionalFieldNoTrailingComma);
9492
9510
}
0 commit comments