| OLD | NEW |
| (Empty) | |
| 1 library dart_code_gen_source; |
| 2 |
| 3 import 'dart_code_gen.dart'; |
| 4 |
| 5 class SourceBuilder { |
| 6 static RegExp NON_WORDS = new RegExp(r'\W'); |
| 7 |
| 8 Map<String, Code> refs = {}; |
| 9 List<Code> codeRefs = []; |
| 10 |
| 11 String str(String s) => '\'' + |
| 12 s.replaceAll('\'', '\\\'') |
| 13 .replaceAll('\n', '\\n') |
| 14 .replaceAll(r'$', r'\$') + '\''; |
| 15 String ident(String s) => '_${s.replaceAll(NON_WORDS, '_')}_${s.hashCode}'; |
| 16 |
| 17 String ref(Code code) { |
| 18 if (!refs.containsKey(code.id)) { |
| 19 refs[code.id] = code; |
| 20 code.toSource(this); // recursively expand; |
| 21 codeRefs.add(code); |
| 22 } |
| 23 return this.ident(code.id); |
| 24 } |
| 25 |
| 26 parens([p1, p2, p3, p4, p5, p6]) => new ParenthesisSource()..call(p1, p2, p3,
p4, p5, p6); |
| 27 body([p1, p2, p3, p4, p5, p6]) => new BodySource()..call(p1, p2, p3, p4, p5, p
6); |
| 28 stmt([p1, p2, p3, p4, p5, p6]) => new StatementSource()..call(p1, p2, p3, p4,
p5, p6); |
| 29 |
| 30 call([p1, p2, p3, p4, p5, p6]) => new Source()..call(p1, p2, p3, p4, p5, p6); |
| 31 |
| 32 } |
| 33 |
| 34 class Source { |
| 35 static String NEW_LINE = '\n'; |
| 36 List source = []; |
| 37 |
| 38 call([p1, p2, p3, p4, p5, p6]) { |
| 39 if (p1 != null) source.add(p1); |
| 40 if (p2 != null) source.add(p2); |
| 41 if (p3 != null) source.add(p3); |
| 42 if (p4 != null) source.add(p4); |
| 43 if (p5 != null) source.add(p5); |
| 44 if (p6 != null) source.add(p6); |
| 45 } |
| 46 |
| 47 toString([String indent='', newLine=false, sep='']) { |
| 48 var lines = []; |
| 49 var trailing = sep == ';'; |
| 50 var _sep = ''; |
| 51 source.forEach((s) { |
| 52 if (!trailing) lines.add(_sep); |
| 53 if (newLine) lines.add('\n' + indent); |
| 54 if (s is Source) { |
| 55 lines.add(s.toString(indent)); |
| 56 } else { |
| 57 lines.add(s); |
| 58 } |
| 59 _sep = sep; |
| 60 if (trailing) lines.add(_sep); |
| 61 }); |
| 62 return lines.join(''); |
| 63 } |
| 64 } |
| 65 |
| 66 |
| 67 class ParenthesisSource extends Source { |
| 68 toString([String indent='', newLine=false, sep='']) { |
| 69 return '(' + super.toString(indent + ' ', true, ',') + ')'; |
| 70 } |
| 71 } |
| 72 |
| 73 class MapSource extends Source { |
| 74 toString([String indent='', newLine=false, sep='']) { |
| 75 return '{' + super.toString(indent + ' ', true, ',') + '}'; |
| 76 } |
| 77 } |
| 78 |
| 79 class BodySource extends Source { |
| 80 BodySource() { |
| 81 //this(''); |
| 82 } |
| 83 toString([String indent='', newLine=false, sep='']) { |
| 84 return '{${super.toString(indent + ' ', true)}\n$indent}'; |
| 85 } |
| 86 } |
| 87 |
| 88 class StatementSource extends Source { |
| 89 toString([String indent='', newLine=false, sep='']) { |
| 90 return '${super.toString(indent + ' ')};'; |
| 91 } |
| 92 } |
| OLD | NEW |