-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.js
270 lines (236 loc) · 7.96 KB
/
compiler.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
var Expression = require('./parser/expression');
var Mustache = require('./parser/mustache');
var Horseshoe = exports;
Horseshoe.template = Horseshoe.Template;
// Setup regex assignments
// remove whitespace according to Mustache spec
var rQuot = /\"/g,
rNewline = /\n/g,
rCr = /\r/g,
rSlash = /\\/g;
function esc(s) {
return s.replace(rSlash, '\\\\')
.replace(rQuot, '\\\"')
.replace(rNewline, '\\n')
.replace(rCr, '\\r');
}
function chooseMethod(s) {
return (~s.indexOf('.')) ? 'd' : 'f';
}
Expression.codegen = function (node, context) {
var types = Expression.types;
switch(node.t) {
case types.NUMBER_LITERAL:
context.code += node.v;
break;
case types.STRING_LITERAL:
context.code += '"' + esc(node.v) + '"';
break;
case types.ARRAY_LITERAL:
Expression.codegen(node.m, context);
break;
case types.OBJECT_LITERAL:
Expression.codegen(node.m, context);
break;
case types.BOOLEAN_LITERAL:
context.code += node.v;
break;
case types.GLOBAL:
context.code += node.v;
break;
case types.KEY_VALUE_PAIR:
context.code += node.k + ':';
Expression.codegen(node.v, context);
break;
case types.REFERENCE:
context.code += 't.' +
chooseMethod(node.n) +
'("' + node.n + '",c,p,0)';
break;
case types.REFINEMENT:
if (node.n) {
context.code += '.' + node.n;
} else {
context.code += '[';
Expression.codegen(node.x, context);
context.code += ']';
}
break;
case types.MEMBER:
Expression.codegen(node.x, context);
Expression.codegen(node.r, context);
break;
case types.PREFIX_OPERATOR:
context.code += node.s + ' ';
Expression.codegen(node.o, context);
break;
case types.BRACKETED:
context.code += '(';
Expression.codegen(node.x, context);
context.code += ')';
break;
case types.CONDITIONAL:
Expression.codegen(node.o[0], context);
context.code += '?';
Expression.codegen(node.o[1], context);
context.code += ':';
Expression.codegen(node.o[2], context);
break;
case types.INFIX_OPERATOR:
Expression.codegen(node.o[0], context);
context.code += node.s;
Expression.codegen(node.o[1], context);
break;
case types.INVOCATION:
Expression.codegen(node.x, context);
context.code += '(';
if (node.o.length) {
Expression.codegen(node.o[0], context);
}
for (var i = 1; i < node.o.length; i++) {
context.code += ',';
Expression.codegen(node.o[i], context);
}
context.code += ')';
break;
}
};
Expression.walk = function(node, context) {
Expression.codegen(node.ast, context);
};
function createPartial(node, context) {
var prefix = "<" + (context.prefix || "");
var sym = prefix + node.n + serialNo++;
context.partials[sym] = {name: node.n, partials: {}};
context.code += 't.b(t.rp("' + esc(sym) + '",c,p,"' + (node.indent || '') + '"));';
return sym;
}
function tripleStache(node, context) {
context.code += 't.b(t.t(';
Expression.walk(node, context);
context.code += '));';
}
function write(s) {
return 't.b(' + s + ');';
}
Mustache.codegen = {
'#': function (node, context) {
context.code += 'if(t.s(';
Expression.walk(node, context);
context.code += ',c,p,0,' + node.i + ',' + node.end + ',"' + node.otag + " " + node.ctag + '")){' +
't.rs(c,p,' + 'function(c,p,t){';
Mustache.walk(node.nodes, context);
context.code += '}';
if (node.ref) {
context.code += ',"' + node.ref + '"';
}
context.code += ');c.pop();}';
},
'^': function (node, context) {
context.code += 'if(!t.s(';
Expression.walk(node, context);
context.code += ',c,p,1,0,0,"")){';
Mustache.walk(node.nodes, context);
context.code += '};';
},
'>': createPartial,
'<': function (node, context) {
var ctx = {partials: {}, code: '', subs: {}, inPartial: true};
Mustache.walk(node.nodes, ctx);
var template = context.partials[createPartial(node, context)];
template.subs = ctx.subs;
template.partials = ctx.partials;
},
'@': function (node, context) {
var ctx = {subs: {}, code: '', partials: context.partials, prefix: node.n};
Mustache.walk(node.nodes, ctx);
context.subs[node.n] = ctx.code;
if (!context.inPartial) {
context.code += 't.sub("' + esc(node.n) + '",c,p,i);';
}
},
'\n': function (node, context) {
context.code += write('"\\n"' + (node.last ? '' : ' + i'));
},
'_v': function (node, context) {
context.code += 't.b(t.v(';
Expression.walk(node, context);
context.code += '));';
},
'_t': function (node, context) {
context.code += write('"' + esc(node.text) + '"');
},
'{': tripleStache,
'&': tripleStache
};
Mustache.walk = function (nodelist, context) {
var func;
for (var i = 0, l = nodelist.length; i < l; i++) {
func = Mustache.codegen[nodelist[i].tag];
func && func(nodelist[i], context);
}
return context;
};
function wrapMain(code) {
return 'var t=this;t.b(i=i||"");' + code + 'return t.fl();';
}
function makePartials(codeObj) {
var key, template = {subs: {}, partials: codeObj.partials, name: codeObj.name};
for (key in template.partials) {
template.partials[key] = makePartials(template.partials[key]);
}
for (key in codeObj.subs) {
template.subs[key] = new Function('c', 'p', 't', 'i', codeObj.subs[key]);
}
return template;
}
function makeTemplate(codeObj, text, options) {
var template = makePartials(codeObj);
template.code = new Function('c', 'p', 'i', wrapMain(codeObj.code));
return new Horseshoe.template(template, text, Horseshoe, options);
}
function stringifySubstitutions(obj) {
var items = [];
for (var key in obj) {
items.push('"' + esc(key) + '": function(c,p,t,i) {' + obj[key] + '}');
}
return "{ " + items.join(",") + " }";
}
function stringifyPartials(codeObj) {
var partials = [];
for (var key in codeObj.partials) {
partials.push('"' + esc(key) + '":{name:"' + esc(codeObj.partials[key].name) + '", ' + stringifyPartials(codeObj.partials[key]) + "}");
}
return "partials: {" + partials.join(",") + "}, subs: " + stringifySubstitutions(codeObj.subs);
}
function stringify(codeObj, text, options) {
return "{code: function (c,p,i) { " + wrapMain(codeObj.code) + " }," + stringifyPartials(codeObj) + "}";
}
var serialNo = 0;
function generate(tree, text, options) {
serialNo = 0;
var context = { code: '', subs: {}, partials: {} };
Mustache.walk(tree, context);
if (options.asString) {
return stringify(context, text, options);
}
return makeTemplate(context, text, options);
}
Horseshoe.cache = {};
Horseshoe.cacheKey = function (text, options) {
return [text, !!options.asString, options.delimiters, !!options.modelGet].join('||');
};
Horseshoe.compile = function (text, options) {
options = options || {};
var key = Horseshoe.cacheKey(text, options);
var template = this.cache[key];
if (template) {
var partials = template.partials;
for (var name in partials) {
delete partials[name].instance;
}
return template;
}
template = generate(Mustache.parse(text, options), text, options);
return this.cache[key] = template;
};