Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(parser): Add new AST based parser
Browse files Browse the repository at this point in the history
The evaluation of the AST nodes is deliberately left out of this
change to keep it as simple as possible, but if you're interested
I do have a full implementation of it.

It probably makes sense to rewrite the static parser generator to
use the new AST visiting support -- so I'll probably do that as
the next step to start cutting down on the dependencies on the old
parser.

Once the new lexer lands there are a number of ways to improve
parsing performance through changes to the token representation.
  • Loading branch information
Kasper Lund authored and mhevery committed Jan 8, 2014
1 parent c893961 commit f2651d4
Show file tree
Hide file tree
Showing 42 changed files with 2,032 additions and 1,417 deletions.
32 changes: 26 additions & 6 deletions bin/parser_generator_for_spec.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import 'dart:io';
import 'package:di/di.dart';
import 'package:di/dynamic_injector.dart';
import 'package:angular/core/parser/parser_library.dart';
import 'package:angular/tools/parser_generator/dart_code_gen.dart';
import 'package:angular/core/module.dart';
import 'package:angular/core/parser/parser.dart';
import 'package:angular/tools/parser_generator/generator.dart';
import 'package:angular/tools/parser_getter_setter/generator.dart';

class NullFilterMap implements FilterMap {
call(name) => null;
Type operator[](annotation) => null;
forEach(fn) { }
annotationsFor(type) => null;
}

main(arguments) {
var isGetter = !arguments.isEmpty;

Module module = new Module()
..type(ParserBackend, implementedBy: isGetter ? DartGetterSetterGen : DartCodeGen);

Module module = new Module()..type(Parser, implementedBy: DynamicParser);
if (isGetter) {
module.type(ParserBackend, implementedBy: DartGetterSetterGen);
} else {
module.type(ParserBackend, implementedBy: DynamicParserBackend);
module.type(FilterMap, implementedBy: NullFilterMap);
}
Injector injector = new DynamicInjector(modules: [module],
allowImplicitInjection: true);

Expand All @@ -23,12 +34,21 @@ main(arguments) {
"const",
"null",
"[1, 2].length",

"doesNotExist",
"doesNotExist()",
"doesNotExist(1)",
"doesNotExist(1, 2)",
"a.doesNotExist()",
"a.doesNotExist(1)",
"a.doesNotExist(1, 2)",

"a.b.c",
"x.b.c",
"e1.b",
"o.f()",
"1", "-1", "+1",
"true?1",
"!true",
"3*4/2%5", "3+6-2",
"2<3", "2>3", "2<=2", "2>=2",
Expand Down Expand Up @@ -77,7 +97,6 @@ main(arguments) {
'a[x()]()',
'boo',
'[].count(',
'doesNotExist()',
'false',
'false && run()',
'!false || true',
Expand Down Expand Up @@ -137,6 +156,7 @@ main(arguments) {
'6[3]=2',

'map.dot = 7',
'map.null',
'exists(doesNotExist())',
'doesNotExists(exists())',
'a[0]()',
Expand Down
3 changes: 2 additions & 1 deletion lib/angular.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import 'package:angular/routing/module.dart';
export 'package:di/di.dart';
export 'package:angular/core/module.dart';
export 'package:angular/core_dom/module.dart';
export 'package:angular/core/parser/parser_library.dart';
export 'package:angular/core/parser/parser.dart';
export 'package:angular/core/parser/lexer.dart';
export 'package:angular/directive/module.dart';
export 'package:angular/filter/module.dart';
export 'package:angular/routing/module.dart';
Expand Down
7 changes: 4 additions & 3 deletions lib/core/interpolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int _endSymbolLength = _endSymbol.length;
class Interpolation {
final String template;
final List<String> seperators;
final List<ParsedGetter> watchExpressions;
final List<Getter> watchExpressions;
Function setter = (_) => _;

Interpolation(this.template, this.seperators, this.watchExpressions);
Expand Down Expand Up @@ -58,14 +58,15 @@ class Interpolate {
bool shouldAddSeparator = true;
String exp;
List<String> separators = [];
List<ParsedGetter> watchExpressions = [];
List<Getter> watchExpressions = [];

while(index < length) {
if ( ((startIndex = template.indexOf(_startSymbol, index)) != -1) &&
((endIndex = template.indexOf(_endSymbol, startIndex + _startSymbolLength)) != -1) ) {
separators.add(template.substring(index, startIndex));
exp = template.substring(startIndex + _startSymbolLength, endIndex);
watchExpressions.add((_parse(exp)..exp = exp).eval);
Expression expression = _parse(exp);
watchExpressions.add(expression.eval);
index = endIndex + _endSymbolLength;
hasInterpolation = true;
} else {
Expand Down
8 changes: 5 additions & 3 deletions lib/core/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import 'dart:mirrors';
import 'package:di/di.dart';
import 'package:perf_api/perf_api.dart';

import 'package:angular/core/parser/parser_library.dart';
import 'package:angular/core/parser/parser.dart';
import 'package:angular/core/parser/lexer.dart';
import 'package:angular/utils.dart';

import 'service.dart';
Expand Down Expand Up @@ -39,9 +40,10 @@ class NgCoreModule extends Module {
type(NgZone);

type(Parser, implementedBy: DynamicParser);
type(ParserBackend, implementedBy: DynamicParserBackend);
type(DynamicParser);
type(DynamicParserBackend);
type(Lexer);
type(ParserBackend);
type(GetterSetter);
type(ClosureMap);
}
}
Loading

0 comments on commit f2651d4

Please sign in to comment.