-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender2.js
335 lines (320 loc) · 12 KB
/
render2.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// # render.js
//
// Hacked-together conversion to HTML.
//
// Expr context is:
// ```
// <div class="e efirst"><span><span>....</span></span></div>
// ```
// The outer div is a 100% width block; the inner span is
// a shrink-to-fit inline-block, then innermost span is an inline-table.
// Each individual expr components should look like:
// ```
// <span class="expr ..."><span>....</span></span>
// ```
// The nested `<span>` allows positioning the text relative to the border.
// For a new continuation line in an expr, use:
// ```
// ...</span></div><div class="e econt"><span>...
// ```
// Usually the full markup will look something like:
// ```
// ...<span class="expr binop brokentop">+</span></span></div>
// <div class="e econt"><span><span class="expr binop brokenbot">
// <!--border image here w/ no text--></span>...
// ```
// For an embedded function, array, object, etc context in an expr, use:
// ```
// ...</span></div><div class="e enest ..."><span>...
// ```
// And at the end of the embedded context:
// ```
// ...</span></div><div class="e econt"><span>...
// ```
//
// Statements should be of the form:
// ```
// <div class="stmt ..."><span> ... </span></div>
// ```
// The outer div is a 100% width block; the inner span is
// a shrink-to-fit inline-block.
//
// The statement is responsible for its own closing semicolon.
// C. Scott Ananian
// 2010-07-02ish
var make_render = function(html_escape, str_escape) {
var render, render_stmt, render_stmts;
var indentation, prec_stack = [ 0 ];
var assert = function(b, obj) {
if (!b) { console.assert(b, "Assertion failure", obj); }
};
// helper function for delimiter-joined lists
var gather = function(lst, delim, f) {
var i = 0, result = [];
while ( i < lst.length ) {
result.push(f(lst[i]));
i += 1;
}
return result.join(delim);
};
// indentation level
var nl = function() {
var n = indentation, result = "\n";
while (n > 0) {
result += " ";
n -= 1;
}
return result;
};
// set the precedence to 'prec' when evaluating f
var with_prec = function(prec, f, obj) {
return function() {
var result;
prec_stack.push(prec);
result = f.apply(obj || this, arguments);
prec_stack.pop();
return result;
};
};
// set the precedence, and parenthesize the result if appropriate.
var with_prec_paren = function(prec, f, obj) {
return function() {
var prev_prec = prec_stack[prec_stack.length - 1];
var result = with_prec(prec, f).apply(obj || this, arguments);
if (prev_prec > prec) {
result = span("expr paren", "(" + result + ")");
}
return result;
};
};
var div = function(classes, text) {
if (!classes) return "<div>"+text+"</div>";
return "<div class='"+classes+"'>" + text + "</div>";
};
var span = function(classes, text) {
if (!classes) return "<span>"+text+"</span>";
return "<span class='"+classes+"'>" + text + "</span>";
};
var spanned = function(classes, f) {
return function() {
return span(classes, f.apply(this, arguments));
};
};
var dispatch = {};
dispatch.name = spanned("expr id", function() { return html_escape(this.value); });
dispatch.literal = spanned("expr lit", function() {
if (this.value === null) { return "null"; }
if (typeof(this.value)==='object') {
if (this.value.length === 0) { return "Array"; }
return "Object";
}
if (typeof(this.value)==='string') { return html_escape(str_escape(this.value)); }
return this.value.toString();
});
// UNARY ASTs
dispatch.unary = function() {
assert(dispatch.unary[this.value], this);
return dispatch.unary[this.value].apply(this);
};
var unary = function(op, prec, f) {
dispatch.unary[op] = f || with_prec_paren(prec, function() {
return span("expr unop", this.value) + render(this.first);
});
};
unary('!', 70);
unary('-', 70);
unary('typeof', 70, with_prec_paren(70, function() {
// XXX
return "typeof("+with_prec(0, render)(this.first)+")";
}));
unary('[', 90/*???*/, with_prec_paren(90, function() {
// new array creation
// XXX
return "[" + gather(this.first, ", ", with_prec(0, render)) +
"]";
}));
unary('{', 90/*???*/, with_prec_paren(90, function() {
// new object creation
var result = span("exprleft newobj", "{");
result += "</span>"+span("connect objconn","")+"</span></div>";
// XXX fix "one line" form.
if (this.first.length > 0) {
indentation += 1;
result += nl();
result += "<div class='e enest objnest'><span><span>";
var mid = span("exprend", ",");
mid += "</span></span></div>";
mid += "</span></span></div>" + nl();
mid += "<div class='e enest objnest'><span><span>";
mid += "<div class='e efirst'><span><span>";
result += "<div class='e efirst'><span><span>";
result += gather(this.first, mid, function(item) {
// XXX suppress quotes around item.key when
// unnecessary
return span("objkey", html_escape(str_escape(item.key)) + ": ") +
with_prec(0, render)(item);
});
result += span("exprend", ""); // cap the final element
indentation -= 1;
result += "</span></span></div>";
result += "</span></span></div>";
result += nl();
}
result += "<div class='e econt'><span><span>";
result +=span("exprright newobj", "}");
return result;
}));
// Binary ASTs
dispatch.binary = function() {
assert(dispatch.binary[this.value], this);
return dispatch.binary[this.value].apply(this);
};
var binary = function(op, prec, f) {
// with_prec_paren will add parentheses if necessary
dispatch.binary[op] = f || with_prec_paren(prec, function() {
var result = render(this.first)+span("binop",' '+this.value+' ');
// handle left associativity
result += with_prec(prec+1, render)(this.second);
return result;
});
};
binary('=', 10);
binary('+=', 10);
binary('-=', 10);
binary('||', 30);
binary('&&', 35);
binary('===',40);
binary('!==',40);
binary('<', 45);
binary('<=',45);
binary('>', 45);
binary('>=',45);
binary('+', 50);
binary('-', 50);
binary('*', 60);
binary('/', 60);
binary(".", 80, with_prec_paren(80, function() {
assert(this.second.arity==='literal', this.second);
return render(this.first)+span("binop", ".")+this.second.value;
}));
binary('[', 80, with_prec_paren(80, function() {
return render(this.first) + "[" +
with_prec(0, render)(this.second) + "]";
}));
binary('(', 80, with_prec_paren(80, function() {
// simple method invocation (doesn't set 'this')
return render(this.first) + "(" +
gather(this.second, ", ", with_prec(0, render)) + ")";
}));
// Ternary ASTs
dispatch.ternary = function() {
assert(dispatch.ternary[this.value], this);
return dispatch.ternary[this.value].apply(this);
};
var ternary = function(op, prec, f) {
dispatch.ternary[op] = with_prec_paren(prec, f);
};
ternary("?", 20, function() {
return render(this.first) + " ? " +
render(this.second) + " : " +
render(this.third);
});
ternary("(", 80, function() {
// precedence is 80, same as . and '(')
assert(this.second.arity==='literal', this.second);
return render(this.first) + "<span class='binop'>.</span>" + this.second.value + "(" +
gather(this.third, ", ", with_prec(0, render)) + ")";
});
// Statements
dispatch.statement = function() {
assert(dispatch.statement[this.value], this);
return dispatch.statement[this.value].apply(this);
};
var stmt = function(value, f) {
dispatch.statement[value] = f;
};
stmt("block", function() {
var result = "{";
if (this.first.length > 0) {
indentation += 1;
result += nl() + render_stmts(this.first);
indentation -= 1;
}
result += nl() + "}";
return result;
});
stmt("var", function() {
return div("stmt", span("", "var "+render(this.first)+";"));
});
stmt("if", function() {
var result = "if ("+render(this.first)+") ";
// this.second.value === block
result += render(this.second);
if (this.third) {
result += " else ";
result += render(this.third);
}
return result;
});
stmt("return", function() {
return div("stmt", span("", span("keyword","return")+(this.first ? (" "+render(this.first)) : "")+span("semi", ";")));
});
stmt("break", function() {
return div("stmt", span("", span("keyword", "break")+span("semi",";")));
});
stmt("while", function() {
return "while ("+render(this.first)+") "+render(this.second);
});
// Odd cases
dispatch['this'] = function() { return span("expr lit", "this"); }; // literal
dispatch['function'] = with_prec(0, function() {
var result = "function";
if (this.name) { result += " " + html_escape(this.name); }
result += " (" + gather(this.first, ", ", render) + ") {";
result = span("exprleft func", result);
result += "</span>"+span("connect funcconn","")+"</span></div>";
// XXX fix "one line" form.
if (this.second.length > 0) {
indentation += 1;
result += nl();
result += "<div class='e enest funcnest'><span><span>";
// XXX make function body context
result += render_stmts(this.second); // function body
indentation -= 1;
result += "</span></span></div>";
}
result += nl();
result += "<div class='e econt'><span><span>";
result +=span("exprright func", "}");
return result;
});
// Helpers
render = function(tree) {
// make 'this' the parse tree in the dispatched function.
assert(dispatch[tree.arity], tree);
return dispatch[tree.arity].apply(tree);
};
render_stmt = function(tree) {
if (tree.arity==='statement') {
return render(tree);
}
// an "expression statement"
result = "<div class='stmt'><span>";
// XXX some bridge to make a expr holder
result += "<div class='e efirst'><span><span>";
result += render(tree);
result += "<span class='exprend semi'>;</span>"; // cap off the end
result += "</span></span></div>"; // close the expression context
result += "</span></div>"; // close the statement
return result;
};
render_stmts = function(tree_list) {
return gather(tree_list, nl(), render_stmt);
};
return function (parse_tree) {
// parse_tree should be an array of statements.
indentation = 0;
prec_stack = [ 0 ];
return render_stmts(parse_tree);
};
};