-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.js
657 lines (527 loc) · 16.6 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
"use strict";
const CodeBlock = require("./internal/code-block");
const voidTags = require("./internal/void-tags");
const escapes = require("./escapes");
const Markup = require("./markup");
const POSSIBLE_COMMENT = /\/\/|<!--/;
const IDENTIFIER_ESCAPE = /\\u([\dA-Fa-f]{4})/g;
// Simplified because ES6 uses the ID_Start and ID_Continue Unicode properties
const SIMPLE_IDENTIFIER = /^[a-zA-Z_$][\w_$]*$/;
const RESERVED_WORDS = new Set([
// Keyword
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"export",
"extends",
"finally",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"return",
"super",
"switch",
"this",
"throw",
"try",
"typeof",
"var",
"void",
"while",
"with",
"yield",
// FutureReservedWord
"enum",
// Strict mode FutureReservedWord
"implements",
"interface",
"package",
"private",
"protected",
"public",
// NullLiteral
"null",
// BooleanLiteral
"true",
"false",
// Additional forbidden `Identifier`s in strict mode
"let",
// Additional forbidden `BindingIdentifier`s in strict mode
"arguments",
"eval",
]);
const parseIdentifierEscape = (match, identifierEscape) =>
String.fromCharCode(parseInt(identifierEscape, 16));
const isIdentifier = text => {
const unescaped = text.replace(IDENTIFIER_ESCAPE, parseIdentifierEscape);
return (
SIMPLE_IDENTIFIER.test(unescaped) &&
!RESERVED_WORDS.has(unescaped)
);
};
const wrapExpression = expression =>
POSSIBLE_COMMENT.test(expression) ? expression + "\n" : expression;
const count = (text, c) => {
let result = 0;
let i = -1;
while ((i = text.indexOf(c, i + 1)) !== -1) {
result++;
}
return result;
};
const shortestAttributeRepresentation = attributeValue => {
if (typeof attributeValue !== "string") {
throw new TypeError("Attribute value must be a string");
}
if (attributeValue === "") {
return "";
}
attributeValue = attributeValue.replace(/&(?=#|[0-9A-Za-z]+;)/g, "&");
if (!/[\t\n\f\r "'=<>`]/.test(attributeValue)) {
return "=" + attributeValue;
}
return count(attributeValue, '"') > count(attributeValue, '"') ?
"='" + attributeValue.replace(/'/g, "'") + "'" :
'="' + attributeValue.replace(/"/g, """) + '"';
};
const unescapeIdentifier = source =>
source.replace(/\\u([0-9a-fA-F]{4})|\\u\{([0-9a-fA-F]+)\}/g, (_, hex4, hexAny) =>
String.fromCodePoint(parseInt(hex4 || hexAny, 16)));
/**
* Finds names that might be referenced by template code and so should be avoided when generating ones for compiler use.
*
* Will generally include many false positives, but false negatives shouldn’t be possible unless `eval` is involved.
*/
const addPossibleConflicts = (possibleConflicts, code) => {
const JS_IDENTIFIER = /(?:[\p{ID_Start}$_]|\\u[0-9a-fA-F]{4}|\\u\{[0-9a-fA-F]+\})(?:[\p{ID_Continue}$\u200c\u200d]|\\u[0-9a-fA-F]{4}|\\u\{[0-9a-fA-F]+\})*/gu;
let match;
while ((match = JS_IDENTIFIER.exec(code))) {
possibleConflicts.add(unescapeIdentifier(match[0]));
}
};
const passThrough = (compiler, context, node) => {
for (const child of node.children) {
compileNode(compiler, context, child);
}
};
const getScriptIdentifier = templateIdentifier =>
"_" + templateIdentifier.replace(/-/g, "_");
const resolveParameters = (macro, node) => {
const l = Math.min(macro.parameters.length, node.parameters.length);
const results = new Array(l);
let i;
for (i = 0; i < l; i++) {
const parameter = node.parameters[i];
if (parameter.name !== null) {
break;
}
results[i] = {
name: macro.parameters[i],
value: parameter.value,
};
}
for (; i < l; i++) {
const parameter = node.parameters[i];
const parameterIndex = macro.parameters.indexOf(parameter.name);
if (parameterIndex === -1) {
throw parameter.nonexistent;
}
if (results[parameterIndex]) {
throw parameter.alreadyProvided;
}
results[parameterIndex] = parameter;
}
if (i < node.parameters.length) {
throw node.parameters[i].unexpected;
}
const missing = [];
for (i = 0; i < macro.parameters.length; i++) {
if (!results[i]) {
missing.push(macro.parameters[i]);
}
}
if (missing.length !== 0) {
throw node.missing(missing);
}
return results;
};
class Scope {
constructor() {
this.used = new Set();
}
getName(name) {
if (this.used.has(name)) {
let i = 1;
while (this.used.has(name + "_" + i)) {
i++;
}
name += "_" + i;
}
this.used.add(name);
return name;
}
}
const transform = {
root: passThrough,
block: passThrough,
element: (compiler, context, node) => {
if (!context.content) {
throw node.unexpected;
}
const name = node.name.toLowerCase();
const isVoid = voidTags.has(name);
const newContext = {
attributes: new CodeBlock().addText(null, "<" + name),
content: isVoid ? null : new CodeBlock(),
classes: new CodeBlock(),
};
for (const child of node.children) {
compileNode(compiler, newContext, child);
}
context.content.addBlock(newContext.attributes);
if (newContext.classes.parts.length) {
for (const part of newContext.classes.parts) {
if (part.type === "text") {
part.value = part.value.substring(1);
break;
}
}
const classText = newContext.classes.toTextOrNull(null);
if (classText === null) {
context.content.addText(null, " class=\"");
context.content.addBlock(newContext.classes);
context.content.addText(null, "\"");
} else {
context.content.addText(null, " class" + shortestAttributeRepresentation(classText));
}
}
context.content.addText(null, ">");
if (!isVoid) {
context.content.addBlock(newContext.content);
context.content.addText(null, "</" + name + ">");
}
},
attribute: (compiler, context, node) => {
if (!context.attributes) {
throw node.unexpected;
}
context.attributes.addText(null, " " + node.name);
if (node.value !== null && node.value.value.parts.length !== 0) {
const text = node.value.value.toTextOrNull(escapes.escapeDoubleQuotedAttributeValue);
if (text === null) {
context.attributes.addText(null, "=\"");
context.attributes.addBlock(node.value.value);
context.attributes.addText(null, "\"");
} else {
context.attributes.addText(null, shortestAttributeRepresentation(text));
}
for (const part of node.value.value.parts) {
if (part.type === "expression") {
addPossibleConflicts(compiler.possibleConflicts, part.value);
}
}
}
},
string: (compiler, context, node) => {
if (!context.content) {
throw node.unexpected;
}
context.content.addBlock(node.value);
for (const part of node.value.parts) {
if (part.type === "expression") {
addPossibleConflicts(compiler.possibleConflicts, part.value);
}
}
},
class: (compiler, context, node) => {
if (!context.classes) {
throw node.unexpected;
}
context.classes.addText(null, " " + node.value);
},
code: (compiler, context, node) => {
context.content.addCode(wrapExpression(node.code) + ";");
addPossibleConflicts(compiler.possibleConflicts, node.code);
},
include: (compiler, context, node) => {
const subtree = compiler.options.load(node.template);
for (const [macroName, macro] of subtree.macros) {
if (compiler.tree.macros.has(macroName)) {
throw new SyntaxError(`Included template ${node.template} redefines the macro named “${macroName}”.`);
}
compiler.tree.macros.set(macroName, macro);
}
compileNode(compiler, context, subtree);
},
if: (compiler, context, node) => {
let condition = wrapExpression(node.condition);
const newContext = {
content: new CodeBlock(),
attributes: context.attributes && new CodeBlock(),
classes: context.classes && new CodeBlock(),
};
for (const child of node.children) {
compileNode(compiler, newContext, child);
}
if (node.elif.length) {
node.else = {
children: [
{
type: "if",
condition: node.elif[0].condition,
elif: node.elif.slice(1),
else: node.else,
children: node.elif[0].children,
},
],
};
}
let elseContext = null;
if (node.else) {
elseContext = {
content: new CodeBlock(),
attributes: context.attributes && new CodeBlock(),
classes: context.classes && new CodeBlock(),
};
for (const child of node.else.children) {
compileNode(compiler, elseContext, child);
}
}
const conditionName = compiler.scope.getName("condition");
(context.attributes || context.content).addCode("var " + conditionName + " = (" + condition + ");");
condition = conditionName;
const hasIfAttributes = newContext.attributes && newContext.attributes.parts.length;
const hasElseAttributes = elseContext && elseContext.attributes && elseContext.attributes.parts.length;
const hasIfClasses = newContext.classes && newContext.classes.parts.length;
const hasElseClasses = elseContext && elseContext.classes && elseContext.classes.parts.length;
const hasIfContent = newContext.content && newContext.content.parts.length;
const hasElseContent = elseContext && elseContext.content && elseContext.content.parts.length;
if (hasIfAttributes || hasElseAttributes) {
context.attributes.addCode("if (" + condition + ") {");
context.attributes.addBlock(newContext.attributes);
context.attributes.addCode("}");
if (hasElseAttributes) {
context.attributes.addCode("else {");
context.attributes.addBlock(elseContext.attributes);
context.attributes.addCode("}");
}
}
if (hasIfClasses || hasElseClasses) {
context.classes.addCode("if (" + condition + ") {");
context.classes.addBlock(newContext.classes);
context.classes.addCode("}");
if (hasElseClasses) {
context.classes.addCode("else {");
context.classes.addBlock(elseContext.classes);
context.classes.addCode("}");
}
}
if (hasIfContent || hasElseContent) {
context.content.addCode("if (" + condition + ") {");
context.content.addBlock(newContext.content);
context.content.addCode("}");
if (hasElseContent) {
context.content.addCode("else {");
context.content.addBlock(elseContext.content);
context.content.addCode("}");
}
}
},
for: (compiler, context, node) => {
const newContext = {
content: context.content,
};
const collection = wrapExpression(node.collection);
let indexNameUsed;
if (node.indexName !== null) {
context.content.addCode("{ let " + node.indexName + " = 0;");
indexNameUsed = compiler.scope.used.has(node.indexName);
compiler.scope.used.add(node.indexName);
}
context.content.addCode("for (const " + node.variable + " of (" + collection + ")) {");
for (const child of node.children) {
compileNode(compiler, newContext, child);
}
if (node.indexName !== null) {
context.content.addCode(node.indexName + "++; }");
if (!indexNameUsed) {
compiler.scope.used.delete(node.indexName);
}
}
context.content.addCode("}");
},
call: (compiler, context, node) => {
const macro = compiler.tree.macros.get(node.name);
if (macro === undefined) {
throw node.macroUndefined;
}
const macroFunctions =
context.attributes ?
compiler.top.attributesMacroFunctions :
compiler.top.contentOnlyMacroFunctions;
let macroFunction = macroFunctions.get(node);
const parameters = resolveParameters(macro, node);
const recursiveIndex = compiler.calls.indexOf(node);
if (recursiveIndex === -1) {
const pushContext = context.attributes || context.content;
const popContext = context.content || context.attributes;
const namesInfo = parameters.map(parameter => {
let originalName = null;
let temporaryName = null;
if (compiler.scope.used.has(parameter.name)) {
originalName = compiler.scope.getName("original_" + parameter.name);
pushContext.addCode("var " + originalName + " = " + parameter.name + ";");
if (context.attributes && context.content) {
temporaryName = compiler.scope.getName("temporary_" + parameter.name);
context.content.addCode(parameter.name + " = " + temporaryName + ";");
}
} else {
compiler.scope.used.add(parameter.name);
}
pushContext.addCode("var " + parameter.name + " = " + wrapExpression(parameter.value) + ";");
return {
originalName: originalName,
temporaryName: temporaryName,
};
});
compiler.calls.push(node);
macro.yieldContent = node.children;
passThrough(compiler, context, macro);
compiler.calls.pop();
parameters.forEach((parameter, i) => {
const nameInfo = namesInfo[i];
if (nameInfo.originalName) {
if (nameInfo.temporaryName) {
context.attributes.addCode("var " + nameInfo.temporaryName + " = " + parameter.name + ";");
context.attributes.addCode(parameter.name + " = " + nameInfo.originalName + ";");
}
popContext.addCode(parameter.name + " = " + nameInfo.originalName + ";");
}
});
return;
}
const redefine = !macroFunction || !context.attributes && !compiler.calls.includes(node, recursiveIndex + 1);
if (!macroFunction) {
macroFunction = compiler.scope.getName(
getScriptIdentifier(node.name) +
(context.attributes ? "_attributes" : "_content_only")
);
macroFunctions.set(node, macroFunction);
}
if (redefine) {
compiler.calls.push(node);
if (context.attributes) {
const newContext = {
content: new CodeBlock(),
attributes: new CodeBlock(),
classes: new CodeBlock(),
};
passThrough(compiler, newContext, macro);
const attributeOutputVariable = compiler.scope.getName("attributeOutput");
const classOutputVariable = compiler.scope.getName("classOutput");
const contentOutputVariable = compiler.scope.getName("contentOutput");
const definition =
"const " + macroFunction + " = (" + macro.parameters.join(", ") + ") => {\n" +
"let " + attributeOutputVariable + " = '" + newContext.attributes.toCode(attributeOutputVariable, "text") +
"let " + classOutputVariable + " = '" + newContext.classes.toCode(classOutputVariable, "text") +
"let " + contentOutputVariable + " = '" + newContext.content.toCode(contentOutputVariable, "text") +
"\n\nreturn { attributes: " + attributeOutputVariable + ", classes: " + classOutputVariable + ", content: " + contentOutputVariable + " };" +
"};\n";
compiler.top.macroDefinitions.push(definition);
} else {
context.content.addCode("const " + macroFunction + " = (" + macro.parameters.join(", ") + ") => {");
passThrough(compiler, context, macro);
context.content.addCode("};");
}
compiler.calls.pop();
}
const parameterList =
parameters
.map(parameter => "(" + wrapExpression(parameter.value) + ")")
.join(", ");
if (context.attributes) {
const resultName = compiler.scope.getName("moutput");
context.attributes.addCode("const " + resultName + " = " + macroFunction + "(" + parameterList + ");");
context.attributes.addExpression(null, resultName + ".attributes");
context.classes.addExpression(null, resultName + ".classes");
context.content.addExpression(null, resultName + ".content");
} else {
context.content.addCode(macroFunction + "(" + parameterList + ");");
}
},
yield: (compiler, context, node) => {
let macro = node;
do {
macro = macro.parent;
} while (macro && macro.type !== "macro");
if (!macro) {
throw node.unexpected;
}
for (const yieldNode of macro.yieldContent) {
compileNode(compiler, context, yieldNode);
}
},
};
const compileNode = (compiler, context, node) => {
const transformer = transform[node.type];
if (!transformer) {
throw new Error("Unknown node type " + node.type + ".");
}
transformer(compiler, context, node);
};
const compile = (tree, options) => {
const scope = new Scope();
const context = {
content: new CodeBlock(),
attributesMacroFunctions: new Map(),
contentOnlyMacroFunctions: new Map(),
macroDefinitions: [],
};
const compiler = {
scope: scope,
possibleConflicts: scope.used,
options: options,
tree: tree,
top: context,
calls: [],
};
compileNode(compiler, context, tree);
const outputVariable = scope.getName("output");
const code =
context.macroDefinitions.join("") +
"let " + outputVariable + " = '" + context.content.toCode(outputVariable, "text") +
"\n\nreturn " + outputVariable + ";";
const globals = Object.assign({}, options.globals, {
escapeDoubleQuotedAttributeValue: escapes.escapeDoubleQuotedAttributeValue,
escapeContent: escapes.escapeContent,
unwrapMarkup: Markup.unwrap,
});
const globalNames = Object.keys(globals);
for (const name of globalNames) {
if (!isIdentifier(name)) {
throw new Error(`Template global “${name}” is not a valid identifier`);
}
}
return new Function(
"__globals",
"'use strict';\n" +
"const {" + globalNames.join(", ") + "} = __globals;\n" +
"return data => {\n" + code + "\n};"
)(globals);
};
module.exports = {
compile,
transform,
};