OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // CHANGES: | |
6 // | |
7 // v1.0 First version available in the SDK github repository. Covers the | |
8 // Dart language as specified in the language specification based on the | |
9 // many grammar rule snippets. That grammar was then adjusted to remove | |
10 // known issues (e.g., misplaced metadata) and to resolve ambiguities. | |
11 // HERE! | |
12 | |
13 grammar Dart; | |
14 | |
15 /* | |
16 options { | |
17 backtrack=true; | |
18 memoize=true; | |
19 } | |
20 */ | |
21 | |
22 @parser::header{ | |
23 import java.util.Stack; | |
24 } | |
25 | |
26 @lexer::header{ | |
27 import java.util.Stack; | |
28 } | |
29 | |
30 @parser::members { | |
31 // Grammar debugging friendly output, 'The Definitive ANTLR Reference', p247. | |
32 public String getErrorMessage(RecognitionException e, String[] tokenNames) { | |
33 List stack = getRuleInvocationStack(e, this.getClass().getName()); | |
34 String msg = null; | |
35 if ( e instanceof NoViableAltException ) { | |
36 NoViableAltException nvae = (NoViableAltException)e; | |
37 msg = "no viable alt; token=" + e.token + | |
38 " (decision=" + nvae.decisionNumber + | |
39 " state " + nvae.stateNumber + ")" + | |
40 " decision=<<" + nvae.grammarDecisionDescription + ">>"; | |
41 } | |
42 else { | |
43 msg = super.getErrorMessage(e, tokenNames); | |
44 } | |
45 return stack + " " + msg; | |
46 } | |
47 | |
48 public String getTokenErrorDisplay(Token t) { | |
49 return t.toString(); | |
50 } | |
51 | |
52 // Enable the parser to treat ASYNC/AWAIT/YIELD as keywords in the body of an | |
53 // `async`, `async*`, or `sync*` function. Access via methods below. | |
54 private Stack<Boolean> asyncEtcAreKeywords = new Stack<Boolean>(); | |
55 { asyncEtcAreKeywords.push(false); } | |
56 | |
57 // Use this to indicate that we are now entering an `async`, `async*`, | |
58 // or `sync*` function. | |
59 void startAsyncFunction() { asyncEtcAreKeywords.push(true); } | |
60 | |
61 // Use this to indicate that we are now entering a function which is | |
62 // neither `async`, `async*`, nor `sync*`. | |
63 void startNonAsyncFunction() { asyncEtcAreKeywords.push(false); } | |
64 | |
65 // Use this to indicate that we are now leaving any funciton. | |
66 void endFunction() { asyncEtcAreKeywords.pop(); } | |
67 | |
68 // Whether we can recognize ASYNC/AWAIT/YIELD as an identifier/typeIdentifier. | |
69 boolean asyncEtcPredicate(int tokenId) { | |
70 if (tokenId == ASYNC || tokenId == AWAIT || tokenId == YIELD) { | |
71 return !asyncEtcAreKeywords.peek(); | |
72 } | |
73 return false; | |
74 } | |
75 | |
76 // Debugging support methods. | |
77 void dp(int indent, String method, String sep) { | |
78 for (int i = 0; i < indent; i++) { | |
79 System.out.print(" "); | |
80 } | |
81 System.out.println(method + sep + " " + input.LT(1) + " " + state.failed); | |
82 } | |
83 | |
84 void dpBegin(int indent, String method) { dp(indent, method, ":"); } | |
85 void dpEnd(int indent, String method) { dp(indent, method, " END:"); } | |
86 void dpCall(int indent, String method) { dp(indent, method, "?"); } | |
87 void dpCalled(int indent, String method) { dp(indent, method, ".."); } | |
88 void dpResult(int indent, String method) { dp(indent, method, "!"); } | |
89 } | |
90 | |
91 @lexer::members{ | |
92 public static final int BRACE_NORMAL = 1; | |
93 public static final int BRACE_SINGLE = 2; | |
94 public static final int BRACE_DOUBLE = 3; | |
95 public static final int BRACE_THREE_SINGLE = 4; | |
96 public static final int BRACE_THREE_DOUBLE = 5; | |
97 | |
98 // Enable the parser to handle string interpolations via brace matching. | |
99 // The top of the `braceLevels` stack describes the most recent unmatched | |
100 // '{'. This is needed in order to enable/disable certain lexer rules. | |
101 // | |
102 // NORMAL: Most recent unmatched '{' was not string literal related. | |
103 // SINGLE: Most recent unmatched '{' was `'...${`. | |
104 // DOUBLE: Most recent unmatched '{' was `"...${`. | |
105 // THREE_SINGLE: Most recent unmatched '{' was `'''...${`. | |
106 // THREE_DOUBLE: Most recent unmatched '{' was `"""...${`. | |
107 // | |
108 // Access via functions below. | |
109 private Stack<Integer> braceLevels = new Stack<Integer>(); | |
110 | |
111 // Whether we are currently in a string literal context, and which one. | |
112 boolean currentBraceLevel(int braceLevel) { | |
113 if (braceLevels.empty()) return false; | |
114 return braceLevels.peek() == braceLevel; | |
115 } | |
116 | |
117 // Use this to indicate that we are now entering a specific '{...}'. | |
118 // Call it after accepting the '{'. | |
119 void enterBrace() { | |
120 braceLevels.push(BRACE_NORMAL); | |
121 } | |
122 void enterBraceSingleQuote() { | |
123 braceLevels.push(BRACE_SINGLE); | |
124 } | |
125 void enterBraceDoubleQuote() { | |
126 braceLevels.push(BRACE_DOUBLE); | |
127 } | |
128 void enterBraceThreeSingleQuotes() { | |
129 braceLevels.push(BRACE_THREE_SINGLE); | |
130 } | |
131 void enterBraceThreeDoubleQuotes() { | |
132 braceLevels.push(BRACE_THREE_DOUBLE); | |
133 } | |
134 | |
135 // Use this to indicate that we are now exiting a specific '{...}', | |
136 // no matter which kind. Call it before accepting the '}'. | |
137 void exitBrace() { | |
138 // We might raise a parse error here if the stack is empty, but the | |
139 // parsing rules should ensure that we get a parse error anyway, and | |
140 // it is not a big problem for the spec parser even if it misinterprets | |
141 // the brace structure of some programs with syntax errors. | |
142 if (!braceLevels.empty()) braceLevels.pop(); | |
143 } | |
144 } | |
145 | |
146 // ---------------------------------------- Grammar rules. | |
147 | |
148 libraryDefinition | |
149 : FEFF? SCRIPT_TAG? | |
150 ((metadata LIBRARY) => libraryName)? | |
151 ((metadata (IMPORT | EXPORT)) => importOrExport)* | |
152 ((metadata PART) => partDirective)* | |
153 (metadata topLevelDefinition)* | |
154 EOF | |
155 ; | |
156 | |
157 // `functionSignature functionBody` is split into two alternatives because | |
158 // this eliminates an ambiguity. | |
159 | |
160 topLevelDefinition | |
161 : classDefinition | |
162 | enumType | |
163 | (TYPEDEF typeIdentifier typeParameters? '=') => typeAlias | |
164 | (TYPEDEF functionPrefix ('<' | '(')) => typeAlias | |
165 | (EXTERNAL functionSignature ';') => EXTERNAL functionSignature ';' | |
166 | (EXTERNAL getterSignature) => EXTERNAL getterSignature ';' | |
167 | (EXTERNAL type? SET identifier '(') => | |
168 EXTERNAL setterSignature ';' | |
169 | (getterSignature functionBodyPrefix) => getterSignature functionBody | |
170 | (type? SET identifier '(') => setterSignature functionBody | |
171 | (identifier typeParameters? '(') => functionSignature functionBody | |
172 | (type identifier typeParameters? '(') => | |
173 functionSignature functionBody | |
174 | ((FINAL | CONST) type? identifier '=') => | |
175 (FINAL | CONST) type? staticFinalDeclarationList ';' | |
176 | initializedVariableDeclaration ';' | |
177 ; | |
178 | |
179 declaredIdentifier | |
180 : COVARIANT? finalConstVarOrType identifier | |
floitsch
2017/08/28 09:18:48
Why is Covariant allowed at the top-level (through
Lasse Reichstein Nielsen
2017/08/28 10:51:32
I believe we handle that in prose: It's an error i
eernst
2017/08/28 11:35:16
Right, it's quite "imprecise" to allow COVARIANT o
| |
181 ; | |
182 | |
183 finalConstVarOrType | |
184 : FINAL type? | |
185 | CONST type? | |
186 | varOrType | |
187 ; | |
188 | |
189 varOrType | |
190 : VAR | |
191 | type | |
192 ; | |
193 | |
194 initializedVariableDeclaration | |
195 : declaredIdentifier ('=' expression)? (',' initializedIdentifier)* | |
196 ; | |
197 | |
198 initializedIdentifier | |
199 : identifier ('=' expression)? | |
200 ; | |
201 | |
202 initializedIdentifierList | |
203 : initializedIdentifier (',' initializedIdentifier)* | |
204 ; | |
205 | |
206 functionSignature | |
207 : (type identifier typeParameters? '(') => | |
208 type identifier formalParameterPart | |
209 | identifier formalParameterPart | |
210 ; | |
211 | |
212 functionBodyPrefix | |
213 : ASYNC? '=>' | |
214 | (ASYNC | ASYNC '*' | SYNC '*')? LBRACE | |
215 ; | |
216 | |
217 functionBody | |
218 : '=>' { startNonAsyncFunction(); } expression { endFunction(); } ';' | |
219 | { startNonAsyncFunction(); } block { endFunction(); } | |
220 | ASYNC '=>' | |
221 { startAsyncFunction(); } expression { endFunction(); } ';' | |
222 | (ASYNC | ASYNC '*' | SYNC '*') | |
223 { startAsyncFunction(); } block { endFunction(); } | |
224 ; | |
225 | |
226 block | |
227 : LBRACE statements RBRACE | |
228 ; | |
229 | |
230 formalParameterPart | |
231 : typeParameters? formalParameterList | |
232 ; | |
233 | |
234 formalParameterList | |
235 : '(' ')' | |
236 | '(' normalFormalParameters (','? | ',' optionalFormalParameters) ')' | |
237 | '(' optionalFormalParameters ')' | |
238 ; | |
239 | |
240 normalFormalParameters | |
241 : normalFormalParameter (',' normalFormalParameter)* | |
242 ; | |
243 | |
244 optionalFormalParameters | |
245 : optionalPositionalFormalParameters | |
246 | namedFormalParameters | |
247 ; | |
248 | |
249 optionalPositionalFormalParameters | |
250 : '[' defaultFormalParameter (',' defaultFormalParameter)* ','? ']' | |
251 ; | |
252 | |
253 namedFormalParameters | |
254 : LBRACE defaultNamedParameter (',' defaultNamedParameter)* ','? RBRACE | |
255 ; | |
256 | |
257 normalFormalParameter | |
258 : metadata normalFormalParameterNoMetadata | |
259 ; | |
260 | |
261 // `functionFormalParameter` is split into two alternatives because | |
262 // this eliminates an ambiguity. | |
263 | |
264 normalFormalParameterNoMetadata | |
265 : (COVARIANT? identifier typeParameters? '(') => functionFormalParameter | |
266 | (COVARIANT? type identifier typeParameters? '(') => | |
267 functionFormalParameter | |
268 | (finalConstVarOrType? THIS) => fieldFormalParameter | |
269 | simpleFormalParameter | |
270 ; | |
271 | |
272 functionFormalParameter | |
273 : (COVARIANT? type identifier typeParameters? '(') => | |
274 COVARIANT? type identifier formalParameterPart | |
275 | COVARIANT? identifier formalParameterPart | |
276 ; | |
277 | |
278 simpleFormalParameter | |
279 : declaredIdentifier | |
280 | COVARIANT? identifier | |
281 ; | |
282 | |
283 fieldFormalParameter | |
284 : finalConstVarOrType? THIS '.' identifier formalParameterPart? | |
285 ; | |
286 | |
287 defaultFormalParameter | |
288 : normalFormalParameter ('=' expression)? | |
289 ; | |
290 | |
291 defaultNamedParameter | |
292 : normalFormalParameter ((':' | '=') expression)? | |
293 ; | |
294 | |
295 typeApplication | |
296 : typeIdentifier typeParameters? | |
297 ; | |
298 | |
299 classDefinition | |
300 : (ABSTRACT? CLASS typeApplication (EXTENDS|IMPLEMENTS|LBRACE)) => | |
301 ABSTRACT? CLASS typeApplication (superclass mixins?)? interfaces? | |
302 LBRACE (metadata classMemberDefinition)* RBRACE | |
303 | (ABSTRACT? CLASS typeApplication '=') => | |
304 ABSTRACT? CLASS mixinApplicationClass | |
305 ; | |
306 | |
307 mixins | |
308 : WITH typeNotVoidNotFunctionList | |
309 ; | |
310 | |
311 classMemberDefinition | |
312 : (methodSignature functionBodyPrefix) => methodSignature functionBody | |
313 | declaration ';' | |
314 ; | |
315 | |
316 // `STATIC? functionSignature` is split into two alternatives because this | |
317 // eliminates an ambiguity. | |
318 | |
319 methodSignature | |
320 : (constructorName '(') => constructorSignature initializers? | |
321 | (FACTORY constructorName '(') => factoryConstructorSignature | |
322 | (STATIC? identifier typeParameters? '(') => STATIC? functionSignature | |
323 | (STATIC? type identifier typeParameters? '(') => | |
324 STATIC? functionSignature | |
325 | (STATIC? type? GET) => STATIC? getterSignature | |
326 | (STATIC? type? SET) => STATIC? setterSignature | |
327 | operatorSignature | |
328 ; | |
329 | |
330 // https://github.com/dart-lang/sdk/issues/29501 reports on the problem which | |
331 // was solved by adding a case for redirectingFactoryConstructorSignature. | |
332 // TODO(eernst): Close that issue when this is integrated into the spec. | |
333 | |
334 // https://github.com/dart-lang/sdk/issues/29502 reports on the problem that | |
335 // than external const factory constructor declaration cannot be derived by | |
336 // the spec grammar (and also not by this grammar). The following fixes were | |
337 // introduced for that: Added the 'factoryConstructorSignature' case below in | |
338 // 'declaration'; also added 'CONST?' in the 'factoryConstructorSignature' | |
339 // rule, such that const factories in general are allowed. | |
340 // TODO(eernst): Close that issue when this is integrated into the spec. | |
341 | |
342 declaration | |
343 : (EXTERNAL CONST? FACTORY constructorName '(') => | |
344 EXTERNAL factoryConstructorSignature | |
345 | EXTERNAL constantConstructorSignature | |
346 | (EXTERNAL constructorName '(') => EXTERNAL constructorSignature | |
347 | ((EXTERNAL STATIC?)? type? GET) => (EXTERNAL STATIC?)? getterSignature | |
348 | ((EXTERNAL STATIC?)? type? SET) => (EXTERNAL STATIC?)? setterSignature | |
349 | (EXTERNAL? type? OPERATOR) => EXTERNAL? operatorSignature | |
350 | (STATIC (FINAL | CONST)) => | |
351 STATIC (FINAL | CONST) type? staticFinalDeclarationList | |
352 | FINAL type? initializedIdentifierList | |
353 | ((STATIC | COVARIANT)? (VAR | type) identifier ('=' | ',' | ';')) => | |
354 (STATIC | COVARIANT)? (VAR | type) initializedIdentifierList | |
355 | (EXTERNAL? STATIC? functionSignature ';') => | |
356 EXTERNAL? STATIC? functionSignature | |
357 | (CONST? FACTORY constructorName formalParameterList '=') => | |
358 redirectingFactoryConstructorSignature | |
359 | constantConstructorSignature (redirection | initializers)? | |
360 | constructorSignature (redirection | initializers)? | |
361 ; | |
362 | |
363 staticFinalDeclarationList | |
364 : staticFinalDeclaration (',' staticFinalDeclaration)* | |
365 ; | |
366 | |
367 staticFinalDeclaration | |
368 : identifier '=' expression | |
369 ; | |
370 | |
371 operatorSignature | |
372 : type? OPERATOR operator formalParameterList | |
373 ; | |
374 | |
375 operator | |
376 : '~' | |
377 | binaryOperator | |
378 | '[' ']' | |
379 | '[' ']' '=' | |
380 ; | |
381 | |
382 binaryOperator | |
383 : multiplicativeOperator | |
384 | additiveOperator | |
385 | (shiftOperator) => shiftOperator | |
386 | relationalOperator | |
387 | '==' | |
388 | bitwiseOperator | |
389 ; | |
390 | |
391 getterSignature | |
392 : type? GET identifier | |
393 ; | |
394 | |
395 setterSignature | |
396 : type? SET identifier formalParameterList | |
397 ; | |
398 | |
399 constructorSignature | |
400 : constructorName formalParameterList | |
401 ; | |
402 | |
403 constructorName | |
404 : typeIdentifier ('.' identifier)? | |
405 ; | |
406 | |
407 redirection | |
408 : ':' THIS ('.' identifier)? arguments | |
409 ; | |
410 | |
411 initializers | |
412 : ':' superCallOrFieldInitializer (',' superCallOrFieldInitializer)* | |
413 ; | |
414 | |
415 superCallOrFieldInitializer | |
416 : SUPER arguments | |
417 | SUPER '.' identifier arguments | |
418 | fieldInitializer | |
419 | assertClause | |
420 ; | |
421 | |
422 fieldInitializer | |
423 : (THIS '.')? identifier '=' conditionalExpression cascadeSection* | |
424 ; | |
425 | |
426 factoryConstructorSignature | |
427 : CONST? FACTORY constructorName formalParameterList | |
428 ; | |
429 | |
430 redirectingFactoryConstructorSignature | |
431 : CONST? FACTORY constructorName formalParameterList '=' | |
432 constructorDesignation | |
433 ; | |
434 | |
435 constantConstructorSignature | |
436 : CONST constructorName formalParameterList | |
437 ; | |
438 | |
439 superclass | |
440 : EXTENDS typeNotVoidNotFunction | |
441 ; | |
442 | |
443 interfaces | |
444 : IMPLEMENTS typeNotVoidNotFunctionList | |
445 ; | |
446 | |
447 mixinApplicationClass | |
448 : typeApplication '=' mixinApplication ';' | |
449 ; | |
450 | |
451 mixinApplication | |
452 : typeNotVoidNotFunction mixins interfaces? | |
453 ; | |
454 | |
455 enumType | |
456 : ENUM typeIdentifier LBRACE identifier (',' identifier)* (',')? RBRACE | |
457 ; | |
458 | |
459 typeParameter | |
460 : metadata typeIdentifier (EXTENDS typeNotVoid)? | |
461 ; | |
462 | |
463 typeParameters | |
464 : '<' typeParameter (',' typeParameter)* '>' | |
465 ; | |
466 | |
467 metadata | |
468 : ('@' metadatum)* | |
469 ; | |
470 | |
471 metadatum | |
472 : constructorDesignation arguments | |
473 | qualified | |
474 ; | |
475 | |
476 expression | |
477 : (formalParameterPart functionExpressionBodyPrefix) => | |
478 functionExpression | |
479 | throwExpression | |
480 | (assignableExpression assignmentOperator) => | |
481 assignableExpression assignmentOperator expression | |
482 | conditionalExpression cascadeSection* | |
483 ; | |
484 | |
485 expressionWithoutCascade | |
486 : (formalParameterPart functionExpressionBodyPrefix) => | |
487 functionExpressionWithoutCascade | |
488 | throwExpressionWithoutCascade | |
489 | (assignableExpression assignmentOperator) => | |
490 assignableExpression assignmentOperator expressionWithoutCascade | |
491 | conditionalExpression | |
492 ; | |
493 | |
494 expressionList | |
495 : expression (',' expression)* | |
496 ; | |
497 | |
498 primary | |
499 : thisExpression | |
500 | SUPER unconditionalAssignableSelector | |
501 | (CONST constructorDesignation) => constObjectExpression | |
502 | newExpression | |
503 | (formalParameterPart functionPrimaryBodyPrefix) => functionPrimary | |
504 | '(' expression ')' | |
505 | literal | |
506 | identifier | |
507 ; | |
508 | |
509 literal | |
510 : nullLiteral | |
511 | booleanLiteral | |
512 | numericLiteral | |
513 | stringLiteral | |
514 | symbolLiteral | |
515 | (CONST? typeArguments? LBRACE) => mapLiteral | |
516 | listLiteral | |
517 ; | |
518 | |
519 nullLiteral | |
520 : NULL | |
521 ; | |
522 | |
523 numericLiteral | |
524 : NUMBER | |
525 | HEX_NUMBER | |
526 ; | |
527 | |
528 booleanLiteral | |
529 : TRUE | |
530 | FALSE | |
531 ; | |
532 | |
533 stringLiteral | |
534 : (multiLineString | singleLineString)+ | |
535 ; | |
536 | |
537 stringLiteralWithoutInterpolation | |
538 : singleLineStringWithoutInterpolation+ | |
539 ; | |
540 | |
541 listLiteral | |
542 : CONST? typeArguments? '[' (expressionList ','?)? ']' | |
543 ; | |
544 | |
545 mapLiteral | |
546 : CONST? typeArguments? | |
547 LBRACE (mapLiteralEntry (',' mapLiteralEntry)* ','?)? RBRACE | |
548 ; | |
549 | |
550 mapLiteralEntry | |
551 : expression ':' expression | |
552 ; | |
553 | |
554 throwExpression | |
555 : THROW expression | |
556 ; | |
557 | |
558 throwExpressionWithoutCascade | |
559 : THROW expressionWithoutCascade | |
560 ; | |
561 | |
562 functionExpression | |
563 : formalParameterPart functionExpressionBody | |
564 ; | |
565 | |
566 functionExpressionBody | |
567 : '=>' { startNonAsyncFunction(); } expression { endFunction(); } | |
568 | ASYNC '=>' { startAsyncFunction(); } expression { endFunction(); } | |
569 ; | |
570 | |
571 functionExpressionBodyPrefix | |
572 : ASYNC? '=>' | |
573 ; | |
574 | |
575 functionExpressionWithoutCascade | |
576 : formalParameterPart functionExpressionWithoutCascadeBody | |
577 ; | |
578 | |
579 functionExpressionWithoutCascadeBody | |
580 : '=>' { startNonAsyncFunction(); } | |
581 expressionWithoutCascade { endFunction(); } | |
582 | ASYNC '=>' { startAsyncFunction(); } | |
583 expressionWithoutCascade { endFunction(); } | |
584 ; | |
585 | |
586 functionPrimary | |
587 : formalParameterPart functionPrimaryBody | |
588 ; | |
589 | |
590 functionPrimaryBody | |
591 : { startNonAsyncFunction(); } block { endFunction(); } | |
592 | (ASYNC | ASYNC '*' | SYNC '*') | |
593 { startAsyncFunction(); } block { endFunction(); } | |
594 ; | |
595 | |
596 functionPrimaryBodyPrefix | |
597 : (ASYNC | ASYNC '*' | SYNC '*')? LBRACE | |
598 ; | |
599 | |
600 thisExpression | |
601 : THIS | |
602 ; | |
603 | |
604 newExpression | |
605 : NEW constructorDesignation arguments | |
606 ; | |
607 | |
608 constObjectExpression | |
609 : CONST constructorDesignation arguments | |
610 ; | |
611 | |
612 arguments | |
613 : '(' (argumentList ','?)? ')' | |
614 ; | |
615 | |
616 argumentList | |
617 : namedArgument (',' namedArgument)* | |
618 | expressionList (',' namedArgument)* | |
619 ; | |
620 | |
621 namedArgument | |
622 : label expression | |
623 ; | |
624 | |
625 cascadeSection | |
626 : '..' | |
627 (cascadeSelector argumentPart*) | |
628 (assignableSelector argumentPart*)* | |
629 (assignmentOperator expressionWithoutCascade)? | |
630 ; | |
631 | |
632 cascadeSelector | |
633 : '[' expression ']' | |
634 | identifier | |
635 ; | |
636 | |
637 assignmentOperator | |
638 : '=' | |
639 | compoundAssignmentOperator | |
640 ; | |
641 | |
642 compoundAssignmentOperator | |
643 : '*=' | |
644 | '/=' | |
645 | '~/=' | |
646 | '%=' | |
647 | '+=' | |
648 | '-=' | |
649 | '<<=' | |
650 | '>' '>' '=' | |
651 | '&=' | |
652 | '^=' | |
653 | '|=' | |
654 | '??=' | |
655 ; | |
656 | |
657 conditionalExpression | |
658 : ifNullExpression | |
659 ('?' expressionWithoutCascade ':' expressionWithoutCascade)? | |
660 ; | |
661 | |
662 ifNullExpression | |
663 : logicalOrExpression ('??' logicalOrExpression)* | |
664 ; | |
665 | |
666 logicalOrExpression | |
667 : logicalAndExpression ('||' logicalAndExpression)* | |
668 ; | |
669 | |
670 logicalAndExpression | |
671 : equalityExpression ('&&' equalityExpression)* | |
672 ; | |
673 | |
674 equalityExpression | |
675 : relationalExpression (equalityOperator relationalExpression)? | |
676 | SUPER equalityOperator relationalExpression | |
677 ; | |
678 | |
679 equalityOperator | |
680 : '==' | |
681 | '!=' | |
682 ; | |
683 | |
684 relationalExpression | |
685 : bitwiseOrExpression | |
686 (typeTest | typeCast | relationalOperator bitwiseOrExpression)? | |
687 | SUPER relationalOperator bitwiseOrExpression | |
688 ; | |
689 | |
690 relationalOperator | |
691 : '>' '=' | |
692 | '>' | |
693 | '<=' | |
694 | '<' | |
695 ; | |
696 | |
697 bitwiseOrExpression | |
698 : bitwiseXorExpression ('|' bitwiseXorExpression)* | |
699 | SUPER ('|' bitwiseXorExpression)+ | |
700 ; | |
701 | |
702 bitwiseXorExpression | |
703 : bitwiseAndExpression ('^' bitwiseAndExpression)* | |
704 | SUPER ('^' bitwiseAndExpression)+ | |
705 ; | |
706 | |
707 bitwiseAndExpression | |
708 : shiftExpression ('&' shiftExpression)* | |
709 | SUPER ('&' shiftExpression)+ | |
710 ; | |
711 | |
712 bitwiseOperator | |
713 : '&' | |
714 | '^' | |
715 | '|' | |
716 ; | |
717 | |
718 shiftExpression | |
719 : additiveExpression (shiftOperator additiveExpression)* | |
720 | SUPER (shiftOperator additiveExpression)+ | |
721 ; | |
722 | |
723 shiftOperator | |
724 : '<<' | |
725 | '>' '>' | |
726 ; | |
727 | |
728 additiveExpression | |
729 : multiplicativeExpression (additiveOperator multiplicativeExpression)* | |
730 | SUPER (additiveOperator multiplicativeExpression)+ | |
731 ; | |
732 | |
733 additiveOperator | |
734 : '+' | |
735 | '-' | |
736 ; | |
737 | |
738 multiplicativeExpression | |
739 : unaryExpression (multiplicativeOperator unaryExpression)* | |
740 | SUPER (multiplicativeOperator unaryExpression)+ | |
741 ; | |
742 | |
743 multiplicativeOperator | |
744 : '*' | |
745 | '/' | |
746 | '%' | |
747 | '~/' | |
748 ; | |
749 | |
750 unaryExpression | |
751 : (prefixOperator ~SUPER) => prefixOperator unaryExpression | |
752 | (awaitExpression) => awaitExpression | |
753 | postfixExpression | |
754 | (minusOperator | tildeOperator) SUPER | |
755 | incrementOperator assignableExpression | |
756 ; | |
757 | |
758 prefixOperator | |
759 : minusOperator | |
760 | negationOperator | |
761 | tildeOperator | |
762 ; | |
763 | |
764 minusOperator | |
765 : '-' | |
766 ; | |
767 | |
768 negationOperator | |
769 : '!' | |
770 ; | |
771 | |
772 tildeOperator | |
773 : '~' | |
774 ; | |
775 | |
776 awaitExpression | |
777 : AWAIT unaryExpression | |
778 ; | |
779 | |
780 // The `(selector)` predicate ensures that the parser commits to the longest | |
781 // possible chain of selectors, e.g., `a<b,c>(d)` as a call rather than as a | |
782 // sequence of two relational expressions. | |
783 | |
784 postfixExpression | |
785 : (assignableExpression postfixOperator) => | |
786 assignableExpression postfixOperator | |
787 | primary ((selector) => selector)* | |
788 ; | |
789 | |
790 postfixOperator | |
791 : incrementOperator | |
792 ; | |
793 | |
794 selector | |
795 : assignableSelector | |
796 | argumentPart | |
797 ; | |
798 | |
799 argumentPart | |
800 : typeArguments? arguments | |
801 ; | |
802 | |
803 incrementOperator | |
804 : '++' | |
805 | '--' | |
806 ; | |
807 | |
808 // The `(assignableSelectorPart)` predicate ensures that the parser | |
809 // commits to the longest possible chain, e.g., `a<b,c>(d).e` as one rather | |
810 // than two expressions. The first `identifier` alternative handles all | |
811 // the simple cases; the final `identifier` alternative at the end catches | |
812 // the case where we have `identifier '<'` and the '<' is used as a | |
813 // relationalOperator, not the beginning of typeArguments. | |
814 | |
815 assignableExpression | |
816 : (SUPER unconditionalAssignableSelector | |
817 ~('<' | '(' | '[' | '.' | '?.')) => | |
818 SUPER unconditionalAssignableSelector | |
819 | (identifier ~('<' | '(' | '[' | '.' | '?.')) => identifier | |
820 | (primary argumentPart* assignableSelector) => | |
821 primary ((assignableSelectorPart) => assignableSelectorPart)+ | |
822 | identifier | |
823 ; | |
824 | |
825 assignableSelectorPart | |
826 : argumentPart* assignableSelector | |
827 ; | |
828 | |
829 unconditionalAssignableSelector | |
830 : '[' expression ']' | |
831 | '.' identifier | |
832 ; | |
833 | |
834 assignableSelector | |
835 : unconditionalAssignableSelector | |
836 | '?.' identifier | |
837 ; | |
838 | |
839 identifier | |
840 : IDENTIFIER | |
841 | ABSTRACT | |
842 | AS | |
843 | COVARIANT | |
844 | DEFERRED | |
845 | DYNAMIC | |
846 | EXPORT | |
847 | EXTERNAL | |
848 | FACTORY | |
849 | GET | |
850 | IMPLEMENTS | |
851 | IMPORT | |
852 | LIBRARY | |
853 | OPERATOR | |
854 | PART | |
855 | SET | |
856 | STATIC | |
857 | TYPEDEF | |
858 | HIDE // Not a built-in identifier. | |
859 | OF // Not a built-in identifier. | |
860 | ON // Not a built-in identifier. | |
861 | SHOW // Not a built-in identifier. | |
862 | SYNC // Not a built-in identifier. | |
863 | FUNCTION // Not a built-in identifier. | |
864 | { asyncEtcPredicate(input.LA(1)) }? (ASYNC|AWAIT|YIELD) | |
865 ; | |
866 | |
867 qualified | |
868 : identifier ('.' identifier)? | |
869 ; | |
870 | |
871 typeIdentifier | |
872 : IDENTIFIER | |
873 | DYNAMIC // The only built-in identifier that can be used as a type. | |
874 | HIDE // Not a built-in identifier. | |
875 | OF // Not a built-in identifier. | |
876 | ON // Not a built-in identifier. | |
877 | SHOW // Not a built-in identifier. | |
878 | SYNC // Not a built-in identifier. | |
879 | FUNCTION // Not a built-in identifier. | |
880 | { asyncEtcPredicate(input.LA(1)) }? (ASYNC|AWAIT|YIELD) | |
881 ; | |
882 | |
883 typeTest | |
884 : isOperator typeNotVoid | |
885 ; | |
886 | |
887 isOperator | |
888 : IS '!'? | |
889 ; | |
890 | |
891 typeCast | |
892 : asOperator typeNotVoid | |
893 ; | |
894 | |
895 asOperator | |
896 : AS | |
897 ; | |
898 | |
899 statements | |
900 : statement* | |
901 ; | |
902 | |
903 statement | |
904 : label* nonLabelledStatement | |
905 ; | |
906 | |
907 // Exception in the language specification: An expressionStatement cannot | |
908 // start with LBRACE. We force anything that starts with LBRACE to be a block, | |
909 // which will prevent an expressionStatement from starting with LBRACE, and | |
910 // which will not interfere with the recognition of any other case. If we | |
911 // add another statement which can start with LBRACE we must adjust this | |
912 // check. | |
913 nonLabelledStatement | |
914 : (LBRACE) => block | |
915 | (declaredIdentifier ('='|','|';')) => localVariableDeclaration | |
916 | (AWAIT? FOR) => forStatement | |
917 | whileStatement | |
918 | doStatement | |
919 | switchStatement | |
920 | ifStatement | |
921 | rethrowStatement | |
922 | tryStatement | |
923 | breakStatement | |
924 | continueStatement | |
925 | returnStatement | |
926 | (functionSignature functionBodyPrefix) => localFunctionDeclaration | |
927 | assertStatement | |
928 | (YIELD ~'*') => yieldStatement | |
929 | yieldEachStatement | |
930 | expressionStatement | |
931 ; | |
932 | |
933 expressionStatement | |
934 : expression? ';' | |
935 ; | |
936 | |
937 localVariableDeclaration | |
938 : initializedVariableDeclaration ';' | |
939 ; | |
940 | |
941 localFunctionDeclaration | |
942 : functionSignature functionBody | |
943 ; | |
944 | |
945 ifStatement | |
946 : IF '(' expression ')' statement ((ELSE) => ELSE statement | ()) | |
947 ; | |
948 | |
949 forStatement | |
950 : AWAIT? FOR '(' forLoopParts ')' statement | |
951 ; | |
952 | |
953 forLoopParts | |
954 : (declaredIdentifier IN) => declaredIdentifier IN expression | |
955 | (identifier IN) => identifier IN expression | |
956 | forInitializerStatement expression? ';' expressionList? | |
957 ; | |
958 | |
959 // The localVariableDeclaration cannot be CONST, but that can | |
960 // be enforced in a later phase, and the grammar allows it. | |
961 forInitializerStatement | |
962 : (localVariableDeclaration) => localVariableDeclaration | |
963 | expression? ';' | |
964 ; | |
965 | |
966 whileStatement | |
967 : WHILE '(' expression ')' statement | |
968 ; | |
969 | |
970 doStatement | |
971 : DO statement WHILE '(' expression ')' ';' | |
972 ; | |
973 | |
974 switchStatement | |
975 : SWITCH '(' expression ')' LBRACE switchCase* defaultCase? RBRACE | |
976 ; | |
977 | |
978 switchCase | |
979 : label* CASE expression ':' statements | |
980 ; | |
981 | |
982 defaultCase | |
983 : label* DEFAULT ':' statements | |
984 ; | |
985 | |
986 rethrowStatement | |
987 : RETHROW ';' | |
988 ; | |
989 | |
990 tryStatement | |
991 : TRY block (onParts finallyPart? | finallyPart) | |
992 ; | |
993 | |
994 onPart | |
995 : catchPart block | |
996 | ON typeNotVoid catchPart? block | |
997 ; | |
998 | |
999 onParts | |
1000 : (onPart (ON|CATCH)) => onPart onParts | |
1001 | onPart | |
1002 ; | |
1003 | |
1004 catchPart | |
1005 : CATCH '(' identifier (',' identifier)? ')' | |
1006 ; | |
1007 | |
1008 finallyPart | |
1009 : FINALLY block | |
1010 ; | |
1011 | |
1012 returnStatement | |
1013 : RETURN expression? ';' | |
1014 ; | |
1015 | |
1016 label | |
1017 : identifier ':' | |
1018 ; | |
1019 | |
1020 breakStatement | |
1021 : BREAK identifier? ';' | |
1022 ; | |
1023 | |
1024 continueStatement | |
1025 : CONTINUE identifier? ';' | |
1026 ; | |
1027 | |
1028 yieldStatement | |
1029 : YIELD expression ';' | |
1030 ; | |
1031 | |
1032 yieldEachStatement | |
1033 : YIELD '*' expression ';' | |
1034 ; | |
1035 | |
1036 assertStatement | |
1037 : assertClause ';' | |
1038 ; | |
1039 | |
1040 assertClause | |
1041 : ASSERT '(' expression (',' expression)? ')' | |
1042 ; | |
1043 | |
1044 libraryName | |
1045 : metadata LIBRARY identifier ('.' identifier)* ';' | |
1046 ; | |
1047 | |
1048 importOrExport | |
1049 : (metadata IMPORT) => libraryImport | |
1050 | (metadata EXPORT) => libraryExport | |
1051 ; | |
1052 | |
1053 libraryImport | |
1054 : metadata importSpecification | |
1055 ; | |
1056 | |
1057 importSpecification | |
1058 : IMPORT uri (AS identifier)? combinator* ';' | |
1059 | IMPORT uri DEFERRED AS identifier combinator* ';' | |
1060 ; | |
1061 | |
1062 combinator | |
1063 : SHOW identifierList | |
1064 | HIDE identifierList | |
1065 ; | |
1066 | |
1067 identifierList | |
1068 : identifier (',' identifier)* | |
1069 ; | |
1070 | |
1071 libraryExport | |
1072 : metadata EXPORT uri combinator* ';' | |
1073 ; | |
1074 | |
1075 partDirective | |
1076 : metadata PART uri ';' | |
1077 ; | |
1078 | |
1079 partHeader | |
1080 : metadata PART OF identifier ('.' identifier)* ';' | |
1081 ; | |
1082 | |
1083 partDeclaration | |
1084 : partHeader topLevelDefinition* EOF | |
1085 ; | |
1086 | |
1087 uri | |
1088 : stringLiteralWithoutInterpolation | |
1089 ; | |
1090 | |
1091 type | |
1092 : (FUNCTION ('('|'<')) => functionTypeTails | |
1093 | (typeNotFunction FUNCTION ('('|'<')) => | |
1094 typeNotFunction functionTypeTails | |
1095 | typeNotFunction | |
1096 ; | |
1097 | |
1098 typeNotFunction | |
1099 : typeNotVoidNotFunction | |
1100 | VOID | |
1101 ; | |
1102 | |
1103 typeNotVoid | |
1104 : (typeNotFunction? FUNCTION ('('|'<')) => functionType | |
1105 | typeNotVoidNotFunction | |
1106 ; | |
1107 | |
1108 typeNotVoidNotFunction | |
1109 : typeName typeArguments? | |
1110 ; | |
1111 | |
1112 typeName | |
1113 : typeIdentifier ('.' typeIdentifier)? | |
1114 ; | |
1115 | |
1116 typeArguments | |
1117 : '<' typeList '>' | |
1118 ; | |
1119 | |
1120 typeList | |
1121 : type (',' type)* | |
1122 ; | |
1123 | |
1124 typeNotVoidNotFunctionList | |
1125 : typeNotVoidNotFunction (',' typeNotVoidNotFunction)* | |
1126 ; | |
1127 | |
1128 typeAlias | |
1129 : (TYPEDEF typeIdentifier typeParameters? '=') => | |
1130 TYPEDEF typeIdentifier typeParameters? '=' functionType ';' | |
1131 | TYPEDEF functionTypeAlias | |
1132 ; | |
1133 | |
1134 functionTypeAlias | |
1135 : functionPrefix formalParameterPart ';' | |
1136 ; | |
1137 | |
1138 functionPrefix | |
1139 : (type identifier) => type identifier | |
1140 | identifier | |
1141 ; | |
1142 | |
1143 functionTypeTail | |
1144 : FUNCTION typeParameters? parameterTypeList | |
1145 ; | |
1146 | |
1147 functionTypeTails | |
1148 : (functionTypeTail FUNCTION ('<'|'(')) => | |
1149 functionTypeTail functionTypeTails | |
1150 | functionTypeTail | |
1151 ; | |
1152 | |
1153 functionType | |
1154 : (FUNCTION ('<'|'(')) => functionTypeTails | |
1155 | typeNotFunction functionTypeTails | |
1156 ; | |
1157 | |
1158 parameterTypeList | |
1159 : ('(' ')') => '(' ')' | |
1160 | ('(' normalParameterTypes ',' ('['|'{')) => | |
1161 '(' normalParameterTypes ',' optionalParameterTypes ')' | |
1162 | ('(' normalParameterTypes ','? ')') => | |
1163 '(' normalParameterTypes ','? ')' | |
1164 | '(' optionalParameterTypes ')' | |
1165 ; | |
1166 | |
1167 normalParameterTypes | |
1168 : normalParameterType (',' normalParameterType)* | |
1169 ; | |
1170 | |
1171 normalParameterType | |
1172 : (typedIdentifier) => typedIdentifier | |
1173 | type | |
1174 ; | |
1175 | |
1176 optionalParameterTypes | |
1177 : optionalPositionalParameterTypes | |
1178 | namedParameterTypes | |
1179 ; | |
1180 | |
1181 optionalPositionalParameterTypes | |
1182 : '[' normalParameterTypes ','? ']' | |
1183 ; | |
1184 | |
1185 namedParameterTypes | |
1186 : '{' typedIdentifier (',' typedIdentifier)* ','? '}' | |
1187 ; | |
1188 | |
1189 typedIdentifier | |
1190 : type identifier | |
1191 ; | |
1192 | |
1193 constructorDesignation | |
1194 : typeIdentifier | |
1195 | identifier '.' identifier | |
1196 | identifier '.' typeIdentifier '.' identifier | |
1197 | typeName typeArguments ('.' identifier)? | |
1198 ; | |
1199 | |
1200 // Predicate: Force resolution as composite symbolLiteral as far as possible. | |
1201 symbolLiteral | |
1202 : '#' (operator | (identifier (('.' identifier) => '.' identifier)*)) | |
1203 ; | |
1204 | |
1205 singleLineStringWithoutInterpolation | |
1206 : RAW_SINGLE_LINE_STRING | |
1207 | SINGLE_LINE_STRING_DQ_BEGIN_END | |
1208 | SINGLE_LINE_STRING_SQ_BEGIN_END | |
1209 ; | |
1210 | |
1211 singleLineString | |
1212 : RAW_SINGLE_LINE_STRING | |
1213 | SINGLE_LINE_STRING_SQ_BEGIN_END | |
1214 | SINGLE_LINE_STRING_SQ_BEGIN_MID expression | |
1215 (SINGLE_LINE_STRING_SQ_MID_MID expression)* | |
1216 SINGLE_LINE_STRING_SQ_MID_END | |
1217 | SINGLE_LINE_STRING_DQ_BEGIN_END | |
1218 | SINGLE_LINE_STRING_DQ_BEGIN_MID expression | |
1219 (SINGLE_LINE_STRING_DQ_MID_MID expression)* | |
1220 SINGLE_LINE_STRING_DQ_MID_END | |
1221 ; | |
1222 | |
1223 multiLineString | |
1224 : RAW_MULTI_LINE_STRING | |
1225 | MULTI_LINE_STRING_SQ_BEGIN_END | |
1226 | MULTI_LINE_STRING_SQ_BEGIN_MID expression | |
1227 (MULTI_LINE_STRING_SQ_MID_MID expression)* | |
1228 MULTI_LINE_STRING_SQ_MID_END | |
1229 | MULTI_LINE_STRING_DQ_BEGIN_END | |
1230 | MULTI_LINE_STRING_DQ_BEGIN_MID expression | |
1231 (MULTI_LINE_STRING_DQ_MID_MID expression)* | |
1232 MULTI_LINE_STRING_DQ_MID_END | |
1233 ; | |
1234 | |
1235 // ---------------------------------------- Lexer rules. | |
1236 | |
1237 fragment | |
1238 LETTER | |
1239 : 'a' .. 'z' | |
1240 | 'A' .. 'Z' | |
1241 ; | |
1242 | |
1243 fragment | |
1244 DIGIT | |
1245 : '0' .. '9' | |
1246 ; | |
1247 | |
1248 fragment | |
1249 EXPONENT | |
1250 : ('e' | 'E') ('+' | '-')? DIGIT+ | |
1251 ; | |
1252 | |
1253 fragment | |
1254 HEX_DIGIT | |
1255 : ('a' | 'b' | 'c' | 'd' | 'e' | 'f') | |
1256 | ('A' | 'B' | 'C' | 'D' | 'E' | 'F') | |
1257 | DIGIT | |
1258 ; | |
1259 | |
1260 FINAL | |
1261 : 'final' | |
1262 ; | |
1263 | |
1264 CONST | |
1265 : 'const' | |
1266 ; | |
1267 | |
1268 VAR | |
1269 : 'var' | |
1270 ; | |
1271 | |
1272 VOID | |
1273 : 'void' | |
1274 ; | |
1275 | |
1276 ASYNC | |
1277 : 'async' | |
1278 ; | |
1279 | |
1280 THIS | |
1281 : 'this' | |
1282 ; | |
1283 | |
1284 ABSTRACT | |
1285 : 'abstract' | |
1286 ; | |
1287 | |
1288 AS | |
1289 : 'as' | |
1290 ; | |
1291 | |
1292 SYNC | |
1293 : 'sync' | |
1294 ; | |
1295 | |
1296 CLASS | |
1297 : 'class' | |
1298 ; | |
1299 | |
1300 WITH | |
1301 : 'with' | |
1302 ; | |
1303 | |
1304 STATIC | |
1305 : 'static' | |
1306 ; | |
1307 | |
1308 DYNAMIC | |
1309 : 'dynamic' | |
1310 ; | |
1311 | |
1312 EXTERNAL | |
1313 : 'external' | |
1314 ; | |
1315 | |
1316 GET | |
1317 : 'get' | |
1318 ; | |
1319 | |
1320 SET | |
1321 : 'set' | |
1322 ; | |
1323 | |
1324 OPERATOR | |
1325 : 'operator' | |
1326 ; | |
1327 | |
1328 SUPER | |
1329 : 'super' | |
1330 ; | |
1331 | |
1332 FACTORY | |
1333 : 'factory' | |
1334 ; | |
1335 | |
1336 EXTENDS | |
1337 : 'extends' | |
1338 ; | |
1339 | |
1340 IMPLEMENTS | |
1341 : 'implements' | |
1342 ; | |
1343 | |
1344 ENUM | |
1345 : 'enum' | |
1346 ; | |
1347 | |
1348 NULL | |
1349 : 'null' | |
1350 ; | |
1351 | |
1352 TRUE | |
1353 : 'true' | |
1354 ; | |
1355 | |
1356 FALSE | |
1357 : 'false' | |
1358 ; | |
1359 | |
1360 THROW | |
1361 : 'throw' | |
1362 ; | |
1363 | |
1364 NEW | |
1365 : 'new' | |
1366 ; | |
1367 | |
1368 AWAIT | |
1369 : 'await' | |
1370 ; | |
1371 | |
1372 DEFERRED | |
1373 : 'deferred' | |
1374 ; | |
1375 | |
1376 EXPORT | |
1377 : 'export' | |
1378 ; | |
1379 | |
1380 IMPORT | |
1381 : 'import' | |
1382 ; | |
1383 | |
1384 LIBRARY | |
1385 : 'library' | |
1386 ; | |
1387 | |
1388 PART | |
1389 : 'part' | |
1390 ; | |
1391 | |
1392 TYPEDEF | |
1393 : 'typedef' | |
1394 ; | |
1395 | |
1396 IS | |
1397 : 'is' | |
1398 ; | |
1399 | |
1400 IF | |
1401 : 'if' | |
1402 ; | |
1403 | |
1404 ELSE | |
1405 : 'else' | |
1406 ; | |
1407 | |
1408 WHILE | |
1409 : 'while' | |
1410 ; | |
1411 | |
1412 FOR | |
1413 : 'for' | |
1414 ; | |
1415 | |
1416 IN | |
1417 : 'in' | |
1418 ; | |
1419 | |
1420 DO | |
1421 : 'do' | |
1422 ; | |
1423 | |
1424 SWITCH | |
1425 : 'switch' | |
1426 ; | |
1427 | |
1428 CASE | |
1429 : 'case' | |
1430 ; | |
1431 | |
1432 DEFAULT | |
1433 : 'default' | |
1434 ; | |
1435 | |
1436 RETHROW | |
1437 : 'rethrow' | |
1438 ; | |
1439 | |
1440 TRY | |
1441 : 'try' | |
1442 ; | |
1443 | |
1444 ON | |
1445 : 'on' | |
1446 ; | |
1447 | |
1448 CATCH | |
1449 : 'catch' | |
1450 ; | |
1451 | |
1452 FINALLY | |
1453 : 'finally' | |
1454 ; | |
1455 | |
1456 RETURN | |
1457 : 'return' | |
1458 ; | |
1459 | |
1460 BREAK | |
1461 : 'break' | |
1462 ; | |
1463 | |
1464 CONTINUE | |
1465 : 'continue' | |
1466 ; | |
1467 | |
1468 YIELD | |
1469 : 'yield' | |
1470 ; | |
1471 | |
1472 SHOW | |
1473 : 'show' | |
1474 ; | |
1475 | |
1476 HIDE | |
1477 : 'hide' | |
1478 ; | |
1479 | |
1480 OF | |
1481 : 'of' | |
1482 ; | |
1483 | |
1484 ASSERT | |
1485 : 'assert' | |
1486 ; | |
1487 | |
1488 COVARIANT | |
1489 : 'covariant' | |
1490 ; | |
1491 | |
1492 FUNCTION | |
1493 : 'Function' | |
1494 ; | |
1495 | |
1496 NUMBER | |
1497 : (DIGIT+ '.' DIGIT) => DIGIT+ '.' DIGIT+ EXPONENT? | |
1498 | DIGIT+ EXPONENT? | |
1499 | '.' DIGIT+ EXPONENT? | |
1500 ; | |
1501 | |
1502 HEX_NUMBER | |
1503 : '0x' HEX_DIGIT+ | |
1504 | '0X' HEX_DIGIT+ | |
1505 ; | |
1506 | |
1507 RAW_SINGLE_LINE_STRING | |
1508 : 'r' '\'' (~('\'' | '\r' | '\n'))* '\'' | |
1509 | 'r' '"' (~('"' | '\r' | '\n'))* '"' | |
1510 ; | |
1511 | |
1512 RAW_MULTI_LINE_STRING | |
1513 : 'r' '"""' (options {greedy=false;} : .)* '"""' | |
1514 | 'r' '\'\'\'' (options {greedy=false;} : .)* '\'\'\'' | |
1515 ; | |
1516 | |
1517 fragment | |
1518 SIMPLE_STRING_INTERPOLATION | |
1519 : '$' IDENTIFIER_NO_DOLLAR | |
1520 ; | |
1521 | |
1522 fragment | |
1523 STRING_CONTENT_SQ | |
1524 : ~('\\' | '\'' | '$' | '\r' | '\n') | |
1525 | '\\' ~( '\r' | '\n') | |
1526 | SIMPLE_STRING_INTERPOLATION | |
1527 ; | |
1528 | |
1529 SINGLE_LINE_STRING_SQ_BEGIN_END | |
1530 : '\'' STRING_CONTENT_SQ* '\'' | |
1531 ; | |
1532 | |
1533 SINGLE_LINE_STRING_SQ_BEGIN_MID | |
1534 : '\'' STRING_CONTENT_SQ* '${' { enterBraceSingleQuote(); } | |
1535 ; | |
1536 | |
1537 SINGLE_LINE_STRING_SQ_MID_MID | |
1538 : { currentBraceLevel(BRACE_SINGLE) }? => | |
1539 ('}' STRING_CONTENT_SQ* '${') => | |
1540 { exitBrace(); } '}' STRING_CONTENT_SQ* '${' | |
1541 { enterBraceSingleQuote(); } | |
1542 ; | |
1543 | |
1544 SINGLE_LINE_STRING_SQ_MID_END | |
1545 : { currentBraceLevel(BRACE_SINGLE) }? => | |
1546 ('}' STRING_CONTENT_SQ* '\'') => | |
1547 { exitBrace(); } '}' STRING_CONTENT_SQ* '\'' | |
1548 ; | |
1549 | |
1550 fragment | |
1551 STRING_CONTENT_DQ | |
1552 : ~('\\' | '"' | '$' | '\r' | '\n') | |
1553 | '\\' ~('\r' | '\n') | |
1554 | SIMPLE_STRING_INTERPOLATION | |
1555 ; | |
1556 | |
1557 SINGLE_LINE_STRING_DQ_BEGIN_END | |
1558 : '"' STRING_CONTENT_DQ* '"' | |
1559 ; | |
1560 | |
1561 SINGLE_LINE_STRING_DQ_BEGIN_MID | |
1562 : '"' STRING_CONTENT_DQ* '${' { enterBraceDoubleQuote(); } | |
1563 ; | |
1564 | |
1565 SINGLE_LINE_STRING_DQ_MID_MID | |
1566 : { currentBraceLevel(BRACE_DOUBLE) }? => | |
1567 ('}' STRING_CONTENT_DQ* '${') => | |
1568 { exitBrace(); } '}' STRING_CONTENT_DQ* '${' | |
1569 { enterBraceDoubleQuote(); } | |
1570 ; | |
1571 | |
1572 SINGLE_LINE_STRING_DQ_MID_END | |
1573 : { currentBraceLevel(BRACE_DOUBLE) }? => | |
1574 ('}' STRING_CONTENT_DQ* '"') => | |
1575 { exitBrace(); } '}' STRING_CONTENT_DQ* '"' | |
1576 ; | |
1577 | |
1578 fragment | |
1579 QUOTES_SQ | |
1580 : | |
1581 | '\'' | |
1582 | '\'\'' | |
1583 ; | |
1584 | |
1585 // Read string contents, which may be almost anything, but stop when seeing | |
1586 // '\'\'\'' and when seeing '${'. We do this by allowing all other | |
1587 // possibilities including escapes, simple interpolation, and fewer than | |
1588 // three '\''. | |
1589 fragment | |
1590 STRING_CONTENT_TSQ | |
1591 : QUOTES_SQ | |
1592 (~('\\' | '$' | '\'') | '\\' . | SIMPLE_STRING_INTERPOLATION) | |
1593 ; | |
1594 | |
1595 MULTI_LINE_STRING_SQ_BEGIN_END | |
1596 : '\'\'\'' STRING_CONTENT_TSQ* '\'\'\'' | |
1597 ; | |
1598 | |
1599 MULTI_LINE_STRING_SQ_BEGIN_MID | |
1600 : '\'\'\'' STRING_CONTENT_TSQ* QUOTES_SQ '${' | |
1601 { enterBraceThreeSingleQuotes(); } | |
1602 ; | |
1603 | |
1604 MULTI_LINE_STRING_SQ_MID_MID | |
1605 : { currentBraceLevel(BRACE_THREE_SINGLE) }? => | |
1606 ('}' STRING_CONTENT_TSQ* QUOTES_SQ '${') => | |
1607 { exitBrace(); } '}' STRING_CONTENT_TSQ* QUOTES_SQ '${' | |
1608 { enterBraceThreeSingleQuotes(); } | |
1609 ; | |
1610 | |
1611 MULTI_LINE_STRING_SQ_MID_END | |
1612 : { currentBraceLevel(BRACE_THREE_SINGLE) }? => | |
1613 ('}' STRING_CONTENT_TSQ* '\'\'\'') => | |
1614 { exitBrace(); } '}' STRING_CONTENT_TSQ* '\'\'\'' | |
1615 ; | |
1616 | |
1617 fragment | |
1618 QUOTES_DQ | |
1619 : | |
1620 | '"' | |
1621 | '""' | |
1622 ; | |
1623 | |
1624 // Read string contents, which may be almost anything, but stop when seeing | |
1625 // '"""' and when seeing '${'. We do this by allowing all other possibilities | |
1626 // including escapes, simple interpolation, and fewer-than-three '"'. | |
1627 fragment | |
1628 STRING_CONTENT_TDQ | |
1629 : QUOTES_DQ | |
1630 (~('\\' | '$' | '"') | '\\' . | SIMPLE_STRING_INTERPOLATION) | |
1631 ; | |
1632 | |
1633 MULTI_LINE_STRING_DQ_BEGIN_END | |
1634 : '"""' STRING_CONTENT_TDQ* '"""' | |
1635 ; | |
1636 | |
1637 MULTI_LINE_STRING_DQ_BEGIN_MID | |
1638 : '"""' STRING_CONTENT_TDQ* QUOTES_DQ '${' | |
1639 { enterBraceThreeDoubleQuotes(); } | |
1640 ; | |
1641 | |
1642 MULTI_LINE_STRING_DQ_MID_MID | |
1643 : { currentBraceLevel(BRACE_THREE_DOUBLE) }? => | |
1644 ('}' STRING_CONTENT_TDQ* QUOTES_DQ '${') => | |
1645 { exitBrace(); } '}' STRING_CONTENT_TDQ* QUOTES_DQ '${' | |
1646 { enterBraceThreeDoubleQuotes(); } | |
1647 ; | |
1648 | |
1649 MULTI_LINE_STRING_DQ_MID_END | |
1650 : { currentBraceLevel(BRACE_THREE_DOUBLE) }? => | |
1651 ('}' STRING_CONTENT_TDQ* '"""') => | |
1652 { exitBrace(); } '}' STRING_CONTENT_TDQ* '"""' | |
1653 ; | |
1654 | |
1655 LBRACE | |
1656 : '{' { enterBrace(); } | |
1657 ; | |
1658 | |
1659 RBRACE | |
1660 : { currentBraceLevel(BRACE_NORMAL) }? => ('}') => { exitBrace(); } '}' | |
1661 ; | |
1662 | |
1663 fragment | |
1664 IDENTIFIER_START_NO_DOLLAR | |
1665 : LETTER | |
1666 | '_' | |
1667 ; | |
1668 | |
1669 fragment | |
1670 IDENTIFIER_PART_NO_DOLLAR | |
1671 : IDENTIFIER_START_NO_DOLLAR | |
1672 | DIGIT | |
1673 ; | |
1674 | |
1675 fragment | |
1676 IDENTIFIER_NO_DOLLAR | |
1677 : IDENTIFIER_START_NO_DOLLAR IDENTIFIER_PART_NO_DOLLAR* | |
1678 ; | |
1679 | |
1680 fragment | |
1681 IDENTIFIER_START | |
1682 : IDENTIFIER_START_NO_DOLLAR | |
1683 | '$' | |
1684 ; | |
1685 | |
1686 fragment | |
1687 IDENTIFIER_PART | |
1688 : IDENTIFIER_START | |
1689 | DIGIT | |
1690 ; | |
1691 | |
1692 SCRIPT_TAG | |
1693 : '#!' (~('\r' | '\n'))* NEWLINE | |
1694 ; | |
1695 | |
1696 IDENTIFIER | |
1697 : IDENTIFIER_START IDENTIFIER_PART* | |
1698 ; | |
1699 | |
1700 SINGLE_LINE_COMMENT | |
1701 : '//' (~('\r' | '\n'))* NEWLINE? | |
1702 { skip(); } | |
1703 ; | |
1704 | |
1705 MULTI_LINE_COMMENT | |
1706 : '/*' (options {greedy=false;} : (MULTI_LINE_COMMENT | .))* '*/' | |
1707 { skip(); } | |
1708 ; | |
1709 | |
1710 fragment | |
1711 NEWLINE | |
1712 : ('\r' | '\n' | '\r\n') | |
1713 ; | |
1714 | |
1715 FEFF | |
1716 : '\uFEFF' | |
1717 ; | |
1718 | |
1719 WS | |
1720 : (' ' | '\t' | '\r' | '\n')+ | |
1721 { skip(); } | |
1722 ; | |
OLD | NEW |