@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,35 @@
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 { ExtendsStatement } from "./../types.js";
22
+
23
+ export const ExtendsParser = {
24
+ name: "extends",
25
+ parse(parser, token) {
26
+ const tokens = parser.tokens;
27
+
28
+ const extendsStatement = new ExtendsStatement(parser.matchExpression());
29
+
30
+ setStartFromToken(extendsStatement, token);
31
+ setEndFromToken(extendsStatement, tokens.expect(Types.TAG_END));
32
+
33
+ return extendsStatement;
34
+ }
35
+ };
@@ -0,0 +1,63 @@
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 { Identifier } from "../../melody-types/index.js";
17
+ import {
18
+ Types,
19
+ setStartFromToken,
20
+ setEndFromToken,
21
+ createNode,
22
+ hasTagStartTokenTrimLeft,
23
+ hasTagEndTokenTrimRight
24
+ } from "../../melody-parser/index.js";
25
+ import { FilterBlockStatement } from "./../types.js";
26
+
27
+ export const FilterParser = {
28
+ name: "filter",
29
+ parse(parser, token) {
30
+ const tokens = parser.tokens;
31
+ const ref = createNode(Identifier, token, "filter");
32
+ const filterExpression = parser.matchFilterExpression(ref);
33
+ tokens.expect(Types.TAG_END);
34
+ const openingTagEndToken = tokens.la(-1);
35
+ let closingTagStartToken;
36
+
37
+ const body = parser.parse((text, token, tokens) => {
38
+ const result =
39
+ token.type === Types.TAG_START &&
40
+ tokens.nextIf(Types.SYMBOL, "endfilter");
41
+
42
+ if (result) {
43
+ closingTagStartToken = token;
44
+ }
45
+ return result;
46
+ }).expressions;
47
+
48
+ const filterBlockStatement = new FilterBlockStatement(
49
+ filterExpression,
50
+ body
51
+ );
52
+ setStartFromToken(filterBlockStatement, token);
53
+ setEndFromToken(filterBlockStatement, tokens.expect(Types.TAG_END));
54
+
55
+ filterBlockStatement.trimRightFilter =
56
+ hasTagEndTokenTrimRight(openingTagEndToken);
57
+ filterBlockStatement.trimLeftEndfilter =
58
+ closingTagStartToken &&
59
+ hasTagStartTokenTrimLeft(closingTagStartToken);
60
+
61
+ return filterBlockStatement;
62
+ }
63
+ };
@@ -0,0 +1,33 @@
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 { FlushStatement } from "./../types.js";
22
+
23
+ export const FlushParser = {
24
+ name: "flush",
25
+ parse(parser, token) {
26
+ const tokens = parser.tokens;
27
+ const flushStatement = new FlushStatement();
28
+
29
+ setStartFromToken(flushStatement, token);
30
+ setEndFromToken(flushStatement, tokens.expect(Types.TAG_END));
31
+ return flushStatement;
32
+ }
33
+ };
@@ -0,0 +1,110 @@
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 { Identifier } from "../../melody-types/index.js";
17
+ import {
18
+ Types,
19
+ setStartFromToken,
20
+ setEndFromToken,
21
+ createNode,
22
+ hasTagStartTokenTrimLeft,
23
+ hasTagEndTokenTrimRight
24
+ } from "../../melody-parser/index.js";
25
+ import { ForStatement } from "./../types.js";
26
+
27
+ export const ForParser = {
28
+ name: "for",
29
+ parse(parser, token) {
30
+ const tokens = parser.tokens;
31
+ const forStatement = new ForStatement();
32
+
33
+ const keyTarget = tokens.expect(Types.SYMBOL);
34
+ if (tokens.nextIf(Types.COMMA)) {
35
+ forStatement.keyTarget = createNode(
36
+ Identifier,
37
+ keyTarget,
38
+ keyTarget.text
39
+ );
40
+ const valueTarget = tokens.expect(Types.SYMBOL);
41
+ forStatement.valueTarget = createNode(
42
+ Identifier,
43
+ valueTarget,
44
+ valueTarget.text
45
+ );
46
+ } else {
47
+ forStatement.keyTarget = null;
48
+ forStatement.valueTarget = createNode(
49
+ Identifier,
50
+ keyTarget,
51
+ keyTarget.text
52
+ );
53
+ }
54
+
55
+ tokens.expect(Types.OPERATOR, "in");
56
+
57
+ forStatement.sequence = parser.matchExpression();
58
+
59
+ if (tokens.nextIf(Types.SYMBOL, "if")) {
60
+ forStatement.condition = parser.matchExpression();
61
+ }
62
+
63
+ tokens.expect(Types.TAG_END);
64
+
65
+ const openingTagEndToken = tokens.la(-1);
66
+ let elseTagStartToken;
67
+ let elseTagEndToken;
68
+
69
+ forStatement.body = parser.parse((tokenText, token, tokens) => {
70
+ const result =
71
+ token.type === Types.TAG_START &&
72
+ (tokens.test(Types.SYMBOL, "else") ||
73
+ tokens.test(Types.SYMBOL, "endfor"));
74
+ if (result && tokens.test(Types.SYMBOL, "else")) {
75
+ elseTagStartToken = token;
76
+ }
77
+ return result;
78
+ });
79
+
80
+ if (tokens.nextIf(Types.SYMBOL, "else")) {
81
+ tokens.expect(Types.TAG_END);
82
+ elseTagEndToken = tokens.la(-1);
83
+ forStatement.otherwise = parser.parse(
84
+ (tokenText, token, tokens) => {
85
+ return (
86
+ token.type === Types.TAG_START &&
87
+ tokens.test(Types.SYMBOL, "endfor")
88
+ );
89
+ }
90
+ );
91
+ }
92
+ const endforTagStartToken = tokens.la(-1);
93
+ tokens.expect(Types.SYMBOL, "endfor");
94
+
95
+ setStartFromToken(forStatement, token);
96
+ setEndFromToken(forStatement, tokens.expect(Types.TAG_END));
97
+
98
+ forStatement.trimRightFor = hasTagEndTokenTrimRight(openingTagEndToken);
99
+ forStatement.trimLeftElse = !!(
100
+ elseTagStartToken && hasTagStartTokenTrimLeft(elseTagStartToken)
101
+ );
102
+ forStatement.trimRightElse = !!(
103
+ elseTagEndToken && hasTagEndTokenTrimRight(elseTagEndToken)
104
+ );
105
+ forStatement.trimLeftEndfor =
106
+ hasTagStartTokenTrimLeft(endforTagStartToken);
107
+
108
+ return forStatement;
109
+ }
110
+ };
@@ -0,0 +1,63 @@
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 { Identifier } from "../../melody-types/index.js";
17
+ import {
18
+ Types,
19
+ setStartFromToken,
20
+ setEndFromToken,
21
+ createNode
22
+ } from "../../melody-parser/index.js";
23
+ import { ImportDeclaration, FromStatement } from "./../types.js";
24
+
25
+ export const FromParser = {
26
+ name: "from",
27
+ parse(parser, token) {
28
+ const tokens = parser.tokens;
29
+ const source = parser.matchExpression();
30
+ const imports = [];
31
+
32
+ tokens.expect(Types.SYMBOL, "import");
33
+
34
+ do {
35
+ const name = tokens.expect(Types.SYMBOL);
36
+
37
+ let alias = name;
38
+ if (tokens.nextIf(Types.SYMBOL, "as")) {
39
+ alias = tokens.expect(Types.SYMBOL);
40
+ }
41
+
42
+ const importDeclaration = new ImportDeclaration(
43
+ createNode(Identifier, name, name.text),
44
+ createNode(Identifier, alias, alias.text)
45
+ );
46
+ setStartFromToken(importDeclaration, name);
47
+ setEndFromToken(importDeclaration, alias);
48
+
49
+ imports.push(importDeclaration);
50
+
51
+ if (!tokens.nextIf(Types.COMMA)) {
52
+ break;
53
+ }
54
+ } while (!tokens.test(Types.EOF));
55
+
56
+ const fromStatement = new FromStatement(source, imports);
57
+
58
+ setStartFromToken(fromStatement, token);
59
+ setEndFromToken(fromStatement, tokens.expect(Types.TAG_END));
60
+
61
+ return fromStatement;
62
+ }
63
+ };
@@ -0,0 +1,100 @@
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
+ import { IfStatement } from "./../types.js";
24
+
25
+ export const IfParser = {
26
+ name: "if",
27
+ parse(parser, token) {
28
+ const tokens = parser.tokens;
29
+ let test = parser.matchExpression();
30
+ let alternate = null;
31
+
32
+ tokens.expect(Types.TAG_END);
33
+ const ifTagEndToken = tokens.la(-1);
34
+
35
+ const ifStatement = new IfStatement(
36
+ test,
37
+ parser.parse(matchConsequent).expressions
38
+ );
39
+
40
+ let elseTagStartToken;
41
+ let elseTagEndToken;
42
+ let elseifTagStartToken;
43
+ let elseifTagEndToken;
44
+
45
+ do {
46
+ if (tokens.nextIf(Types.SYMBOL, "else")) {
47
+ elseTagStartToken = tokens.la(-2);
48
+ tokens.expect(Types.TAG_END);
49
+ elseTagEndToken = tokens.la(-1);
50
+ (alternate || ifStatement).alternate =
51
+ parser.parse(matchAlternate).expressions;
52
+ } else if (tokens.nextIf(Types.SYMBOL, "elseif")) {
53
+ elseifTagStartToken = tokens.la(-2);
54
+ test = parser.matchExpression();
55
+ tokens.expect(Types.TAG_END);
56
+ elseifTagEndToken = tokens.la(-1);
57
+ const consequent = parser.parse(matchConsequent).expressions;
58
+ alternate = (alternate || ifStatement).alternate =
59
+ new IfStatement(test, consequent);
60
+ alternate.trimLeft =
61
+ hasTagStartTokenTrimLeft(elseifTagStartToken);
62
+ alternate.trimRightIf =
63
+ hasTagEndTokenTrimRight(elseifTagEndToken);
64
+ }
65
+
66
+ if (tokens.nextIf(Types.SYMBOL, "endif")) {
67
+ break;
68
+ }
69
+ } while (!tokens.test(Types.EOF));
70
+
71
+ const endifTagStartToken = tokens.la(-2);
72
+
73
+ setStartFromToken(ifStatement, token);
74
+ setEndFromToken(ifStatement, tokens.expect(Types.TAG_END));
75
+
76
+ ifStatement.trimRightIf = hasTagEndTokenTrimRight(ifTagEndToken);
77
+ ifStatement.trimLeftElse = !!(
78
+ elseTagStartToken && hasTagStartTokenTrimLeft(elseTagStartToken)
79
+ );
80
+ ifStatement.trimRightElse = !!(
81
+ elseTagEndToken && hasTagEndTokenTrimRight(elseTagEndToken)
82
+ );
83
+ ifStatement.trimLeftEndif =
84
+ hasTagStartTokenTrimLeft(endifTagStartToken);
85
+
86
+ return ifStatement;
87
+ }
88
+ };
89
+
90
+ function matchConsequent(tokenText, token, tokens) {
91
+ if (token.type === Types.TAG_START) {
92
+ const next = tokens.la(0).text;
93
+ return next === "else" || next === "endif" || next === "elseif";
94
+ }
95
+ return false;
96
+ }
97
+
98
+ function matchAlternate(tokenText, token, tokens) {
99
+ return token.type === Types.TAG_START && tokens.test(Types.SYMBOL, "endif");
100
+ }
@@ -0,0 +1,44 @@
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 { Identifier } from "../../melody-types/index.js";
17
+ import {
18
+ Types,
19
+ setStartFromToken,
20
+ setEndFromToken,
21
+ createNode
22
+ } from "../../melody-parser/index.js";
23
+ import { ImportDeclaration } from "./../types.js";
24
+
25
+ export const ImportParser = {
26
+ name: "import",
27
+ parse(parser, token) {
28
+ const tokens = parser.tokens;
29
+ const source = parser.matchExpression();
30
+
31
+ tokens.expect(Types.SYMBOL, "as");
32
+ const alias = tokens.expect(Types.SYMBOL);
33
+
34
+ const importStatement = new ImportDeclaration(
35
+ source,
36
+ createNode(Identifier, alias, alias.text)
37
+ );
38
+
39
+ setStartFromToken(importStatement, token);
40
+ setEndFromToken(importStatement, tokens.expect(Types.TAG_END));
41
+
42
+ return importStatement;
43
+ }
44
+ };
@@ -0,0 +1,48 @@
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 { IncludeStatement } from "./../types.js";
22
+
23
+ export const IncludeParser = {
24
+ name: "include",
25
+ parse(parser, token) {
26
+ const tokens = parser.tokens;
27
+
28
+ const includeStatement = new IncludeStatement(parser.matchExpression());
29
+
30
+ if (tokens.nextIf(Types.SYMBOL, "ignore")) {
31
+ tokens.expect(Types.SYMBOL, "missing");
32
+ includeStatement.ignoreMissing = true;
33
+ }
34
+
35
+ if (tokens.nextIf(Types.SYMBOL, "with")) {
36
+ includeStatement.argument = parser.matchExpression();
37
+ }
38
+
39
+ if (tokens.nextIf(Types.SYMBOL, "only")) {
40
+ includeStatement.contextFree = true;
41
+ }
42
+
43
+ setStartFromToken(includeStatement, token);
44
+ setEndFromToken(includeStatement, tokens.expect(Types.TAG_END));
45
+
46
+ return includeStatement;
47
+ }
48
+ };
@@ -0,0 +1,94 @@
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 { Identifier } from "../../melody-types/index.js";
17
+ import {
18
+ Types,
19
+ setStartFromToken,
20
+ setEndFromToken,
21
+ createNode,
22
+ hasTagStartTokenTrimLeft,
23
+ hasTagEndTokenTrimRight
24
+ } from "../../melody-parser/index.js";
25
+ import { MacroDeclarationStatement } from "./../types.js";
26
+
27
+ export const MacroParser = {
28
+ name: "macro",
29
+ parse(parser, token) {
30
+ const tokens = parser.tokens;
31
+
32
+ const nameToken = tokens.expect(Types.SYMBOL);
33
+ const args = [];
34
+
35
+ tokens.expect(Types.LPAREN);
36
+ while (!tokens.test(Types.RPAREN) && !tokens.test(Types.EOF)) {
37
+ const arg = tokens.expect(Types.SYMBOL);
38
+ args.push(createNode(Identifier, arg, arg.text));
39
+
40
+ if (!tokens.nextIf(Types.COMMA) && !tokens.test(Types.RPAREN)) {
41
+ // not followed by comma or rparen
42
+ parser.error({
43
+ title: 'Expected comma or ")"',
44
+ pos: tokens.la(0).pos,
45
+ advice: "The argument list of a macro can only consist of parameter names separated by commas."
46
+ });
47
+ }
48
+ }
49
+ tokens.expect(Types.RPAREN);
50
+
51
+ const openingTagEndToken = tokens.la(0);
52
+ let closingTagStartToken;
53
+
54
+ const body = parser.parse((tokenText, token, tokens) => {
55
+ const result = !!(
56
+ token.type === Types.TAG_START &&
57
+ tokens.nextIf(Types.SYMBOL, "endmacro")
58
+ );
59
+ if (result) {
60
+ closingTagStartToken = token;
61
+ }
62
+ return result;
63
+ });
64
+
65
+ if (tokens.test(Types.SYMBOL)) {
66
+ const nameEndToken = tokens.next();
67
+ if (nameToken.text !== nameEndToken.text) {
68
+ parser.error({
69
+ title: `Macro name mismatch, expected "${nameToken.text}" but found "${nameEndToken.text}"`,
70
+ pos: nameEndToken.pos
71
+ });
72
+ }
73
+ }
74
+
75
+ const macroDeclarationStatement = new MacroDeclarationStatement(
76
+ createNode(Identifier, nameToken, nameToken.text),
77
+ args,
78
+ body
79
+ );
80
+
81
+ setStartFromToken(macroDeclarationStatement, token);
82
+ setEndFromToken(
83
+ macroDeclarationStatement,
84
+ tokens.expect(Types.TAG_END)
85
+ );
86
+
87
+ macroDeclarationStatement.trimRightMacro =
88
+ hasTagEndTokenTrimRight(openingTagEndToken);
89
+ macroDeclarationStatement.trimLeftEndmacro =
90
+ hasTagStartTokenTrimLeft(closingTagStartToken);
91
+
92
+ return macroDeclarationStatement;
93
+ }
94
+ };