From 56568065178527aaed2d422f36a657f6b44f3750 Mon Sep 17 00:00:00 2001 From: Erik Ernst Date: Mon, 3 Nov 2025 01:35:25 -0800 Subject: [PATCH] Update the spec parser to support new();` This CL changes the spec parser specification (Dart.g and Dart.g4) such that the grammar allows for the new style of constructor declarations (such as `new();` and `new name();`). It also adjusts the declaring body constructor syntax to use the same style (`this();` and `this name();`). Finally, it updates a few non-terminals to use a naming that avoids the ambiguity of `classNamePart`. These changes introduce about 5 new failures when parsing $SDK/tests/language. Change-Id: I3e6ac9f0782e3bde863a13c93a54a089c93c7e13 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/456640 Reviewed-by: Chloe Stefantsova Commit-Queue: Erik Ernst --- tools/spec_parser/Dart.g | 41 ++++++++++++++-------- tools/spec_parser/dart_spec_parser/Dart.g4 | 41 ++++++++++++++-------- 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/tools/spec_parser/Dart.g b/tools/spec_parser/Dart.g index 815bbabc7ef..ca4c9152086 100644 --- a/tools/spec_parser/Dart.g +++ b/tools/spec_parser/Dart.g @@ -4,6 +4,9 @@ // CHANGES: // +// v0.56 Update constructor declaration syntax to allow `constructorHead`. +// Recatogerize 'factory' to be a reserved word and not a built-in identifier. +// // v0.55 Use `typeWithParameters` consistently, simplify primary constructor // rule. // @@ -125,7 +128,7 @@ // `builtinIdentifier` and `reservedWord`; update `typeAlias` to enable // non-function type aliases; add missing `metadata` to formal parameter // declarations; correct `symbolLiteral` to allow `VOID`; - +// // v0.12 (82403371ac00ddf004be60fa7b705474d2864509) Cf. language issue #1341: // correct `metadata`. Change `qualifiedName` such that it only includes the // cases with a '.'; the remaining case is added where `qualifiedName` is used. @@ -425,7 +428,7 @@ typeWithParameters classDeclaration : AUGMENT? (classModifiers | mixinClassModifiers) - CLASS classNamePart superclass? interfaces? classBody + CLASS classNameMaybePrimary superclass? interfaces? classBody | classModifiers MIXIN? CLASS mixinApplicationClass ; @@ -434,7 +437,7 @@ primaryConstructor declaringParameterList ; -classNamePart +classNameMaybePrimary : primaryConstructor | typeWithParameters ; @@ -482,7 +485,7 @@ mixinDeclaration ; extensionTypeDeclaration - : EXTENSION TYPE classNamePart interfaces? extensionTypeBody + : EXTENSION TYPE classNameMaybePrimary interfaces? extensionTypeBody | AUGMENT EXTENSION TYPE typeWithParameters interfaces? extensionTypeBody ; @@ -561,16 +564,17 @@ setterSignature ; constructorSignature - : constructorName formalParameterList + : constructorName formalParameterList // Old form. + | constructorHead formalParameterList // New form. | declaringConstructorSignature ; declaringConstructorSignature - : THIS ('.' identifierOrNew)? declaringParameterList? + : THIS identifier? declaringParameterList? // New form only. ; declaringConstantConstructorSignature - : CONST THIS ('.' identifierOrNew)? declaringParameterList + : CONST THIS identifier? declaringParameterList // New form only. ; declaringParameterList @@ -630,12 +634,15 @@ defaultDeclaringNamedParameter ; constructorName - : typeIdentifierOrNew ('.' identifierOrNew)? + : typeIdentifier ('.' identifierOrNew)? ; -typeIdentifierOrNew - : typeIdentifier - | NEW +constructorHead + : NEW identifier? + ; + +factoryConstructorHead + : FACTORY identifier? ; identifierOrNew @@ -670,16 +677,20 @@ initializerExpression ; factoryConstructorSignature - : CONST? FACTORY constructorName formalParameterList + : CONST? FACTORY constructorName formalParameterList // Old form. + | CONST? factoryConstructorHead formalParameterList // New form. ; redirectingFactoryConstructorSignature : CONST? FACTORY constructorName formalParameterList '=' - constructorDesignation + constructorDesignation // Old form. + | CONST? factoryConstructorHead formalParameterList '=' + constructorDesignation // New form. ; constantConstructorSignature - : CONST constructorName formalParameterList + : CONST constructorName formalParameterList // Old form. + | CONST constructorHead formalParameterList // New form. | declaringConstantConstructorSignature ; @@ -688,7 +699,7 @@ mixinApplication ; enumType - : AUGMENT? ENUM classNamePart mixins? interfaces? LBRACE + : AUGMENT? ENUM classNameMaybePrimary mixins? interfaces? LBRACE enumEntry (',' enumEntry)* ','? (';' (metadata memberDeclaration)*)? RBRACE diff --git a/tools/spec_parser/dart_spec_parser/Dart.g4 b/tools/spec_parser/dart_spec_parser/Dart.g4 index 97f46157aa0..09b10323fab 100644 --- a/tools/spec_parser/dart_spec_parser/Dart.g4 +++ b/tools/spec_parser/dart_spec_parser/Dart.g4 @@ -4,6 +4,9 @@ // CHANGES: // +// v0.57 Update constructor declaration syntax to allow `constructorHead`. +// Recatogerize 'factory' to be a reserved word and not a built-in identifier. +// // v0.56 Use `typeWithParameters` consistently, simplify primary constructor // rule. // @@ -435,7 +438,7 @@ typeWithParameters classDeclaration : AUGMENT? (classModifiers | mixinClassModifiers) - CLASS classNamePart superclass? interfaces? classBody + CLASS classNameMaybePrimary superclass? interfaces? classBody | classModifiers MIXIN? CLASS mixinApplicationClass ; @@ -444,7 +447,7 @@ primaryConstructor declaringParameterList ; -classNamePart +classNameMaybePrimary : primaryConstructor | typeWithParameters ; @@ -492,7 +495,7 @@ mixinDeclaration ; extensionTypeDeclaration - : EXTENSION TYPE classNamePart interfaces? extensionTypeBody + : EXTENSION TYPE classNameMaybePrimary interfaces? extensionTypeBody | AUGMENT EXTENSION TYPE typeWithParameters interfaces? extensionTypeBody ; @@ -571,16 +574,17 @@ setterSignature ; constructorSignature - : constructorName formalParameterList + : constructorName formalParameterList // Old form. + | constructorHead formalParameterList // New form. | declaringConstructorSignature ; declaringConstructorSignature - : THIS ('.' identifierOrNew)? declaringParameterList? + : THIS identifier? declaringParameterList? // New form only. ; declaringConstantConstructorSignature - : CONST THIS ('.' identifierOrNew)? declaringParameterList + : CONST THIS identifier? declaringParameterList // New form only. ; declaringParameterList @@ -640,12 +644,15 @@ defaultDeclaringNamedParameter ; constructorName - : typeIdentifierOrNew ('.' identifierOrNew)? + : typeIdentifier ('.' identifierOrNew)? ; -typeIdentifierOrNew - : typeIdentifier - | NEW +constructorHead + : NEW identifier? + ; + +factoryConstructorHead + : FACTORY identifier? ; identifierOrNew @@ -680,16 +687,20 @@ initializerExpression ; factoryConstructorSignature - : CONST? FACTORY constructorName formalParameterList + : CONST? FACTORY constructorName formalParameterList // Old form. + | CONST? factoryConstructorHead formalParameterList // New form. ; redirectingFactoryConstructorSignature : CONST? FACTORY constructorName formalParameterList '=' - constructorDesignation + constructorDesignation // Old form. + | CONST? factoryConstructorHead formalParameterList '=' + constructorDesignation // New form. ; constantConstructorSignature - : CONST constructorName formalParameterList + : CONST constructorName formalParameterList // Old form. + | CONST constructorHead formalParameterList // New form. | declaringConstantConstructorSignature ; @@ -698,7 +709,7 @@ mixinApplication ; enumType - : AUGMENT? ENUM classNamePart mixins? interfaces? LBRACE + : AUGMENT? ENUM classNameMaybePrimary mixins? interfaces? LBRACE enumEntry (',' enumEntry)* ','? (';' (metadata memberDeclaration)*)? RBRACE @@ -1834,7 +1845,7 @@ EXPONENT fragment DIGITS - : DIGIT ('_'* DIGIT)* + : DIGIT ('_'* DIGIT)* ; fragment