-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathabstract.js
302 lines (263 loc) · 6.79 KB
/
abstract.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
'use strict';
const invariant = require('invariant');
const NULL = 'null';
const If = function(node) {
invariant(this.K_IF, 'Encountered If without K_IF');
invariant(this.K_END_IF, 'Encountered If without K_END_IF');
const parts = [
this.C_OPEN, this.WS,
this.K_IF, this.WS,
this.node(node.cond), this.WS,
this.C_CLOSE,
this.node(node.body)
];
if (node.else_) {
invariant(this.K_ELSE, 'Encountered If..Else without K_ELSE');
// TODO: produce elseif expressions, rather than nested if/else
parts.push(
this.C_OPEN, this.WS,
this.K_ELSE, this.WS,
this.C_CLOSE,
this.node(node.else_)
);
}
return parts.concat([
this.C_OPEN, this.WS,
this.K_END_IF, this.WS,
this.C_CLOSE
]).join('');
};
const For = function(node) {
invariant(this.K_FOR, 'Encountered For..In without K_FOR');
invariant(this.K_FOR_IN, 'Encountered For..In without K_FOR_IN');
invariant(this.K_END_FOR, 'Encountered For..In without K_END_FOR');
const parts = [
this.C_OPEN, this.WS,
this.K_FOR, this.WS,
this.node(node.name), this.WS,
this.K_FOR_IN, this.WS,
this.node(node.arr), this.WS,
this.C_CLOSE
];
// TODO: node.else_
return parts.concat([
this.node(node.body),
this.C_OPEN, this.WS,
this.K_END_FOR, this.WS,
this.C_CLOSE
]).join('');
};
const LookupVal = function(node) {
invariant(typeof this.quote === 'function',
'Encountered LookupVal without quote() method');
invariant(typeof this.accessor === 'function',
'Encountered LookupVal without accessor() method');
const stack = [node.val.value];
let target = node.target;
while (target.type === 'LookupVal') {
stack.unshift(target.val.value);
target = target.target;
}
const str = this.VAR_PREFIX
? this.VAR_PREFIX + target.value
: target.value;
return stack.reduce((out, symbol) => {
return out + this.accessor(symbol);
}, str);
};
const Literal = function(node) {
const value = node.value;
if (this.literalAliases && value in this.literalAliases) {
return this.literalAliases[value];
} else if (Array.isArray(this.literals)) {
if (this.literals.indexOf(value) === -1) {
throw new Error('Unsupported literal: "' + value + '"');
}
return value;
}
return this.quote(value, true);
};
const Output = function(node) {
return node.children.map(child => {
let out = this.node(child, node);
if (child.type === 'TemplateData') {
return out;
} else {
return [
this.O_OPEN, this.WS,
out, this.WS,
this.O_CLOSE
].join('');
}
}).join(this.WS || '');
};
const NodeList = function(node) {
return node.children.map(child => this.node(child)).join('');
};
const Not = function(node) {
return [
this.K_NOT,
this.WS,
this.node(node.target)
].join('');
};
const TemplateData = function(node) {
return node.value;
};
const Symbol = function(node) {
let value = node.value;
if (value === NULL && this.literalAliases) {
return this.literalAliases[value] || value;
}
return value;
};
const Block = function(node) {
return [
this.C_OPEN, this.WS,
this.K_BLOCK, this.WS,
this.node(node.name), this.WS,
this.C_CLOSE,
this.node(node.body),
this.C_OPEN, this.WS,
this.K_END_BLOCK, this.WS,
this.C_CLOSE,
].join('');
};
const Extends = function(node) {
return [
this.C_OPEN, this.WS,
this.K_EXTENDS, this.WS,
this.node(node.template), this.WS,
this.C_CLOSE
].join('');
};
const Include = function(node) {
return [
this.C_OPEN, this.WS,
this.K_INCLUDE, this.WS,
this.node(node.template), this.WS,
// TODO: support 'ignore missing'?
this.C_CLOSE
].join('');
};
const Compare = function(node) {
let type = node.ops[0].type;
let alias = this.operatorAliases ? this.operatorAliases[type] : null;
if (alias) {
type = alias;
}
return [
this.node(node.expr),
type,
this.node(node.ops[0].expr, node)
].join(' ');
};
const Operator = (symbol) => {
return function(node) {
return [
this.node(node.left),
symbol,
this.node(node.right)
].join(this.WS);
};
};
const Group = function(node) {
return '(' + node.children
.map(child => this.node(child))
.join(this.WS) + ')';
};
const Capture = function(node) {
invariant(Array.isArray(node.targets) || node.name,
'Capture is missing a name or targets: ' + Object.keys(node));
invariant(node.body && typeof node.body === 'object',
'Capure.body is not an Object');
const name = node.name || node.targets[0];
return [
this.C_OPEN, this.WS,
this.K_CAPTURE, this.WS,
this.node(name), this.WS,
this.C_CLOSE,
this.node(node.body),
this.C_OPEN, this.WS,
this.K_END_CAPTURE, this.WS,
this.C_CLOSE
].join('');
};
const quote = function(symbol, force) {
invariant(this.P_NUMERIC instanceof RegExp,
'quote() requires P_NUMERIC regexp');
invariant(this.P_WORD instanceof RegExp,
'quote() requires P_WORD regexp');
switch (typeof symbol) {
case 'boolean':
case 'number':
return symbol;
case 'string':
if (this.P_NUMERIC.test(symbol)) {
return symbol;
}
return (!force && this.P_WORD.test(symbol))
? symbol
: "'" + symbol.replace(/'/g, "\\'") + "'";
default:
throw new Error('Unexpected symbol type: ' + (typeof symbol));
}
};
const accessor = function(symbol) {
const str = this.quote(symbol)
return /^[0-9'"]/.test(str)
? '[' + str + ']'
: '.' + str;
};
module.exports = {
// output open delimiter
O_OPEN: '{{',
// output close delimiter
O_CLOSE: '}}',
// control structure open
C_OPEN: '{% ',
// control structure close
C_CLOSE: '%}',
// whitespace around control structures
C_WS: ' ',
// if keyword
K_IF: null,
// else keyword
K_ELSE: null,
// else if keyword
K_ELSE_IF: null,
// end if
K_END_IF: null,
// not operator
K_NOT: null,
// for/foreach/each keyword
K_FOR: null,
// for..in "in" keyword
K_FOR_IN: null,
// end for/foreach/each
K_END_FOR: null,
// regex matching numeric expressions
P_NUMERIC: /^\d+(\.\d+)?$/,
// regex matching bare words (not requiring quotes)
P_WORD: /^[a-z]\w*$/i,
// abstract word quoting helper
quote: quote,
accessor: accessor,
Block: Block,
Capture: Capture,
Compare: Compare,
Extends: Extends,
For: For,
Group: Group,
If: If,
Include: Include,
Literal: Literal,
LookupVal: LookupVal,
NodeList: NodeList,
Not: Not,
Operator: Operator,
Output: Output,
Root: NodeList,
Symbol: Symbol,
TemplateData: TemplateData,
};