OLD | NEW |
---|---|
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library fasta.analyzer.ast_builder; | 5 library fasta.analyzer.ast_builder; |
6 | 6 |
7 import 'dart:collection' show Queue; | |
8 | |
9 import 'package:analyzer/analyzer.dart'; | |
10 import 'package:analyzer/dart/ast/ast_factory.dart' show AstFactory; | |
11 import 'package:analyzer/dart/ast/standard_ast_factory.dart' as standard; | |
12 import 'package:analyzer/dart/ast/token.dart' as analyzer show Token; | |
13 import 'package:analyzer/dart/element/element.dart' show Element; | |
7 import 'package:front_end/src/fasta/scanner/token.dart' | 14 import 'package:front_end/src/fasta/scanner/token.dart' |
8 show BeginGroupToken, Token; | 15 show BeginGroupToken, Token; |
9 | |
10 import 'package:analyzer/analyzer.dart'; | |
11 | |
12 import 'package:analyzer/dart/ast/token.dart' as analyzer show Token; | |
13 | |
14 import 'package:analyzer/dart/element/element.dart' show Element; | |
15 | |
16 import 'package:analyzer/dart/ast/ast_factory.dart' show AstFactory; | |
17 | |
18 import 'package:analyzer/dart/ast/standard_ast_factory.dart' as standard; | |
19 | |
20 import 'package:kernel/ast.dart' show AsyncMarker; | 16 import 'package:kernel/ast.dart' show AsyncMarker; |
21 | 17 |
22 import '../errors.dart' show internalError; | 18 import '../errors.dart' show internalError; |
23 | 19 import '../kernel/kernel_builder.dart' |
20 show Builder, KernelLibraryBuilder, ProcedureBuilder; | |
21 import '../parser/parser.dart' show optional; | |
22 import '../quote.dart'; | |
23 import '../source/outline_builder.dart' show asyncMarkerFromTokens; | |
24 import '../source/scope_listener.dart' | 24 import '../source/scope_listener.dart' |
25 show JumpTargetKind, NullValue, Scope, ScopeListener; | 25 show JumpTargetKind, NullValue, Scope, ScopeListener; |
26 | 26 import 'analyzer.dart' show toKernel; |
27 import '../kernel/kernel_builder.dart' | |
28 show Builder, KernelLibraryBuilder, ProcedureBuilder; | |
29 | |
30 import '../parser/parser.dart' show optional; | |
31 | |
32 import '../quote.dart'; | |
33 | |
34 import '../source/outline_builder.dart' show asyncMarkerFromTokens; | |
35 | |
36 import 'element_store.dart' | 27 import 'element_store.dart' |
37 show | 28 show |
38 AnalyzerLocalVariableElemment, | 29 AnalyzerLocalVariableElemment, |
39 AnalyzerParameterElement, | 30 AnalyzerParameterElement, |
40 ElementStore, | 31 ElementStore, |
41 KernelClassElement; | 32 KernelClassElement; |
42 | |
43 import 'token_utils.dart' show toAnalyzerToken; | 33 import 'token_utils.dart' show toAnalyzerToken; |
44 | 34 |
45 import 'analyzer.dart' show toKernel; | |
46 | |
47 class AstBuilder extends ScopeListener { | 35 class AstBuilder extends ScopeListener { |
48 final AstFactory ast = standard.astFactory; | 36 final AstFactory ast = standard.astFactory; |
49 | 37 |
50 final KernelLibraryBuilder library; | 38 final KernelLibraryBuilder library; |
51 | 39 |
52 final Builder member; | 40 final Builder member; |
53 | 41 |
54 final ElementStore elementStore; | 42 final ElementStore elementStore; |
55 | 43 |
56 bool isFirstIdentifier = false; | 44 bool isFirstIdentifier = false; |
57 | 45 |
58 @override | 46 @override |
59 final Uri uri; | 47 final Uri uri; |
60 | 48 |
61 /// If `true`, the first call to [handleIdentifier] should push a | 49 /// If `true`, the first call to [handleIdentifier] should push a |
62 /// List<SimpleIdentifier> on the stack, and [handleQualified] should append | 50 /// List<SimpleIdentifier> on the stack, and [handleQualified] should append |
63 /// to the list. | 51 /// to the list. |
64 var accumulateIdentifierComponents = false; | 52 var accumulateIdentifierComponents = false; |
65 | 53 |
54 /// The kind of current formal parameters, optional or required. | |
55 final Queue<ParameterKind> currentParameterKind = new Queue<ParameterKind>() | |
56 ..add(ParameterKind.REQUIRED); | |
57 | |
58 /// The token `=` or `:` before the current parameter default value, or | |
59 /// `null` if not the current parameter is not default. | |
60 Token parameterDefaultValueToken; | |
61 | |
62 /// The current parameter default value expression, or `null` if not the | |
63 /// current parameter is not default. | |
64 Expression parameterDefaultValueExpression; | |
65 | |
66 AstBuilder(this.library, this.member, this.elementStore, Scope scope, | 66 AstBuilder(this.library, this.member, this.elementStore, Scope scope, |
67 [Uri uri]) | 67 [Uri uri]) |
68 : uri = uri ?? library.fileUri, | 68 : uri = uri ?? library.fileUri, |
69 super(scope); | 69 super(scope); |
70 | 70 |
71 createJumpTarget(JumpTargetKind kind, int charOffset) { | 71 createJumpTarget(JumpTargetKind kind, int charOffset) { |
72 // TODO(ahe): Implement jump targets. | 72 // TODO(ahe): Implement jump targets. |
73 return null; | 73 return null; |
74 } | 74 } |
75 | 75 |
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
495 Expression condition = pop(); | 495 Expression condition = pop(); |
496 push(ast.conditionalExpression(condition, toAnalyzerToken(question), | 496 push(ast.conditionalExpression(condition, toAnalyzerToken(question), |
497 thenExpression, toAnalyzerToken(colon), elseExpression)); | 497 thenExpression, toAnalyzerToken(colon), elseExpression)); |
498 } | 498 } |
499 | 499 |
500 void endThrowExpression(Token throwToken, Token endToken) { | 500 void endThrowExpression(Token throwToken, Token endToken) { |
501 debugEvent("ThrowExpression"); | 501 debugEvent("ThrowExpression"); |
502 push(ast.throwExpression(toAnalyzerToken(throwToken), pop())); | 502 push(ast.throwExpression(toAnalyzerToken(throwToken), pop())); |
503 } | 503 } |
504 | 504 |
505 @override | |
506 void beginOptionalFormalParameters(Token token) { | |
507 String value = token.stringValue; | |
508 if (identical('[', value)) { | |
509 currentParameterKind.addLast(ParameterKind.POSITIONAL); | |
ahe
2017/02/22 09:25:15
I suggest that you push this on the stack instead
scheglov
2017/02/22 17:18:14
How can I access it later?
After parsing the first
ahe
2017/02/22 17:30:23
That sounds like a good idea. We need to take adva
| |
510 } else if (identical('{', value)) { | |
511 currentParameterKind.addLast(ParameterKind.NAMED); | |
512 } | |
513 } | |
514 | |
515 @override | |
516 void endOptionalFormalParameters( | |
517 int count, Token beginToken, Token endToken) { | |
518 debugEvent("OptionalFormalParameters"); | |
519 currentParameterKind.removeLast(); | |
520 } | |
521 | |
522 void handleValuedFormalParameter(Token equals, Token token) { | |
523 debugEvent("ValuedFormalParameter"); | |
524 parameterDefaultValueToken = equals; | |
525 parameterDefaultValueExpression = pop(); | |
ahe
2017/02/22 09:25:15
I would recommend against setting state in the lis
scheglov
2017/02/22 17:18:14
OK, I will add handleFormalParameterWithoutValue()
| |
526 } | |
527 | |
505 void endFormalParameter(Token thisKeyword) { | 528 void endFormalParameter(Token thisKeyword) { |
506 debugEvent("FormalParameter"); | 529 debugEvent("FormalParameter"); |
507 if (thisKeyword != null) { | 530 if (thisKeyword != null) { |
508 internalError("'this' can't be used here."); | 531 internalError("'this' can't be used here."); |
509 } | 532 } |
510 SimpleIdentifier name = pop(); | 533 SimpleIdentifier name = pop(); |
511 TypeName type = pop(); | 534 TypeName type = pop(); |
512 pop(); // Modifiers. | 535 pop(); // Modifiers. |
513 pop(); // Metadata. | 536 pop(); // Metadata. |
514 SimpleFormalParameter node = ast.simpleFormalParameter( | 537 FormalParameter node = ast.simpleFormalParameter( |
515 null, null, toAnalyzerToken(thisKeyword), type, name); | 538 null, null, toAnalyzerToken(thisKeyword), type, name); |
539 | |
540 if (parameterDefaultValueToken != null) { | |
541 node = ast.defaultFormalParameter( | |
542 node, | |
543 currentParameterKind.last, | |
544 toAnalyzerToken(parameterDefaultValueToken), | |
545 parameterDefaultValueExpression); | |
546 } | |
547 | |
516 scope[name.name] = name.staticElement = new AnalyzerParameterElement(node); | 548 scope[name.name] = name.staticElement = new AnalyzerParameterElement(node); |
517 push(node); | 549 push(node); |
550 parameterDefaultValueToken = null; | |
551 parameterDefaultValueExpression = null; | |
518 } | 552 } |
519 | 553 |
520 void endFormalParameters(int count, Token beginToken, Token endToken) { | 554 void endFormalParameters(int count, Token beginToken, Token endToken) { |
521 debugEvent("FormalParameters"); | 555 debugEvent("FormalParameters"); |
522 List<FormalParameter> parameters = popList(count) ?? <FormalParameter>[]; | 556 List<FormalParameter> parameters = popList(count) ?? <FormalParameter>[]; |
523 push(ast.formalParameterList(toAnalyzerToken(beginToken), parameters, null, | 557 push(ast.formalParameterList(toAnalyzerToken(beginToken), parameters, null, |
524 null, toAnalyzerToken(endToken))); | 558 null, toAnalyzerToken(endToken))); |
525 } | 559 } |
526 | 560 |
527 void handleCatchBlock(Token onKeyword, Token catchKeyword) { | 561 void handleCatchBlock(Token onKeyword, Token catchKeyword) { |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
866 debugEvent("MixinApplication"); | 900 debugEvent("MixinApplication"); |
867 List<TypeName> mixinTypes = pop(); | 901 List<TypeName> mixinTypes = pop(); |
868 // TODO(paulberry,ahe): the parser doesn't give us enough information to | 902 // TODO(paulberry,ahe): the parser doesn't give us enough information to |
869 // locate the "with" keyword. | 903 // locate the "with" keyword. |
870 Token withKeyword; | 904 Token withKeyword; |
871 TypeName supertype = pop(); | 905 TypeName supertype = pop(); |
872 push(new _MixinApplication(supertype, withKeyword, mixinTypes)); | 906 push(new _MixinApplication(supertype, withKeyword, mixinTypes)); |
873 } | 907 } |
874 | 908 |
875 @override | 909 @override |
876 void endNamedMixinApplication( | 910 void endNamedMixinApplication(Token beginToken, Token equalsToken, |
877 Token beginToken, Token equalsToken, Token implementsKeyword, Token endTok en) { | 911 Token implementsKeyword, Token endToken) { |
878 debugEvent("NamedMixinApplication"); | 912 debugEvent("NamedMixinApplication"); |
879 ImplementsClause implementsClause; | 913 ImplementsClause implementsClause; |
880 if (implementsKeyword != null) { | 914 if (implementsKeyword != null) { |
881 List<TypeName> interfaces = pop(); | 915 List<TypeName> interfaces = pop(); |
882 implementsClause = | 916 implementsClause = |
883 ast.implementsClause(toAnalyzerToken(implementsKeyword), interfaces); | 917 ast.implementsClause(toAnalyzerToken(implementsKeyword), interfaces); |
884 } | 918 } |
885 _MixinApplication mixinApplication = pop(); | 919 _MixinApplication mixinApplication = pop(); |
886 var superclass = mixinApplication.supertype; | 920 var superclass = mixinApplication.supertype; |
887 var withClause = ast.withClause( | 921 var withClause = ast.withClause( |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1159 /// [ClassDeclaration] or [ClassTypeAlias] object. | 1193 /// [ClassDeclaration] or [ClassTypeAlias] object. |
1160 class _MixinApplication { | 1194 class _MixinApplication { |
1161 final TypeName supertype; | 1195 final TypeName supertype; |
1162 | 1196 |
1163 final Token withKeyword; | 1197 final Token withKeyword; |
1164 | 1198 |
1165 final List<TypeName> mixinTypes; | 1199 final List<TypeName> mixinTypes; |
1166 | 1200 |
1167 _MixinApplication(this.supertype, this.withKeyword, this.mixinTypes); | 1201 _MixinApplication(this.supertype, this.withKeyword, this.mixinTypes); |
1168 } | 1202 } |
OLD | NEW |