@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.
Files changed (63) hide show
  1. package/README.md +5 -4
  2. package/package.json +10 -8
  3. package/src/index.js +53 -53
  4. package/src/melody/melody-code-frame/index.js +44 -0
  5. package/src/melody/melody-code-frame/lineNumbers.js +45 -0
  6. package/src/melody/melody-extension-core/index.js +167 -0
  7. package/src/melody/melody-extension-core/operators.js +414 -0
  8. package/src/melody/melody-extension-core/parser/autoescape.js +92 -0
  9. package/src/melody/melody-extension-core/parser/block.js +93 -0
  10. package/src/melody/melody-extension-core/parser/do.js +32 -0
  11. package/src/melody/melody-extension-core/parser/embed.js +76 -0
  12. package/src/melody/melody-extension-core/parser/extends.js +35 -0
  13. package/src/melody/melody-extension-core/parser/filter.js +63 -0
  14. package/src/melody/melody-extension-core/parser/flush.js +33 -0
  15. package/src/melody/melody-extension-core/parser/for.js +110 -0
  16. package/src/melody/melody-extension-core/parser/from.js +63 -0
  17. package/src/melody/melody-extension-core/parser/if.js +100 -0
  18. package/src/melody/melody-extension-core/parser/import.js +44 -0
  19. package/src/melody/melody-extension-core/parser/include.js +48 -0
  20. package/src/melody/melody-extension-core/parser/macro.js +94 -0
  21. package/src/melody/melody-extension-core/parser/mount.js +147 -0
  22. package/src/melody/melody-extension-core/parser/set.js +103 -0
  23. package/src/melody/melody-extension-core/parser/spaceless.js +56 -0
  24. package/src/melody/melody-extension-core/parser/use.js +58 -0
  25. package/src/melody/melody-extension-core/types.js +402 -0
  26. package/src/melody/melody-extension-core/visitors/filters.js +146 -0
  27. package/src/melody/melody-extension-core/visitors/for.js +391 -0
  28. package/src/melody/melody-extension-core/visitors/functions.js +100 -0
  29. package/src/melody/melody-extension-core/visitors/tests.js +127 -0
  30. package/src/melody/melody-parser/Associativity.js +17 -0
  31. package/src/melody/melody-parser/CharStream.js +82 -0
  32. package/src/melody/melody-parser/GenericMultiTagParser.js +66 -0
  33. package/src/melody/melody-parser/GenericTagParser.js +53 -0
  34. package/src/melody/melody-parser/Lexer.js +665 -0
  35. package/src/melody/melody-parser/Parser.js +954 -0
  36. package/src/melody/melody-parser/TokenStream.js +183 -0
  37. package/src/melody/melody-parser/TokenTypes.js +74 -0
  38. package/src/melody/melody-parser/elementInfo.js +43 -0
  39. package/src/melody/melody-parser/index.js +94 -0
  40. package/src/melody/melody-parser/util.js +100 -0
  41. package/src/melody/melody-traverse/Binding.js +61 -0
  42. package/src/melody/melody-traverse/Path.js +427 -0
  43. package/src/melody/melody-traverse/Scope.js +218 -0
  44. package/src/melody/melody-traverse/TraversalContext.js +166 -0
  45. package/src/melody/melody-traverse/index.js +21 -0
  46. package/src/melody/melody-traverse/traverse.js +48 -0
  47. package/src/melody/melody-traverse/visitors.js +112 -0
  48. package/src/melody/melody-types/index.js +506 -0
  49. package/src/parser.js +7 -2
  50. package/src/print/Attribute.js +1 -1
  51. package/src/print/BinaryExpression.js +2 -2
  52. package/src/print/BlockStatement.js +1 -1
  53. package/src/print/CallExpression.js +1 -1
  54. package/src/print/ExpressionStatement.js +1 -1
  55. package/src/print/FilterExpression.js +1 -1
  56. package/src/print/GenericTwigTag.js +1 -1
  57. package/src/print/IfStatement.js +1 -1
  58. package/src/print/ObjectProperty.js +1 -1
  59. package/src/print/SetStatement.js +1 -1
  60. package/src/print/SwitchTag.js +1 -1
  61. package/src/print/UnarySubclass.js +1 -1
  62. package/src/util/printFunctions.js +1 -1
  63. package/src/util/publicFunctions.js +1 -1
@@ -0,0 +1,954 @@
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 * as n from "../melody-types/index.js";
17
+ import * as Types from "./TokenTypes.js";
18
+ import { LEFT, RIGHT } from "./Associativity.js";
19
+ import {
20
+ setStartFromToken,
21
+ setEndFromToken,
22
+ setMarkFromToken,
23
+ copyStart,
24
+ copyEnd,
25
+ copyLoc,
26
+ createNode
27
+ } from "./util.js";
28
+ import { GenericTagParser } from "./GenericTagParser.js";
29
+ import { createMultiTagParser } from "./GenericMultiTagParser.js";
30
+ import { voidElements } from "./elementInfo.js";
31
+ import * as he from "he";
32
+
33
+ /**
34
+ * @typedef {Object} UnaryOperator
35
+ * @property {string} text - The text representation of the unary operator.
36
+ * @property {number} precedence - The precedence of the unary operator.
37
+ * @property {Function} createNode - A function to create a node for the unary operator.
38
+ */
39
+
40
+ /**
41
+ * @typedef {Object} BinaryOperator
42
+ * @property {string} text - The text representation of the binary operator.
43
+ * @property {number} precedence - The precedence of the binary operator.
44
+ * @property {Function} createNode - A function to create a node for the binary operator.
45
+ * @property {('LEFT'|'RIGHT')} associativity - The associativity of the binary operator.
46
+ * @property {Function} parse - A function to parse the binary operator.
47
+ */
48
+
49
+ const UNARY = Symbol("UNARY");
50
+ const BINARY = Symbol("BINARY");
51
+ const TAG = Symbol("TAG");
52
+ const TEST = Symbol("TEST");
53
+
54
+ export default class Parser {
55
+ constructor(tokenStream, options) {
56
+ this.tokens = tokenStream;
57
+ this[UNARY] = {};
58
+ this[BINARY] = {};
59
+ this[TAG] = {};
60
+ this[TEST] = {};
61
+ this.options = Object.assign(
62
+ {},
63
+ {
64
+ ignoreComments: true,
65
+ ignoreHtmlComments: true,
66
+ ignoreDeclarations: true,
67
+ decodeEntities: true,
68
+ preserveSourceLiterally: false,
69
+ allowUnknownTags: false,
70
+ multiTags: {} // e.g. { "nav": ["endnav"], "switch": ["case", "default", "endswitch"]}
71
+ },
72
+ options
73
+ );
74
+ // If there are custom multi tags, then we allow all custom tags
75
+ if (Object.keys(this.options.multiTags).length > 0) {
76
+ this.options.allowUnknownTags = true;
77
+ }
78
+ }
79
+
80
+ applyExtension(ext) {
81
+ if (ext.tags) {
82
+ for (const tag of ext.tags) {
83
+ this.addTag(tag);
84
+ }
85
+ }
86
+ if (ext.unaryOperators) {
87
+ for (const op of ext.unaryOperators) {
88
+ this.addUnaryOperator(op);
89
+ }
90
+ }
91
+ if (ext.binaryOperators) {
92
+ for (const op of ext.binaryOperators) {
93
+ this.addBinaryOperator(op);
94
+ }
95
+ }
96
+ if (ext.tests) {
97
+ for (const test of ext.tests) {
98
+ this.addTest(test);
99
+ }
100
+ }
101
+ }
102
+
103
+ /**
104
+ * @param {UnaryOperator} op
105
+ */
106
+ addUnaryOperator(op) {
107
+ this[UNARY][op.text] = op;
108
+ return this;
109
+ }
110
+
111
+ /**
112
+ * @param {BinaryOperator} op
113
+ */
114
+ addBinaryOperator(op) {
115
+ this[BINARY][op.text] = op;
116
+ return this;
117
+ }
118
+
119
+ addTag(tag) {
120
+ this[TAG][tag.name] = tag;
121
+ return this;
122
+ }
123
+
124
+ addTest(test) {
125
+ this[TEST][test.text] = test;
126
+ }
127
+
128
+ hasTest(test) {
129
+ return !!this[TEST][test];
130
+ }
131
+
132
+ getTest(test) {
133
+ return this[TEST][test];
134
+ }
135
+
136
+ isUnary(token) {
137
+ return token.type === Types.OPERATOR && !!this[UNARY][token.text];
138
+ }
139
+
140
+ getBinaryOperator(token) {
141
+ return token.type === Types.OPERATOR && this[BINARY][token.text];
142
+ }
143
+
144
+ parse(test = null) {
145
+ const tokens = this.tokens;
146
+ let p = setStartFromToken(new n.SequenceExpression(), tokens.la(0));
147
+ while (!tokens.test(Types.EOF)) {
148
+ const token = tokens.next();
149
+ if (!p) {
150
+ p = setStartFromToken(new n.SequenceExpression(), token);
151
+ }
152
+ if (test && test(tokens.la(0).text, token, tokens)) {
153
+ setEndFromToken(p, token);
154
+ return p;
155
+ }
156
+ switch (token.type) {
157
+ case Types.EXPRESSION_START: {
158
+ const expression = this.matchExpression();
159
+ const statement = new n.PrintExpressionStatement(
160
+ expression
161
+ );
162
+ const endToken = tokens.expect(Types.EXPRESSION_END);
163
+ setStartFromToken(statement, token);
164
+ setEndFromToken(statement, endToken);
165
+ setEndFromToken(p, endToken);
166
+ statement.trimLeft = !!expression.trimLeft;
167
+ statement.trimRight = !!expression.trimRight;
168
+ p.add(statement);
169
+
170
+ break;
171
+ }
172
+ case Types.TAG_START:
173
+ p.add(this.matchTag());
174
+ break;
175
+ case Types.TEXT: {
176
+ const textStringLiteral = createNode(
177
+ n.StringLiteral,
178
+ token,
179
+ token.text
180
+ );
181
+ const textTextStatement = createNode(
182
+ n.PrintTextStatement,
183
+ token,
184
+ textStringLiteral
185
+ );
186
+ p.add(textTextStatement);
187
+ break;
188
+ }
189
+ case Types.ENTITY: {
190
+ const entityStringLiteral = createNode(
191
+ n.StringLiteral,
192
+ token,
193
+ !this.options.decodeEntities ||
194
+ this.options.preserveSourceLiterally
195
+ ? token.text
196
+ : he.decode(token.text)
197
+ );
198
+ const entityTextStatement = createNode(
199
+ n.PrintTextStatement,
200
+ token,
201
+ entityStringLiteral
202
+ );
203
+ p.add(entityTextStatement);
204
+ break;
205
+ }
206
+ case Types.ELEMENT_START:
207
+ p.add(this.matchElement());
208
+ break;
209
+ case Types.DECLARATION_START: {
210
+ const declarationNode = this.matchDeclaration();
211
+ if (!this.options.ignoreDeclarations) {
212
+ p.add(declarationNode);
213
+ }
214
+ break;
215
+ }
216
+ case Types.COMMENT:
217
+ if (!this.options.ignoreComments) {
218
+ const stringLiteral = createNode(
219
+ n.StringLiteral,
220
+ token,
221
+ token.text
222
+ );
223
+ const twigComment = createNode(
224
+ n.TwigComment,
225
+ token,
226
+ stringLiteral
227
+ );
228
+ p.add(twigComment);
229
+ }
230
+ break;
231
+ case Types.HTML_COMMENT:
232
+ if (!this.options.ignoreHtmlComments) {
233
+ const stringLiteral = createNode(
234
+ n.StringLiteral,
235
+ token,
236
+ token.text
237
+ );
238
+ const htmlComment = createNode(
239
+ n.HtmlComment,
240
+ token,
241
+ stringLiteral
242
+ );
243
+ p.add(htmlComment);
244
+ }
245
+ break;
246
+ }
247
+ }
248
+ return p;
249
+ }
250
+
251
+ /**
252
+ * e.g., <!DOCTYPE html>
253
+ */
254
+ matchDeclaration() {
255
+ const tokens = this.tokens;
256
+ const declarationStartToken = tokens.la(-1);
257
+ let declarationType = null;
258
+ let currentToken = null;
259
+
260
+ if (!(declarationType = tokens.nextIf(Types.SYMBOL))) {
261
+ this.error({
262
+ title: "Expected declaration start",
263
+ pos: declarationStartToken.pos,
264
+ advice: "After '<!', an unquoted symbol like DOCTYPE is expected"
265
+ });
266
+ }
267
+
268
+ const declaration = new n.Declaration(declarationType.text);
269
+ while ((currentToken = tokens.next())) {
270
+ if (currentToken.type === Types.SYMBOL) {
271
+ const symbol = createNode(
272
+ n.Identifier,
273
+ currentToken,
274
+ currentToken.text
275
+ );
276
+ declaration.parts.push(symbol);
277
+ } else if (currentToken.type === Types.STRING_START) {
278
+ const stringToken = tokens.expect(Types.STRING);
279
+ declaration.parts.push(
280
+ createNode(n.StringLiteral, stringToken, stringToken.text)
281
+ );
282
+ tokens.expect(Types.STRING_END);
283
+ } else if (currentToken.type === Types.EXPRESSION_START) {
284
+ const expression = this.matchExpression();
285
+ declaration.parts.push(
286
+ copyLoc(
287
+ new n.PrintExpressionStatement(expression),
288
+ expression
289
+ )
290
+ );
291
+ tokens.expect(Types.EXPRESSION_END);
292
+ } else if (currentToken.type === Types.ELEMENT_END) {
293
+ break;
294
+ } else {
295
+ this.error({
296
+ title: "Expected string, symbol, or expression",
297
+ pos: currentToken.pos,
298
+ advice: "Only strings or symbols can be part of a declaration"
299
+ });
300
+ }
301
+ }
302
+ setStartFromToken(declaration, declarationStartToken);
303
+ setEndFromToken(declaration, currentToken);
304
+
305
+ return declaration;
306
+ }
307
+
308
+ /**
309
+ * matchElement = '<' SYMBOL attributes* '/'? '>' (children)* '<' '/' SYMBOL '>'
310
+ * attributes = SYMBOL '=' (matchExpression | matchString)
311
+ * | matchExpression
312
+ */
313
+ matchElement() {
314
+ const tokens = this.tokens;
315
+ const elementNameToken = tokens.la(0);
316
+ const tagStartToken = tokens.la(-1);
317
+ let elementName;
318
+ if (!(elementName = tokens.nextIf(Types.SYMBOL))) {
319
+ this.error({
320
+ title: "Expected element start",
321
+ pos: elementNameToken.pos,
322
+ advice:
323
+ tokens.lat(0) === Types.SLASH
324
+ ? `Unexpected closing "${
325
+ tokens.la(1).text
326
+ }" tag. Seems like your DOM is out of control.`
327
+ : "Expected an element to start"
328
+ });
329
+ }
330
+
331
+ const element = new n.Element(elementName.text);
332
+
333
+ this.matchAttributes(element, tokens);
334
+
335
+ if (tokens.nextIf(Types.SLASH)) {
336
+ tokens.expect(Types.ELEMENT_END);
337
+ element.selfClosing = true;
338
+ } else {
339
+ tokens.expect(Types.ELEMENT_END);
340
+ if (voidElements[elementName.text]) {
341
+ element.selfClosing = true;
342
+ } else {
343
+ element.children = this.parse((_, token, tokens) => {
344
+ if (
345
+ token.type === Types.ELEMENT_START &&
346
+ tokens.lat(0) === Types.SLASH
347
+ ) {
348
+ const name = tokens.la(1);
349
+ if (
350
+ name.type === Types.SYMBOL &&
351
+ name.text === elementName.text
352
+ ) {
353
+ tokens.next(); // SLASH
354
+ tokens.next(); // elementName
355
+ tokens.expect(Types.ELEMENT_END);
356
+ return true;
357
+ }
358
+ }
359
+ return false;
360
+ }).expressions;
361
+ }
362
+ }
363
+
364
+ setStartFromToken(element, tagStartToken);
365
+ setEndFromToken(element, tokens.la(-1));
366
+ setMarkFromToken(element, "elementNameLoc", elementNameToken);
367
+
368
+ return element;
369
+ }
370
+
371
+ matchAttributes(element, tokens) {
372
+ while (
373
+ tokens.lat(0) !== Types.SLASH &&
374
+ tokens.lat(0) !== Types.ELEMENT_END
375
+ ) {
376
+ const key = tokens.nextIf(Types.SYMBOL);
377
+ if (key) {
378
+ const keyNode = new n.Identifier(key.text);
379
+ setStartFromToken(keyNode, key);
380
+ setEndFromToken(keyNode, key);
381
+
382
+ // match an attribute
383
+ if (tokens.nextIf(Types.ASSIGNMENT)) {
384
+ const start = tokens.expect(Types.STRING_START);
385
+ let canBeString = true;
386
+ const nodes = [];
387
+ let token;
388
+ while (!tokens.test(Types.STRING_END)) {
389
+ if (
390
+ canBeString &&
391
+ (token = tokens.nextIf(Types.STRING))
392
+ ) {
393
+ nodes[nodes.length] = createNode(
394
+ n.StringLiteral,
395
+ token,
396
+ token.text
397
+ );
398
+ canBeString = false;
399
+ } else if (
400
+ (token = tokens.nextIf(Types.EXPRESSION_START))
401
+ ) {
402
+ nodes[nodes.length] = this.matchExpression();
403
+ tokens.expect(Types.EXPRESSION_END);
404
+ canBeString = true;
405
+ } else {
406
+ break;
407
+ }
408
+ }
409
+ tokens.expect(Types.STRING_END);
410
+ if (!nodes.length) {
411
+ const node = createNode(n.StringLiteral, start, "");
412
+ nodes.push(node);
413
+ }
414
+
415
+ let expr = nodes[0];
416
+ for (let i = 1, len = nodes.length; i < len; i++) {
417
+ const { line, column } = expr.loc.start;
418
+ expr = new n.BinaryConcatExpression(expr, nodes[i]);
419
+ expr.loc.start.line = line;
420
+ expr.loc.start.column = column;
421
+ copyEnd(expr, expr.right);
422
+ }
423
+ // Distinguish between BinaryConcatExpression generated by
424
+ // this Parser (implicit before parsing), and those that the
425
+ // user wrote explicitly.
426
+ if (nodes.length > 1) {
427
+ expr.wasImplicitConcatenation = true;
428
+ }
429
+ const attr = new n.Attribute(keyNode, expr);
430
+ copyStart(attr, keyNode);
431
+ copyEnd(attr, expr);
432
+ element.attributes.push(attr);
433
+ } else {
434
+ element.attributes.push(
435
+ copyLoc(new n.Attribute(keyNode), keyNode)
436
+ );
437
+ }
438
+ } else if (tokens.nextIf(Types.EXPRESSION_START)) {
439
+ element.attributes.push(this.matchExpression());
440
+ tokens.expect(Types.EXPRESSION_END);
441
+ } else {
442
+ this.error({
443
+ title: "Invalid token",
444
+ pos: tokens.la(0).pos,
445
+ advice: "A tag must consist of attributes or expressions. Twig Tags are not allowed."
446
+ });
447
+ }
448
+ }
449
+ }
450
+
451
+ error(options, metadata = {}) {
452
+ this.tokens.error(
453
+ options.title,
454
+ options.pos,
455
+ options.advice,
456
+ 1,
457
+ metadata
458
+ );
459
+ }
460
+
461
+ getGenericParserFor(tagName) {
462
+ if (this.options.multiTags[tagName]) {
463
+ return createMultiTagParser(
464
+ tagName,
465
+ this.options.multiTags[tagName]
466
+ );
467
+ }
468
+ return GenericTagParser;
469
+ }
470
+
471
+ matchTag() {
472
+ const tokens = this.tokens;
473
+ const tagStartToken = tokens.la(-1);
474
+
475
+ const tag = tokens.expect(Types.SYMBOL);
476
+ let parser = this[TAG][tag.text];
477
+ let isUsingGenericParser = false;
478
+ if (!parser) {
479
+ if (this.options.allowUnknownTags) {
480
+ parser = this.getGenericParserFor(tag.text);
481
+ isUsingGenericParser = true;
482
+ } else {
483
+ tokens.error(
484
+ `Unknown tag "${tag.text}"`,
485
+ tag.pos,
486
+ `Expected a known tag such as\n- ${Object.getOwnPropertyNames(
487
+ this[TAG]
488
+ ).join("\n- ")}`,
489
+ tag.length
490
+ );
491
+ }
492
+ }
493
+
494
+ const result = parser.parse(this, tag);
495
+ const tagEndToken = tokens.la(-1);
496
+ if (!isUsingGenericParser) {
497
+ result.trimLeft = tagStartToken.text.endsWith("-");
498
+ result.trimRight = tagEndToken.text.startsWith("-");
499
+ }
500
+
501
+ setStartFromToken(result, tagStartToken);
502
+ setEndFromToken(result, tagEndToken);
503
+ setMarkFromToken(result, "tagNameLoc", tag);
504
+
505
+ return result;
506
+ }
507
+
508
+ matchExpression(precedence = 0) {
509
+ const tokens = this.tokens;
510
+ const exprStartToken = tokens.la(0);
511
+ let token;
512
+ let op;
513
+ let trimLeft = false;
514
+
515
+ // Check for {{- (trim preceding whitespace)
516
+ if (
517
+ tokens.la(-1).type === Types.EXPRESSION_START &&
518
+ tokens.la(-1).text.endsWith("-")
519
+ ) {
520
+ trimLeft = true;
521
+ }
522
+
523
+ let expr = this.getPrimary();
524
+ while (
525
+ (token = tokens.la(0)) &&
526
+ token.type !== Types.EOF &&
527
+ (op = this.getBinaryOperator(token)) &&
528
+ op.precedence >= precedence
529
+ ) {
530
+ const opToken = tokens.next(); // consume the operator
531
+ if (op.parse) {
532
+ expr = op.parse(this, opToken, expr);
533
+ } else {
534
+ const expr1 = this.matchExpression(
535
+ op.associativity === LEFT
536
+ ? op.precedence + 1
537
+ : op.precedence
538
+ );
539
+ expr = op.createNode(token, expr, expr1);
540
+ }
541
+ token = tokens.la(0);
542
+ }
543
+
544
+ let result = expr;
545
+ if (precedence === 0) {
546
+ setEndFromToken(expr, tokens.la(-1));
547
+ result = this.matchConditionalExpression(expr);
548
+ // Update the local token variable because the stream pointer already advanced.
549
+ token = tokens.la(0);
550
+ }
551
+
552
+ // Check for -}} (trim following whitespace)
553
+ if (token.type === Types.EXPRESSION_END && token.text.startsWith("-")) {
554
+ result.trimRight = true;
555
+ }
556
+ if (trimLeft) {
557
+ result.trimLeft = trimLeft;
558
+ }
559
+
560
+ const exprEndToken = tokens.la(-1);
561
+ setStartFromToken(result, exprStartToken);
562
+ setEndFromToken(result, exprEndToken);
563
+
564
+ return result;
565
+ }
566
+
567
+ getPrimary() {
568
+ const tokens = this.tokens;
569
+ const token = tokens.la(0);
570
+ if (this.isUnary(token)) {
571
+ const op = this[UNARY][token.text];
572
+ tokens.next(); // consume operator
573
+ const expr = this.matchExpression(op.precedence);
574
+ return this.matchPostfixExpression(op.createNode(token, expr));
575
+ } else if (tokens.test(Types.LPAREN)) {
576
+ tokens.next(); // consume '('
577
+ const expr = this.matchExpression();
578
+ tokens.expect(Types.RPAREN);
579
+ return this.matchPostfixExpression(expr);
580
+ }
581
+
582
+ return this.matchPrimaryExpression();
583
+ }
584
+
585
+ matchPrimaryExpression() {
586
+ const tokens = this.tokens;
587
+ const token = tokens.la(0);
588
+ let node;
589
+ switch (token.type) {
590
+ case Types.NULL:
591
+ node = createNode(n.NullLiteral, tokens.next());
592
+ break;
593
+ case Types.FALSE:
594
+ node = createNode(n.BooleanLiteral, tokens.next(), false);
595
+ break;
596
+ case Types.TRUE:
597
+ node = createNode(n.BooleanLiteral, tokens.next(), true);
598
+ break;
599
+ case Types.SYMBOL:
600
+ tokens.next();
601
+ if (tokens.test(Types.LPAREN)) {
602
+ // SYMBOL '(' arguments* ')'
603
+ node = new n.CallExpression(
604
+ createNode(n.Identifier, token, token.text),
605
+ this.matchArguments()
606
+ );
607
+ copyStart(node, node.callee);
608
+ setEndFromToken(node, tokens.la(-1)); // ')'
609
+ } else {
610
+ node = createNode(n.Identifier, token, token.text);
611
+ }
612
+ break;
613
+ case Types.NUMBER:
614
+ node = createNode(
615
+ n.NumericLiteral,
616
+ token,
617
+ Number(tokens.next())
618
+ );
619
+ break;
620
+ case Types.STRING_START:
621
+ node = this.matchStringExpression();
622
+ break;
623
+ // potentially missing: OPERATOR type
624
+ default:
625
+ if (token.type === Types.LBRACE) {
626
+ node = this.matchArray();
627
+ } else if (token.type === Types.LBRACKET) {
628
+ node = this.matchMap();
629
+ } else {
630
+ this.error(
631
+ {
632
+ title:
633
+ 'Unexpected token "' +
634
+ token.type +
635
+ '" of value "' +
636
+ token.text +
637
+ '"',
638
+ pos: token.pos
639
+ },
640
+ {
641
+ errorType: "UNEXPECTED_TOKEN",
642
+ tokenText: token.text,
643
+ tokenType: token.type
644
+ }
645
+ );
646
+ }
647
+ break;
648
+ }
649
+
650
+ return this.matchPostfixExpression(node);
651
+ }
652
+
653
+ matchStringExpression() {
654
+ let canBeString = true;
655
+ let token;
656
+ const tokens = this.tokens;
657
+ const nodes = [];
658
+ const stringStart = tokens.expect(Types.STRING_START);
659
+ while (!tokens.test(Types.STRING_END)) {
660
+ if (canBeString && (token = tokens.nextIf(Types.STRING))) {
661
+ nodes[nodes.length] = createNode(
662
+ n.StringLiteral,
663
+ token,
664
+ token.text
665
+ );
666
+ canBeString = false;
667
+ } else if ((token = tokens.nextIf(Types.INTERPOLATION_START))) {
668
+ nodes[nodes.length] = this.matchExpression();
669
+ tokens.expect(Types.INTERPOLATION_END);
670
+ canBeString = true;
671
+ } else {
672
+ break;
673
+ }
674
+ }
675
+ const stringEnd = tokens.expect(Types.STRING_END);
676
+
677
+ if (!nodes.length) {
678
+ return setEndFromToken(
679
+ createNode(n.StringLiteral, stringStart, ""),
680
+ stringEnd
681
+ );
682
+ }
683
+
684
+ let expr = nodes[0];
685
+ for (let i = 1, len = nodes.length; i < len; i++) {
686
+ const { line, column } = expr.loc.start;
687
+ expr = new n.BinaryConcatExpression(expr, nodes[i]);
688
+ expr.loc.start.line = line;
689
+ expr.loc.start.column = column;
690
+ copyEnd(expr, expr.right);
691
+ }
692
+
693
+ if (nodes.length > 1) {
694
+ expr.wasImplicitConcatenation = true;
695
+ }
696
+
697
+ setStartFromToken(expr, stringStart);
698
+ setEndFromToken(expr, stringEnd);
699
+
700
+ return expr;
701
+ }
702
+
703
+ /**
704
+ * @param {Node} test
705
+ */
706
+ matchConditionalExpression(test) {
707
+ const tokens = this.tokens;
708
+ let condition = test;
709
+ let consequent;
710
+ let alternate;
711
+ while (tokens.nextIf(Types.QUESTION_MARK)) {
712
+ if (!tokens.nextIf(Types.COLON)) {
713
+ consequent = this.matchExpression();
714
+ if (tokens.nextIf(Types.COLON)) {
715
+ alternate = this.matchExpression();
716
+ } else {
717
+ alternate = null;
718
+ }
719
+ } else {
720
+ consequent = null;
721
+ alternate = this.matchExpression();
722
+ }
723
+ const { line, column } = condition.loc.start;
724
+ condition = new n.ConditionalExpression(
725
+ condition,
726
+ consequent,
727
+ alternate
728
+ );
729
+ condition.loc.start = { line, column };
730
+ copyEnd(condition, alternate || consequent);
731
+ }
732
+ return condition;
733
+ }
734
+
735
+ matchArray() {
736
+ const tokens = this.tokens;
737
+ const array = new n.ArrayExpression();
738
+ const start = tokens.expect(Types.LBRACE);
739
+ setStartFromToken(array, start);
740
+ while (!tokens.test(Types.RBRACE) && !tokens.test(Types.EOF)) {
741
+ array.elements.push(this.matchExpression());
742
+ if (!tokens.test(Types.RBRACE)) {
743
+ tokens.expect(Types.COMMA);
744
+ // support trailing commas
745
+ if (tokens.test(Types.RBRACE)) {
746
+ break;
747
+ }
748
+ }
749
+ }
750
+ setEndFromToken(array, tokens.expect(Types.RBRACE));
751
+ return array;
752
+ }
753
+
754
+ matchMap() {
755
+ const tokens = this.tokens;
756
+ let token;
757
+ const obj = new n.ObjectExpression();
758
+ const startToken = tokens.expect(Types.LBRACKET);
759
+ setStartFromToken(obj, startToken);
760
+ while (!tokens.test(Types.RBRACKET) && !tokens.test(Types.EOF)) {
761
+ let computed = false;
762
+ let key;
763
+ if (tokens.test(Types.STRING_START)) {
764
+ key = this.matchStringExpression();
765
+ if (!n.is(key, "StringLiteral")) {
766
+ computed = true;
767
+ }
768
+ } else if ((token = tokens.nextIf(Types.SYMBOL))) {
769
+ key = createNode(n.Identifier, token, token.text);
770
+ } else if ((token = tokens.nextIf(Types.NUMBER))) {
771
+ key = createNode(n.NumericLiteral, token, Number(token.text));
772
+ } else if (tokens.test(Types.LPAREN)) {
773
+ key = this.matchExpression();
774
+ computed = true;
775
+ } else {
776
+ this.error({
777
+ title: "Invalid map key",
778
+ pos: tokens.la(0).pos,
779
+ advice:
780
+ "Key must be a string, symbol or a number but was " +
781
+ tokens.next()
782
+ });
783
+ }
784
+ tokens.expect(Types.COLON);
785
+ const value = this.matchExpression();
786
+ const prop = new n.ObjectProperty(key, value, computed);
787
+ copyStart(prop, key);
788
+ copyEnd(prop, value);
789
+ obj.properties.push(prop);
790
+ if (!tokens.test(Types.RBRACKET)) {
791
+ tokens.expect(Types.COMMA);
792
+ // support trailing comma
793
+ if (tokens.test(Types.RBRACKET)) {
794
+ break;
795
+ }
796
+ }
797
+ }
798
+ setEndFromToken(obj, tokens.expect(Types.RBRACKET));
799
+ return obj;
800
+ }
801
+
802
+ matchPostfixExpression(expr) {
803
+ const tokens = this.tokens;
804
+ let node = expr;
805
+ while (!tokens.test(Types.EOF)) {
806
+ if (tokens.test(Types.DOT) || tokens.test(Types.LBRACE)) {
807
+ node = this.matchSubscriptExpression(node);
808
+ } else if (tokens.test(Types.PIPE)) {
809
+ tokens.next();
810
+ node = this.matchFilterExpression(node);
811
+ } else {
812
+ break;
813
+ }
814
+ }
815
+
816
+ return node;
817
+ }
818
+
819
+ matchSubscriptExpression(node) {
820
+ const tokens = this.tokens;
821
+ const op = tokens.next();
822
+ if (op.type === Types.DOT) {
823
+ const token = tokens.next();
824
+ let computed = false;
825
+ let property;
826
+ if (token.type === Types.SYMBOL) {
827
+ property = createNode(n.Identifier, token, token.text);
828
+ } else if (token.type === Types.NUMBER) {
829
+ property = createNode(
830
+ n.NumericLiteral,
831
+ token,
832
+ Number(token.text)
833
+ );
834
+ computed = true;
835
+ } else {
836
+ this.error({
837
+ title: "Invalid token",
838
+ pos: token.pos,
839
+ advice:
840
+ "Expected number or symbol, found " + token + " instead"
841
+ });
842
+ }
843
+
844
+ const memberExpr = new n.MemberExpression(node, property, computed);
845
+ copyStart(memberExpr, node);
846
+ copyEnd(memberExpr, property);
847
+ if (tokens.test(Types.LPAREN)) {
848
+ const callExpr = new n.CallExpression(
849
+ memberExpr,
850
+ this.matchArguments()
851
+ );
852
+ copyStart(callExpr, memberExpr);
853
+ setEndFromToken(callExpr, tokens.la(-1));
854
+ return callExpr;
855
+ }
856
+ return memberExpr;
857
+ }
858
+ let arg;
859
+ let start;
860
+ if (tokens.test(Types.COLON)) {
861
+ // slice
862
+ tokens.next();
863
+ start = null;
864
+ } else {
865
+ arg = this.matchExpression();
866
+ if (tokens.test(Types.COLON)) {
867
+ start = arg;
868
+ arg = null;
869
+ tokens.next();
870
+ }
871
+ }
872
+
873
+ if (arg) {
874
+ return setEndFromToken(
875
+ copyStart(new n.MemberExpression(node, arg, true), node),
876
+ tokens.expect(Types.RBRACE)
877
+ );
878
+ }
879
+ // slice
880
+ const result = new n.SliceExpression(
881
+ node,
882
+ start,
883
+ tokens.test(Types.RBRACE) ? null : this.matchExpression()
884
+ );
885
+ copyStart(result, node);
886
+ setEndFromToken(result, tokens.expect(Types.RBRACE));
887
+ return result;
888
+ }
889
+
890
+ matchFilterExpression(node) {
891
+ const tokens = this.tokens;
892
+ let target = node;
893
+ while (!tokens.test(Types.EOF)) {
894
+ const token = tokens.expect(Types.SYMBOL);
895
+ const name = createNode(n.Identifier, token, token.text);
896
+ let args;
897
+ if (tokens.test(Types.LPAREN)) {
898
+ args = this.matchArguments();
899
+ } else {
900
+ args = [];
901
+ }
902
+ const newTarget = new n.FilterExpression(target, name, args);
903
+ copyStart(newTarget, target);
904
+ if (newTarget.arguments.length) {
905
+ copyEnd(
906
+ newTarget,
907
+ newTarget.arguments[newTarget.arguments.length - 1]
908
+ );
909
+ } else {
910
+ copyEnd(newTarget, target);
911
+ }
912
+ target = newTarget;
913
+
914
+ if (!tokens.test(Types.PIPE) || tokens.test(Types.EOF)) {
915
+ break;
916
+ }
917
+
918
+ tokens.next(); // consume '|'
919
+ }
920
+ return target;
921
+ }
922
+
923
+ matchArguments() {
924
+ const tokens = this.tokens;
925
+ const args = [];
926
+ tokens.expect(Types.LPAREN);
927
+ while (!tokens.test(Types.RPAREN) && !tokens.test(Types.EOF)) {
928
+ if (
929
+ tokens.test(Types.SYMBOL) &&
930
+ tokens.lat(1) === Types.ASSIGNMENT
931
+ ) {
932
+ const name = tokens.next();
933
+ tokens.next();
934
+ const value = this.matchExpression();
935
+ const arg = new n.NamedArgumentExpression(
936
+ createNode(n.Identifier, name, name.text),
937
+ value
938
+ );
939
+ copyEnd(arg, value);
940
+ args.push(arg);
941
+ } else {
942
+ args.push(this.matchExpression());
943
+ }
944
+
945
+ if (!tokens.test(Types.COMMA)) {
946
+ tokens.expect(Types.RPAREN);
947
+ return args;
948
+ }
949
+ tokens.expect(Types.COMMA);
950
+ }
951
+ tokens.expect(Types.RPAREN);
952
+ return args;
953
+ }
954
+ }