Skip to content

Commit 771f863

Browse files
committed
reflactor(parser): rename DynamicParser into Parser
Since there is only one implementation of the Parser interface, which is used for both dynamic and static deployments, calling it DynamicParser is misleading. - Remove the Parser interface - Rename rename DynamicParser into Parser - Rename DynamicParserBackend into RuntimeParserBackend Closes dart-archive#1251
1 parent 16e9622 commit 771f863

15 files changed

+162
-194
lines changed

bin/parser_generator_for_spec.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ main(arguments) {
1010
Module module = new Module()
1111
..bind(Lexer)
1212
..bind(ParserGetterSetter)
13-
..bind(Parser, toImplementation: DynamicParser)
13+
..bind(Parser)
1414
..install(new CacheModule());
1515
module.bind(ParserBackend, toImplementation: DartGetterSetterGen);
1616
Injector injector = new ModuleInjector([module]);

lib/application_factory.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import 'dart:html';
4040
'angular.routing',
4141
'angular.core.annotation_src',
4242
'angular.core.parser.Parser',
43-
'angular.core.parser.dynamic_parser',
43+
'angular.core.parser',
4444
'angular.core.parser.lexer',
4545
'angular.core_dynamic.DynamicMetadataExtractor',
4646
'perf_api',

lib/application_factory_static.dart

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ library angular.app.factory.static;
2626

2727
import 'package:angular/application.dart';
2828
import 'package:angular/core/registry.dart';
29-
import 'package:angular/core/parser/dynamic_parser.dart';
3029
import 'package:angular/core/parser/parser.dart';
3130
import 'package:angular/core/parser/static_closure_map.dart';
3231
import 'package:angular/core/registry_static.dart';

lib/core/module.dart

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ export "package:angular/change_detection/watch_group.dart" show
1313
ReactionFn;
1414

1515
export "package:angular/core/parser/parser.dart" show
16-
Parser;
17-
18-
export "package:angular/core/parser/dynamic_parser.dart" show
19-
ClosureMap;
16+
Parser, ClosureMap;
2017

2118
export "package:angular/change_detection/change_detection.dart" show
2219
AvgStopwatch,

lib/core/module_internal.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ class CoreModule extends Module {
4848
bind(ScopeStatsConfig);
4949
bind(Object, toValue: {}); // RootScope context
5050

51-
bind(Parser, toInstanceOf: DynamicParser);
52-
bind(ParserBackend, toInstanceOf: DynamicParserBackend);
53-
bind(DynamicParser);
54-
bind(DynamicParserBackend);
51+
bind(Parser);
52+
bind(RuntimeParserBackend);
53+
bind(ParserBackend, toInstanceOf: RuntimeParserBackend);
5554
bind(Lexer);
5655
bind(ASTParser);
5756
}

lib/core/parser/dynamic_parser.dart

-143
This file was deleted.

lib/core/parser/dynamic_parser_impl.dart renamed to lib/core/parser/parse_expression.dart

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
library angular.core.parser.dynamic_parser_impl;
1+
library angular.core.parser.parse_expression;
22

33
import 'package:angular/core/parser/parser.dart' show ParserBackend;
44
import 'package:angular/core/parser/lexer.dart';
55
import 'package:angular/core/parser/syntax.dart';
66
import 'package:angular/core/parser/characters.dart';
77
import 'package:angular/utils.dart' show isReservedWord;
88

9-
class DynamicParserImpl {
9+
Expression parseExpression(Lexer lexer, ParserBackend backend, String input) =>
10+
new _ParseExpression(lexer, backend, input).parseChain();
11+
12+
class _ParseExpression {
1013
final ParserBackend backend;
1114
final String input;
1215
final List<Token> tokens;
1316
int index = 0;
1417

15-
DynamicParserImpl(Lexer lexer, this.backend, String input)
18+
_ParseExpression(Lexer lexer, this.backend, String input)
1619
: this.input = input, tokens = lexer.call(input);
1720

1821
Token get next => peek(0);

lib/core/parser/parser.dart

+137-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
library angular.core.parser;
22

3+
import 'package:di/annotations.dart';
34
import 'package:angular/core/parser/syntax.dart'
4-
show CallArguments;
5+
show defaultFormatterMap, Expression, Visitor, CallArguments;
6+
import 'package:angular/core/parser/eval.dart';
7+
import 'package:angular/core/parser/utils.dart' show EvalError;
8+
import 'package:angular/cache/module.dart';
9+
import 'package:angular/core/annotation_src.dart' hide Formatter;
10+
import 'package:angular/core/module_internal.dart' show FormatterMap;
11+
import 'package:angular/core/parser/lexer.dart';
12+
import 'package:angular/core/parser/parse_expression.dart';
13+
import 'package:angular/utils.dart';
14+
515
export 'package:angular/core/parser/syntax.dart'
6-
show Visitor, Expression, BoundExpression, CallArguments;
7-
export 'package:angular/core/parser/dynamic_parser.dart'
8-
show DynamicParser, DynamicParserBackend, ClosureMap;
16+
show Visitor, Expression, BoundExpression, CallArguments;
917

1018
typedef LocalsWrapper(context, locals);
1119
typedef Getter(self);
@@ -14,9 +22,11 @@ typedef BoundGetter([locals]);
1422
typedef BoundSetter(value, [locals]);
1523
typedef MethodClosure(obj, List posArgs, Map namedArgs);
1624

17-
/// Placeholder for DI. The parser you are looking for is [DynamicParser].
18-
abstract class Parser<T> {
19-
T call(String input);
25+
abstract class ClosureMap {
26+
Getter lookupGetter(String name);
27+
Setter lookupSetter(String name);
28+
Symbol lookupSymbol(String name);
29+
MethodClosure lookupFunction(String name, CallArguments arguments);
2030
}
2131

2232
abstract class ParserBackend<T> {
@@ -67,3 +77,123 @@ abstract class ParserBackend<T> {
6777
T newLiteralNumber(num value) => newLiteralPrimitive(value);
6878
T newLiteralString(String value) => null;
6979
}
80+
81+
@Injectable()
82+
class Parser {
83+
final Lexer _lexer;
84+
final ParserBackend _backend;
85+
final Map<String, Expression> _cache = {};
86+
Parser(this._lexer, this._backend, CacheRegister cacheRegister) {
87+
cacheRegister.registerCache("Parser", _cache);
88+
}
89+
90+
Expression call(String input) {
91+
if (input == null) input = '';
92+
return _cache.putIfAbsent(input, () => _parse(input));
93+
}
94+
95+
Expression _parse(String input) {
96+
Expression expression = parseExpression(_lexer, _backend, input);
97+
return new _UnwrapExceptionDecorator(expression);
98+
}
99+
}
100+
101+
class _UnwrapExceptionDecorator extends Expression {
102+
final Expression _expression;
103+
_UnwrapExceptionDecorator (this._expression);
104+
105+
bool get isAssignable => _expression.isAssignable;
106+
bool get isChain => _expression.isChain;
107+
108+
accept(Visitor visitor) => _expression.accept(visitor);
109+
toString() => _expression.toString();
110+
111+
eval(scope, [FormatterMap formatters = defaultFormatterMap]) {
112+
try {
113+
return _expression.eval(scope, formatters);
114+
} on EvalError catch (e, s) {
115+
throw e.unwrap("$this", s);
116+
}
117+
}
118+
119+
assign(scope, value) {
120+
try {
121+
return _expression.assign(scope, value);
122+
} on EvalError catch (e, s) {
123+
throw e.unwrap("$this", s);
124+
}
125+
}
126+
}
127+
128+
@Injectable()
129+
class RuntimeParserBackend extends ParserBackend {
130+
final ClosureMap _closures;
131+
RuntimeParserBackend(this._closures);
132+
133+
bool isAssignable(Expression expression) => expression.isAssignable;
134+
135+
Expression newFormatter(expression, name, arguments) {
136+
List allArguments = new List(arguments.length + 1);
137+
allArguments[0] = expression;
138+
allArguments.setAll(1, arguments);
139+
return new Formatter(expression, name, arguments, allArguments);
140+
}
141+
142+
Expression newChain(expressions) => new Chain(expressions);
143+
Expression newAssign(target, value) => new Assign(target, value);
144+
Expression newConditional(condition, yes, no) =>
145+
new Conditional(condition, yes, no);
146+
147+
Expression newAccessKeyed(object, key) => new AccessKeyed(object, key);
148+
Expression newCallFunction(function, arguments) =>
149+
new CallFunction(function, _closures, arguments);
150+
151+
Expression newPrefixNot(expression) => new PrefixNot(expression);
152+
153+
Expression newBinary(operation, left, right) =>
154+
new Binary(operation, left, right);
155+
156+
Expression newLiteralPrimitive(value) => new LiteralPrimitive(value);
157+
Expression newLiteralArray(elements) => new LiteralArray(elements);
158+
Expression newLiteralObject(keys, values) => new LiteralObject(keys, values);
159+
Expression newLiteralString(value) => new LiteralString(value);
160+
161+
Expression newAccessScope(name) {
162+
Getter getter;
163+
Setter setter;
164+
if (name == 'this') {
165+
getter = (o) => o;
166+
} else {
167+
_assertNotReserved(name);
168+
getter = _closures.lookupGetter(name);
169+
setter = _closures.lookupSetter(name);
170+
}
171+
return new AccessScopeFast(name, getter, setter);
172+
}
173+
174+
Expression newAccessMember(object, name) {
175+
_assertNotReserved(name);
176+
Getter getter = _closures.lookupGetter(name);
177+
Setter setter = _closures.lookupSetter(name);
178+
return new AccessMemberFast(object, name, getter, setter);
179+
}
180+
181+
Expression newCallScope(name, arguments) {
182+
_assertNotReserved(name);
183+
MethodClosure function = _closures.lookupFunction(name, arguments);
184+
return new CallScope(name, function, arguments);
185+
}
186+
187+
Expression newCallMember(object, name, arguments) {
188+
_assertNotReserved(name);
189+
MethodClosure function = _closures.lookupFunction(name, arguments);
190+
return new CallMember(object, function, name, arguments);
191+
}
192+
193+
_assertNotReserved(name) {
194+
if (isReservedWord(name)) {
195+
throw "Identifier '$name' is a reserved word.";
196+
}
197+
}
198+
}
199+

lib/core/parser/static_closure_map.dart

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ library angular.core.static_closure_map;
22

33
import 'package:angular/core/parser/parser.dart';
44

5-
65
class StaticClosureMap extends ClosureMap {
76
final Map<String, Getter> getters;
87
final Map<String, Setter> setters;

0 commit comments

Comments
 (0)