/
parser.ts
446 lines (356 loc) · 15.2 KB
/
parser.ts
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
import {TwingEnvironment} from "./environment";
import {TwingTokenStream} from "./token-stream";
import {TwingNodeBlock} from "./node/block";
import {TwingTokenParserInterface} from "./token-parser-interface";
import {TwingNodeVisitorInterface} from "./node-visitor-interface";
import {TwingExpressionParser} from "./expression-parser";
import {TwingErrorSyntax} from "./error/syntax";
import {TwingNode, TwingNodeType} from "./node";
import {TwingToken} from "./token";
import {TwingNodeText} from "./node/text";
import {TwingNodePrint} from "./node/print";
import {TwingNodeExpression} from "./node/expression";
import {TwingNodeBody} from "./node/body";
import {TwingNodeModule} from "./node/module";
import {TwingNodeTraverser} from "./node-traverser";
import {TwingNodeMacro} from "./node/macro";
import {TwingTokenParser} from "./token-parser";
import {first} from "./helper/first";
import {push} from "./helper/push";
import {TwingNodeComment} from "./node/comment";
import {ctypeSpace} from "./helper/ctype_space";
const mt_rand = require('locutus/php/math/mt_rand');
const sha256 = require('crypto-js/sha256');
const hex = require('crypto-js/enc-hex');
class TwingParserStackEntry {
stream: TwingTokenStream;
parent: TwingNode;
blocks: Map<string, TwingNodeBlock>;
blockStack: Array<string>;
macros: Map<string, TwingNode>;
importedSymbols: Array<Map<string, Map<string, { node: TwingNodeExpression, name: string }>>>;
traits: Map<string, TwingNode>;
embeddedTemplates: Array<TwingNodeModule>;
constructor(stream: TwingTokenStream, parent: TwingNode = null, blocks: Map<string, TwingNodeBlock>, blockStack: Array<string>, macros: Map<string, TwingNode>, importedSymbols: Array<Map<string, Map<string, { name: string, node: TwingNodeExpression }>>>, traits: Map<string, TwingNode>, embeddedTemplates: Array<TwingNodeModule>) {
this.stream = stream;
this.parent = parent;
this.blocks = blocks;
this.blockStack = blockStack;
this.macros = macros;
this.importedSymbols = importedSymbols;
this.traits = traits;
this.embeddedTemplates = embeddedTemplates;
}
}
export class TwingParser {
private stack: Array<TwingParserStackEntry> = [];
private stream: TwingTokenStream;
private parent: TwingNode;
private handlers: Map<string, TwingTokenParserInterface> = null;
private visitors: Array<TwingNodeVisitorInterface>;
private expressionParser: TwingExpressionParser = null;
private blocks: Map<string, TwingNodeBody>;
private blockStack: Array<string>;
private macros: Map<string, TwingNode>;
private env: TwingEnvironment;
private importedSymbols: Array<Map<string, Map<string, { name: string, node: TwingNodeExpression }>>>;
private traits: Map<string, TwingNode>;
private embeddedTemplates: Array<TwingNodeModule> = [];
private varNameSalt: number = 0;
constructor(env: TwingEnvironment) {
this.env = env;
}
getVarName(prefix: string = '__internal_'): string {
return `${prefix}${hex.stringify(sha256('TwingParser::getVarName' + this.stream.getSourceContext().getCode() + this.varNameSalt++))}`;
}
parse(stream: TwingTokenStream, test: Array<any> = null, dropNeedle: boolean = false): TwingNodeModule {
this.stack.push(new TwingParserStackEntry(
this.stream,
this.parent,
this.blocks,
this.blockStack,
this.macros,
this.importedSymbols,
this.traits,
this.embeddedTemplates
));
// tag handlers
if (this.handlers === null) {
this.handlers = new Map();
for (let handler of this.env.getTokenParsers()) {
handler.setParser(this);
this.handlers.set(handler.getTag(), handler);
}
}
// node visitors
if (!this.visitors) {
this.visitors = this.env.getNodeVisitors();
}
if (this.expressionParser === null) {
this.expressionParser = new TwingExpressionParser(this, this.env);
}
this.stream = stream;
this.parent = null;
this.blocks = new Map();
this.macros = new Map();
this.traits = new Map();
this.blockStack = [];
this.importedSymbols = [new Map()];
this.embeddedTemplates = [];
this.varNameSalt = 0;
let body: TwingNode;
try {
body = this.subparse(test, dropNeedle);
if (this.parent !== null && (body = this.filterBodyNodes(body)) === null) {
body = new TwingNode();
}
}
catch (e) {
if (e instanceof TwingErrorSyntax) {
if (!e.getSourceContext()) {
e.setSourceContext(this.stream.getSourceContext());
}
}
throw e;
}
let nodes = new Map();
nodes.set(0, body);
let node = new TwingNodeModule(new TwingNodeBody(nodes), this.parent, new TwingNode(this.blocks), new TwingNode(this.macros), new TwingNode(this.traits), this.embeddedTemplates, stream.getSourceContext());
let traverser = new TwingNodeTraverser(this.env, this.visitors);
node = traverser.traverse(node) as TwingNodeModule;
// restore previous stack so previous parse() call can resume working
let key: string;
let stack = this.stack.pop();
this.stream = stack.stream;
this.parent = stack.parent;
this.blocks = stack.blocks;
this.blockStack = stack.blockStack;
this.macros = stack.macros;
this.importedSymbols = stack.importedSymbols;
this.traits = stack.traits;
this.embeddedTemplates = stack.embeddedTemplates;
return node;
}
getExpressionParser(): TwingExpressionParser {
return this.expressionParser;
}
getParent(): TwingNode {
return this.parent;
}
setParent(parent: TwingNode) {
this.parent = parent;
}
subparse(test: Array<any>, dropNeedle: boolean = false): TwingNode {
let lineno = this.getCurrentToken().getLine();
let rv = new Map();
let i: number = 0;
let token;
while (!this.stream.isEOF()) {
switch (this.getCurrentToken().getType()) {
case TwingToken.TEXT_TYPE:
token = this.stream.next();
rv.set(i++, new TwingNodeText(token.getValue(), token.getLine(), token.getColumn()));
break;
case TwingToken.VAR_START_TYPE:
token = this.stream.next();
let expression = this.expressionParser.parseExpression();
this.stream.expect(TwingToken.VAR_END_TYPE);
rv.set(i++, new TwingNodePrint(expression, token.getLine(), token.getColumn()));
break;
case TwingToken.BLOCK_START_TYPE:
this.stream.next();
token = this.getCurrentToken();
if (token.getType() !== TwingToken.NAME_TYPE) {
throw new TwingErrorSyntax('A block must start with a tag name.', token.getLine(), this.stream.getSourceContext());
}
if (test !== null && test[1](token)) {
if (dropNeedle) {
this.stream.next();
}
if (rv.size === 1) {
return first(rv);
}
return new TwingNode(rv, new Map(), lineno);
}
if (!this.handlers.has(token.getValue())) {
let e;
if (test !== null) {
e = new TwingErrorSyntax(
`Unexpected "${token.getValue()}" tag`,
token.getLine(),
this.stream.getSourceContext()
);
if (Array.isArray(test) && (test.length > 1) && (test[0] instanceof TwingTokenParser)) {
e.appendMessage(` (expecting closing tag for the "${test[0].getTag()}" tag defined near line ${lineno}).`);
}
}
else {
e = new TwingErrorSyntax(
`Unknown "${token.getValue()}" tag.`,
token.getLine(),
this.stream.getSourceContext()
);
e.addSuggestions(token.getValue(), Array.from(this.env.getTags().keys()));
}
throw e;
}
this.stream.next();
let subparser = this.handlers.get(token.getValue());
let node = subparser.parse(token);
if (node !== null) {
rv.set(i++, node);
}
break;
case TwingToken.COMMENT_START_TYPE:
this.stream.next();
token = this.stream.expect(TwingToken.TEXT_TYPE);
this.stream.expect(TwingToken.COMMENT_END_TYPE);
rv.set(i++, new TwingNodeComment(token.getValue(), token.getLine(), token.getColumn()));
break;
default:
throw new TwingErrorSyntax(
'Lexer or parser ended up in unsupported state.',
this.getCurrentToken().getLine(),
this.stream.getSourceContext()
);
}
}
if (rv.size === 1) {
return first(rv);
}
return new TwingNode(rv, new Map(), lineno);
}
getBlockStack() {
return this.blockStack;
}
peekBlockStack() {
return this.blockStack[this.blockStack.length - 1];
}
popBlockStack() {
this.blockStack.pop();
}
pushBlockStack(name: string) {
this.blockStack.push(name);
}
hasBlock(name: string) {
return this.blocks.has(name);
}
getBlock(name: string) {
return this.blocks.get(name);
}
setBlock(name: string, value: TwingNodeBlock) {
let bodyNodes = new Map();
bodyNodes.set(0, value);
this.blocks.set(name, new TwingNodeBody(bodyNodes, new Map(), value.getTemplateLine()));
}
addTrait(trait: TwingNode) {
push(this.traits, trait);
}
hasTraits() {
return this.traits.size > 0;
}
embedTemplate(template: TwingNodeModule) {
template.setIndex(mt_rand());
this.embeddedTemplates.push(template);
}
/**
* @return {TwingToken}
*/
getCurrentToken(): TwingToken {
return this.stream.getCurrent();
}
/**
*
* @return {TwingTokenStream}
*/
getStream(): TwingTokenStream {
return this.stream;
}
addImportedSymbol(type: string, alias: string, name: string = null, node: TwingNodeExpression = null) {
let localScope = this.importedSymbols[0];
if (!localScope.has(type)) {
localScope.set(type, new Map());
}
let localScopeType = localScope.get(type);
localScopeType.set(alias, {name: name, node: node});
}
getImportedSymbol(type: string, alias: string): { node: TwingNodeExpression, name: string } {
let result = null;
this.importedSymbols.forEach(function (importedSymbol) {
if (importedSymbol.has(type)) {
let importedSymbolType = importedSymbol.get(type);
if (importedSymbolType.has(alias)) {
result = importedSymbolType.get(alias);
}
}
});
return result;
}
hasMacro(name: string) {
return this.macros.has(name);
}
setMacro(name: string, node: TwingNodeMacro) {
this.macros.set(name, node);
}
isMainScope() {
return this.importedSymbols.length === 1;
}
pushLocalScope() {
this.importedSymbols.unshift(new Map());
}
popLocalScope() {
this.importedSymbols.shift();
}
/**
*
* @param node
* @param nested {boolean}
* @returns {TwingNode}
*/
filterBodyNodes(node: TwingNode, nested: boolean = false): TwingNode {
// check that the body does not contain non-empty output nodes
if ((node.getType() === TwingNodeType.TEXT && !ctypeSpace(node.getAttribute('data'))) ||
((node.getType() !== TwingNodeType.TEXT) && (node.getType() !== TwingNodeType.BLOCK_REFERENCE) && ((node as any).TwingNodeOutputInterfaceImpl) && (node.getType() !== TwingNodeType.SPACELESS))) {
let nodeData: string = node.getAttribute('data') as string;
if (nodeData.indexOf(String.fromCharCode(0xEF, 0xBB, 0xBF)) > -1) {
let trailingData = nodeData.substring(3);
if (trailingData === '' || ctypeSpace(trailingData)) {
// bypass empty nodes starting with a BOM
return null;
}
}
throw new TwingErrorSyntax(
`A template that extends another one cannot include content outside Twig blocks. Did you forget to put the content inside a {% block %} tag?`,
node.getTemplateLine(),
this.stream.getSourceContext());
}
// bypass nodes that "capture" the output
if ((node as any).TwingNodeCaptureInterfaceImpl) {
// a "block" tag in such a node will serve as a block definition AND be displayed in place as well
return node;
}
// to be removed completely in Twig 3.0
if (!nested && (node.getType() === TwingNodeType.SPACELESS)) {
console.error(`Using the spaceless tag at the root level of a child template in "${this.stream.getSourceContext().getName()}" at line ${node.getTemplateLine()} is deprecated since Twig 2.5.0 and will become a syntax error in Twig 3.0.`);
}
// "block" tags that are not captured (see above) are only used for defining
// the content of the block. In such a case, nesting it does not work as
// expected as the definition is not part of the default template code flow.
if (nested && (node.getType() === TwingNodeType.BLOCK_REFERENCE)) {
console.error(`Nesting a block definition under a non-capturing node in "${this.stream.getSourceContext().getName()}" at line ${node.getTemplateLine()} is deprecated since Twig 2.5.0 and will become a syntax error in Twig 3.0.`);
return null;
}
if ((node as any).TwingNodeOutputInterfaceImpl && (node.getType() !== TwingNodeType.SPACELESS)) {
return null;
}
// here, nested means "being at the root level of a child template"
// we need to discard the wrapping "TwingNode" for the "body" node
nested = nested || (node.getType() !== null);
for (let [k, n] of node.getNodes()) {
if (n !== null && (this.filterBodyNodes(n, nested) === null)) {
node.removeNode(k);
}
}
return node;
}
}