| OLD | NEW |
| (Empty) | |
| 1 library generator; |
| 2 |
| 3 import 'package:angular/core/module.dart'; |
| 4 import 'package:angular/core/parser/parser.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 } |
| 13 |
| 14 class SourcePrinter { |
| 15 printSrc(src) { |
| 16 print(src); |
| 17 } |
| 18 } |
| 19 |
| 20 class ParserGenerator { |
| 21 final Parser _parser; |
| 22 final DartCodeGen _codegen; |
| 23 final SourcePrinter _printer; |
| 24 ParserGenerator(this._parser, this._codegen, this._printer); |
| 25 |
| 26 void print(object) { |
| 27 _printer.printSrc('$object'); |
| 28 } |
| 29 |
| 30 generateParser(Iterable<String> expressions) { |
| 31 print("StaticParserFunctions functions()"); |
| 32 print(" => new StaticParserFunctions("); |
| 33 print(" buildEval(), buildAssign());"); |
| 34 print(""); |
| 35 |
| 36 // Compute the function maps. |
| 37 Map eval = {}; |
| 38 Map assign = {}; |
| 39 expressions.forEach((exp) { |
| 40 generateCode(exp, eval, assign); |
| 41 }); |
| 42 |
| 43 // Generate the code. |
| 44 generateBuildFunction('buildEval', eval); |
| 45 generateBuildFunction('buildAssign', assign); |
| 46 _codegen.getters.helpers.values.forEach(print); |
| 47 _codegen.holders.helpers.values.forEach(print); |
| 48 _codegen.setters.helpers.values.forEach(print); |
| 49 } |
| 50 |
| 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() {"); |
| 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); |
| 61 try { |
| 62 Expression e = _parser(exp); |
| 63 if (e.isAssignable) assign[escaped] = getCode(e, true); |
| 64 eval[escaped] = getCode(e, false); |
| 65 } catch (e) { |
| 66 if ("$e".contains('Parser Error') || |
| 67 "$e".contains('Lexer Error') || |
| 68 "$e".contains('Unexpected end of expression')) { |
| 69 eval[escaped] = '"${escape(e.toString())}"'; |
| 70 } else { |
| 71 rethrow; |
| 72 } |
| 73 } |
| 74 } |
| 75 |
| 76 String getCode(Expression e, bool assign) { |
| 77 String args = assign ? "scope, value" : "scope, filters"; |
| 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 } |
| 86 } |
| OLD | NEW |