| OLD | NEW |
| 1 library generator; | 1 library generator; |
| 2 | 2 |
| 3 import 'dart_code_gen.dart'; | 3 import 'package:angular/core/module.dart'; |
| 4 import '../../core/parser/parser_library.dart'; | 4 import 'package:angular/core/parser/parser.dart'; |
| 5 import 'source.dart'; | 5 import 'package:angular/tools/parser_generator/dart_code_gen.dart'; |
| 6 |
| 7 class NullFilterMap implements FilterMap { |
| 8 call(name) => null; |
| 9 Type operator[](annotation) => null; |
| 10 forEach(fn) { } |
| 11 annotationsFor(type) => null; |
| 12 } |
| 6 | 13 |
| 7 class SourcePrinter { | 14 class SourcePrinter { |
| 8 printSrc(src) { | 15 printSrc(src) { |
| 9 print(src); | 16 print(src); |
| 10 } | 17 } |
| 11 } | 18 } |
| 12 | 19 |
| 13 class ParserGenerator { | 20 class ParserGenerator { |
| 14 DynamicParser _parser; | 21 final Parser _parser; |
| 15 Map<String, bool> _printedFunctions = {}; | 22 final DartCodeGen _codegen; |
| 16 GetterSetterGenerator _getters; | 23 final SourcePrinter _printer; |
| 17 SourceBuilder _ = new SourceBuilder(); | 24 ParserGenerator(this._parser, this._codegen, this._printer); |
| 18 SourcePrinter _prt; | |
| 19 | 25 |
| 20 ParserGenerator(this._parser, this._getters, this._prt); | 26 void print(object) { |
| 27 _printer.printSrc('$object'); |
| 28 } |
| 21 | 29 |
| 22 generateParser(Iterable<String> expressions) { | 30 generateParser(Iterable<String> expressions) { |
| 23 _prt..printSrc("genEvalError(msg) { throw msg; }") | 31 print("StaticParserFunctions functions(FilterLookup filters)"); |
| 24 ..printSrc("functions(FilterLookup filters) => " | 32 print(" => new StaticParserFunctions("); |
| 25 "new StaticParserFunctions(buildExpressions(filters));") | 33 print(" buildEval(filters), buildAssign(filters));"); |
| 26 ..printSrc('var evalError = (text, [s]) => text;') | 34 print(""); |
| 27 ..printSrc(""); | |
| 28 BodySource body = new BodySource(); | |
| 29 MapSource map = new MapSource(); | |
| 30 | 35 |
| 31 // deterimine the order. | 36 // Compute the function maps. |
| 37 Map eval = {}; |
| 38 Map assign = {}; |
| 32 expressions.forEach((exp) { | 39 expressions.forEach((exp) { |
| 33 var code = safeCode(exp); | 40 generateCode(exp, eval, assign); |
| 34 map('${_.str(exp)}: ${_.ref(code)}'); | |
| 35 }); | 41 }); |
| 36 // now do it in actual order | 42 |
| 37 _.codeRefs.forEach((code) { | 43 // Generate the code. |
| 38 body(_.stmt('Expression ${_.ref(code)} = ', code.toSource(_))); | 44 generateBuildFunction('buildEval', eval); |
| 39 }); | 45 generateBuildFunction('buildAssign', assign); |
| 40 body(_.stmt('return ', map)); | 46 _codegen.getters.helpers.values.forEach(print); |
| 41 _prt..printSrc("Map<String, Expression> buildExpressions(FilterLookup filter
s) ${body}") | 47 _codegen.holders.helpers.values.forEach(print); |
| 42 ..printSrc("\n") | 48 _codegen.setters.helpers.values.forEach(print); |
| 43 ..printSrc(_getters.functions); | |
| 44 } | 49 } |
| 45 | 50 |
| 46 Code safeCode(String exp) { | 51 void generateBuildFunction(String name, Map map) { |
| 52 String mapLiteral = map.keys.map((e) => ' "$e": ${map[e]}').join(',\n'); |
| 53 print("Map<String, Function> $name(FilterLookup filters) {"); |
| 54 print(" return {\n$mapLiteral\n };"); |
| 55 print("}"); |
| 56 print(""); |
| 57 } |
| 58 |
| 59 void generateCode(String exp, Map eval, Map assign) { |
| 60 String escaped = escape(exp); |
| 47 try { | 61 try { |
| 48 return _parser(exp); | 62 Expression e = _parser(exp); |
| 63 if (e.isAssignable) assign[escaped] = getCode(e, true); |
| 64 eval[escaped] = getCode(e, false); |
| 49 } catch (e) { | 65 } catch (e) { |
| 50 if ("$e".contains('Parser Error') || | 66 if ("$e".contains('Parser Error') || |
| 51 "$e".contains('Lexer Error') || | 67 "$e".contains('Lexer Error') || |
| 52 "$e".contains('Unexpected end of expression')) { | 68 "$e".contains('Unexpected end of expression')) { |
| 53 return new ThrowCode("'${escape(e.toString())}';"); | 69 eval[escaped] = '"${escape(e.toString())}"'; |
| 54 } else { | 70 } else { |
| 55 rethrow; | 71 rethrow; |
| 56 } | 72 } |
| 57 } | 73 } |
| 58 } | 74 } |
| 59 | 75 |
| 76 String getCode(Expression e, bool assign) { |
| 77 String args = assign ? "scope, value" : "scope"; |
| 78 String code = _codegen.generate(e, assign); |
| 79 if (e.isChain) { |
| 80 code = code.replaceAll('\n', '\n '); |
| 81 return "($args) {\n $code\n }"; |
| 82 } else { |
| 83 return "($args) => $code"; |
| 84 } |
| 85 } |
| 60 } | 86 } |
| OLD | NEW |