@@ -5,6 +5,7 @@ import 'dart:collection';
5
5
import 'package:angular/core/parser/syntax.dart' as syntax;
6
6
import 'package:angular/core/parser/parser.dart' ;
7
7
import 'package:angular/core/formatter.dart' ;
8
+ import 'package:angular/core/annotation_src.dart' ;
8
9
import 'package:angular/change_detection/watch_group.dart' ;
9
10
import 'package:angular/change_detection/change_detection.dart' ;
10
11
import 'package:angular/core/parser/utils.dart' ;
@@ -18,48 +19,30 @@ class _FunctionChain {
18
19
}
19
20
}
20
21
21
- class AstParser {
22
+ @Injectable ()
23
+ class ASTParser {
22
24
final Parser _parser;
23
- int _id = 0 ;
24
- final ExpressionVisitor _visitor;
25
+ final ClosureMap _closureMap;
25
26
26
- AstParser (this ._parser, ClosureMap closureMap)
27
- : _visitor = new ExpressionVisitor (closureMap);
27
+ ASTParser (this ._parser, ClosureMap this ._closureMap);
28
28
29
29
AST call (String input, {FormatterMap formatters,
30
30
bool collection: false }) {
31
- _visitor.formatters = formatters;
32
- AST contextRef = _visitor.contextRef;
33
- try {
34
- var exp = _parser (input);
35
- return collection ? _visitor.visitCollection (exp) : _visitor.visit (exp);
36
- } finally {
37
- _visitor.contextRef = contextRef;
38
- _visitor.formatters = null ;
39
- }
31
+ var visitor = new _ExpressionVisitor (_closureMap, formatters);
32
+ var exp = _parser (input);
33
+ return collection ? visitor.visitCollection (exp) : visitor.visit (exp);
40
34
}
41
35
}
42
36
43
- class ExpressionVisitor implements syntax.Visitor {
37
+ class _ExpressionVisitor implements syntax.Visitor {
44
38
static final ContextReferenceAST scopeContextRef = new ContextReferenceAST ();
45
39
final ClosureMap _closureMap;
46
- AST contextRef = scopeContextRef;
47
-
40
+ final AST contextRef = scopeContextRef;
41
+ final FormatterMap _formatters;
48
42
49
- ExpressionVisitor (this ._closureMap);
43
+ _ExpressionVisitor (this ._closureMap, this ._formatters );
50
44
51
- AST ast;
52
- FormatterMap formatters;
53
-
54
- AST visit (syntax.Expression exp) {
55
- exp.accept (this );
56
- assert (ast != null );
57
- try {
58
- return ast;
59
- } finally {
60
- ast = null ;
61
- }
62
- }
45
+ AST visit (syntax.Expression exp) => exp.accept (this );
63
46
64
47
AST visitCollection (syntax.Expression exp) => new CollectionAST (visit (exp));
65
48
AST _mapToAst (syntax.Expression expression) => visit (expression);
@@ -76,71 +59,71 @@ class ExpressionVisitor implements syntax.Visitor {
76
59
return result;
77
60
}
78
61
79
- void visitCallScope (syntax.CallScope exp) {
62
+ AST visitCallScope (syntax.CallScope exp) {
80
63
List <AST > positionals = _toAst (exp.arguments.positionals);
81
64
Map <Symbol , AST > named = _toAstMap (exp.arguments.named);
82
- ast = new MethodAST (contextRef, exp.name, positionals, named);
65
+ return new MethodAST (contextRef, exp.name, positionals, named);
83
66
}
84
- void visitCallMember (syntax.CallMember exp) {
67
+ AST visitCallMember (syntax.CallMember exp) {
85
68
List <AST > positionals = _toAst (exp.arguments.positionals);
86
69
Map <Symbol , AST > named = _toAstMap (exp.arguments.named);
87
- ast = new MethodAST (visit (exp.object), exp.name, positionals, named);
70
+ return new MethodAST (visit (exp.object), exp.name, positionals, named);
88
71
}
89
- void visitAccessScope (syntax.AccessScope exp) {
90
- ast = new FieldReadAST (contextRef, exp.name);
72
+ AST visitAccessScope (syntax.AccessScope exp) {
73
+ return new FieldReadAST (contextRef, exp.name);
91
74
}
92
- void visitAccessMember (syntax.AccessMember exp) {
93
- ast = new FieldReadAST (visit (exp.object), exp.name);
75
+ AST visitAccessMember (syntax.AccessMember exp) {
76
+ return new FieldReadAST (visit (exp.object), exp.name);
94
77
}
95
- void visitBinary (syntax.Binary exp) {
96
- ast = new PureFunctionAST (exp.operation,
78
+ AST visitBinary (syntax.Binary exp) {
79
+ return new PureFunctionAST (exp.operation,
97
80
_operationToFunction (exp.operation),
98
81
[visit (exp.left), visit (exp.right)]);
99
82
}
100
- void visitPrefix (syntax.Prefix exp) {
101
- ast = new PureFunctionAST (exp.operation,
83
+ AST visitPrefix (syntax.Prefix exp) {
84
+ return new PureFunctionAST (exp.operation,
102
85
_operationToFunction (exp.operation),
103
86
[visit (exp.expression)]);
104
87
}
105
- void visitConditional (syntax.Conditional exp) {
106
- ast = new PureFunctionAST ('?:' , _operation_ternary,
88
+ AST visitConditional (syntax.Conditional exp) {
89
+ return new PureFunctionAST ('?:' , _operation_ternary,
107
90
[visit (exp.condition), visit (exp.yes),
108
91
visit (exp.no)]);
109
92
}
110
- void visitAccessKeyed (syntax.AccessKeyed exp) {
111
- ast = new ClosureAST ('[]' , _operation_bracket,
93
+ AST visitAccessKeyed (syntax.AccessKeyed exp) {
94
+ return new ClosureAST ('[]' , _operation_bracket,
112
95
[visit (exp.object), visit (exp.key)]);
113
96
}
114
- void visitLiteralPrimitive (syntax.LiteralPrimitive exp) {
115
- ast = new ConstantAST (exp.value);
97
+ AST visitLiteralPrimitive (syntax.LiteralPrimitive exp) {
98
+ return new ConstantAST (exp.value);
116
99
}
117
- void visitLiteralString (syntax.LiteralString exp) {
118
- ast = new ConstantAST (exp.value);
100
+ AST visitLiteralString (syntax.LiteralString exp) {
101
+ return new ConstantAST (exp.value);
119
102
}
120
- void visitLiteralArray (syntax.LiteralArray exp) {
103
+ AST visitLiteralArray (syntax.LiteralArray exp) {
121
104
List <AST > items = _toAst (exp.elements);
122
- ast = new PureFunctionAST ('[${items .join (', ' )}]' , new ArrayFn (), items);
105
+ return new PureFunctionAST ('[${items .join (', ' )}]' , new ArrayFn (), items);
123
106
}
124
107
125
- void visitLiteralObject (syntax.LiteralObject exp) {
108
+ AST visitLiteralObject (syntax.LiteralObject exp) {
126
109
List <String > keys = exp.keys;
127
110
List <AST > values = _toAst (exp.values);
128
111
assert (keys.length == values.length);
129
112
var kv = < String > [];
130
113
for (var i = 0 ; i < keys.length; i++ ) {
131
114
kv.add ('${keys [i ]}: ${values [i ]}' );
132
115
}
133
- ast = new PureFunctionAST ('{${kv .join (', ' )}}' , new MapFn (keys), values);
116
+ return new PureFunctionAST ('{${kv .join (', ' )}}' , new MapFn (keys), values);
134
117
}
135
118
136
- void visitFormatter (syntax.Formatter exp) {
137
- if (formatters == null ) {
119
+ AST visitFormatter (syntax.Formatter exp) {
120
+ if (_formatters == null ) {
138
121
throw new Exception ("No formatters have been registered" );
139
122
}
140
- Function formatterFunction = formatters (exp.name);
123
+ Function formatterFunction = _formatters (exp.name);
141
124
List <AST > args = [visitCollection (exp.expression)];
142
125
args.addAll (_toAst (exp.arguments).map ((ast) => new CollectionAST (ast)));
143
- ast = new PureFunctionAST ('|${exp .name }' ,
126
+ return new PureFunctionAST ('|${exp .name }' ,
144
127
new _FormatterWrapper (formatterFunction, args.length), args);
145
128
}
146
129
0 commit comments