@zackad/prettier-plugin-twig 0.7.0-alpha.1 → 0.7.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/package.json +10 -8
- package/src/index.js +53 -53
- package/src/melody/melody-code-frame/index.js +44 -0
- package/src/melody/melody-code-frame/lineNumbers.js +45 -0
- package/src/melody/melody-extension-core/index.js +167 -0
- package/src/melody/melody-extension-core/operators.js +414 -0
- package/src/melody/melody-extension-core/parser/autoescape.js +92 -0
- package/src/melody/melody-extension-core/parser/block.js +93 -0
- package/src/melody/melody-extension-core/parser/do.js +32 -0
- package/src/melody/melody-extension-core/parser/embed.js +76 -0
- package/src/melody/melody-extension-core/parser/extends.js +35 -0
- package/src/melody/melody-extension-core/parser/filter.js +63 -0
- package/src/melody/melody-extension-core/parser/flush.js +33 -0
- package/src/melody/melody-extension-core/parser/for.js +110 -0
- package/src/melody/melody-extension-core/parser/from.js +63 -0
- package/src/melody/melody-extension-core/parser/if.js +100 -0
- package/src/melody/melody-extension-core/parser/import.js +44 -0
- package/src/melody/melody-extension-core/parser/include.js +48 -0
- package/src/melody/melody-extension-core/parser/macro.js +94 -0
- package/src/melody/melody-extension-core/parser/mount.js +147 -0
- package/src/melody/melody-extension-core/parser/set.js +103 -0
- package/src/melody/melody-extension-core/parser/spaceless.js +56 -0
- package/src/melody/melody-extension-core/parser/use.js +58 -0
- package/src/melody/melody-extension-core/types.js +402 -0
- package/src/melody/melody-extension-core/visitors/filters.js +146 -0
- package/src/melody/melody-extension-core/visitors/for.js +391 -0
- package/src/melody/melody-extension-core/visitors/functions.js +100 -0
- package/src/melody/melody-extension-core/visitors/tests.js +127 -0
- package/src/melody/melody-parser/Associativity.js +17 -0
- package/src/melody/melody-parser/CharStream.js +82 -0
- package/src/melody/melody-parser/GenericMultiTagParser.js +66 -0
- package/src/melody/melody-parser/GenericTagParser.js +53 -0
- package/src/melody/melody-parser/Lexer.js +665 -0
- package/src/melody/melody-parser/Parser.js +954 -0
- package/src/melody/melody-parser/TokenStream.js +183 -0
- package/src/melody/melody-parser/TokenTypes.js +74 -0
- package/src/melody/melody-parser/elementInfo.js +43 -0
- package/src/melody/melody-parser/index.js +94 -0
- package/src/melody/melody-parser/util.js +100 -0
- package/src/melody/melody-traverse/Binding.js +61 -0
- package/src/melody/melody-traverse/Path.js +427 -0
- package/src/melody/melody-traverse/Scope.js +218 -0
- package/src/melody/melody-traverse/TraversalContext.js +166 -0
- package/src/melody/melody-traverse/index.js +21 -0
- package/src/melody/melody-traverse/traverse.js +48 -0
- package/src/melody/melody-traverse/visitors.js +112 -0
- package/src/melody/melody-types/index.js +506 -0
- package/src/parser.js +7 -2
- package/src/print/Attribute.js +1 -1
- package/src/print/BinaryExpression.js +2 -2
- package/src/print/BlockStatement.js +1 -1
- package/src/print/CallExpression.js +1 -1
- package/src/print/ExpressionStatement.js +1 -1
- package/src/print/FilterExpression.js +1 -1
- package/src/print/GenericTwigTag.js +1 -1
- package/src/print/IfStatement.js +1 -1
- package/src/print/ObjectProperty.js +1 -1
- package/src/print/SetStatement.js +1 -1
- package/src/print/SwitchTag.js +1 -1
- package/src/print/UnarySubclass.js +1 -1
- package/src/util/printFunctions.js +1 -1
- package/src/util/publicFunctions.js +1 -1
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2017 trivago N.V.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import {
|
|
17
|
+
Node,
|
|
18
|
+
BinaryExpression,
|
|
19
|
+
BinaryConcatExpression,
|
|
20
|
+
UnaryExpression,
|
|
21
|
+
type,
|
|
22
|
+
alias,
|
|
23
|
+
visitor
|
|
24
|
+
} from "../melody-types/index.js";
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
Types,
|
|
28
|
+
setStartFromToken,
|
|
29
|
+
setEndFromToken,
|
|
30
|
+
copyStart,
|
|
31
|
+
copyEnd,
|
|
32
|
+
copyLoc,
|
|
33
|
+
LEFT
|
|
34
|
+
} from "../melody-parser/index.js";
|
|
35
|
+
|
|
36
|
+
export const unaryOperators = [];
|
|
37
|
+
export const binaryOperators = [];
|
|
38
|
+
export const tests = [];
|
|
39
|
+
|
|
40
|
+
//region Unary Expressions
|
|
41
|
+
export const UnaryNotExpression = createUnaryOperator(
|
|
42
|
+
"not",
|
|
43
|
+
"UnaryNotExpression",
|
|
44
|
+
50
|
|
45
|
+
);
|
|
46
|
+
export const UnaryNeqExpression = createUnaryOperator(
|
|
47
|
+
"-",
|
|
48
|
+
"UnaryNeqExpression",
|
|
49
|
+
500
|
|
50
|
+
);
|
|
51
|
+
export const UnaryPosExpression = createUnaryOperator(
|
|
52
|
+
"+",
|
|
53
|
+
"UnaryPosExpression",
|
|
54
|
+
500
|
|
55
|
+
);
|
|
56
|
+
//endregion
|
|
57
|
+
|
|
58
|
+
//region Binary Expressions
|
|
59
|
+
export const BinaryOrExpression = createBinaryOperatorNode({
|
|
60
|
+
text: "or",
|
|
61
|
+
type: "BinaryOrExpression",
|
|
62
|
+
precedence: 10,
|
|
63
|
+
associativity: LEFT
|
|
64
|
+
});
|
|
65
|
+
export const BinaryAndExpression = createBinaryOperatorNode({
|
|
66
|
+
text: "and",
|
|
67
|
+
type: "BinaryAndExpression",
|
|
68
|
+
precedence: 15,
|
|
69
|
+
associativity: LEFT
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
export const BitwiseOrExpression = createBinaryOperatorNode({
|
|
73
|
+
text: "b-or",
|
|
74
|
+
type: "BitwiseOrExpression",
|
|
75
|
+
precedence: 16,
|
|
76
|
+
associativity: LEFT
|
|
77
|
+
});
|
|
78
|
+
export const BitwiseXorExpression = createBinaryOperatorNode({
|
|
79
|
+
text: "b-xor",
|
|
80
|
+
type: "BitwiseXOrExpression",
|
|
81
|
+
precedence: 17,
|
|
82
|
+
associativity: LEFT
|
|
83
|
+
});
|
|
84
|
+
export const BitwiseAndExpression = createBinaryOperatorNode({
|
|
85
|
+
text: "b-and",
|
|
86
|
+
type: "BitwiseAndExpression",
|
|
87
|
+
precedence: 18,
|
|
88
|
+
associativity: LEFT
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
export const BinaryEqualsExpression = createBinaryOperatorNode({
|
|
92
|
+
text: "==",
|
|
93
|
+
type: "BinaryEqualsExpression",
|
|
94
|
+
precedence: 20,
|
|
95
|
+
associativity: LEFT
|
|
96
|
+
});
|
|
97
|
+
export const BinaryNotEqualsExpression = createBinaryOperatorNode({
|
|
98
|
+
text: "!=",
|
|
99
|
+
type: "BinaryNotEqualsExpression",
|
|
100
|
+
precedence: 20,
|
|
101
|
+
associativity: LEFT
|
|
102
|
+
});
|
|
103
|
+
export const BinaryLessThanExpression = createBinaryOperatorNode({
|
|
104
|
+
text: "<",
|
|
105
|
+
type: "BinaryLessThanExpression",
|
|
106
|
+
precedence: 20,
|
|
107
|
+
associativity: LEFT
|
|
108
|
+
});
|
|
109
|
+
export const BinaryGreaterThanExpression = createBinaryOperatorNode({
|
|
110
|
+
text: ">",
|
|
111
|
+
type: "BinaryGreaterThanExpression",
|
|
112
|
+
precedence: 20,
|
|
113
|
+
associativity: LEFT
|
|
114
|
+
});
|
|
115
|
+
export const BinaryLessThanOrEqualExpression = createBinaryOperatorNode({
|
|
116
|
+
text: "<=",
|
|
117
|
+
type: "BinaryLessThanOrEqualExpression",
|
|
118
|
+
precedence: 20,
|
|
119
|
+
associativity: LEFT
|
|
120
|
+
});
|
|
121
|
+
export const BinaryGreaterThanOrEqualExpression = createBinaryOperatorNode({
|
|
122
|
+
text: ">=",
|
|
123
|
+
type: "BinaryGreaterThanOrEqualExpression",
|
|
124
|
+
precedence: 20,
|
|
125
|
+
associativity: LEFT
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
export const BinaryNotInExpression = createBinaryOperatorNode({
|
|
129
|
+
text: "not in",
|
|
130
|
+
type: "BinaryNotInExpression",
|
|
131
|
+
precedence: 20,
|
|
132
|
+
associativity: LEFT
|
|
133
|
+
});
|
|
134
|
+
export const BinaryInExpression = createBinaryOperatorNode({
|
|
135
|
+
text: "in",
|
|
136
|
+
type: "BinaryInExpression",
|
|
137
|
+
precedence: 20,
|
|
138
|
+
associativity: LEFT
|
|
139
|
+
});
|
|
140
|
+
export const BinaryMatchesExpression = createBinaryOperatorNode({
|
|
141
|
+
text: "matches",
|
|
142
|
+
type: "BinaryMatchesExpression",
|
|
143
|
+
precedence: 20,
|
|
144
|
+
associativity: LEFT
|
|
145
|
+
});
|
|
146
|
+
export const BinaryStartsWithExpression = createBinaryOperatorNode({
|
|
147
|
+
text: "starts with",
|
|
148
|
+
type: "BinaryStartsWithExpression",
|
|
149
|
+
precedence: 20,
|
|
150
|
+
associativity: LEFT
|
|
151
|
+
});
|
|
152
|
+
export const BinaryEndsWithExpression = createBinaryOperatorNode({
|
|
153
|
+
text: "ends with",
|
|
154
|
+
type: "BinaryEndsWithExpression",
|
|
155
|
+
precedence: 20,
|
|
156
|
+
associativity: LEFT
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
export const BinaryRangeExpression = createBinaryOperatorNode({
|
|
160
|
+
text: "..",
|
|
161
|
+
type: "BinaryRangeExpression",
|
|
162
|
+
precedence: 25,
|
|
163
|
+
associativity: LEFT
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
export const BinaryAddExpression = createBinaryOperatorNode({
|
|
167
|
+
text: "+",
|
|
168
|
+
type: "BinaryAddExpression",
|
|
169
|
+
precedence: 30,
|
|
170
|
+
associativity: LEFT
|
|
171
|
+
});
|
|
172
|
+
export const BinarySubExpression = createBinaryOperatorNode({
|
|
173
|
+
text: "-",
|
|
174
|
+
type: "BinarySubExpression",
|
|
175
|
+
precedence: 30,
|
|
176
|
+
associativity: LEFT
|
|
177
|
+
});
|
|
178
|
+
binaryOperators.push({
|
|
179
|
+
text: "~",
|
|
180
|
+
precedence: 40,
|
|
181
|
+
associativity: LEFT,
|
|
182
|
+
createNode(token, lhs, rhs) {
|
|
183
|
+
const op = new BinaryConcatExpression(lhs, rhs);
|
|
184
|
+
copyStart(op, lhs);
|
|
185
|
+
copyEnd(op, rhs);
|
|
186
|
+
return op;
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
export const BinaryMulExpression = createBinaryOperatorNode({
|
|
190
|
+
text: "*",
|
|
191
|
+
type: "BinaryMulExpression",
|
|
192
|
+
precedence: 60,
|
|
193
|
+
associativity: LEFT
|
|
194
|
+
});
|
|
195
|
+
export const BinaryDivExpression = createBinaryOperatorNode({
|
|
196
|
+
text: "/",
|
|
197
|
+
type: "BinaryDivExpression",
|
|
198
|
+
precedence: 60,
|
|
199
|
+
associativity: LEFT
|
|
200
|
+
});
|
|
201
|
+
export const BinaryFloorDivExpression = createBinaryOperatorNode({
|
|
202
|
+
text: "//",
|
|
203
|
+
type: "BinaryFloorDivExpression",
|
|
204
|
+
precedence: 60,
|
|
205
|
+
associativity: LEFT
|
|
206
|
+
});
|
|
207
|
+
export const BinaryModExpression = createBinaryOperatorNode({
|
|
208
|
+
text: "%",
|
|
209
|
+
type: "BinaryModExpression",
|
|
210
|
+
precedence: 60,
|
|
211
|
+
associativity: LEFT
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
binaryOperators.push({
|
|
215
|
+
text: "is",
|
|
216
|
+
precedence: 100,
|
|
217
|
+
associativity: LEFT,
|
|
218
|
+
parse(parser, token, expr) {
|
|
219
|
+
const tokens = parser.tokens;
|
|
220
|
+
|
|
221
|
+
let not = false;
|
|
222
|
+
if (tokens.nextIf(Types.OPERATOR, "not")) {
|
|
223
|
+
not = true;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const test = getTest(parser);
|
|
227
|
+
let args = null;
|
|
228
|
+
if (tokens.test(Types.LPAREN)) {
|
|
229
|
+
args = parser.matchArguments();
|
|
230
|
+
}
|
|
231
|
+
const testExpression = test.createNode(expr, args);
|
|
232
|
+
setStartFromToken(testExpression, token);
|
|
233
|
+
setEndFromToken(testExpression, tokens.la(-1));
|
|
234
|
+
if (not) {
|
|
235
|
+
return copyLoc(
|
|
236
|
+
new UnaryNotExpression(testExpression),
|
|
237
|
+
testExpression
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
return testExpression;
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
function getTest(parser) {
|
|
245
|
+
const tokens = parser.tokens;
|
|
246
|
+
const nameToken = tokens.la(0);
|
|
247
|
+
if (nameToken.type !== Types.NULL) {
|
|
248
|
+
tokens.expect(Types.SYMBOL);
|
|
249
|
+
} else {
|
|
250
|
+
tokens.next();
|
|
251
|
+
}
|
|
252
|
+
let testName = nameToken.text;
|
|
253
|
+
if (!parser.hasTest(testName)) {
|
|
254
|
+
// try 2-words tests
|
|
255
|
+
const continuedNameToken = tokens.expect(Types.SYMBOL);
|
|
256
|
+
testName += " " + continuedNameToken.text;
|
|
257
|
+
if (!parser.hasTest(testName)) {
|
|
258
|
+
parser.error({
|
|
259
|
+
title: `Unknown test "${testName}"`,
|
|
260
|
+
pos: nameToken.pos
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return parser.getTest(testName);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export const BinaryPowerExpression = createBinaryOperatorNode({
|
|
269
|
+
text: "**",
|
|
270
|
+
type: "BinaryPowerExpression",
|
|
271
|
+
precedence: 200,
|
|
272
|
+
associativity: LEFT
|
|
273
|
+
});
|
|
274
|
+
export const BinaryNullCoalesceExpression = createBinaryOperatorNode({
|
|
275
|
+
text: "??",
|
|
276
|
+
type: "BinaryNullCoalesceExpression",
|
|
277
|
+
precedence: 300,
|
|
278
|
+
associativity: LEFT
|
|
279
|
+
});
|
|
280
|
+
//endregion
|
|
281
|
+
|
|
282
|
+
//region Test Expressions
|
|
283
|
+
export const TestEvenExpression = createTest("even", "TestEvenExpression");
|
|
284
|
+
export const TestOddExpression = createTest("odd", "TestOddExpression");
|
|
285
|
+
export const TestDefinedExpression = createTest(
|
|
286
|
+
"defined",
|
|
287
|
+
"TestDefinedExpression"
|
|
288
|
+
);
|
|
289
|
+
export const TestSameAsExpression = createTest(
|
|
290
|
+
"same as",
|
|
291
|
+
"TestSameAsExpression"
|
|
292
|
+
);
|
|
293
|
+
tests.push({
|
|
294
|
+
text: "sameas",
|
|
295
|
+
createNode(expr, args) {
|
|
296
|
+
// todo: add deprecation warning
|
|
297
|
+
return new TestSameAsExpression(expr, args);
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
export const TestNullExpression = createTest("null", "TestNullExpression");
|
|
301
|
+
tests.push({
|
|
302
|
+
text: "none",
|
|
303
|
+
createNode(expr, args) {
|
|
304
|
+
return new TestNullExpression(expr, args);
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
export const TestDivisibleByExpression = createTest(
|
|
308
|
+
"divisible by",
|
|
309
|
+
"TestDivisibleByExpression"
|
|
310
|
+
);
|
|
311
|
+
tests.push({
|
|
312
|
+
text: "divisibleby",
|
|
313
|
+
createNode(expr, args) {
|
|
314
|
+
// todo: add deprecation warning
|
|
315
|
+
return new TestDivisibleByExpression(expr, args);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
export const TestConstantExpression = createTest(
|
|
319
|
+
"constant",
|
|
320
|
+
"TestConstantExpression"
|
|
321
|
+
);
|
|
322
|
+
export const TestEmptyExpression = createTest("empty", "TestEmptyExpression");
|
|
323
|
+
export const TestIterableExpression = createTest(
|
|
324
|
+
"iterable",
|
|
325
|
+
"TestIterableExpression"
|
|
326
|
+
);
|
|
327
|
+
//endregion
|
|
328
|
+
|
|
329
|
+
//region Utilities
|
|
330
|
+
function createTest(text, typeName) {
|
|
331
|
+
const TestExpression = class extends Node {
|
|
332
|
+
/**
|
|
333
|
+
* @param {Node} expr
|
|
334
|
+
* @param {Array<Node>} [args]
|
|
335
|
+
*/
|
|
336
|
+
constructor(expr, args) {
|
|
337
|
+
super();
|
|
338
|
+
this.expression = expr;
|
|
339
|
+
this.arguments = args;
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
type(TestExpression, typeName);
|
|
343
|
+
alias(TestExpression, "Expression", "TestExpression");
|
|
344
|
+
visitor(TestExpression, "expression", "arguments");
|
|
345
|
+
|
|
346
|
+
tests.push({
|
|
347
|
+
text,
|
|
348
|
+
createNode(expr, args) {
|
|
349
|
+
return new TestExpression(expr, args);
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
return TestExpression;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function createBinaryOperatorNode(options) {
|
|
357
|
+
const { text, precedence, associativity } = options;
|
|
358
|
+
const BinarySubclass = class extends BinaryExpression {
|
|
359
|
+
/**
|
|
360
|
+
* @param {Node} left
|
|
361
|
+
* @param {Node} right
|
|
362
|
+
*/
|
|
363
|
+
constructor(left, right) {
|
|
364
|
+
super(text, left, right);
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
type(BinarySubclass, options.type);
|
|
368
|
+
alias(BinarySubclass, "BinaryExpression", "Binary", "Expression");
|
|
369
|
+
visitor(BinarySubclass, "left", "right");
|
|
370
|
+
|
|
371
|
+
const operator = {
|
|
372
|
+
text,
|
|
373
|
+
precedence,
|
|
374
|
+
associativity
|
|
375
|
+
};
|
|
376
|
+
if (options.parse) {
|
|
377
|
+
operator.parse = options.parse;
|
|
378
|
+
} else if (options.createNode) {
|
|
379
|
+
operator.createNode = options.createNode;
|
|
380
|
+
} else {
|
|
381
|
+
operator.createNode = (token, lhs, rhs) => new BinarySubclass(lhs, rhs);
|
|
382
|
+
}
|
|
383
|
+
binaryOperators.push(operator);
|
|
384
|
+
|
|
385
|
+
return BinarySubclass;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function createUnaryOperator(operator, typeName, precedence) {
|
|
389
|
+
const UnarySubclass = class extends UnaryExpression {
|
|
390
|
+
/**
|
|
391
|
+
* @param {Node} argument
|
|
392
|
+
*/
|
|
393
|
+
constructor(argument) {
|
|
394
|
+
super(operator, argument);
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
type(UnarySubclass, typeName);
|
|
398
|
+
alias(UnarySubclass, "Expression", "UnaryLike");
|
|
399
|
+
visitor(UnarySubclass, "argument");
|
|
400
|
+
|
|
401
|
+
unaryOperators.push({
|
|
402
|
+
text: operator,
|
|
403
|
+
precedence,
|
|
404
|
+
createNode(token, expr) {
|
|
405
|
+
const op = new UnarySubclass(expr);
|
|
406
|
+
setStartFromToken(op, token);
|
|
407
|
+
copyEnd(op, expr);
|
|
408
|
+
return op;
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
return UnarySubclass;
|
|
413
|
+
}
|
|
414
|
+
//endregion
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2017 trivago N.V.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import {
|
|
17
|
+
Types,
|
|
18
|
+
setStartFromToken,
|
|
19
|
+
setEndFromToken,
|
|
20
|
+
hasTagStartTokenTrimLeft,
|
|
21
|
+
hasTagEndTokenTrimRight
|
|
22
|
+
} from "../../melody-parser/index.js";
|
|
23
|
+
|
|
24
|
+
import { AutoescapeBlock } from "./../types.js";
|
|
25
|
+
|
|
26
|
+
export const AutoescapeParser = {
|
|
27
|
+
name: "autoescape",
|
|
28
|
+
parse(parser, token) {
|
|
29
|
+
const tokens = parser.tokens;
|
|
30
|
+
|
|
31
|
+
let escapeType = null;
|
|
32
|
+
let stringStartToken;
|
|
33
|
+
let openingTagEndToken;
|
|
34
|
+
let closingTagStartToken;
|
|
35
|
+
if (tokens.nextIf(Types.TAG_END)) {
|
|
36
|
+
openingTagEndToken = tokens.la(-1);
|
|
37
|
+
escapeType = null;
|
|
38
|
+
} else if ((stringStartToken = tokens.nextIf(Types.STRING_START))) {
|
|
39
|
+
escapeType = tokens.expect(Types.STRING).text;
|
|
40
|
+
if (!tokens.nextIf(Types.STRING_END)) {
|
|
41
|
+
parser.error({
|
|
42
|
+
title: "autoescape type declaration must be a simple string",
|
|
43
|
+
pos: tokens.la(0).pos,
|
|
44
|
+
advice: `The type declaration for autoescape must be a simple string such as 'html' or 'js'.
|
|
45
|
+
I expected the current string to end with a ${
|
|
46
|
+
stringStartToken.text
|
|
47
|
+
} but instead found ${
|
|
48
|
+
Types.ERROR_TABLE[tokens.lat(0)] || tokens.lat(0)
|
|
49
|
+
}.`
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
openingTagEndToken = tokens.la(0);
|
|
53
|
+
} else if (tokens.nextIf(Types.FALSE)) {
|
|
54
|
+
escapeType = false;
|
|
55
|
+
openingTagEndToken = tokens.la(0);
|
|
56
|
+
} else if (tokens.nextIf(Types.TRUE)) {
|
|
57
|
+
escapeType = true;
|
|
58
|
+
openingTagEndToken = tokens.la(0);
|
|
59
|
+
} else {
|
|
60
|
+
parser.error({
|
|
61
|
+
title: "Invalid autoescape type declaration",
|
|
62
|
+
pos: tokens.la(0).pos,
|
|
63
|
+
advice: `Expected type of autoescape to be a string, boolean or not specified. Found ${
|
|
64
|
+
tokens.la(0).type
|
|
65
|
+
} instead.`
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const autoescape = new AutoescapeBlock(escapeType);
|
|
70
|
+
setStartFromToken(autoescape, token);
|
|
71
|
+
let tagEndToken;
|
|
72
|
+
autoescape.expressions = parser.parse((_, token, tokens) => {
|
|
73
|
+
if (
|
|
74
|
+
token.type === Types.TAG_START &&
|
|
75
|
+
tokens.nextIf(Types.SYMBOL, "endautoescape")
|
|
76
|
+
) {
|
|
77
|
+
closingTagStartToken = token;
|
|
78
|
+
tagEndToken = tokens.expect(Types.TAG_END);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}).expressions;
|
|
83
|
+
setEndFromToken(autoescape, tagEndToken);
|
|
84
|
+
|
|
85
|
+
autoescape.trimRightAutoescape =
|
|
86
|
+
hasTagEndTokenTrimRight(openingTagEndToken);
|
|
87
|
+
autoescape.trimLeftEndautoescape =
|
|
88
|
+
hasTagStartTokenTrimLeft(closingTagStartToken);
|
|
89
|
+
|
|
90
|
+
return autoescape;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2017 trivago N.V.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import {
|
|
17
|
+
Identifier,
|
|
18
|
+
PrintExpressionStatement
|
|
19
|
+
} from "../../melody-types/index.js";
|
|
20
|
+
import {
|
|
21
|
+
Types,
|
|
22
|
+
setStartFromToken,
|
|
23
|
+
setEndFromToken,
|
|
24
|
+
createNode,
|
|
25
|
+
hasTagStartTokenTrimLeft,
|
|
26
|
+
hasTagEndTokenTrimRight
|
|
27
|
+
} from "../../melody-parser/index.js";
|
|
28
|
+
import { BlockStatement } from "./../types.js";
|
|
29
|
+
|
|
30
|
+
export const BlockParser = {
|
|
31
|
+
name: "block",
|
|
32
|
+
parse(parser, token) {
|
|
33
|
+
const tokens = parser.tokens;
|
|
34
|
+
const nameToken = tokens.expect(Types.SYMBOL);
|
|
35
|
+
|
|
36
|
+
let blockStatement;
|
|
37
|
+
let openingTagEndToken;
|
|
38
|
+
let closingTagStartToken;
|
|
39
|
+
if ((openingTagEndToken = tokens.nextIf(Types.TAG_END))) {
|
|
40
|
+
blockStatement = new BlockStatement(
|
|
41
|
+
createNode(Identifier, nameToken, nameToken.text),
|
|
42
|
+
parser.parse((tokenText, token, tokens) => {
|
|
43
|
+
const result = !!(
|
|
44
|
+
token.type === Types.TAG_START &&
|
|
45
|
+
tokens.nextIf(Types.SYMBOL, "endblock")
|
|
46
|
+
);
|
|
47
|
+
if (result) {
|
|
48
|
+
closingTagStartToken = token;
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}).expressions
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
if (tokens.nextIf(Types.SYMBOL, nameToken.text)) {
|
|
55
|
+
if (tokens.lat(0) !== Types.TAG_END) {
|
|
56
|
+
const unexpectedToken = tokens.next();
|
|
57
|
+
parser.error({
|
|
58
|
+
title: "Block name mismatch",
|
|
59
|
+
pos: unexpectedToken.pos,
|
|
60
|
+
advice:
|
|
61
|
+
unexpectedToken.type == Types.SYMBOL
|
|
62
|
+
? `Expected end of block ${
|
|
63
|
+
nameToken.text
|
|
64
|
+
} but instead found end of block ${
|
|
65
|
+
tokens.la(0).text
|
|
66
|
+
}.`
|
|
67
|
+
: `endblock must be followed by either '%}' or the name of the open block. Found a token of type ${
|
|
68
|
+
Types.ERROR_TABLE[unexpectedToken.type] ||
|
|
69
|
+
unexpectedToken.type
|
|
70
|
+
} instead.`
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
blockStatement = new BlockStatement(
|
|
76
|
+
createNode(Identifier, nameToken, nameToken.text),
|
|
77
|
+
new PrintExpressionStatement(parser.matchExpression())
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
setStartFromToken(blockStatement, token);
|
|
82
|
+
setEndFromToken(blockStatement, tokens.expect(Types.TAG_END));
|
|
83
|
+
|
|
84
|
+
blockStatement.trimRightBlock =
|
|
85
|
+
openingTagEndToken && hasTagEndTokenTrimRight(openingTagEndToken);
|
|
86
|
+
blockStatement.trimLeftEndblock = !!(
|
|
87
|
+
closingTagStartToken &&
|
|
88
|
+
hasTagStartTokenTrimLeft(closingTagStartToken)
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
return blockStatement;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2017 trivago N.V.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import {
|
|
17
|
+
Types,
|
|
18
|
+
setStartFromToken,
|
|
19
|
+
setEndFromToken
|
|
20
|
+
} from "../../melody-parser/index.js";
|
|
21
|
+
import { DoStatement } from "./../types.js";
|
|
22
|
+
|
|
23
|
+
export const DoParser = {
|
|
24
|
+
name: "do",
|
|
25
|
+
parse(parser, token) {
|
|
26
|
+
const tokens = parser.tokens;
|
|
27
|
+
const doStatement = new DoStatement(parser.matchExpression());
|
|
28
|
+
setStartFromToken(doStatement, token);
|
|
29
|
+
setEndFromToken(doStatement, tokens.expect(Types.TAG_END));
|
|
30
|
+
return doStatement;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2017 trivago N.V.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { Node } from "../../melody-types/index.js";
|
|
17
|
+
import {
|
|
18
|
+
Types,
|
|
19
|
+
setStartFromToken,
|
|
20
|
+
setEndFromToken,
|
|
21
|
+
hasTagStartTokenTrimLeft,
|
|
22
|
+
hasTagEndTokenTrimRight
|
|
23
|
+
} from "../../melody-parser/index.js";
|
|
24
|
+
import filter from "lodash/filter.js";
|
|
25
|
+
import { EmbedStatement } from "./../types.js";
|
|
26
|
+
|
|
27
|
+
export const EmbedParser = {
|
|
28
|
+
name: "embed",
|
|
29
|
+
parse(parser, token) {
|
|
30
|
+
const tokens = parser.tokens;
|
|
31
|
+
|
|
32
|
+
const embedStatement = new EmbedStatement(parser.matchExpression());
|
|
33
|
+
|
|
34
|
+
if (tokens.nextIf(Types.SYMBOL, "ignore")) {
|
|
35
|
+
tokens.expect(Types.SYMBOL, "missing");
|
|
36
|
+
embedStatement.ignoreMissing = true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (tokens.nextIf(Types.SYMBOL, "with")) {
|
|
40
|
+
embedStatement.argument = parser.matchExpression();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (tokens.nextIf(Types.SYMBOL, "only")) {
|
|
44
|
+
embedStatement.contextFree = true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
tokens.expect(Types.TAG_END);
|
|
48
|
+
const openingTagEndToken = tokens.la(-1);
|
|
49
|
+
let closingTagStartToken;
|
|
50
|
+
|
|
51
|
+
embedStatement.blocks = filter(
|
|
52
|
+
parser.parse((tokenText, token, tokens) => {
|
|
53
|
+
const result = !!(
|
|
54
|
+
token.type === Types.TAG_START &&
|
|
55
|
+
tokens.nextIf(Types.SYMBOL, "endembed")
|
|
56
|
+
);
|
|
57
|
+
if (result) {
|
|
58
|
+
closingTagStartToken = token;
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}).expressions,
|
|
62
|
+
Node.isBlockStatement
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
setStartFromToken(embedStatement, token);
|
|
66
|
+
setEndFromToken(embedStatement, tokens.expect(Types.TAG_END));
|
|
67
|
+
|
|
68
|
+
embedStatement.trimRightEmbed =
|
|
69
|
+
hasTagEndTokenTrimRight(openingTagEndToken);
|
|
70
|
+
embedStatement.trimLeftEndembed =
|
|
71
|
+
closingTagStartToken &&
|
|
72
|
+
hasTagStartTokenTrimLeft(closingTagStartToken);
|
|
73
|
+
|
|
74
|
+
return embedStatement;
|
|
75
|
+
}
|
|
76
|
+
};
|