@tbela99/css-parser 1.8.2 → 2.0.1

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 (105) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +129 -31
  3. package/dist/index-umd-web.js +27383 -24083
  4. package/dist/index.d.ts +2737 -2200
  5. package/dist/package.json.js +2 -2
  6. package/dist/src/data/properties.json.js +1 -1
  7. package/dist/src/data/validation.json.js +1 -1
  8. package/dist/src/lib/ast/arena-node.js +548 -0
  9. package/dist/src/lib/ast/arena.js +3198 -0
  10. package/dist/src/lib/ast/cache.js +333 -0
  11. package/dist/src/lib/ast/features/calc.js +85 -76
  12. package/dist/src/lib/ast/features/if.js +155 -115
  13. package/dist/src/lib/ast/features/index.js +1 -1
  14. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  15. package/dist/src/lib/ast/features/prefix.js +184 -183
  16. package/dist/src/lib/ast/features/shorthand.js +40 -46
  17. package/dist/src/lib/ast/features/transform.js +24 -24
  18. package/dist/src/lib/ast/features/type.js +1 -1
  19. package/dist/src/lib/ast/math/expression.js +458 -543
  20. package/dist/src/lib/ast/math/math.js +19 -41
  21. package/dist/src/lib/ast/minify.js +590 -647
  22. package/dist/src/lib/ast/transform/compute.js +82 -85
  23. package/dist/src/lib/ast/transform/matrix.js +21 -38
  24. package/dist/src/lib/ast/transform/minify.js +82 -193
  25. package/dist/src/lib/ast/transform/perspective.js +2 -3
  26. package/dist/src/lib/ast/transform/rotate.js +1 -1
  27. package/dist/src/lib/ast/transform/scale.js +1 -1
  28. package/dist/src/lib/ast/transform/skew.js +1 -1
  29. package/dist/src/lib/ast/transform/translate.js +1 -1
  30. package/dist/src/lib/ast/transform/utils.js +2 -2
  31. package/dist/src/lib/ast/types.js +263 -258
  32. package/dist/src/lib/ast/walk.js +246 -610
  33. package/dist/src/lib/fs/resolve.js +1 -1
  34. package/dist/src/lib/parser/declaration/list.js +223 -139
  35. package/dist/src/lib/parser/declaration/map.js +251 -238
  36. package/dist/src/lib/parser/declaration/set.js +74 -66
  37. package/dist/src/lib/parser/linesmap.js +1 -1
  38. package/dist/src/lib/parser/parse.js +1379 -1658
  39. package/dist/src/lib/parser/source.js +7 -4
  40. package/dist/src/lib/parser/tokenize.js +406 -199
  41. package/dist/src/lib/parser/utils/angle.js +11 -13
  42. package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
  43. package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
  44. package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
  45. package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
  46. package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
  47. package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
  48. package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
  49. package/dist/src/lib/parser/utils/at-rule.js +5 -5
  50. package/dist/src/lib/parser/utils/base64.js +2 -2
  51. package/dist/src/lib/parser/utils/cache.js +1 -1
  52. package/dist/src/lib/parser/utils/config.js +1 -1
  53. package/dist/src/lib/parser/utils/declaration.js +233 -234
  54. package/dist/src/lib/parser/utils/eq.js +50 -3
  55. package/dist/src/lib/parser/utils/hash.js +2 -35
  56. package/dist/src/lib/parser/utils/selector.js +309 -387
  57. package/dist/src/lib/parser/utils/text.js +18 -4
  58. package/dist/src/lib/parser/utils/token.js +16 -69
  59. package/dist/src/lib/parser/utils/type.js +37 -24
  60. package/dist/src/lib/printer/render.js +965 -511
  61. package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
  62. package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
  63. package/dist/src/lib/syntax/color/a98rgb.js +1 -1
  64. package/dist/src/lib/syntax/color/alpha.js +36 -37
  65. package/dist/src/lib/syntax/color/cmyk.js +35 -52
  66. package/dist/src/lib/syntax/color/color-mix.js +96 -132
  67. package/dist/src/lib/syntax/color/color.js +222 -231
  68. package/dist/src/lib/syntax/color/hex.js +45 -55
  69. package/dist/src/lib/syntax/color/hsl.js +54 -62
  70. package/dist/src/lib/syntax/color/hwb.js +60 -68
  71. package/dist/src/lib/syntax/color/lab.js +59 -67
  72. package/dist/src/lib/syntax/color/lch.js +59 -67
  73. package/dist/src/lib/syntax/color/oklab.js +60 -68
  74. package/dist/src/lib/syntax/color/oklch.js +60 -67
  75. package/dist/src/lib/syntax/color/p3.js +1 -1
  76. package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
  77. package/dist/src/lib/syntax/color/rec2020.js +1 -1
  78. package/dist/src/lib/syntax/color/relative-color.js +94 -144
  79. package/dist/src/lib/syntax/color/rgb.js +54 -57
  80. package/dist/src/lib/syntax/color/srgb.js +55 -53
  81. package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
  82. package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
  83. package/dist/src/lib/syntax/color/utils/components.js +19 -20
  84. package/dist/src/lib/syntax/color/utils/distance.js +15 -13
  85. package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
  86. package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
  87. package/dist/src/lib/syntax/color/xyz.js +1 -1
  88. package/dist/src/lib/syntax/color/xyzd50.js +1 -1
  89. package/dist/src/lib/syntax/constants.js +2 -16
  90. package/dist/src/lib/syntax/syntax.js +301 -407
  91. package/dist/src/lib/validation/config.js +1 -1
  92. package/dist/src/lib/validation/match.js +959 -723
  93. package/dist/src/lib/validation/parser/parse.js +230 -112
  94. package/dist/src/lib/validation/parser/typedef.js +1 -1
  95. package/dist/src/lib/validation/utils/list.js +27 -12
  96. package/dist/src/node.js +12 -72
  97. package/dist/src/types.js +1 -1
  98. package/dist/src/utils/sync.js +9 -6
  99. package/dist/src/web.js +11 -73
  100. package/package.json +9 -10
  101. package/dist/index.cjs +0 -35870
  102. package/dist/src/lib/ast/clone.js +0 -51
  103. package/dist/src/lib/ast/expand.js +0 -311
  104. package/dist/src/lib/ast/find.js +0 -192
  105. package/dist/src/lib/ast/node.js +0 -59
@@ -2,9 +2,9 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.1
6
6
  License: MIT
7
7
  */
8
- var version = "1.8.2";
8
+ var version = "2.0.1";
9
9
 
10
10
  export { version };
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.1
6
6
  License: MIT
7
7
  */
8
8
  var properties = {
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.1
6
6
  License: MIT
7
7
  */
8
8
  var declarations = {
@@ -0,0 +1,548 @@
1
+ /*
2
+ CSS parser, minifier and validator for node and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.0.1
6
+ License: MIT
7
+ */
8
+ import { EnumToken, EnumAstNodeStatus, ColorType } from './types.js';
9
+ import { numericTokenTypes, structuredFields } from './arena.js';
10
+
11
+ /**
12
+ * keyed symbol (`Symbol.for()`) of the arena node id of an {@link AstArenaNode}. the symbol is shared by every copy of
13
+ * the library loaded in the same realm
14
+ *
15
+ * ```ts
16
+ * const node = arena.toObject(id);
17
+ *
18
+ * console.log(node[ARENAID] === id); // true
19
+ * ```
20
+ */
21
+ const ARENAID = Symbol.for("@tbela99/css-parser/ARENAID");
22
+ /**
23
+ * keyed symbol (`Symbol.for()`) of the arena of an {@link AstArenaNode}
24
+ */
25
+ const ARENA = Symbol.for("@tbela99/css-parser/ARENA");
26
+ /**
27
+ * true if the node type is an ast node type (stylesheet, rule, at-rule, declaration, comment, css module nodes). the
28
+ * other types are value tokens
29
+ * @param typ
30
+ */
31
+ function isNodeObjectType(typ) {
32
+ switch (typ) {
33
+ case EnumToken.StyleSheetNodeType:
34
+ case EnumToken.RuleNodeType:
35
+ case EnumToken.KeyframesRuleNodeType:
36
+ case EnumToken.AtRuleNodeType:
37
+ case EnumToken.KeyframesAtRuleNodeType:
38
+ case EnumToken.CommentNodeType:
39
+ case EnumToken.CDOCOMMNodeType:
40
+ case EnumToken.DeclarationNodeType:
41
+ case EnumToken.CssVariableTokenType:
42
+ case EnumToken.CssVariableImportTokenType:
43
+ case EnumToken.CssVariableDeclarationMapTokenType:
44
+ return true;
45
+ }
46
+ return false;
47
+ }
48
+ /**
49
+ * node types whose value (`val`) is the token list: declarations and css module variables
50
+ * @param typ
51
+ */
52
+ function hasValueTokens(typ) {
53
+ return (typ == EnumToken.DeclarationNodeType ||
54
+ typ == EnumToken.CssVariableTokenType ||
55
+ typ == EnumToken.CssVariableImportTokenType ||
56
+ typ == EnumToken.CssVariableDeclarationMapTokenType);
57
+ }
58
+ /**
59
+ * convert setter values to node ids of the arena. {@link AstArenaNode} of the arena are used as is (no copy), the nodes
60
+ * of another arena are imported, plain objects are allocated
61
+ * @param arena
62
+ * @param values
63
+ */
64
+ function toIds(arena, values) {
65
+ const result = [];
66
+ if (values == null) {
67
+ return result;
68
+ }
69
+ if (!Array.isArray(values)) {
70
+ const value = values;
71
+ result.push(typeof value == "number" ? value : arena.fromObject(value));
72
+ return result;
73
+ }
74
+ const list = values;
75
+ for (let i = 0; i < list.length; i++) {
76
+ const value = list[i];
77
+ result.push(typeof value == "number" ? value : arena.fromObject(value));
78
+ }
79
+ return result;
80
+ }
81
+ /**
82
+ * object views of a linked list, in a frozen array: the array cannot be modified, assign the property instead
83
+ * @param arena
84
+ * @param first first id of the list
85
+ */
86
+ function listViews(arena, first) {
87
+ const result = [];
88
+ for (let id = first; id != 0; id = arena.nextSibling[id]) {
89
+ result.push(new AstArenaNode(arena, id));
90
+ }
91
+ return Object.freeze(result);
92
+ }
93
+ /**
94
+ * Object view of an arena node or token, returned by {@link AstArena.toObject}. The view does not copy the data: its
95
+ * properties are getters and setters that read and write the arena columns. A view only holds the arena and the node
96
+ * id ({@link ARENAID}).
97
+ *
98
+ * - the properties are the properties of the plain objects ({@link PlainAstNode}, {@link PlainToken}): `typ`, `nam`,
99
+ * `sel`, `val`, `chi`, `tokens`, `unit`, `kin`, `l`, `r`, `op`, ... and `loc`, `state`, `errors`, `parent`,
100
+ * - the nodes and tokens (children, value tokens, fields) are returned as new views in a frozen array. the arrays,
101
+ * the locations (`loc`) and the error lists (`errors`) cannot be modified (modifying them throws in strict mode):
102
+ * assign the property to modify the arena (`node.chi = [...node.chi, rule]`),
103
+ * - the values assigned can be node ids, views or plain objects. importing a view of the same arena is a noop: the
104
+ * node is used as is, the views of another arena are copied,
105
+ * - two views of the same node are different objects: compare their {@link ARENAID},
106
+ * - `JSON.stringify()` and `toJSON()` return a deep plain copy ({@link AstArena.toPlainObject}).
107
+ *
108
+ * ```ts
109
+ * function visitor(arena: AstArena, id: number) {
110
+ *
111
+ * const node = arena.toObject(id);
112
+ *
113
+ * if (node.nam == 'width') {
114
+ *
115
+ * node.nam = 'height';
116
+ * node.val = [{typ: EnumToken.LengthTokenType, val: 3, unit: 'px'}];
117
+ * }
118
+ * }
119
+ * ```
120
+ */
121
+ class AstArenaNode {
122
+ /**
123
+ * arena of the node
124
+ */
125
+ [ARENA];
126
+ /**
127
+ * node id in the arena
128
+ */
129
+ [ARENAID];
130
+ /**
131
+ * @param arena
132
+ * @param id
133
+ */
134
+ constructor(arena, id) {
135
+ this[ARENA] = arena;
136
+ this[ARENAID] = id;
137
+ }
138
+ /**
139
+ * node or token type
140
+ */
141
+ get typ() {
142
+ return this[ARENA].dataTypes[this[ARENAID]];
143
+ }
144
+ set typ(typ) {
145
+ this[ARENA].dataTypes[this[ARENAID]] = typ;
146
+ }
147
+ /**
148
+ * at-rule / declaration name, token name. undefined for the other nodes
149
+ */
150
+ get nam() {
151
+ const arena = this[ARENA];
152
+ const id = this[ARENAID];
153
+ const typ = arena.dataTypes[id];
154
+ if (isNodeObjectType(typ)) {
155
+ return typ == EnumToken.AtRuleNodeType || typ == EnumToken.KeyframesAtRuleNodeType || hasValueTokens(typ)
156
+ ? arena.strings[arena.name[id]]
157
+ : undefined;
158
+ }
159
+ return arena.name[id] == 0 ? undefined : arena.strings[arena.name[id]];
160
+ }
161
+ set nam(nam) {
162
+ this[ARENA].name[this[ARENAID]] = this[ARENA].internString(nam);
163
+ }
164
+ /**
165
+ * rule selector. undefined for the other nodes
166
+ */
167
+ get sel() {
168
+ const arena = this[ARENA];
169
+ const id = this[ARENAID];
170
+ const typ = arena.dataTypes[id];
171
+ return typ == EnumToken.RuleNodeType || typ == EnumToken.KeyframesRuleNodeType
172
+ ? arena.strings[arena.name[id]]
173
+ : undefined;
174
+ }
175
+ set sel(sel) {
176
+ this[ARENA].name[this[ARENAID]] = this[ARENA].internString(sel);
177
+ }
178
+ /**
179
+ * value:
180
+ * - at-rule prelude, comment: string,
181
+ * - declaration: value tokens,
182
+ * - number, dimension, percentage: number (or the fraction token),
183
+ * - identifier, string, function name, ...: string,
184
+ * - unary expression: the operand,
185
+ * - structured tokens with a value field: the field tokens
186
+ */
187
+ get val() {
188
+ const arena = this[ARENA];
189
+ const id = this[ARENAID];
190
+ const typ = arena.dataTypes[id];
191
+ if (isNodeObjectType(typ)) {
192
+ if (hasValueTokens(typ)) {
193
+ return listViews(arena, arena.firstToken[id]);
194
+ }
195
+ return typ == EnumToken.StyleSheetNodeType ||
196
+ typ == EnumToken.RuleNodeType ||
197
+ typ == EnumToken.KeyframesRuleNodeType
198
+ ? undefined
199
+ : arena.strings[arena.stringValue[id]];
200
+ }
201
+ if (typ == EnumToken.BinaryExpressionTokenType || typ == EnumToken.FractionTokenType) {
202
+ return undefined;
203
+ }
204
+ if (typ == EnumToken.UnaryExpressionTokenType) {
205
+ return arena.firstChild[id] == 0 ? null : new AstArenaNode(arena, arena.firstChild[id]);
206
+ }
207
+ if (numericTokenTypes.has(typ)) {
208
+ const fraction = arena.getFraction(id);
209
+ return fraction == 0 ? arena.numericValue[id] : new AstArenaNode(arena, fraction);
210
+ }
211
+ const field = this.getField("val");
212
+ if (field !== undefined) {
213
+ return field;
214
+ }
215
+ return arena.stringValue[id] != 0 || typ == EnumToken.IdenTokenType || typ == EnumToken.StringTokenType
216
+ ? arena.strings[arena.stringValue[id]]
217
+ : undefined;
218
+ }
219
+ set val(val) {
220
+ const arena = this[ARENA];
221
+ const id = this[ARENAID];
222
+ const typ = arena.dataTypes[id];
223
+ if (isNodeObjectType(typ) && hasValueTokens(typ)) {
224
+ arena.setTokenList(id, toIds(arena, val));
225
+ return;
226
+ }
227
+ if (typeof val == "string") {
228
+ if (numericTokenTypes.has(typ)) {
229
+ arena.numericValue[id] = +val;
230
+ arena.setFraction(id, null);
231
+ }
232
+ else {
233
+ arena.stringValue[id] = arena.internString(val);
234
+ }
235
+ return;
236
+ }
237
+ if (typeof val == "number") {
238
+ arena.numericValue[id] = val;
239
+ if (numericTokenTypes.has(typ)) {
240
+ arena.setFraction(id, null);
241
+ }
242
+ return;
243
+ }
244
+ if (typ == EnumToken.UnaryExpressionTokenType) {
245
+ arena.setChildList(id, toIds(arena, val));
246
+ }
247
+ else if (numericTokenTypes.has(typ)) {
248
+ arena.setFraction(id, val == null ? null : toIds(arena, val)[0]);
249
+ }
250
+ else if (!this.setField("val", val)) {
251
+ arena.stringValue[id] = 0;
252
+ }
253
+ }
254
+ /**
255
+ * children: the child nodes of a stylesheet, a rule or an at-rule with a block, the tokens of a function token
256
+ * (arguments), a parentheses token, ... undefined when the node has no children
257
+ */
258
+ get chi() {
259
+ const arena = this[ARENA];
260
+ const id = this[ARENAID];
261
+ if (arena.flags[id] & EnumAstNodeStatus.HasChildren) {
262
+ return listViews(arena, arena.firstChild[id]);
263
+ }
264
+ if (isNodeObjectType(arena.dataTypes[id]) || arena.firstToken[id] == 0) {
265
+ return undefined;
266
+ }
267
+ return listViews(arena, arena.firstToken[id]);
268
+ }
269
+ set chi(chi) {
270
+ const arena = this[ARENA];
271
+ const id = this[ARENAID];
272
+ const typ = arena.dataTypes[id];
273
+ if (typ == EnumToken.StyleSheetNodeType ||
274
+ typ == EnumToken.RuleNodeType ||
275
+ typ == EnumToken.KeyframesRuleNodeType ||
276
+ typ == EnumToken.AtRuleNodeType ||
277
+ typ == EnumToken.KeyframesAtRuleNodeType) {
278
+ if (chi == null) {
279
+ // an at-rule without a block
280
+ arena.setChildList(id, []);
281
+ if (typ == EnumToken.AtRuleNodeType || typ == EnumToken.KeyframesAtRuleNodeType) {
282
+ arena.flags[id] &= ~EnumAstNodeStatus.HasChildren;
283
+ }
284
+ return;
285
+ }
286
+ arena.flags[id] |= EnumAstNodeStatus.HasChildren;
287
+ arena.setChildList(id, toIds(arena, chi));
288
+ return;
289
+ }
290
+ arena.setTokenList(id, toIds(arena, chi));
291
+ }
292
+ /**
293
+ * prelude tokens of an at-rule, selector tokens of a rule. null when the node has no prelude tokens
294
+ */
295
+ get tokens() {
296
+ const arena = this[ARENA];
297
+ const id = this[ARENAID];
298
+ return (arena.flags[id] & EnumAstNodeStatus.HasValues) != 0 || arena.firstToken[id] == 0
299
+ ? null
300
+ : listViews(arena, arena.firstToken[id]);
301
+ }
302
+ set tokens(tokens) {
303
+ this[ARENA].setTokenList(this[ARENAID], toIds(this[ARENA], tokens));
304
+ }
305
+ /**
306
+ * unit of a dimension, calendar (`cal`) of a color, attribute (`attr`) of a match expression
307
+ */
308
+ get unit() {
309
+ const arena = this[ARENA];
310
+ const id = this[ARENAID];
311
+ const typ = arena.dataTypes[id];
312
+ return arena.unit[id] == 0 || typ == EnumToken.ColorTokenType || typ == EnumToken.MatchExpressionTokenType
313
+ ? undefined
314
+ : arena.strings[arena.unit[id]];
315
+ }
316
+ set unit(unit) {
317
+ this[ARENA].setUnitString(this[ARENAID], unit ?? null);
318
+ }
319
+ /**
320
+ * color space of a color
321
+ */
322
+ get cal() {
323
+ const arena = this[ARENA];
324
+ const id = this[ARENAID];
325
+ return arena.unit[id] == 0 || arena.dataTypes[id] != EnumToken.ColorTokenType
326
+ ? undefined
327
+ : arena.strings[arena.unit[id]];
328
+ }
329
+ set cal(cal) {
330
+ this[ARENA].setUnitString(this[ARENAID], cal ?? null);
331
+ }
332
+ /**
333
+ * attribute of a match expression
334
+ */
335
+ get attr() {
336
+ const arena = this[ARENA];
337
+ const id = this[ARENAID];
338
+ return arena.unit[id] == 0 || arena.dataTypes[id] != EnumToken.MatchExpressionTokenType
339
+ ? undefined
340
+ : arena.strings[arena.unit[id]];
341
+ }
342
+ set attr(attr) {
343
+ this[ARENA].setUnitString(this[ARENAID], attr ?? null);
344
+ }
345
+ /**
346
+ * color type of a color token
347
+ */
348
+ get kin() {
349
+ const arena = this[ARENA];
350
+ const id = this[ARENAID];
351
+ return arena.dataTypes[id] != EnumToken.ColorTokenType || arena.colorType[id] == ColorType.NONE
352
+ ? undefined
353
+ : arena.colorType[id];
354
+ }
355
+ set kin(kin) {
356
+ this[ARENA].colorType[this[ARENAID]] = kin ?? ColorType.NONE;
357
+ }
358
+ /**
359
+ * sign of a unary expression
360
+ */
361
+ get sign() {
362
+ const arena = this[ARENA];
363
+ const id = this[ARENAID];
364
+ return arena.dataTypes[id] == EnumToken.UnaryExpressionTokenType ? arena.numericValue[id] : undefined;
365
+ }
366
+ set sign(sign) {
367
+ this[ARENA].numericValue[this[ARENAID]] = sign;
368
+ }
369
+ /**
370
+ * operator: the operator of a binary expression, the operator tokens of a structured token
371
+ */
372
+ get op() {
373
+ const arena = this[ARENA];
374
+ const id = this[ARENAID];
375
+ return arena.dataTypes[id] == EnumToken.BinaryExpressionTokenType ? arena.numericValue[id] : this.getField("op");
376
+ }
377
+ set op(op) {
378
+ const arena = this[ARENA];
379
+ const id = this[ARENAID];
380
+ if (arena.dataTypes[id] == EnumToken.BinaryExpressionTokenType) {
381
+ arena.numericValue[id] = op;
382
+ }
383
+ else {
384
+ this.setField("op", op);
385
+ }
386
+ }
387
+ /**
388
+ * first operator of a media range query
389
+ */
390
+ get op1() {
391
+ return this.getField("op1");
392
+ }
393
+ set op1(op) {
394
+ this.setField("op1", op);
395
+ }
396
+ /**
397
+ * second operator of a media range query
398
+ */
399
+ get op2() {
400
+ return this.getField("op2");
401
+ }
402
+ set op2(op) {
403
+ this.setField("op2", op);
404
+ }
405
+ /**
406
+ * left hand side of a structured token, numerator of a fraction
407
+ */
408
+ get l() {
409
+ const arena = this[ARENA];
410
+ const id = this[ARENAID];
411
+ if (arena.dataTypes[id] == EnumToken.FractionTokenType) {
412
+ return arena.firstChild[id] == 0 ? null : new AstArenaNode(arena, arena.firstChild[id]);
413
+ }
414
+ return this.getField("l");
415
+ }
416
+ set l(l) {
417
+ const arena = this[ARENA];
418
+ const id = this[ARENAID];
419
+ if (arena.dataTypes[id] == EnumToken.FractionTokenType) {
420
+ const r = arena.lastChild[id];
421
+ const values = toIds(arena, l);
422
+ if (r != 0 && r != arena.firstChild[id]) {
423
+ values.push(r);
424
+ }
425
+ arena.setChildList(id, values);
426
+ return;
427
+ }
428
+ this.setField("l", l);
429
+ }
430
+ /**
431
+ * right hand side of a structured token, denominator of a fraction
432
+ */
433
+ get r() {
434
+ const arena = this[ARENA];
435
+ const id = this[ARENAID];
436
+ if (arena.dataTypes[id] == EnumToken.FractionTokenType) {
437
+ return arena.lastChild[id] == 0 ? null : new AstArenaNode(arena, arena.lastChild[id]);
438
+ }
439
+ return this.getField("r");
440
+ }
441
+ set r(r) {
442
+ const arena = this[ARENA];
443
+ const id = this[ARENAID];
444
+ if (arena.dataTypes[id] == EnumToken.FractionTokenType) {
445
+ const l = arena.firstChild[id];
446
+ const values = toIds(arena, r);
447
+ if (l != 0) {
448
+ values.unshift(l);
449
+ }
450
+ arena.setChildList(id, values);
451
+ return;
452
+ }
453
+ this.setField("r", r);
454
+ }
455
+ /**
456
+ * source location. null when the node has no location
457
+ */
458
+ get loc() {
459
+ const loc = this[ARENA].getLocation(this[ARENAID]);
460
+ // a copy of the location: it cannot be modified, assign the property instead
461
+ return loc == null ? null : Object.freeze(loc);
462
+ }
463
+ set loc(loc) {
464
+ this[ARENA].setLocation(this[ARENAID], loc?.srcId, loc?.sta, loc?.end);
465
+ }
466
+ /**
467
+ * validation state
468
+ */
469
+ get state() {
470
+ return this[ARENA].flags[this[ARENAID]] & EnumAstNodeStatus.StateMask;
471
+ }
472
+ set state(state) {
473
+ this[ARENA].setState(this[ARENAID], state);
474
+ }
475
+ /**
476
+ * validation errors. null when the node has no errors
477
+ */
478
+ get errors() {
479
+ const errors = this[ARENA].errors.get(this[ARENAID]);
480
+ // the error list may be shared by the clones of the node: a frozen copy, assign the property instead
481
+ return errors == null ? null : Object.freeze(errors.slice());
482
+ }
483
+ set errors(errors) {
484
+ this[ARENA].setErrors(this[ARENAID], errors == null ? null : errors.slice());
485
+ }
486
+ /**
487
+ * parent node, or the token that owns the token (see {@link AstArena.getValueParent}). null when the node is
488
+ * detached
489
+ */
490
+ get parent() {
491
+ const parent = this[ARENA].getValueParent(this[ARENAID]);
492
+ return parent == 0 ? null : new AstArenaNode(this[ARENA], parent);
493
+ }
494
+ /**
495
+ * value of a structured token field. undefined when the token has no such field
496
+ * @param key
497
+ */
498
+ getField(key) {
499
+ const arena = this[ARENA];
500
+ const id = this[ARENAID];
501
+ const fields = structuredFields(arena.dataTypes[id]);
502
+ for (let i = 0; i < fields.length; i++) {
503
+ if (fields[i][0] != key) {
504
+ continue;
505
+ }
506
+ const list = arena.getChildAt(id, fields[i][1]);
507
+ if (list == null) {
508
+ return undefined;
509
+ }
510
+ if (fields[i][2]) {
511
+ return arena.firstToken[list] == 0 ? null : new AstArenaNode(arena, arena.firstToken[list]);
512
+ }
513
+ return listViews(arena, arena.firstToken[list]);
514
+ }
515
+ return undefined;
516
+ }
517
+ /**
518
+ * set the value of a structured token field. false when the token has no such field
519
+ * @param key
520
+ * @param value
521
+ */
522
+ setField(key, value) {
523
+ const arena = this[ARENA];
524
+ const id = this[ARENAID];
525
+ const fields = structuredFields(arena.dataTypes[id]);
526
+ for (let i = 0; i < fields.length; i++) {
527
+ if (fields[i][0] == key) {
528
+ arena.setTokenList(arena.fieldList(id, fields[i][1]), toIds(arena, value));
529
+ return true;
530
+ }
531
+ }
532
+ return false;
533
+ }
534
+ /**
535
+ * deep plain copy of the node, see {@link AstArena.toPlainObject}
536
+ */
537
+ toJSON() {
538
+ return this[ARENA].toPlainObject(this[ARENAID]);
539
+ }
540
+ /**
541
+ * node.js `util.inspect()` / `console.log()`: the plain copy of the node
542
+ */
543
+ [Symbol.for("nodejs.util.inspect.custom")]() {
544
+ return this.toJSON();
545
+ }
546
+ }
547
+
548
+ export { ARENA, ARENAID, AstArenaNode, isNodeObjectType };