@mrhenry/twig-parser 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mr. Henry
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ This license applies only to the code in this repository.
24
+ Images are explicitly excluded.
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@mrhenry/twig-parser",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js"
9
+ },
10
+ "dependencies": {
11
+ "@mrhenry/twig-tokenizer": "0.1.0"
12
+ }
13
+ }
@@ -0,0 +1,69 @@
1
+ // @ts-check
2
+ /**
3
+ * The array/mapping literal expression node.
4
+ *
5
+ * Mirrors `Twig\Node\Expression\ArrayExpression`. Pairs are stored as
6
+ * `[key, value]` tuples; list elements have an auto-incremented constant key.
7
+ *
8
+ * @module twig-parser
9
+ */
10
+ import { Node, NodeType, n } from './node.js';
11
+
12
+ /**
13
+ * An array expression: a sequence or mapping literal.
14
+ */
15
+ export class ArrayExpression extends Node {
16
+ /**
17
+ * @param {number} line The template line.
18
+ */
19
+ constructor(line) {
20
+ super(NodeType.ArrayExpr, {}, { pairs: [], index: -1 }, line);
21
+ }
22
+
23
+ /**
24
+ * Adds an element.
25
+ *
26
+ * @param {Node} value The element value.
27
+ * @param {Node|null} [key] The element key (auto-incremented when null).
28
+ */
29
+ addElement(value, key = null) {
30
+ const pairs = /** @type {Array<[Node, Node]>} */ (this.attributes.pairs);
31
+ let index = /** @type {number} */ (this.attributes.index);
32
+ if (key === null) {
33
+ index += 1;
34
+ key = n(NodeType.Constant, {}, { value: index }, value.getTemplateLine());
35
+ } else if (
36
+ key.type === NodeType.Constant &&
37
+ /^[0-9]+$/.test(String(key.getAttribute('value')))
38
+ ) {
39
+ const numeric = Number(key.getAttribute('value'));
40
+ if (numeric > index) {
41
+ index = numeric;
42
+ }
43
+ }
44
+ this.attributes.index = index;
45
+ pairs.push([key, value]);
46
+ }
47
+
48
+ /**
49
+ * @returns {Array<[Node, Node]>} The `[key, value]` pairs.
50
+ */
51
+ getKeyValuePairs() {
52
+ return /** @type {Array<[Node, Node]>} */ (this.attributes.pairs);
53
+ }
54
+
55
+ /**
56
+ * Whether the array is a list (keys are sequential integers from 0).
57
+ *
58
+ * @returns {boolean} True when the array is a sequence.
59
+ */
60
+ isSequence() {
61
+ return this.getKeyValuePairs().every((pair, i) => {
62
+ const key = pair[0];
63
+ if (!key || key.type !== NodeType.Constant) {
64
+ return false;
65
+ }
66
+ return key.getAttribute('value') === i;
67
+ });
68
+ }
69
+ }
@@ -0,0 +1,127 @@
1
+ // @ts-check
2
+ /**
3
+ * Metadata for the core Twig filters, functions and tests used at parse time.
4
+ *
5
+ * The actual runtime implementations live in `@mrhenry/twig-js`. This module only
6
+ * carries the names and parse-time flags needed by the parser, mirroring
7
+ * `CoreExtension::getFilters()`, `getFunctions()` and `getTests()`.
8
+ *
9
+ * @module twig-parser
10
+ */
11
+
12
+ /**
13
+ * A callable descriptor returned by the parser environment.
14
+ *
15
+ * @typedef {object} TwigCallable
16
+ * @property {string} name The callable name.
17
+ * @property {'function'|'filter'|'test'} type The callable kind.
18
+ * @property {boolean} [parserCallable] Whether the function has a compile-time parser callable (`parent`, `block`, `attribute`).
19
+ * @property {boolean} [oneMandatoryArgument] Whether a test requires its argument without parentheses (`divisible by`, `same as`).
20
+ */
21
+
22
+ /**
23
+ * Core filter names (the first argument is the piped value).
24
+ *
25
+ * @type {string[]}
26
+ */
27
+ export const CORE_FILTER_NAMES = [
28
+ 'date',
29
+ 'date_modify',
30
+ 'format',
31
+ 'replace',
32
+ 'number_format',
33
+ 'abs',
34
+ 'round',
35
+ 'url_encode',
36
+ 'json_encode',
37
+ 'convert_encoding',
38
+ 'title',
39
+ 'capitalize',
40
+ 'upper',
41
+ 'lower',
42
+ 'striptags',
43
+ 'trim',
44
+ 'nl2br',
45
+ 'spaceless',
46
+ 'join',
47
+ 'split',
48
+ 'sort',
49
+ 'merge',
50
+ 'batch',
51
+ 'column',
52
+ 'filter',
53
+ 'map',
54
+ 'reduce',
55
+ 'find',
56
+ 'keys',
57
+ 'reverse',
58
+ 'shuffle',
59
+ 'length',
60
+ 'slice',
61
+ 'first',
62
+ 'last',
63
+ 'default',
64
+ 'invoke',
65
+ 'escape',
66
+ 'e',
67
+ 'raw',
68
+ ];
69
+
70
+ /**
71
+ * Core function names.
72
+ *
73
+ * @type {string[]}
74
+ */
75
+ export const CORE_FUNCTION_NAMES = [
76
+ 'parent',
77
+ 'block',
78
+ 'attribute',
79
+ 'max',
80
+ 'min',
81
+ 'range',
82
+ 'constant',
83
+ 'cycle',
84
+ 'random',
85
+ 'date',
86
+ 'include',
87
+ 'include_only',
88
+ 'source',
89
+ 'enum_cases',
90
+ 'enum',
91
+ 'template_from_string',
92
+ ];
93
+
94
+ /**
95
+ * Names of functions resolved at compile time (parser callables).
96
+ *
97
+ * @type {Set<string>}
98
+ */
99
+ export const PARSER_CALLABLE_FUNCTIONS = new Set(['parent', 'block', 'attribute']);
100
+
101
+ /**
102
+ * Core test names.
103
+ *
104
+ * @type {string[]}
105
+ */
106
+ export const CORE_TEST_NAMES = [
107
+ 'even',
108
+ 'odd',
109
+ 'defined',
110
+ 'same as',
111
+ 'none',
112
+ 'null',
113
+ 'divisible by',
114
+ 'constant',
115
+ 'empty',
116
+ 'iterable',
117
+ 'sequence',
118
+ 'mapping',
119
+ 'true',
120
+ ];
121
+
122
+ /**
123
+ * Tests requiring an argument without parentheses (`x is divisible by(3)`).
124
+ *
125
+ * @type {Set<string>}
126
+ */
127
+ export const ONE_MANDATORY_ARGUMENT_TESTS = new Set(['divisible by', 'same as']);