Skip to content

Commit 37c3b5b

Browse files
eernstgCommit Queue
authored andcommitted
Add support for declaring constructors to the spec parser
Also fixes a bug whereby the file name message came after the first syntax error diagnostic for each file. Change-Id: Ice2da1e337fa54787696989b387d057ab5674819 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/453280 Commit-Queue: Erik Ernst <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent e3fe4c3 commit 37c3b5b

File tree

3 files changed

+218
-55
lines changed

3 files changed

+218
-55
lines changed

tools/spec_parser/Dart.g

Lines changed: 103 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
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

399397
simpleFormalParameter
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()`).
405401
fieldFormalParameter
406-
: finalConstVarOrType? THIS '.' identifier (formalParameterPart '?'?)?
402+
: type? THIS '.' identifier (formalParameterPart '?'?)?
407403
;
408404

409405
superFormalParameter
@@ -424,11 +420,25 @@ typeWithParameters
424420

425421
classDeclaration
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+
432442
classModifiers
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.
470479
mixinMemberDeclaration
471480
: classMemberDeclaration
472481
;
473482

474483
extensionTypeDeclaration
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.
488494
extensionTypeMemberDeclaration
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.
502507
extensionMemberDeclaration
503508
: classMemberDeclaration
504509
;
@@ -564,13 +569,82 @@ setterSignature
564569

565570
constructorSignature
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

569639
constructorName
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.
574648
identifierOrNew
575649
: identifier
576650
| NEW
@@ -613,15 +687,16 @@ redirectingFactoryConstructorSignature
613687

614688
constantConstructorSignature
615689
: CONST constructorName formalParameterList
690+
| declaringConstantConstructorSignature
616691
;
617692

618693
mixinApplication
619694
: typeNotVoidNotFunction mixins interfaces?
620695
;
621696

622697
enumType
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
;

tools/spec_parser/SpecParser.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ParsingResult {
1414
}
1515

1616
class DartErrorListener implements ANTLRErrorListener {
17+
@Override
1718
public void reportAmbiguity(
1819
Parser recognizer,
1920
DFA dfa,
@@ -23,6 +24,7 @@ public void reportAmbiguity(
2324
BitSet ambigAlts,
2425
ATNConfigSet configs) {}
2526

27+
@Override
2628
public void reportAttemptingFullContext(
2729
Parser recognizer,
2830
DFA dfa,
@@ -31,6 +33,7 @@ public void reportAttemptingFullContext(
3133
BitSet conflictingAlts,
3234
ATNConfigSet configs) {}
3335

36+
@Override
3437
public void reportContextSensitivity(
3538
Parser recognizer,
3639
DFA dfa,
@@ -39,14 +42,19 @@ public void reportContextSensitivity(
3942
int prediction,
4043
ATNConfigSet configs) {}
4144

42-
public void syntaxError(
43-
Recognizer<?,?> recognizer,
44-
Object offendingSymbol,
45-
int line,
46-
int charPositionInLine,
47-
String msg,
48-
RecognitionException e) {
49-
if (!DartParser.errorHasOccurred) DartParser.prepareForErrors();
45+
@Override
46+
public void syntaxError(Recognizer<?, ?> recognizer,
47+
Object offendingSymbol,
48+
int line,
49+
int charPositionInLine,
50+
String msg,
51+
RecognitionException e) {
52+
DartParser.errorHasOccurred = true;
53+
if (!DartParser.errorHeaderHasBeenPrinted) {
54+
System.err.println("Syntax error in " + DartParser.filePath + ":");
55+
DartParser.errorHeaderHasBeenPrinted = true;
56+
}
57+
System.err.println("line " + line + ":" + charPositionInLine + " " + msg);
5058
}
5159
}
5260

0 commit comments

Comments
 (0)