1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | "use strict";
|
45 |
|
46 | function TreeTransformer(before, after) {
|
47 | TreeWalker.call(this);
|
48 | this.before = before;
|
49 | this.after = after;
|
50 | }
|
51 | TreeTransformer.prototype = new TreeWalker;
|
52 |
|
53 | (function(DEF) {
|
54 | function do_list(list, tw) {
|
55 | return MAP(list, function(node) {
|
56 | return node.transform(tw, true);
|
57 | });
|
58 | }
|
59 |
|
60 | DEF(AST_Node, noop);
|
61 | DEF(AST_LabeledStatement, function(self, tw) {
|
62 | self.label = self.label.transform(tw);
|
63 | self.body = self.body.transform(tw);
|
64 | });
|
65 | DEF(AST_SimpleStatement, function(self, tw) {
|
66 | self.body = self.body.transform(tw);
|
67 | });
|
68 | DEF(AST_Block, function(self, tw) {
|
69 | self.body = do_list(self.body, tw);
|
70 | });
|
71 | DEF(AST_Do, function(self, tw) {
|
72 | self.body = self.body.transform(tw);
|
73 | self.condition = self.condition.transform(tw);
|
74 | });
|
75 | DEF(AST_While, function(self, tw) {
|
76 | self.condition = self.condition.transform(tw);
|
77 | self.body = self.body.transform(tw);
|
78 | });
|
79 | DEF(AST_For, function(self, tw) {
|
80 | if (self.init) self.init = self.init.transform(tw);
|
81 | if (self.condition) self.condition = self.condition.transform(tw);
|
82 | if (self.step) self.step = self.step.transform(tw);
|
83 | self.body = self.body.transform(tw);
|
84 | });
|
85 | DEF(AST_ForIn, function(self, tw) {
|
86 | self.init = self.init.transform(tw);
|
87 | self.object = self.object.transform(tw);
|
88 | self.body = self.body.transform(tw);
|
89 | });
|
90 | DEF(AST_With, function(self, tw) {
|
91 | self.expression = self.expression.transform(tw);
|
92 | self.body = self.body.transform(tw);
|
93 | });
|
94 | DEF(AST_Exit, function(self, tw) {
|
95 | if (self.value) self.value = self.value.transform(tw);
|
96 | });
|
97 | DEF(AST_LoopControl, function(self, tw) {
|
98 | if (self.label) self.label = self.label.transform(tw);
|
99 | });
|
100 | DEF(AST_If, function(self, tw) {
|
101 | self.condition = self.condition.transform(tw);
|
102 | self.body = self.body.transform(tw);
|
103 | if (self.alternative) self.alternative = self.alternative.transform(tw);
|
104 | });
|
105 | DEF(AST_Switch, function(self, tw) {
|
106 | self.expression = self.expression.transform(tw);
|
107 | self.body = do_list(self.body, tw);
|
108 | });
|
109 | DEF(AST_Case, function(self, tw) {
|
110 | self.expression = self.expression.transform(tw);
|
111 | self.body = do_list(self.body, tw);
|
112 | });
|
113 | DEF(AST_Try, function(self, tw) {
|
114 | self.body = do_list(self.body, tw);
|
115 | if (self.bcatch) self.bcatch = self.bcatch.transform(tw);
|
116 | if (self.bfinally) self.bfinally = self.bfinally.transform(tw);
|
117 | });
|
118 | DEF(AST_Catch, function(self, tw) {
|
119 | self.argname = self.argname.transform(tw);
|
120 | self.body = do_list(self.body, tw);
|
121 | });
|
122 | DEF(AST_Definitions, function(self, tw) {
|
123 | self.definitions = do_list(self.definitions, tw);
|
124 | });
|
125 | DEF(AST_VarDef, function(self, tw) {
|
126 | self.name = self.name.transform(tw);
|
127 | if (self.value) self.value = self.value.transform(tw);
|
128 | });
|
129 | DEF(AST_Lambda, function(self, tw) {
|
130 | if (self.name) self.name = self.name.transform(tw);
|
131 | self.argnames = do_list(self.argnames, tw);
|
132 | self.body = do_list(self.body, tw);
|
133 | });
|
134 | DEF(AST_Call, function(self, tw) {
|
135 | self.expression = self.expression.transform(tw);
|
136 | self.args = do_list(self.args, tw);
|
137 | });
|
138 | DEF(AST_Sequence, function(self, tw) {
|
139 | self.expressions = do_list(self.expressions, tw);
|
140 | });
|
141 | DEF(AST_Dot, function(self, tw) {
|
142 | self.expression = self.expression.transform(tw);
|
143 | });
|
144 | DEF(AST_Sub, function(self, tw) {
|
145 | self.expression = self.expression.transform(tw);
|
146 | self.property = self.property.transform(tw);
|
147 | });
|
148 | DEF(AST_Unary, function(self, tw) {
|
149 | self.expression = self.expression.transform(tw);
|
150 | });
|
151 | DEF(AST_Binary, function(self, tw) {
|
152 | self.left = self.left.transform(tw);
|
153 | self.right = self.right.transform(tw);
|
154 | });
|
155 | DEF(AST_Conditional, function(self, tw) {
|
156 | self.condition = self.condition.transform(tw);
|
157 | self.consequent = self.consequent.transform(tw);
|
158 | self.alternative = self.alternative.transform(tw);
|
159 | });
|
160 | DEF(AST_Array, function(self, tw) {
|
161 | self.elements = do_list(self.elements, tw);
|
162 | });
|
163 | DEF(AST_Object, function(self, tw) {
|
164 | self.properties = do_list(self.properties, tw);
|
165 | });
|
166 | DEF(AST_ObjectProperty, function(self, tw) {
|
167 | self.value = self.value.transform(tw);
|
168 | });
|
169 | })(function(node, descend) {
|
170 | node.DEFMETHOD("transform", function(tw, in_list) {
|
171 | var x, y;
|
172 | tw.push(this);
|
173 | if (tw.before) x = tw.before(this, descend, in_list);
|
174 | if (typeof x === "undefined") {
|
175 | x = this;
|
176 | descend(x, tw);
|
177 | if (tw.after) {
|
178 | y = tw.after(x, in_list);
|
179 | if (typeof y !== "undefined") x = y;
|
180 | }
|
181 | }
|
182 | tw.pop();
|
183 | return x;
|
184 | });
|
185 | });
|