44
55// CHANGES:
66//
7+ // v0.53 Support declaring constructors.
8+ //
79// v0.52 Support static access shorthands.
810//
911// v0.51 Support a `switchExpression` with no cases.
@@ -176,18 +178,14 @@ import java.util.Stack;
176178@parser::members {
177179 static String filePath = null;
178180 static boolean errorHasOccurred = false;
179-
180- /// Must be invoked before the first error is reported for a library.
181- /// Will print the name of the library and indicate that it has errors.
182- static void prepareForErrors() {
183- errorHasOccurred = true;
184- System.err.println("Syntax error in " + filePath + ":");
185- }
181+ static boolean errorHeaderHasBeenPrinted = false;
186182
187183 /// Parse library, return true if success, false if errors occurred.
188184 public boolean parseLibrary(String filePath) throws RecognitionException {
189185 this.filePath = filePath;
190- errorHasOccurred = false;
186+ errorHeaderHasBeenPrinted = false;
187+ this.removeErrorListeners(); // Remove the default ConsoleErrorListener.
188+ this.addErrorListener(new DartErrorListener()); // Add our custom one.
191189 startSymbol();
192190 return !errorHasOccurred;
193191 }
@@ -397,13 +395,11 @@ functionFormalParameter
397395 ;
398396
399397simpleFormalParameter
400- : declaredIdentifier
401- | COVARIANT? identifier
398+ : COVARIANT? type? identifier
402399 ;
403400
404- // NB: It is an anomaly that VAR can be a return type (`var this.x()`).
405401fieldFormalParameter
406- : finalConstVarOrType ? THIS ' .' identifier (formalParameterPart ' ?' ?)?
402+ : type ? THIS ' .' identifier (formalParameterPart ' ?' ?)?
407403 ;
408404
409405superFormalParameter
@@ -424,11 +420,25 @@ typeWithParameters
424420
425421classDeclaration
426422 : AUGMENT? (classModifiers | mixinClassModifiers)
427- CLASS typeWithParameters superclass? interfaces?
428- LBRACE (metadata classMemberDeclaration)* RBRACE
423+ CLASS classNamePart superclass? interfaces? classBody
429424 | classModifiers MIXIN? CLASS mixinApplicationClass
430425 ;
431426
427+ primaryConstructorNoConst
428+ : typeIdentifier typeParameters?
429+ (' .' identifierOrNew)? declaringParameterList
430+ ;
431+
432+ classNamePart
433+ : CONST? primaryConstructorNoConst
434+ | typeWithParameters
435+ ;
436+
437+ classBody
438+ : LBRACE (metadata classMemberDeclaration)* RBRACE
439+ | ' ;'
440+ ;
441+
432442classModifiers
433443 : SEALED
434444 | ABSTRACT? (BASE | INTERFACE | FINAL)?
@@ -466,25 +476,21 @@ mixinDeclaration
466476 LBRACE (metadata mixinMemberDeclaration)* RBRACE
467477 ;
468478
469- // TODO: We might want to make this more strict.
470479mixinMemberDeclaration
471480 : classMemberDeclaration
472481 ;
473482
474483extensionTypeDeclaration
475- : EXTENSION TYPE CONST? typeWithParameters
476- representationDeclaration interfaces?
477- LBRACE (metadata extensionTypeMemberDeclaration)* RBRACE
484+ : EXTENSION TYPE classNamePart interfaces? extensionTypeBody
478485 | AUGMENT EXTENSION TYPE typeWithParameters interfaces?
479- LBRACE (metadata extensionTypeMemberDeclaration) * RBRACE
486+ extensionTypeBody
480487 ;
481488
482- representationDeclaration
483- : (' .' identifierOrNew)? ' (' metadata typedIdentifier ' )'
489+ extensionTypeBody
490+ : LBRACE (metadata extensionTypeMemberDeclaration)* RBRACE
491+ | ' ;'
484492 ;
485493
486-
487- // TODO: We might want to make this more strict.
488494extensionTypeMemberDeclaration
489495 : classMemberDeclaration
490496 ;
@@ -498,7 +504,6 @@ extensionBody
498504 : LBRACE (metadata extensionMemberDeclaration)* RBRACE
499505 ;
500506
501- // TODO: We might want to make this more strict.
502507extensionMemberDeclaration
503508 : classMemberDeclaration
504509 ;
@@ -564,13 +569,82 @@ setterSignature
564569
565570constructorSignature
566571 : constructorName formalParameterList
572+ | declaringConstructorSignature
573+ ;
574+
575+ declaringConstructorSignature
576+ : THIS (' .' identifierOrNew)? declaringParameterList?
577+ ;
578+
579+ declaringConstantConstructorSignature
580+ : CONST THIS (' .' identifierOrNew)? declaringParameterList
581+ ;
582+
583+ declaringParameterList
584+ : ' (' ' )'
585+ | ' (' declaringFormalParameters ' ,' ? ' )'
586+ | ' (' declaringFormalParameters ' ,'
587+ optionalOrNamedDeclaringFormalParameters ' )'
588+ | ' (' optionalOrNamedDeclaringFormalParameters ' )'
589+ ;
590+
591+ declaringFormalParameters
592+ : declaringFormalParameter (' ,' declaringFormalParameter)*
593+ ;
594+
595+ declaringFormalParameter
596+ : metadata declaringFormalParameterNoMetadata
597+ ;
598+
599+ declaringFormalParameterNoMetadata
600+ : declaringFunctionFormalParameter
601+ | fieldFormalParameter
602+ | declaringSimpleFormalParameter
603+ | superFormalParameter
604+ ;
605+
606+ declaringFunctionFormalParameter
607+ : COVARIANT? (VAR | FINAL)? type?
608+ identifier formalParameterPart ' ?' ?
609+ ;
610+
611+ declaringSimpleFormalParameter
612+ : COVARIANT? (VAR | FINAL)? type? identifier
613+ ;
614+
615+ optionalOrNamedDeclaringFormalParameters
616+ : optionalPositionalDeclaringFormalParameters
617+ | namedDeclaringFormalParameters
618+ ;
619+
620+ optionalPositionalDeclaringFormalParameters
621+ : ' [' defaultDeclaringFormalParameter
622+ (' ,' defaultDeclaringFormalParameter)* ' ,' ? ' ]'
623+ ;
624+
625+ defaultDeclaringFormalParameter
626+ : declaringFormalParameter (' =' expression)?
627+ ;
628+
629+ namedDeclaringFormalParameters
630+ : LBRACE defaultDeclaringNamedParameter
631+ (' ,' defaultDeclaringNamedParameter)* ' ,' ? RBRACE
632+ ;
633+
634+ defaultDeclaringNamedParameter
635+ : metadata REQUIRED? declaringFormalParameterNoMetadata
636+ (' =' expression)?
567637 ;
568638
569639constructorName
570- : typeIdentifier (' .' identifierOrNew)?
640+ : typeIdentifierOrNew (' .' identifierOrNew)?
641+ ;
642+
643+ typeIdentifierOrNew
644+ : typeIdentifier
645+ | NEW
571646 ;
572647
573- // TODO: Add this in the language specification, use it in grammar rules.
574648identifierOrNew
575649 : identifier
576650 | NEW
@@ -613,15 +687,16 @@ redirectingFactoryConstructorSignature
613687
614688constantConstructorSignature
615689 : CONST constructorName formalParameterList
690+ | declaringConstantConstructorSignature
616691 ;
617692
618693mixinApplication
619694 : typeNotVoidNotFunction mixins interfaces?
620695 ;
621696
622697enumType
623- : AUGMENT? ENUM typeWithParameters mixins? interfaces? LBRACE
624- enumEntry (' ,' enumEntry)* ( ' ,' ) ?
698+ : AUGMENT? ENUM classNamePart mixins? interfaces? LBRACE
699+ enumEntry (' ,' enumEntry)* ' ,' ?
625700 (' ;' (metadata classMemberDeclaration)* )?
626701 RBRACE
627702 ;
0 commit comments