OLD | NEW |
| (Empty) |
1 library angular.core.parser.unparser; | |
2 | |
3 import 'package:angular/core/parser/syntax.dart'; | |
4 | |
5 class Unparser extends Visitor { | |
6 final StringBuffer buffer; | |
7 Unparser(this.buffer); | |
8 | |
9 static String unparse(Expression expression) { | |
10 StringBuffer buffer = new StringBuffer(); | |
11 Unparser unparser = new Unparser(buffer); | |
12 unparser.visit(expression); | |
13 return "$buffer"; | |
14 } | |
15 | |
16 write(String string) { | |
17 buffer.write(string); | |
18 } | |
19 | |
20 writeArguments(List<Expression> arguments) { | |
21 write('('); | |
22 for (int i = 0; i < arguments.length; i++) { | |
23 if (i != 0) write(','); | |
24 visit(arguments[i]); | |
25 } | |
26 write(')'); | |
27 } | |
28 | |
29 visitChain(Chain chain) { | |
30 for (int i = 0; i < chain.expressions.length; i++) { | |
31 if (i != 0) write(';'); | |
32 visit(chain.expressions[i]); | |
33 } | |
34 } | |
35 | |
36 visitFilter(Filter filter) { | |
37 write('('); | |
38 visit(filter.expression); | |
39 write('|${filter.name}'); | |
40 for (int i = 0; i < filter.arguments.length; i++) { | |
41 write(' :'); | |
42 visit(filter.arguments[i]); | |
43 } | |
44 write(')'); | |
45 } | |
46 | |
47 visitAssign(Assign assign) { | |
48 visit(assign.target); | |
49 write('='); | |
50 visit(assign.value); | |
51 } | |
52 | |
53 visitConditional(Conditional conditional) { | |
54 visit(conditional.condition); | |
55 write('?'); | |
56 visit(conditional.yes); | |
57 write(':'); | |
58 visit(conditional.no); | |
59 } | |
60 | |
61 visitAccessScope(AccessScope access) { | |
62 write(access.name); | |
63 } | |
64 | |
65 visitAccessMember(AccessMember access) { | |
66 visit(access.object); | |
67 write('.${access.name}'); | |
68 } | |
69 | |
70 visitAccessKeyed(AccessKeyed access) { | |
71 visit(access.object); | |
72 write('['); | |
73 visit(access.key); | |
74 write(']'); | |
75 } | |
76 | |
77 visitCallScope(CallScope call) { | |
78 write(call.name); | |
79 writeArguments(call.arguments); | |
80 } | |
81 | |
82 visitCallFunction(CallFunction call) { | |
83 visit(call.function); | |
84 writeArguments(call.arguments); | |
85 } | |
86 | |
87 visitCallMember(CallMember call) { | |
88 visit(call.object); | |
89 write('.${call.name}'); | |
90 writeArguments(call.arguments); | |
91 } | |
92 | |
93 visitPrefix(Prefix prefix) { | |
94 write('(${prefix.operation}'); | |
95 visit(prefix.expression); | |
96 write(')'); | |
97 } | |
98 | |
99 visitBinary(Binary binary) { | |
100 write('('); | |
101 visit(binary.left); | |
102 write(binary.operation); | |
103 visit(binary.right); | |
104 write(')'); | |
105 } | |
106 | |
107 visitLiteralPrimitive(LiteralPrimitive literal) { | |
108 write("${literal.value}"); | |
109 } | |
110 | |
111 visitLiteralArray(LiteralArray literal) { | |
112 write('['); | |
113 for (int i = 0; i < literal.elements.length; i++) { | |
114 if (i != 0) write(','); | |
115 visit(literal.elements[i]); | |
116 } | |
117 write(']'); | |
118 } | |
119 | |
120 visitLiteralObject(LiteralObject literal) { | |
121 write('{'); | |
122 List<String> keys = literal.keys; | |
123 for (int i = 0; i < keys.length; i++) { | |
124 if (i != 0) write(','); | |
125 write("'${keys[i]}':"); | |
126 visit(literal.values[i]); | |
127 } | |
128 write('}'); | |
129 } | |
130 | |
131 visitLiteralString(LiteralString literal) { | |
132 String escaped = literal.value.replaceAll("'", "\\'"); | |
133 write("'$escaped'"); | |
134 } | |
135 } | |
OLD | NEW |