@tbela99/css-parser 2.0.1 → 2.2.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.
Files changed (128) hide show
  1. package/CHANGELOG.md +86 -1
  2. package/README.md +15 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/data → data}/properties.json.js +86 -2
  5. package/dist/{src/data → data}/validation.json.js +4848 -3750
  6. package/dist/index-umd-web.js +18234 -9498
  7. package/dist/index.d.ts +1251 -373
  8. package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
  9. package/dist/{src/lib → lib}/ast/arena.js +1034 -655
  10. package/dist/{src/lib → lib}/ast/cache.js +36 -31
  11. package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
  12. package/dist/{src/lib → lib}/ast/features/if.js +24 -19
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
  17. package/dist/lib/ast/features/shorthand.js +129 -0
  18. package/dist/lib/ast/features/transform.js +188 -0
  19. package/dist/{src/lib → lib}/ast/features/type.js +2 -2
  20. package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
  21. package/dist/{src/lib → lib}/ast/math/math.js +8 -8
  22. package/dist/{src/lib → lib}/ast/minify.js +932 -354
  23. package/dist/lib/ast/mixins/custom-function.js +393 -0
  24. package/dist/lib/ast/mixins/utils.js +53 -0
  25. package/dist/lib/ast/mixins/variables.js +678 -0
  26. package/dist/lib/ast/strings.js +906 -0
  27. package/dist/lib/ast/transform/compute.js +560 -0
  28. package/dist/lib/ast/transform/matrix.js +67 -0
  29. package/dist/lib/ast/transform/minify.js +325 -0
  30. package/dist/lib/ast/transform/perspective.js +24 -0
  31. package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
  32. package/dist/lib/ast/transform/scale.js +43 -0
  33. package/dist/lib/ast/transform/skew.js +38 -0
  34. package/dist/lib/ast/transform/translate.js +56 -0
  35. package/dist/lib/ast/transform/utils.js +390 -0
  36. package/dist/{src/lib → lib}/ast/types.js +7 -15
  37. package/dist/{src/lib → lib}/ast/walk.js +19 -19
  38. package/dist/{src/lib → lib}/fs/resolve.js +15 -15
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
  42. package/dist/lib/parser/input.js +85 -0
  43. package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
  44. package/dist/{src/lib → lib}/parser/parse.js +852 -619
  45. package/dist/lib/parser/source.js +71 -0
  46. package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
  49. package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
  50. package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
  55. package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
  56. package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
  57. package/dist/lib/parser/utils/cache.js +53 -0
  58. package/dist/lib/parser/utils/canonical.js +78 -0
  59. package/dist/lib/parser/utils/codes.js +93 -0
  60. package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
  61. package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
  64. package/dist/lib/parser/utils/selector.js +720 -0
  65. package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
  66. package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
  67. package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
  68. package/dist/{src/lib → lib}/printer/render.js +536 -243
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
  77. package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
  94. package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
  95. package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
  99. package/dist/{src/lib → lib}/syntax/constants.js +30 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
  101. package/dist/lib/validation/config.js +173 -0
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +1256 -579
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +92 -80
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +13 -8
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +80 -75
  113. package/package.json +13 -13
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -87
  116. package/dist/src/lib/ast/features/transform.js +0 -85
  117. package/dist/src/lib/ast/transform/compute.js +0 -358
  118. package/dist/src/lib/ast/transform/matrix.js +0 -88
  119. package/dist/src/lib/ast/transform/minify.js +0 -267
  120. package/dist/src/lib/ast/transform/perspective.js +0 -16
  121. package/dist/src/lib/ast/transform/scale.js +0 -39
  122. package/dist/src/lib/ast/transform/skew.js +0 -30
  123. package/dist/src/lib/ast/transform/translate.js +0 -39
  124. package/dist/src/lib/ast/transform/utils.js +0 -287
  125. package/dist/src/lib/parser/source.js +0 -127
  126. package/dist/src/lib/parser/utils/cache.js +0 -54
  127. package/dist/src/lib/parser/utils/selector.js +0 -440
  128. package/dist/src/lib/validation/config.js +0 -120
@@ -1,18 +1,19 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { TokenField } from '../ast/arena.js';
9
- import { getParsedSyntax, getSyntaxConfig } from './config.js';
10
- import { EnumToken } from '../ast/types.js';
9
+ import { getParsedSyntax, hasSyntax, getSyntax, getSyntaxConfig } from './config.js';
10
+ import { EnumToken, ColorType } from '../ast/types.js';
11
11
  import { ValidationSyntaxGroupEnum, ValidationTokenEnum, MediaFeatureType } from './parser/typedef.js';
12
- import { tokensfuncDefMap, tokensfuncSet, funcLike, mFLT, mFGT } from '../syntax/constants.js';
12
+ import { isFunctionDefType, isFunctionType, funcLike, tokensfuncDefMap, mFLT, mFGT } from '../syntax/constants.js';
13
13
  import { isColor } from '../syntax/syntax.js';
14
14
  import { equalsIgnoreCase, lowercase } from '../parser/utils/text.js';
15
15
  import { parseTokens } from '../parser/parse.js';
16
+ import { matchingAlternatives, isNullable, mayStartWith } from './first.js';
16
17
 
17
18
  const config = getSyntaxConfig();
18
19
  // @ts-expect-error
@@ -101,6 +102,7 @@ const funcTypes = Array.from(tokensfuncDefMap.values());
101
102
  funcTypes.push(EnumToken.FunctionTokenType, EnumToken.PseudoClassFuncTokenType);
102
103
  /**
103
104
  * trim leading and trailing whitespace
105
+ * @param arena
104
106
  * @param tokens
105
107
  * @returns
106
108
  */
@@ -124,6 +126,7 @@ function getMFInfo(featureName) {
124
126
  }
125
127
  /**
126
128
  *
129
+ * @param arena
127
130
  * @param featureName
128
131
  * @param tokens
129
132
  * @param isMFRange
@@ -133,7 +136,8 @@ function getMFInfo(featureName) {
133
136
  */
134
137
  function isMFValue(arena, featureName, tokens, isMFRange) {
135
138
  // mf-value: <number> | <dimension> | <ident> | <ratio>
136
- tokens = tokens.filter((token) => arena.dataTypes[token] !== EnumToken.WhitespaceTokenType && arena.dataTypes[token] !== EnumToken.CommentTokenType);
139
+ tokens = tokens.filter((token) => arena.dataTypes[token] !== EnumToken.WhitespaceTokenType &&
140
+ arena.dataTypes[token] !== EnumToken.CommentTokenType);
137
141
  // if (tokens.length === 0) {
138
142
  // return { valid: true, success: false };
139
143
  // }
@@ -162,38 +166,52 @@ function isMFValue(arena, featureName, tokens, isMFRange) {
162
166
  valid: true,
163
167
  success: tokens.length === 1 &&
164
168
  arena.dataTypes[tokens[0]] === EnumToken.IdenTokenType &&
165
- mediaFeature.values.includes(arena.strings[arena.stringValue[tokens[0]]].toLowerCase()),
169
+ mediaFeature.values.includes(arena.resolveString(arena.stringValue[tokens[0]]).toLowerCase()),
166
170
  };
167
171
  case MediaFeatureType.LengthType:
168
- return { valid: true, success: tokens.length === 1 && arena.dataTypes[tokens[0]] == EnumToken.LengthTokenType };
172
+ return {
173
+ valid: true,
174
+ success: tokens.length === 1 &&
175
+ (arena.dataTypes[tokens[0]] === EnumToken.LengthTokenType ||
176
+ // a length may be the number 0: `(width > 0)`
177
+ (arena.dataTypes[tokens[0]] === EnumToken.NumberTokenType &&
178
+ arena.numericValue[tokens[0]] === 0 &&
179
+ !arena.hasFraction(tokens[0]))),
180
+ };
169
181
  case MediaFeatureType.IntergerType:
170
182
  return {
171
183
  valid: true,
172
184
  success: tokens.length === 1 &&
173
- arena.dataTypes[tokens[0]] == EnumToken.NumberTokenType &&
185
+ arena.dataTypes[tokens[0]] === EnumToken.NumberTokenType &&
174
186
  !arena.numericValue[tokens[0]].toString().includes("."),
175
187
  };
176
188
  case MediaFeatureType.NumberType:
177
189
  return {
178
190
  valid: true,
179
191
  success: tokens.length === 1 &&
180
- arena.dataTypes[tokens[0]] == EnumToken.NumberTokenType &&
192
+ arena.dataTypes[tokens[0]] === EnumToken.NumberTokenType &&
181
193
  !arena.hasFraction(tokens[0]),
182
194
  };
183
195
  case MediaFeatureType.StringType:
184
- return { valid: true, success: tokens.length === 1 && arena.dataTypes[tokens[0]] == EnumToken.StringTokenType };
196
+ return {
197
+ valid: true,
198
+ success: tokens.length === 1 && arena.dataTypes[tokens[0]] === EnumToken.StringTokenType,
199
+ };
185
200
  case MediaFeatureType.ResolutionType:
186
- return { valid: true, success: tokens.length === 1 && arena.dataTypes[tokens[0]] == EnumToken.ResolutionTokenType };
201
+ return {
202
+ valid: true,
203
+ success: tokens.length === 1 && arena.dataTypes[tokens[0]] === EnumToken.ResolutionTokenType,
204
+ };
187
205
  case MediaFeatureType.RatioType:
188
206
  return {
189
207
  valid: true,
190
- success: (tokens.length == 1 && arena.dataTypes[tokens[0]] == EnumToken.NumberTokenType) ||
208
+ success: (tokens.length === 1 && arena.dataTypes[tokens[0]] === EnumToken.NumberTokenType) ||
191
209
  (tokens.length === 3 &&
192
- arena.dataTypes[tokens[0]] == EnumToken.NumberTokenType &&
210
+ arena.dataTypes[tokens[0]] === EnumToken.NumberTokenType &&
193
211
  !arena.hasFraction(tokens[0]) &&
194
- arena.dataTypes[tokens[1]] == EnumToken.LiteralTokenType &&
195
- arena.strings[arena.stringValue[tokens[1]]] === "/" &&
196
- arena.dataTypes[tokens[2]] == EnumToken.NumberTokenType &&
212
+ arena.dataTypes[tokens[1]] === EnumToken.LiteralTokenType &&
213
+ arena.resolveString(arena.stringValue[tokens[1]]) === "/" &&
214
+ arena.dataTypes[tokens[2]] === EnumToken.NumberTokenType &&
197
215
  !arena.hasFraction(tokens[2])),
198
216
  };
199
217
  default:
@@ -226,9 +244,9 @@ class ValidationTokenContext {
226
244
  let index = this.index;
227
245
  let token = this.tokens[++index];
228
246
  while (offset >= 0 && index < this.tokens.length) {
229
- while (this.arena.dataTypes[token] == EnumToken.WhitespaceTokenType ||
230
- this.arena.dataTypes[token] == EnumToken.CommentTokenType ||
231
- this.arena.dataTypes[token] == EnumToken.CDOCOMMTokenType) {
247
+ while (this.arena.dataTypes[token] === EnumToken.WhitespaceTokenType ||
248
+ this.arena.dataTypes[token] === EnumToken.CommentTokenType ||
249
+ this.arena.dataTypes[token] === EnumToken.CDOCOMMTokenType) {
232
250
  token = this.tokens[++index];
233
251
  }
234
252
  if (token == null || offset === 0) {
@@ -242,7 +260,6 @@ class ValidationTokenContext {
242
260
  /**
243
261
  *
244
262
  * @returns
245
- * @param open
246
263
  * @param close
247
264
  * @param counter
248
265
  */
@@ -251,22 +268,32 @@ class ValidationTokenContext {
251
268
  let token = this.tokens[index];
252
269
  // track balanced parens
253
270
  let matchCount = 0;
271
+ // track balanced curly braces: `{a, b}` is a single comma containing value (mixin arguments)
272
+ let blockCount = 0;
254
273
  const tokens = [];
255
- while (index + 1 < this.tokens.length && this.arena.dataTypes[this.tokens[index + 1]] === EnumToken.WhitespaceTokenType) {
274
+ while (index + 1 < this.tokens.length &&
275
+ this.arena.dataTypes[this.tokens[index + 1]] === EnumToken.WhitespaceTokenType) {
256
276
  index++;
257
277
  }
258
278
  while (index + 1 < this.tokens.length) {
259
279
  token = this.tokens[++index];
260
- if (this.arena.dataTypes[token] === EnumToken.StartParensTokenType || tokensfuncDefMap.has(this.arena.dataTypes[token])) {
280
+ if (this.arena.dataTypes[token] === EnumToken.StartParensTokenType ||
281
+ isFunctionDefType(this.arena.dataTypes[token])) {
261
282
  matchCount++;
262
283
  }
263
284
  else if (this.arena.dataTypes[token] === EnumToken.EndParensTokenType) {
264
285
  matchCount--;
265
286
  }
287
+ else if (this.arena.dataTypes[token] === EnumToken.BlockStartTokenType) {
288
+ blockCount++;
289
+ }
290
+ else if (this.arena.dataTypes[token] === EnumToken.BlockEndTokenType && blockCount > 0) {
291
+ blockCount--;
292
+ }
266
293
  if (matchCount >= 0) {
267
294
  tokens.push(token);
268
295
  }
269
- if (matchCount === 0) {
296
+ if (matchCount === 0 && blockCount === 0) {
270
297
  if (close !== EnumToken.EndParensTokenType && this.arena.dataTypes[token] === close) {
271
298
  counter--;
272
299
  }
@@ -286,7 +313,8 @@ class ValidationTokenContext {
286
313
  const tokens = [[]];
287
314
  while (index + 1 < this.tokens.length) {
288
315
  token = this.tokens[++index];
289
- if (this.arena.dataTypes[token] === EnumToken.StartParensTokenType || tokensfuncDefMap.has(this.arena.dataTypes[token])) {
316
+ if (this.arena.dataTypes[token] === EnumToken.StartParensTokenType ||
317
+ isFunctionDefType(this.arena.dataTypes[token])) {
290
318
  matchCount++;
291
319
  }
292
320
  else if (this.arena.dataTypes[token] === EnumToken.EndParensTokenType) {
@@ -322,11 +350,11 @@ class ValidationTokenContext {
322
350
  return token;
323
351
  }
324
352
  slice() {
325
- return new ValidationTokenContext(this.arena, this.index == -1 ? this.tokens.slice() : this.tokens.slice(this.index), this.index == -1 ? -1 : 0);
353
+ return new ValidationTokenContext(this.arena, this.index === -1 ? this.tokens.slice() : this.tokens.slice(this.index), this.index === -1 ? -1 : 0);
326
354
  }
327
355
  update(token) {
328
356
  const index = token == null ? -1 : this.tokens.indexOf(token);
329
- if (index != -1) {
357
+ if (index !== -1) {
330
358
  this.index = index;
331
359
  }
332
360
  return this;
@@ -349,6 +377,7 @@ class ValidationTokenContext {
349
377
  }
350
378
  /**
351
379
  * create validation context
380
+ * @param arena
352
381
  * @param tokens
353
382
  * @returns
354
383
  */
@@ -369,6 +398,7 @@ function createValidationContext(arena, tokens) {
369
398
  }
370
399
  /**
371
400
  * match selector syntax
401
+ * @param arena
372
402
  * @param stream
373
403
  * @param errors
374
404
  * @param options
@@ -382,7 +412,8 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
382
412
  let i = 0;
383
413
  let success = true;
384
414
  while (i < stream.length &&
385
- (arena.dataTypes[stream[i]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[i]] === EnumToken.CommentTokenType)) {
415
+ (arena.dataTypes[stream[i]] === EnumToken.WhitespaceTokenType ||
416
+ arena.dataTypes[stream[i]] === EnumToken.CommentTokenType)) {
386
417
  i++;
387
418
  }
388
419
  if (i < stream.length) {
@@ -403,8 +434,10 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
403
434
  message: `Unexpected token ${EnumToken[arena.dataTypes[stream[i]]]}`,
404
435
  node: stream[i],
405
436
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
406
- // @ts-expect-error
407
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
437
+ location: [
438
+ options.source.file,
439
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i])),
440
+ ],
408
441
  },
409
442
  ],
410
443
  };
@@ -417,7 +450,7 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
417
450
  arena.dataTypes[token] = EnumToken.UniversalSelectorTokenType;
418
451
  }
419
452
  tokens.push(token);
420
- if (tokensfuncDefMap.has(arena.dataTypes[token])) {
453
+ if (isFunctionDefType(arena.dataTypes[token])) {
421
454
  if (stack.length > 0 && selectorNodeTypes.includes(arena.dataTypes[stack[stack.length - 1]])) {
422
455
  stack.pop();
423
456
  }
@@ -435,7 +468,7 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
435
468
  switch (arena.dataTypes[token]) {
436
469
  case EnumToken.PseudoClassFuncTokenType:
437
470
  {
438
- const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, arena.strings[arena.stringValue[token]] + "()")?.[0]?.chi ?? [], createValidationContext(arena, arena.tokens(token)), options);
471
+ const result = matchAllSyntaxes(arena, selectorFunctionSyntax(arena, token)?.[0]?.chi ?? [], createValidationContext(arena, arena.tokens(token)), options);
439
472
  if (!result.success) {
440
473
  success = false;
441
474
  if (result.errors.length > 0) {
@@ -460,8 +493,10 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
460
493
  message: `Nesting selector is not allowed`,
461
494
  node: token,
462
495
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(token))
463
- // @ts-expect-error
464
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(token))],
496
+ location: [
497
+ options.source.file,
498
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(token)),
499
+ ],
465
500
  },
466
501
  ],
467
502
  };
@@ -474,9 +509,12 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
474
509
  case EnumToken.Tilda:
475
510
  case EnumToken.GtTokenType:
476
511
  arena.dataTypes[token] = selectorEnumMap.get(arena.dataTypes[token]);
512
+ case EnumToken.UniversalSelectorTokenType:
513
+ // `*` is a type selector, not a combinator: the whitespace that follows it is a descendant
514
+ // combinator and is kept. `.a * .b` is not `.a .b`
515
+ break;
477
516
  case EnumToken.ColumnCombinatorTokenType:
478
517
  case EnumToken.ChildCombinatorTokenType:
479
- case EnumToken.UniversalSelectorTokenType:
480
518
  case EnumToken.DescendantCombinatorTokenType:
481
519
  case EnumToken.NextSiblingCombinatorTokenType:
482
520
  case EnumToken.SubsequentSiblingCombinatorTokenType:
@@ -484,11 +522,13 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
484
522
  i++;
485
523
  }
486
524
  // inlined: arena.typeOf(stack.at(-1))
487
- if (stack.length > 0 && arena.dataTypes[stack.at(-1)] === EnumToken.UniversalSelectorTokenType) {
525
+ if (stack.length > 0 &&
526
+ arena.dataTypes[stack.at(-1)] === EnumToken.UniversalSelectorTokenType) {
488
527
  stack.pop();
489
528
  }
490
529
  // inlined: arena.typeOf(stack.at(-1))
491
- if (stack.length > 0 && selectorNodeTypes.includes((stack.at(-1) == null ? null : arena.dataTypes[stack.at(-1)]))) {
530
+ if (stack.length > 0 &&
531
+ selectorNodeTypes.includes((stack.at(-1) == null ? null : arena.dataTypes[stack.at(-1)]))) {
492
532
  return {
493
533
  success: false,
494
534
  errors: [
@@ -497,8 +537,10 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
497
537
  message: `Unexpected combinator ${EnumToken[arena.dataTypes[token]]}`,
498
538
  node: token,
499
539
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(token))
500
- // @ts-expect-error
501
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(token))],
540
+ location: [
541
+ options.source.file,
542
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(token)),
543
+ ],
502
544
  },
503
545
  ],
504
546
  };
@@ -506,7 +548,8 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
506
548
  stack.push(token);
507
549
  break;
508
550
  case EnumToken.CommaTokenType:
509
- if (stack.length > 0 && arena.dataTypes[stack[stack.length - 1]] === EnumToken.UniversalSelectorTokenType) {
551
+ if (stack.length > 0 &&
552
+ arena.dataTypes[stack[stack.length - 1]] === EnumToken.UniversalSelectorTokenType) {
510
553
  stack.pop();
511
554
  }
512
555
  stack.push(token);
@@ -530,7 +573,8 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
530
573
  stack.push(token);
531
574
  break;
532
575
  case EnumToken.AttrEndTokenType:
533
- if (stack.length > 0 && arena.dataTypes[stack[stack.length - 1]] === EnumToken.UniversalSelectorTokenType) {
576
+ if (stack.length > 0 &&
577
+ arena.dataTypes[stack[stack.length - 1]] === EnumToken.UniversalSelectorTokenType) {
534
578
  stack.pop();
535
579
  }
536
580
  // inlined: arena.typeOf(stack.at(-1))
@@ -543,8 +587,10 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
543
587
  message: `Unexpected token ${EnumToken[arena.dataTypes[token]]}`,
544
588
  node: token,
545
589
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(token))
546
- // @ts-expect-error
547
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(token))],
590
+ location: [
591
+ options.source.file,
592
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(token)),
593
+ ],
548
594
  },
549
595
  ],
550
596
  };
@@ -574,9 +620,9 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
574
620
  slice.shift();
575
621
  slice.shift();
576
622
  if (arena.dataTypes[slice[0]] === EnumToken.StringTokenType &&
577
- /^[a-zA-Z0-9_-]+$/.test(arena.strings[arena.stringValue[slice[0]]].slice(1, -1))) {
578
- // inlined: arena.setStr(slice[0], arena.strings[arena.stringValue[slice[0]]].slice(1, -1))
579
- arena.stringValue[slice[0]] = arena.internString(arena.strings[arena.stringValue[slice[0]]].slice(1, -1));
623
+ /^[a-zA-Z0-9_-]+$/.test(arena.resolveString(arena.stringValue[slice[0]]).slice(1, -1))) {
624
+ // inlined: arena.setStr(slice[0], arena.resolveString(arena.stringValue[slice[0]]).slice(1, -1))
625
+ arena.stringValue[slice[0]] = arena.internString(arena.resolveString(arena.stringValue[slice[0]]).slice(1, -1));
580
626
  arena.dataTypes[slice[0]] = EnumToken.IdenTokenType;
581
627
  }
582
628
  slice.shift();
@@ -585,7 +631,8 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
585
631
  break;
586
632
  }
587
633
  if (arena.dataTypes[slice[0]] !== EnumToken.IdenTokenType ||
588
- (arena.strings[arena.stringValue[slice[0]]] != "i" && arena.strings[arena.stringValue[slice[0]]] != "s")) {
634
+ (arena.resolveString(arena.stringValue[slice[0]]) !== "i" &&
635
+ arena.resolveString(arena.stringValue[slice[0]]) !== "s")) {
589
636
  return {
590
637
  success: false,
591
638
  errors: [
@@ -594,8 +641,10 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
594
641
  message: `Unexpected token ${EnumToken[arena.dataTypes[slice[0]]]}`,
595
642
  node: slice[0],
596
643
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(slice[0]))
597
- // @ts-expect-error
598
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(slice[0]))],
644
+ location: [
645
+ options.source.file,
646
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(slice[0])),
647
+ ],
599
648
  },
600
649
  ],
601
650
  };
@@ -613,14 +662,17 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
613
662
  message: `Unexpected token ${EnumToken[arena.dataTypes[token]]}`,
614
663
  node: token,
615
664
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(token))
616
- // @ts-expect-error
617
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(token))],
665
+ location: [
666
+ options.source.file,
667
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(token)),
668
+ ],
618
669
  },
619
670
  ],
620
671
  };
621
672
  case EnumToken.EndParensTokenType:
622
673
  // inlined: arena.typeOf(stack.at(-1))
623
- if (stack.length > 0 && arena.dataTypes[stack.at(-1)] === EnumToken.UniversalSelectorTokenType) {
674
+ if (stack.length > 0 &&
675
+ arena.dataTypes[stack.at(-1)] === EnumToken.UniversalSelectorTokenType) {
624
676
  stack.pop();
625
677
  }
626
678
  if (
@@ -630,7 +682,7 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
630
682
  arena.dataTypes[stack.at(-1)] === EnumToken.PseudoElementTokenType) {
631
683
  const token = stack.at(-1);
632
684
  const index = tokens.indexOf(token);
633
- const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, arena.strings[arena.stringValue[token]] + "()")?.[0]?.chi ?? [], createValidationContext(arena, tokens.slice(index + 1, tokens.length - 1)), options);
685
+ const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, arena.resolveString(arena.stringValue[token]) + "()")?.[0]?.chi ?? [], createValidationContext(arena, tokens.slice(index + 1, tokens.length - 1)), options);
634
686
  if (!result.success) {
635
687
  success = false;
636
688
  if (result.errors.length > 0) {
@@ -650,8 +702,10 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
650
702
  message: `Unexpected token ${EnumToken[arena.dataTypes[token]]}`,
651
703
  node: token,
652
704
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(token))
653
- // @ts-expect-error
654
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(token))],
705
+ location: [
706
+ options.source.file,
707
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(token)),
708
+ ],
655
709
  },
656
710
  ],
657
711
  };
@@ -671,8 +725,10 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
671
725
  message: `Unsupported selector token ${EnumToken[arena.dataTypes[token]]}`,
672
726
  node: token,
673
727
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(token))
674
- // @ts-expect-error
675
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(token))],
728
+ location: [
729
+ options.source.file,
730
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(token)),
731
+ ],
676
732
  },
677
733
  ],
678
734
  };
@@ -685,7 +741,8 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
685
741
  tokens.pop();
686
742
  // inlined: arena.typeOf(token), arena.typeOf(tokens.at(-2))
687
743
  }
688
- else if (trimWhitespaceBefore.includes(arena.dataTypes[token]) && arena.dataTypes[tokens.at(-2)] === EnumToken.WhitespaceTokenType) {
744
+ else if (trimWhitespaceBefore.includes(arena.dataTypes[token]) &&
745
+ arena.dataTypes[tokens.at(-2)] === EnumToken.WhitespaceTokenType) {
689
746
  tokens.splice(tokens.length - 2, 1);
690
747
  }
691
748
  }
@@ -701,8 +758,10 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
701
758
  message: `Unmatched token ${EnumToken[arena.dataTypes[stack[stack.length - 1]]]}`,
702
759
  node: stack[stack.length - 1],
703
760
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(stack[stack.length - 1]))
704
- // @ts-expect-error
705
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stack[stack.length - 1]))],
761
+ location: [
762
+ options.source.file,
763
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(stack[stack.length - 1])),
764
+ ],
706
765
  },
707
766
  ],
708
767
  };
@@ -715,10 +774,51 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
715
774
  }
716
775
  /**
717
776
  * declaration validations (successful and failed), by string table of the arena, by syntax and by hash of the value
718
- * encoding (see {@link encodeToken}). the encoding contains interned string ids: the string table is the key, it is
719
- * replaced when the arena is compacted and the ids change (see {@link AstArena.compact})
777
+ * encoding (see {@link encodeToken}). the encoding contains interned string ids: the identity of the string ids is the
778
+ * key, it is replaced when the arena is compacted and the ids change (see {@link AstArena.stringEpoch})
720
779
  */
721
780
  const declarationValidations = new WeakMap();
781
+ /**
782
+ * the validation cache of the string table of an arena
783
+ * @param arena
784
+ * @param create create the cache when missing
785
+ */
786
+ function validationCache(arena, create) {
787
+ let cache = declarationValidations.get(arena.stringEpoch);
788
+ if (cache === undefined) {
789
+ if (!create) {
790
+ return null;
791
+ }
792
+ cache = { bySyntax: new Map(), pool: new Float64Array(1024), used: 0 };
793
+ declarationValidations.set(arena.stringEpoch, cache);
794
+ }
795
+ return cache;
796
+ }
797
+ /**
798
+ * copy the current encoding into the pool of the cache (see {@link ValidationCache.pool})
799
+ * @param arena
800
+ * @returns the offset of the encoding in the pool
801
+ */
802
+ function storeEncoding(arena) {
803
+ const cache = validationCache(arena, true);
804
+ const offset = cache.used;
805
+ if (offset + encodingLength > cache.pool.length) {
806
+ let length = cache.pool.length * 2;
807
+ while (length < offset + encodingLength) {
808
+ length *= 2;
809
+ }
810
+ const pool = new Float64Array(length);
811
+ pool.set(cache.pool.subarray(0, offset));
812
+ cache.pool = pool;
813
+ }
814
+ const pool = cache.pool;
815
+ // a loop: subarray() would allocate a view
816
+ for (let i = 0; i < encodingLength; i++) {
817
+ pool[offset + i] = encoding[i];
818
+ }
819
+ cache.used = offset + encodingLength;
820
+ return offset;
821
+ }
722
822
  /**
723
823
  * shared result of a declaration whose validation is cached
724
824
  */
@@ -743,7 +843,7 @@ let encodingHash = 0;
743
843
  * @param value
744
844
  */
745
845
  function encode(value) {
746
- if (encodingLength == encoding.length) {
846
+ if (encodingLength === encoding.length) {
747
847
  const previous = encoding;
748
848
  encoding = new Float64Array(previous.length * 2);
749
849
  encoding.set(previous);
@@ -753,27 +853,31 @@ function encode(value) {
753
853
  }
754
854
  /**
755
855
  * encode a token and its descendants. the validation only depends on these fields: two values with the same encoding
756
- * are validated the same way
856
+ * are validated the same way. the errors of a failed validation do not depend on the content of the strings either:
857
+ * they reference the tokens by position and their messages only contain token types
757
858
  * @param arena
758
859
  * @param token
759
860
  */
760
861
  function encodeToken(arena, token) {
761
- encode(arena.dataTypes[token]);
862
+ const typ = arena.dataTypes[token];
863
+ encode(typ);
762
864
  encode(arena.name[token]);
763
- encode(arena.stringValue[token]);
865
+ // the validation does not read the content of the strings (only their type): `content: "\f101"` and
866
+ // `content: "\f102"` are validated once
867
+ encode(typ === EnumToken.StringTokenType ? 0 : arena.stringValue[token]);
764
868
  encode(arena.numericValue[token]);
765
869
  encode(arena.unit[token]);
766
870
  encode(arena.colorType[token]);
767
- if (arena.firstToken[token] != 0) {
871
+ if (arena.firstToken[token] !== 0) {
768
872
  encode(-1);
769
- for (let child = arena.firstToken[token]; child != 0; child = arena.nextSibling[child]) {
873
+ for (let child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
770
874
  encodeToken(arena, child);
771
875
  }
772
876
  encode(-2);
773
877
  }
774
- if (arena.firstChild[token] != 0) {
878
+ if (arena.firstChild[token] !== 0) {
775
879
  encode(-3);
776
- for (let child = arena.firstChild[token]; child != 0; child = arena.nextSibling[child]) {
880
+ for (let child = arena.firstChild[token]; child !== 0; child = arena.nextSibling[child]) {
777
881
  encodeToken(arena, child);
778
882
  }
779
883
  encode(-4);
@@ -786,14 +890,11 @@ function encodeToken(arena, token) {
786
890
  * @param create create the bucket when missing
787
891
  */
788
892
  function cachedValidations(arena, syntaxes, create) {
789
- let bySyntax = declarationValidations.get(arena.strings);
790
- if (bySyntax == null) {
791
- if (!create) {
792
- return null;
793
- }
794
- bySyntax = new Map();
795
- declarationValidations.set(arena.strings, bySyntax);
893
+ const cache = validationCache(arena, create);
894
+ if (cache === null) {
895
+ return null;
796
896
  }
897
+ const bySyntax = cache.bySyntax;
797
898
  let byHash = bySyntax.get(syntaxes);
798
899
  if (byHash == null) {
799
900
  if (!create) {
@@ -817,14 +918,15 @@ function cachedValidations(arena, syntaxes, create) {
817
918
  function findValidation(arena, syntaxes) {
818
919
  const bucket = cachedValidations(arena, syntaxes, false);
819
920
  if (bucket != null) {
921
+ const pool = validationCache(arena, false).pool;
820
922
  for (let i = 0; i < bucket.length; i++) {
821
- const candidate = bucket[i].encoding;
822
- if (candidate.length == encodingLength) {
923
+ const offset = bucket[i].offset;
924
+ if (bucket[i].length === encodingLength) {
823
925
  let k = 0;
824
- while (k < encodingLength && candidate[k] === encoding[k]) {
926
+ while (k < encodingLength && pool[offset + k] === encoding[k]) {
825
927
  k++;
826
928
  }
827
- if (k == encodingLength) {
929
+ if (k === encodingLength) {
828
930
  return bucket[i];
829
931
  }
830
932
  }
@@ -836,14 +938,15 @@ function findValidation(arena, syntaxes) {
836
938
  * cache the validation of an encoding for the syntax
837
939
  * @param arena
838
940
  * @param syntaxes
839
- * @param value encoding
941
+ * @param offset offset of the encoding in the pool of the cache, see {@link storeEncoding}
942
+ * @param length length of the encoding
840
943
  * @param hash encoding hash
841
944
  * @param valid
842
945
  * @param errors errors of a failed validation, null when unknown
843
946
  */
844
- function cacheValidation(arena, syntaxes, value, hash, valid, errors) {
947
+ function cacheValidation(arena, syntaxes, offset, length, hash, valid, errors) {
845
948
  encodingHash = hash;
846
- cachedValidations(arena, syntaxes, true).push({ encoding: value, valid, errors });
949
+ cachedValidations(arena, syntaxes, true).push({ offset, length, valid, errors });
847
950
  }
848
951
  /**
849
952
  * the value tokens and their descendants, in the encoding order (see {@link encodeToken})
@@ -853,10 +956,10 @@ function cacheValidation(arena, syntaxes, value, hash, valid, errors) {
853
956
  */
854
957
  function collectTokens(arena, token, result) {
855
958
  result.push(token);
856
- for (let child = arena.firstToken[token]; child != 0; child = arena.nextSibling[child]) {
959
+ for (let child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
857
960
  collectTokens(arena, child, result);
858
961
  }
859
- for (let child = arena.firstChild[token]; child != 0; child = arena.nextSibling[child]) {
962
+ for (let child = arena.firstChild[token]; child !== 0; child = arena.nextSibling[child]) {
860
963
  collectTokens(arena, child, result);
861
964
  }
862
965
  }
@@ -884,34 +987,39 @@ function bindErrors(arena, tokens, errors, options) {
884
987
  let location = -1;
885
988
  if (error.node != null) {
886
989
  node = tokens.indexOf(error.node);
887
- if (node == -1) {
990
+ if (node === -1) {
888
991
  return null;
889
992
  }
890
993
  }
891
994
  if (error.location != null) {
892
995
  // the location is computed from a token of the value: the error node, the last token, ...
893
- if (node != -1 &&
996
+ if (node !== -1 &&
894
997
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(tokens[node]))
895
- sameLocation(error.location, [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[node]))])) {
998
+ sameLocation(error.location, [
999
+ options.source.file,
1000
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[node])),
1001
+ ])) {
896
1002
  location = node;
897
1003
  }
898
1004
  else {
899
1005
  for (let k = 0; k < tokens.length; k++) {
900
1006
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(tokens[k]))
901
- if (sameLocation(error.location, [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[k]))])) {
1007
+ if (sameLocation(error.location, [
1008
+ options.source.file,
1009
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[k])),
1010
+ ])) {
902
1011
  location = k;
903
1012
  break;
904
1013
  }
905
1014
  }
906
- if (location == -1) {
1015
+ if (location === -1) {
907
1016
  return null;
908
1017
  }
909
1018
  }
910
1019
  }
911
- const copy = { ...error };
912
- delete copy.node;
913
- delete copy.location;
914
- result.push({ error: copy, node, location });
1020
+ // the node and the location keep their place in the keys: they are replaced when the errors are rebuilt, the
1021
+ // rebuilt errors have the same shape as the errors of a validation
1022
+ result.push({ error: { ...error }, node, location });
915
1023
  }
916
1024
  return result;
917
1025
  }
@@ -926,12 +1034,15 @@ function rebuildErrors(arena, tokens, errors, options) {
926
1034
  const result = [];
927
1035
  for (let i = 0; i < errors.length; i++) {
928
1036
  const error = { ...errors[i].error };
929
- if (errors[i].node != -1) {
1037
+ if (errors[i].node !== -1) {
930
1038
  error.node = tokens[errors[i].node];
931
1039
  }
932
- if (errors[i].location != -1) {
1040
+ if (errors[i].location !== -1) {
933
1041
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(tokens[errors[i].location]))
934
- error.location = [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[errors[i].location]))];
1042
+ error.location = [
1043
+ options.source.file,
1044
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[errors[i].location])),
1045
+ ];
935
1046
  }
936
1047
  result.push(error);
937
1048
  }
@@ -980,12 +1091,13 @@ function matchDeclarationSyntax(arena, syntaxes, tokens, options) {
980
1091
  };
981
1092
  }
982
1093
  }
983
- // the encoding is copied before the validation, which may encode other values
984
- const value = encoding.slice(0, encodingLength);
1094
+ // the encoding is stored before the validation, which may encode other values
1095
+ const length = encodingLength;
1096
+ const offset = storeEncoding(arena);
985
1097
  const hash = encodingHash;
986
1098
  const result = matchAllSyntaxes(arena, syntaxes, createValidationContext(arena, tokens), options);
987
1099
  if (result.success) {
988
- cacheValidation(arena, syntaxes, value, hash, true, null);
1100
+ cacheValidation(arena, syntaxes, offset, length, hash, true, null);
989
1101
  }
990
1102
  else {
991
1103
  const errors = bindErrors(arena, valueTokens(arena, tokens), result.errors, options);
@@ -994,7 +1106,7 @@ function matchDeclarationSyntax(arena, syntaxes, tokens, options) {
994
1106
  cached.errors = errors;
995
1107
  }
996
1108
  else {
997
- cacheValidation(arena, syntaxes, value, hash, false, errors);
1109
+ cacheValidation(arena, syntaxes, offset, length, hash, false, errors);
998
1110
  }
999
1111
  }
1000
1112
  return result;
@@ -1011,55 +1123,114 @@ function matchDeclarationSyntax(arena, syntaxes, tokens, options) {
1011
1123
  function isValidDeclarationValue(arena, syntaxes, declaration, options) {
1012
1124
  encodingLength = 0;
1013
1125
  encodingHash = 0x811c9dc5;
1014
- for (let token = arena.firstToken[declaration]; token != 0; token = arena.nextSibling[token]) {
1126
+ for (let token = arena.firstToken[declaration]; token !== 0; token = arena.nextSibling[token]) {
1015
1127
  encodeToken(arena, token);
1016
1128
  }
1017
1129
  const cached = findValidation(arena, syntaxes);
1018
1130
  if (cached != null) {
1019
1131
  return cached.valid;
1020
1132
  }
1021
- // the encoding is copied before the validation, which may encode other values
1022
- const value = encoding.slice(0, encodingLength);
1133
+ // the encoding is stored before the validation, which may encode other values
1134
+ const length = encodingLength;
1135
+ const offset = storeEncoding(arena);
1023
1136
  const hash = encodingHash;
1024
1137
  const valid = matchAllSyntaxes(arena, syntaxes, createValidationContext(arena, arena.tokens(declaration)), options).success;
1025
1138
  // the errors are not needed: they are computed if a declaration with the same value is validated by the parser
1026
- cacheValidation(arena, syntaxes, value, hash, valid, null);
1139
+ cacheValidation(arena, syntaxes, offset, length, hash, valid, null);
1027
1140
  return valid;
1028
1141
  }
1142
+ /**
1143
+ * the syntaxes being matched and the token they are matched at (a stack, see {@link enterSyntax}): a syntax that is
1144
+ * matched again at the same token while it is being matched is a cyclic definition. the matched syntaxes are set to
1145
+ * null. the entries of a call of {@link matchAllSyntaxes} start at {@link visitedBase}
1146
+ */
1147
+ let visitedTokens = new Int32Array(256);
1148
+ /**
1149
+ * see {@link visitedTokens}
1150
+ */
1151
+ const visitedSyntaxes = [];
1152
+ /**
1153
+ * number of entries of {@link visitedTokens}
1154
+ */
1155
+ let visitedLength = 0;
1156
+ /**
1157
+ * first entry of the current call of {@link matchAllSyntaxes}
1158
+ */
1159
+ let visitedBase = 0;
1160
+ /**
1161
+ * true if a syntax is being matched at a token
1162
+ * @param token
1163
+ * @param syntax
1164
+ */
1165
+ function isVisited(token, syntax) {
1166
+ for (let i = visitedBase; i < visitedLength; i++) {
1167
+ if (visitedTokens[i] === token && visitedSyntaxes[i] === syntax) {
1168
+ return true;
1169
+ }
1170
+ }
1171
+ return false;
1172
+ }
1173
+ /**
1174
+ * record that a syntax is being matched at a token
1175
+ * @param token
1176
+ * @param syntax
1177
+ * @returns the entry, released when the syntax is no longer being matched (`visitedLength = entry`)
1178
+ */
1179
+ function enterSyntax(token, syntax) {
1180
+ if (visitedLength === visitedTokens.length) {
1181
+ const tokens = new Int32Array(visitedTokens.length * 2);
1182
+ tokens.set(visitedTokens);
1183
+ visitedTokens = tokens;
1184
+ }
1185
+ visitedTokens[visitedLength] = token;
1186
+ visitedSyntaxes[visitedLength] = syntax;
1187
+ return visitedLength++;
1188
+ }
1029
1189
  /**
1030
1190
  * matches all syntaxes
1191
+ * @param arena
1031
1192
  * @param syntaxes
1032
1193
  * @param context
1033
1194
  * @param options
1034
1195
  * @returns
1035
1196
  */
1036
1197
  function matchAllSyntaxes(arena, syntaxes, context, options) {
1037
- // the visited syntaxes are tracked per call. the options are not copied: the previous map is restored
1038
- const visited = options.visited;
1198
+ // the syntaxes being matched are tracked per call: the syntaxes being matched by the caller are not seen
1199
+ const base = visitedBase;
1200
+ const length = visitedLength;
1039
1201
  // the result object is passed to the matching functions, which fill it
1040
1202
  const out = createMatch();
1041
1203
  let result;
1042
- options.visited = new Map();
1204
+ visitedBase = visitedLength;
1043
1205
  try {
1044
1206
  result = matchSyntax(arena, syntaxes, context, options, out);
1045
1207
  }
1046
1208
  finally {
1047
- options.visited = visited;
1209
+ visitedBase = base;
1210
+ visitedLength = length;
1048
1211
  }
1049
1212
  if (syntaxes != null && result.success && result.syntaxToken != null) {
1050
1213
  const index = syntaxes.indexOf(result.syntaxToken);
1051
- if (index != -1) {
1052
- for (let i = index; i < syntaxes.length; i++) {
1053
- if (syntaxes[i].typ == ValidationTokenEnum.Whitespace ||
1054
- syntaxes[i].isOptional ||
1055
- syntaxes[i].isRepeatable ||
1056
- syntaxes[i].match?.min?.val === 0) {
1057
- continue;
1058
- }
1059
- return setMatch(out, false, result.valid, result.token, result.context, syntaxes[i], ownErrors(result.errors));
1214
+ if (index !== -1) {
1215
+ const mandatory = findMandatorySyntax(syntaxes, index);
1216
+ if (mandatory !== -1) {
1217
+ return setMatch(out, false, result.valid, result.token, result.context, syntaxes[mandatory], ownErrors(result.errors));
1060
1218
  }
1061
1219
  }
1062
1220
  }
1221
+ // the syntaxes are exhausted, the tokens must be consumed: `color: red blue`
1222
+ if (syntaxes != null && result.success && !result.context.done()) {
1223
+ const token = result.context.peek();
1224
+ return setMatch(out, false, result.valid, token, result.context, null, [
1225
+ {
1226
+ action: "drop",
1227
+ message: "unexpected token",
1228
+ node: token,
1229
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(token))
1230
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(token))],
1231
+ },
1232
+ ]);
1233
+ }
1063
1234
  return setMatch(out, result.success, result.valid, result.token, result.context, !result.success ? result.syntaxToken : null, !result.success && result.errors.length === 0
1064
1235
  ? [
1065
1236
  {
@@ -1068,13 +1239,32 @@ function matchAllSyntaxes(arena, syntaxes, context, options) {
1068
1239
  node: result.token,
1069
1240
  syntax: result.syntaxToken,
1070
1241
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(result.token ?? (context.tokens[context.tokens.length - 1] as Token)))
1071
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(result.token ?? context.tokens[context.tokens.length - 1]))],
1242
+ location: [
1243
+ options.source.file,
1244
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(result.token ?? context.tokens[context.tokens.length - 1])),
1245
+ ],
1072
1246
  },
1073
1247
  ]
1074
1248
  : ownErrors(result.errors));
1075
1249
  }
1250
+ /**
1251
+ * number of the mandatory commas of a list item: the items of `[ <number [0,1]>, <number> ]#{2}` span two values
1252
+ * @param syntax
1253
+ */
1254
+ function countItemCommas(syntax) {
1255
+ let count = 0;
1256
+ if (syntax.typ === ValidationTokenEnum.Bracket) {
1257
+ for (const token of syntax.chi) {
1258
+ if (token.typ === ValidationTokenEnum.Comma) {
1259
+ count++;
1260
+ }
1261
+ }
1262
+ }
1263
+ return count;
1264
+ }
1076
1265
  /**
1077
1266
  * matches a list of syntaxes
1267
+ * @param arena
1078
1268
  * @param syntax
1079
1269
  * @param context
1080
1270
  * @param options
@@ -1082,18 +1272,32 @@ function matchAllSyntaxes(arena, syntaxes, context, options) {
1082
1272
  */
1083
1273
  function matchListSyntax(arena, syntax, context, options) {
1084
1274
  const { isList, match, isOptional, ...rest } = syntax;
1085
- let success = true;
1275
+ let success;
1086
1276
  let result = null;
1087
1277
  let tmpResult;
1088
1278
  let count = 0;
1089
1279
  let range;
1090
1280
  let done = false;
1281
+ // an item that contains commas spans several comma separated values
1282
+ const commas = countItemCommas(rest);
1091
1283
  success = false;
1092
1284
  do {
1093
- range = context.peekRange(EnumToken.CommaTokenType, 1);
1285
+ range = context.peekRange(EnumToken.CommaTokenType, 1 + commas);
1094
1286
  tmpResult = matchSyntax(arena, [rest], createValidationContext(arena,
1095
1287
  // inlined: arena.typeOf(range.at(-1))
1096
1288
  arena.dataTypes[range.at(-1)] === EnumToken.CommaTokenType ? range.slice(0, -1) : range), options);
1289
+ if (tmpResult.success && !tmpResult.context.done()) {
1290
+ // the item does not match all the tokens before the next comma: the list ends with this item, the next
1291
+ // syntaxes match the remaining tokens. `<ident># from <ident>` matches `a, b from c`
1292
+ const current = tmpResult.context.current();
1293
+ if (current != null) {
1294
+ count++;
1295
+ success = true;
1296
+ result = tmpResult;
1297
+ context.update(current);
1298
+ }
1299
+ break;
1300
+ }
1097
1301
  if (tmpResult.success) {
1098
1302
  count++;
1099
1303
  success = true;
@@ -1101,7 +1305,7 @@ function matchListSyntax(arena, syntax, context, options) {
1101
1305
  if (Number.isFinite(match?.max?.val) && count === match.max.val) {
1102
1306
  context.update(
1103
1307
  // inlined: arena.typeOf(range.at(-1))
1104
- arena.dataTypes[range.at(-1)] === EnumToken.CommaTokenType ? (range.at(-2)) : (range.at(-1)));
1308
+ arena.dataTypes[range.at(-1)] === EnumToken.CommaTokenType ? range.at(-2) : range.at(-1));
1105
1309
  break;
1106
1310
  }
1107
1311
  else {
@@ -1110,6 +1314,10 @@ function matchListSyntax(arena, syntax, context, options) {
1110
1314
  done = context.done();
1111
1315
  }
1112
1316
  } while (tmpResult.success && !done);
1317
+ // <foo>#{2}: too few items
1318
+ if (result != null && match?.min?.val != null && count < match.min.val) {
1319
+ result = null;
1320
+ }
1113
1321
  return result == null
1114
1322
  ? {
1115
1323
  success: false,
@@ -1128,6 +1336,7 @@ function matchListSyntax(arena, syntax, context, options) {
1128
1336
  }
1129
1337
  /**
1130
1338
  * matches a list of syntaxes
1339
+ * @param arena
1131
1340
  * @param syntax
1132
1341
  * @param context
1133
1342
  * @param options
@@ -1147,7 +1356,8 @@ function matchOccurenceSyntax(arena, syntax, context, options) {
1147
1356
  context.end();
1148
1357
  break;
1149
1358
  }
1150
- if (tmpResult.context.index === context.index) {
1359
+ // no token matched. the indexes of the contexts are not comparable: the result context is a slice
1360
+ if (tmpResult.context.current() === context.current()) {
1151
1361
  break;
1152
1362
  }
1153
1363
  context.update(tmpResult.context.current());
@@ -1184,6 +1394,215 @@ function matchOccurenceSyntax(arena, syntax, context, options) {
1184
1394
  * @param options
1185
1395
  * @returns
1186
1396
  */
1397
+ /**
1398
+ * index of the first syntax that must be matched, starting at index. -1 if the remaining syntaxes may match no token
1399
+ * @param syntaxes
1400
+ * @param index
1401
+ */
1402
+ function findMandatorySyntax(syntaxes, index) {
1403
+ for (let i = index; i < syntaxes.length; i++) {
1404
+ if (!isNullable(syntaxes[i])) {
1405
+ return i;
1406
+ }
1407
+ }
1408
+ return -1;
1409
+ }
1410
+ /**
1411
+ * true if a token is a literal number or dimension of a syntax: `0`, `90deg`. the unit is case-insensitive
1412
+ * @param arena
1413
+ * @param token
1414
+ * @param syntax
1415
+ */
1416
+ function isLiteralNumber(arena, token, syntax) {
1417
+ if (arena.hasFraction(token) || arena.numericValue[token] !== syntax.val) {
1418
+ return false;
1419
+ }
1420
+ if (syntax.typ === ValidationTokenEnum.Number) {
1421
+ return arena.dataTypes[token] === EnumToken.NumberTokenType;
1422
+ }
1423
+ const type = EnumToken[syntax.unit];
1424
+ return (arena.dataTypes[token] === type &&
1425
+ equalsIgnoreCase(arena.resolveString(arena.unit[token]), syntax.unitText));
1426
+ }
1427
+ /**
1428
+ * true if a numeric token is in the range of the syntax: `<integer [1,∞]>`
1429
+ * @param arena
1430
+ * @param token
1431
+ * @param syntax
1432
+ */
1433
+ function isInRange(arena, token, syntax) {
1434
+ if (syntax.range == null ||
1435
+ arena.dataTypes[token] === EnumToken.MathFunctionTokenType ||
1436
+ arena.dataTypes[token] === EnumToken.MathFunctionTokenDefType ||
1437
+ arena.dataTypes[token] === EnumToken.WildCardFunctionTokenType ||
1438
+ arena.dataTypes[token] === EnumToken.WildCardFunctionTokenDefType) {
1439
+ return true;
1440
+ }
1441
+ const range = syntax.range;
1442
+ // a null lower bound is -∞
1443
+ let success = !arena.hasFraction(token) && (range.min == null || arena.numericValue[token] >= range.min.val);
1444
+ if (range.min?.typ === ValidationTokenEnum.Dimension) {
1445
+ success =
1446
+ arena.dataTypes[token] ===
1447
+ EnumToken[
1448
+ // @ts-expect-error
1449
+ range.min.unit];
1450
+ }
1451
+ if (success && range.max != null) {
1452
+ success = arena.numericValue[token] <= range.max.val;
1453
+ }
1454
+ return success;
1455
+ }
1456
+ /**
1457
+ * the validation syntax of a function token (`name()`): looked up once per function name
1458
+ * @param arena
1459
+ * @param token
1460
+ */
1461
+ function functionSyntax(arena, token) {
1462
+ syntaxArena = arena;
1463
+ return arena.stringDerived(arena.stringValue[token], 0 /* StringDerived.FunctionSyntax */, computeFunctionSyntax);
1464
+ }
1465
+ /**
1466
+ * the arena of the function names read by {@link computeFunctionSyntax} and {@link computeSelectorFunctionSyntax}:
1467
+ * the functions passed to the string table are not closures (no allocation per call)
1468
+ */
1469
+ let syntaxArena = null;
1470
+ /**
1471
+ * see {@link functionSyntax}
1472
+ * @param id
1473
+ */
1474
+ function computeFunctionSyntax(id) {
1475
+ return getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, syntaxArena.resolveString(id) + "()");
1476
+ }
1477
+ /**
1478
+ * see {@link selectorFunctionSyntax}
1479
+ * @param id
1480
+ */
1481
+ function computeSelectorFunctionSyntax(id) {
1482
+ return getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, syntaxArena.resolveString(id) + "()");
1483
+ }
1484
+ /**
1485
+ * the validation syntax of a selector function token (`:name()`): looked up once per function name
1486
+ * @param arena
1487
+ * @param token
1488
+ */
1489
+ function selectorFunctionSyntax(arena, token) {
1490
+ syntaxArena = arena;
1491
+ return arena.stringDerived(arena.stringValue[token], 1 /* StringDerived.SelectorFunctionSyntax */, computeSelectorFunctionSyntax);
1492
+ }
1493
+ /**
1494
+ * the wildcard functions being matched with a syntax, see {@link matchWildCardFunction}
1495
+ */
1496
+ const wildCardProbes = new Map();
1497
+ /**
1498
+ * var(), env() and if() may be substituted by any number of values. the function is matched:
1499
+ * - as a part of the syntax at index: `var(--color) 65%` is matched by `<linear-color-stop>` (`<color>` is the function)
1500
+ * - as the syntax at index, or as all the remaining syntaxes when it is the last token
1501
+ *
1502
+ * the next syntaxes must match the tokens that follow. otherwise, the syntax at index is skipped if it is optional and
1503
+ * the function is matched with the next syntax
1504
+ * @param arena
1505
+ * @param syntaxes
1506
+ * @param index index of the syntax matched by the function
1507
+ * @param context
1508
+ * @param token the function token
1509
+ * @param end last token of the function
1510
+ * @param options
1511
+ * @param out
1512
+ * @returns the result, null if the syntax at index is skipped
1513
+ */
1514
+ function matchWildCardFunction(arena, syntaxes, index, context, token, end, options, out) {
1515
+ let errors = noErrors;
1516
+ const candidates = [];
1517
+ // the function is a part of the syntax. a keyword, a comma, ... is matched by the function
1518
+ if (syntaxes[index].typ !== ValidationTokenEnum.Keyword &&
1519
+ syntaxes[index].typ !== ValidationTokenEnum.Comma &&
1520
+ syntaxes[index].typ !== ValidationTokenEnum.Separator &&
1521
+ syntaxes[index].typ !== ValidationTokenEnum.StringToken &&
1522
+ syntaxes[index].typ !== ValidationTokenEnum.OpenParenthesis &&
1523
+ syntaxes[index].typ !== ValidationTokenEnum.CloseParenthesis) {
1524
+ const previous = wildCardProbes.get(token);
1525
+ let probe;
1526
+ wildCardProbes.set(token, syntaxes[index]);
1527
+ try {
1528
+ probe = matchSyntax(arena, [syntaxes[index]], context.slice(), options);
1529
+ }
1530
+ finally {
1531
+ if (previous == null) {
1532
+ wildCardProbes.delete(token);
1533
+ }
1534
+ else {
1535
+ wildCardProbes.set(token, previous);
1536
+ }
1537
+ }
1538
+ if (probe.success) {
1539
+ if (probe.context.done()) {
1540
+ // the syntax matched all the tokens: the remaining syntaxes must be optional
1541
+ if (findMandatorySyntax(syntaxes, index + 1) === -1) {
1542
+ context.end();
1543
+ return setMatch(out, true, probe.valid, null, context, null, probe.errors);
1544
+ }
1545
+ }
1546
+ else if (probe.context.current() != null) {
1547
+ // the syntax matched some tokens
1548
+ candidates.push(probe.context);
1549
+ }
1550
+ }
1551
+ }
1552
+ // the function is the syntax
1553
+ const next = context.slice();
1554
+ next.update(end);
1555
+ if (next.done()) {
1556
+ // all the remaining syntaxes
1557
+ context.end();
1558
+ return setMatch(out, true, true, null, context, null, noErrors);
1559
+ }
1560
+ candidates.push(next);
1561
+ for (const candidate of candidates) {
1562
+ const rest = matchSyntax(arena, syntaxes.slice(index + 1), candidate.slice(), options);
1563
+ if (rest.success) {
1564
+ if (rest.context.done()) {
1565
+ context.end();
1566
+ }
1567
+ else {
1568
+ context.update(rest.context.current() ?? candidate.current());
1569
+ }
1570
+ return setMatch(out, true, rest.valid, rest.token, context, null, rest.errors);
1571
+ }
1572
+ errors = rest.errors;
1573
+ }
1574
+ if (isNullable(syntaxes[index])) {
1575
+ return null;
1576
+ }
1577
+ return setMatch(out, false, true, token, context, syntaxes[index], errors);
1578
+ }
1579
+ /**
1580
+ * the tokens are consumed: the match succeeds if the remaining syntaxes, starting at index, are optional.
1581
+ * `[ <number [0,1]>, <number> ]` does not match `.2`
1582
+ * @param out
1583
+ * @param syntaxes
1584
+ * @param index index of the next syntax
1585
+ * @param valid
1586
+ * @param token
1587
+ * @param context
1588
+ * @param errors
1589
+ */
1590
+ function setDoneMatch(out, syntaxes, index, valid, token, context, errors) {
1591
+ const mandatory = findMandatorySyntax(syntaxes, index);
1592
+ if (mandatory !== -1) {
1593
+ return setMatch(out, false, valid, token, context, syntaxes[mandatory], noErrors);
1594
+ }
1595
+ return setMatch(out, true, valid, token, context, syntaxes[index] ?? null, errors);
1596
+ }
1597
+ /**
1598
+ * matches a list of syntaxes
1599
+ * @param arena
1600
+ * @param syntaxes
1601
+ * @param context
1602
+ * @param options
1603
+ * @param out
1604
+ * @returns
1605
+ */
1187
1606
  function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1188
1607
  if (syntaxes == null) {
1189
1608
  return setMatch(out, true, false, context.peek(), context, null, noErrors);
@@ -1193,14 +1612,14 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1193
1612
  let token = null;
1194
1613
  let result = null;
1195
1614
  let isOptional;
1196
- if (context.tokens.length == 1 &&
1197
- arena.dataTypes[context.tokens[0]] == EnumToken.IdenTokenType &&
1198
- isCssWideKeyword(arena.strings[arena.stringValue[context.tokens[0]]])) {
1615
+ if (context.tokens.length === 1 &&
1616
+ arena.dataTypes[context.tokens[0]] === EnumToken.IdenTokenType &&
1617
+ isCssWideKeyword(arena.resolveString(arena.stringValue[context.tokens[0]]))) {
1199
1618
  context.end();
1200
1619
  return setMatch(out, true, true, context.peek(), context, null, noErrors);
1201
1620
  }
1202
1621
  while (++i < syntaxes.length) {
1203
- if (syntaxes[i].typ == ValidationTokenEnum.Whitespace) {
1622
+ if (syntaxes[i].typ === ValidationTokenEnum.Whitespace) {
1204
1623
  continue;
1205
1624
  }
1206
1625
  isOptional =
@@ -1210,36 +1629,44 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1210
1629
  !syntaxes[i].isMandatatoryGroup;
1211
1630
  token = context.peek();
1212
1631
  if (token == null) {
1213
- if (!isOptional) {
1214
- return setMatch(out, false, true, null, context, syntaxes[i], [
1632
+ // the remaining syntaxes must be optional: `<a>? <b>`
1633
+ const mandatory = findMandatorySyntax(syntaxes, isOptional ? i + 1 : i);
1634
+ if (mandatory !== -1) {
1635
+ return setMatch(out, false, true, null, context, syntaxes[mandatory], [
1215
1636
  {
1216
1637
  action: "drop",
1217
1638
  message: "could not match syntax",
1218
1639
  node: null,
1219
- syntax: syntaxes[i],
1640
+ syntax: syntaxes[mandatory],
1220
1641
  },
1221
1642
  ]);
1222
1643
  }
1223
1644
  break;
1224
1645
  }
1225
- if (arena.dataTypes[token] === EnumToken.WildCardFunctionTokenType && arena.strings[arena.stringValue[token]] === "var") {
1226
- result = matchSyntax(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, arena.strings[arena.stringValue[token]] + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(token)), options, out);
1646
+ if (arena.dataTypes[token] === EnumToken.WildCardFunctionTokenType &&
1647
+ arena.resolveString(arena.stringValue[token]) === "var" &&
1648
+ wildCardProbes.get(token) !== syntaxes[i]) {
1649
+ result = matchSyntax(arena, functionSyntax(arena, token)?.[0]?.chi, createValidationContext(arena, arena.tokens(token)), options, out);
1227
1650
  if (result.success && result.context.done()) {
1228
- context.next();
1229
- continue;
1651
+ const wildCardResult = matchWildCardFunction(arena, syntaxes, i, context, token, token, options, out);
1652
+ if (wildCardResult == null) {
1653
+ continue;
1654
+ }
1655
+ return wildCardResult;
1230
1656
  }
1231
1657
  }
1232
1658
  if (
1233
1659
  // @ts-ignore
1234
- arena.dataTypes[token] === EnumToken.WildCardFunctionTokenDefType) {
1660
+ arena.dataTypes[token] === EnumToken.WildCardFunctionTokenDefType &&
1661
+ wildCardProbes.get(token) !== syntaxes[i]) {
1235
1662
  const range = trimArray(arena, context.peekRange());
1236
- result = matchSyntax(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, arena.strings[arena.stringValue[token]] + "()")?.[0]?.chi, createValidationContext(arena, range.slice(1, -1)), options, out);
1663
+ result = matchSyntax(arena, functionSyntax(arena, token)?.[0]?.chi, createValidationContext(arena, range.slice(1, -1)), options, out);
1237
1664
  if (result.success) {
1238
- context.update(range.at(-1));
1239
- if (context.done()) {
1240
- return setMatch(out, result.success, result.valid, result.token, context, syntaxes[i + 1], result.errors);
1665
+ const wildCardResult = matchWildCardFunction(arena, syntaxes, i, context, token, range.at(-1), options, out);
1666
+ if (wildCardResult == null) {
1667
+ continue;
1241
1668
  }
1242
- continue;
1669
+ return wildCardResult;
1243
1670
  }
1244
1671
  }
1245
1672
  if (syntaxes[i].isRepeatableAtLeastOnce || syntaxes[i].isRepeatable) {
@@ -1247,7 +1674,7 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1247
1674
  if (result.success) {
1248
1675
  if (result.context.done()) {
1249
1676
  context.end();
1250
- return setMatch(out, result.success, result.valid, result.token, result.context, syntaxes[i + 1], result.errors);
1677
+ return setDoneMatch(out, syntaxes, i + 1, result.valid, result.token, result.context, result.errors);
1251
1678
  }
1252
1679
  context.update(result.context.current());
1253
1680
  continue;
@@ -1260,10 +1687,9 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1260
1687
  if (syntaxes[i].isList) {
1261
1688
  result = matchListSyntax(arena, syntaxes[i], context.slice(), options);
1262
1689
  if (result.success) {
1263
- options.visited.get(token)?.delete?.(syntaxes[i]);
1264
1690
  if (result.context.done()) {
1265
1691
  context.end();
1266
- return setMatch(out, true, true, null, context, syntaxes[i + 1], result.errors);
1692
+ return setDoneMatch(out, syntaxes, i + 1, true, null, context, result.errors);
1267
1693
  }
1268
1694
  context.update(result.context.current());
1269
1695
  continue;
@@ -1276,10 +1702,9 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1276
1702
  if (syntaxes[i].match != null) {
1277
1703
  result = matchOccurenceSyntax(arena, syntaxes[i], context.slice(), options);
1278
1704
  if (result.success) {
1279
- options.visited.get(token).delete(syntaxes[i]);
1280
1705
  if (result.context.done()) {
1281
1706
  context.end();
1282
- return setMatch(out, result.success, result.valid, result.token, context, syntaxes[++i], result.errors);
1707
+ return setDoneMatch(out, syntaxes, i + 1, result.valid, result.token, context, result.errors);
1283
1708
  }
1284
1709
  context.update(result.context.current());
1285
1710
  continue;
@@ -1289,349 +1714,391 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1289
1714
  }
1290
1715
  return setMatch(out, false, true, token, context, syntaxes[i], result.errors);
1291
1716
  }
1292
- if (!options.visited.has(token)) {
1293
- options.visited.set(token, new Set());
1294
- }
1295
- if (options.visited.get(token).has(syntaxes[i])) {
1717
+ if (isVisited(token, syntaxes[i])) {
1296
1718
  // cyclic definition
1297
1719
  return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1298
1720
  }
1299
- options.visited.get(token).add(syntaxes[i]);
1721
+ // the syntax is being matched at the token, until it is matched (see visitedSyntaxes)
1722
+ const visit = enterSyntax(token, syntaxes[i]);
1300
1723
  success = false;
1301
- switch (syntaxes[i].typ) {
1302
- case ValidationTokenEnum.Comma:
1303
- if (arena.dataTypes[token] == EnumToken.CommaTokenType) {
1304
- success = true;
1305
- options.visited.get(token).delete(syntaxes[i]);
1306
- context.next();
1307
- break;
1308
- }
1309
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1310
- case ValidationTokenEnum.Keyword:
1311
- if (arena.dataTypes[token] == EnumToken.IdenTokenType &&
1312
- lowercase(arena.strings[arena.stringValue[token]]) ===
1313
- lowercase(syntaxes[i].val)) {
1314
- success = true;
1315
- options.visited.get(token).delete(syntaxes[i]);
1316
- context.next();
1317
- break;
1318
- }
1319
- if (isOptional) {
1320
- break;
1321
- }
1322
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1323
- case ValidationTokenEnum.DeclarationType:
1324
- result = matchSyntax(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, syntaxes[i].val), context, options, out);
1325
- if (result.success) {
1326
- success = true;
1327
- options.visited.get(token).delete(syntaxes[i]);
1328
- if (result.context.done()) {
1329
- context.end();
1330
- return setMatch(out, result.success, result.valid, token, context, syntaxes[1 + i], result.errors);
1724
+ try {
1725
+ switch (syntaxes[i].typ) {
1726
+ case ValidationTokenEnum.Colon:
1727
+ // `<ident-token> : <declaration-value>?`
1728
+ if (arena.dataTypes[token] === EnumToken.ColonTokenType) {
1729
+ success = true;
1730
+ context.next();
1731
+ break;
1331
1732
  }
1332
- else {
1333
- if (result.context.current() != null) {
1334
- context.update(result.context.current());
1335
- }
1733
+ if (isOptional) {
1734
+ break;
1336
1735
  }
1337
- }
1338
- if (success || isOptional) {
1339
- break;
1340
- }
1341
- return setMatch(out, false, true, token, context, syntaxes[i], result.errors);
1342
- case ValidationTokenEnum.OptionalGroupToken: {
1343
- //
1344
- const tokens = context.split();
1345
- let j = 0;
1346
- for (; j < tokens.length; j++) {
1347
- result = matchSyntax(arena, syntaxes[i].chi, context.slice(), options, out);
1348
- if (result.success) {
1349
- context.update(tokens[j].at(-1));
1736
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1737
+ case ValidationTokenEnum.Comma:
1738
+ if (arena.dataTypes[token] === EnumToken.CommaTokenType) {
1739
+ success = true;
1740
+ visitedSyntaxes[visit] = null;
1741
+ context.next();
1742
+ break;
1743
+ }
1744
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1745
+ case ValidationTokenEnum.Keyword:
1746
+ if (arena.dataTypes[token] === EnumToken.IdenTokenType &&
1747
+ lowercase(arena.resolveString(arena.stringValue[token])) ===
1748
+ lowercase(syntaxes[i].val)) {
1749
+ success = true;
1750
+ visitedSyntaxes[visit] = null;
1751
+ context.next();
1752
+ break;
1350
1753
  }
1351
- break;
1352
- }
1353
- if (result?.context?.done?.()) {
1354
- return result;
1355
- }
1356
- break;
1357
- }
1358
- case ValidationTokenEnum.StringToken:
1359
- if (arena.dataTypes[token] === EnumToken.Add) {
1360
- success = syntaxes[i].val.slice(1, -1) === "+";
1361
- }
1362
- else if (arena.dataTypes[token] === EnumToken.Sub) {
1363
- success = syntaxes[i].val.slice(1, -1) === "-";
1364
- }
1365
- else if (arena.dataTypes[token] === EnumToken.LiteralTokenType) {
1366
- success = arena.strings[arena.stringValue[token]] === syntaxes[i].val.slice(1, -1);
1367
- }
1368
- else if (arena.dataTypes[token] == EnumToken.Plus) {
1369
- success = true;
1370
- }
1371
- if (success) {
1372
- context.next();
1373
- break;
1374
- }
1375
- if (isOptional) {
1376
- break;
1377
- }
1378
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1379
- case ValidationTokenEnum.PropertyType:
1380
- result = matchProperty(arena, syntaxes[i], context, options);
1381
- if (!result.success) {
1382
1754
  if (isOptional) {
1383
1755
  break;
1384
1756
  }
1385
- return setMatch(out, false, true, context.current() ?? token, context, syntaxes[i], noErrors);
1386
- }
1387
- success = true;
1388
- options.visited.get(token).delete(syntaxes[i]);
1389
- if (result.context.done()) {
1390
- context.end();
1391
- return setMatch(out, success, true, null, context, syntaxes[i + 1], noErrors);
1392
- }
1393
- else {
1394
- context.update(result.context.current());
1395
- }
1396
- break;
1397
- case ValidationTokenEnum.PipeToken:
1398
- {
1399
- // each alternative is matched in the same scratch result, the best match is copied
1400
- const scratch = createMatch();
1401
- let best = null;
1402
- let tmp = null;
1403
- for (const syntax of syntaxes[i].chi) {
1404
- tmp = matchSyntax(arena, syntax, context.slice(), options, scratch);
1405
- if (tmp.success) {
1406
- if (best == null || tmp.context.index >= best.context.index) {
1407
- best = copyMatch(best ?? createMatch(), tmp);
1408
- }
1409
- if (tmp.context.done()) {
1410
- context.end();
1411
- return setMatch(out, tmp.success, tmp.valid, tmp.token, context, syntaxes[i + 1], tmp.errors);
1757
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1758
+ case ValidationTokenEnum.DeclarationType:
1759
+ result = matchSyntax(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, syntaxes[i].val), context, options, out);
1760
+ if (result.success) {
1761
+ success = true;
1762
+ visitedSyntaxes[visit] = null;
1763
+ if (result.context.done()) {
1764
+ context.end();
1765
+ return setDoneMatch(out, syntaxes, i + 1, result.valid, token, context, result.errors);
1766
+ }
1767
+ else {
1768
+ if (result.context.current() != null) {
1769
+ context.update(result.context.current());
1412
1770
  }
1413
1771
  }
1414
1772
  }
1415
- result = best;
1416
- if (result?.success) {
1417
- success = true;
1418
- options.visited.get(token).delete(syntaxes[i]);
1419
- context.update(result.context.current());
1773
+ if (success || isOptional) {
1420
1774
  break;
1421
1775
  }
1422
- }
1423
- return setMatch(out, false, true, token, context, syntaxes[i], [
1424
- {
1425
- action: "drop",
1426
- message: "could not match syntax",
1427
- node: token,
1428
- // @ts-ignore
1429
- // location: options.source!.getSourceLocation(token[LOC]?.sta),
1430
- syntax: syntaxes[i],
1431
- },
1432
- ]);
1433
- case ValidationTokenEnum.Bracket:
1434
- result = matchSyntax(arena, syntaxes[i].chi, context.slice(), options, out);
1435
- if (result.success) {
1436
- success = true;
1437
- options.visited.get(token).delete(syntaxes[i]);
1438
- if (result.context.done()) {
1439
- context.end();
1440
- return setMatch(out, success, true, null, context, syntaxes[1 + i], noErrors);
1776
+ return setMatch(out, false, true, token, context, syntaxes[i], result.errors);
1777
+ case ValidationTokenEnum.OptionalGroupToken: {
1778
+ const chi = syntaxes[i].chi;
1779
+ // `<bg-layer>#? , <final-bg-layer>` (the group of the optional list, the comma is the list separator) is
1780
+ // matched as `[ <bg-layer> , ]* <final-bg-layer>`: the last item is left to the next syntax
1781
+ if (chi.length === 1 && chi[0].isList) {
1782
+ const { isList, isOptional, match, ...item } = chi[0];
1783
+ const group = [
1784
+ item,
1785
+ { typ: ValidationTokenEnum.Comma },
1786
+ ];
1787
+ while (context.peek() != null) {
1788
+ const tmp = matchSyntax(arena, group, context.slice(), options);
1789
+ if (!tmp.success || tmp.context.current() == null) {
1790
+ break;
1791
+ }
1792
+ context.update(tmp.context.current());
1793
+ if (tmp.context.done()) {
1794
+ break;
1795
+ }
1796
+ }
1797
+ success = true;
1798
+ break;
1441
1799
  }
1442
- else {
1800
+ // the group is optional: it is skipped if it does not match
1801
+ result = matchSyntax(arena, chi, context.slice(), options);
1802
+ if (result.success) {
1803
+ success = true;
1804
+ if (result.context.done()) {
1805
+ context.end();
1806
+ return setDoneMatch(out, syntaxes, i + 1, true, null, context, result.errors);
1807
+ }
1443
1808
  context.update(result.context.current());
1444
1809
  }
1445
1810
  break;
1446
1811
  }
1447
- if (isOptional) {
1448
- break;
1449
- }
1450
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1451
- case ValidationTokenEnum.ColumnToken:
1452
- result = matchColumnSyntax(arena, syntaxes[i], context.slice(), options);
1453
- if (result.success) {
1454
- success = true;
1455
- options.visited.get(token).delete(syntaxes[i]);
1456
- if (result.context.done()) {
1457
- context.end();
1458
- return setMatch(out, success, true, null, context, syntaxes[1 + i], noErrors);
1812
+ case ValidationTokenEnum.StringToken:
1813
+ if (arena.dataTypes[token] === EnumToken.Add) {
1814
+ success = syntaxes[i].val.slice(1, -1) === "+";
1459
1815
  }
1460
- else {
1461
- context.update(result.context.current());
1816
+ else if (arena.dataTypes[token] === EnumToken.Sub) {
1817
+ success = syntaxes[i].val.slice(1, -1) === "-";
1818
+ }
1819
+ else if (arena.dataTypes[token] === EnumToken.LiteralTokenType) {
1820
+ success =
1821
+ arena.resolveString(arena.stringValue[token]) ===
1822
+ syntaxes[i].val.slice(1, -1);
1823
+ }
1824
+ else if (arena.dataTypes[token] === EnumToken.Plus) {
1825
+ success = true;
1826
+ }
1827
+ if (success) {
1828
+ context.next();
1829
+ break;
1830
+ }
1831
+ if (isOptional) {
1832
+ break;
1833
+ }
1834
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1835
+ case ValidationTokenEnum.PropertyType:
1836
+ result = matchProperty(arena, syntaxes[i], context, options);
1837
+ if (!result.success) {
1838
+ if (isOptional) {
1839
+ break;
1840
+ }
1841
+ return setMatch(out, false, true, context.current() ?? token, context, syntaxes[i], noErrors);
1462
1842
  }
1463
- break;
1464
- }
1465
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1466
- case ValidationTokenEnum.AmpersandToken:
1467
- result = matchAmpersandSyntax(arena, syntaxes[i], context.slice(), options);
1468
- if (result.success) {
1469
1843
  success = true;
1470
- options.visited.get(token).delete(syntaxes[i]);
1844
+ visitedSyntaxes[visit] = null;
1471
1845
  if (result.context.done()) {
1846
+ if (!isInRange(arena, token, syntaxes[i])) {
1847
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1848
+ }
1472
1849
  context.end();
1473
- return setMatch(out, success, true, null, context, syntaxes[1 + i], noErrors);
1850
+ return setDoneMatch(out, syntaxes, i + 1, true, null, context, noErrors);
1474
1851
  }
1475
1852
  else {
1476
1853
  context.update(result.context.current());
1477
1854
  }
1478
1855
  break;
1479
- }
1480
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1481
- case ValidationTokenEnum.Function:
1482
- if ((!tokensfuncDefMap.has(arena.dataTypes[token]) &&
1483
- !funcLike.includes(arena.dataTypes[token]) &&
1484
- !funcTypes.includes(arena.dataTypes[token])) ||
1485
- !equalsIgnoreCase(syntaxes[i].val, arena.strings[arena.stringValue[token]].toLowerCase())) {
1856
+ case ValidationTokenEnum.PipeToken:
1857
+ {
1858
+ // each alternative is matched in the same scratch result, the best match is copied
1859
+ const scratch = createMatch();
1860
+ const alternatives = syntaxes[i].chi;
1861
+ // the alternatives that may match the token, in order (see matchingAlternatives()): the other
1862
+ // alternatives cannot match it. null: all the alternatives
1863
+ const candidates = matchingAlternatives(arena, alternatives, token);
1864
+ const count = candidates === null ? alternatives.length : candidates.length;
1865
+ let best = null;
1866
+ let tmp = null;
1867
+ for (let k = 0; k < count; k++) {
1868
+ const syntax = alternatives[candidates === null ? k : candidates[k]];
1869
+ tmp = matchSyntax(arena, syntax, context.slice(), options, scratch);
1870
+ if (tmp.success) {
1871
+ // an alternative that consumes the tokens is skipped if mandatory syntaxes remain
1872
+ if (tmp.context.done() && findMandatorySyntax(syntaxes, i + 1) !== -1) {
1873
+ continue;
1874
+ }
1875
+ if (best == null || tmp.context.index >= best.context.index) {
1876
+ best = copyMatch(best ?? createMatch(), tmp);
1877
+ }
1878
+ if (tmp.context.done()) {
1879
+ context.end();
1880
+ return setMatch(out, tmp.success, tmp.valid, tmp.token, context, syntaxes[i + 1], tmp.errors);
1881
+ }
1882
+ }
1883
+ }
1884
+ result = best;
1885
+ if (result?.success) {
1886
+ success = true;
1887
+ visitedSyntaxes[visit] = null;
1888
+ context.update(result.context.current());
1889
+ break;
1890
+ }
1891
+ }
1486
1892
  return setMatch(out, false, true, token, context, syntaxes[i], [
1487
1893
  {
1488
1894
  action: "drop",
1489
- message: `Expecting a function ${EnumToken[arena.dataTypes[token]]}`,
1895
+ message: "could not match syntax",
1490
1896
  node: token,
1897
+ // @ts-ignore
1898
+ // location: options.source!.getSourceLocation(token[LOC]?.sta),
1899
+ syntax: syntaxes[i],
1491
1900
  },
1492
1901
  ]);
1493
- }
1494
- if (tokensfuncDefMap.has(arena.dataTypes[token])) {
1495
- const range = context.peekRange();
1496
- result = matchSyntax(arena, syntaxes[i].chi, createValidationContext(arena, range.slice(1, -1)), options, out);
1497
- if (result.success && result.context.done()) {
1498
- context.update(range.at(-1));
1499
- options.visited.get(token).delete(syntaxes[i]);
1902
+ case ValidationTokenEnum.Bracket:
1903
+ result = matchSyntax(arena, syntaxes[i].chi, context.slice(), options, out);
1904
+ if (result.success) {
1905
+ success = true;
1906
+ visitedSyntaxes[visit] = null;
1907
+ if (result.context.done()) {
1908
+ context.end();
1909
+ return setDoneMatch(out, syntaxes, i + 1, true, null, context, noErrors);
1910
+ }
1911
+ else {
1912
+ context.update(result.context.current());
1913
+ }
1500
1914
  break;
1501
1915
  }
1502
- }
1503
- else {
1504
- result = matchSyntax(arena, syntaxes[i].chi, createValidationContext(arena, arena.tokens(token)), options, out);
1505
- if (result.success && result.context.done()) {
1916
+ if (isOptional) {
1917
+ break;
1918
+ }
1919
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1920
+ case ValidationTokenEnum.ColumnToken:
1921
+ result = matchColumnSyntax(arena, syntaxes[i], context.slice(), options);
1922
+ if (result.success) {
1506
1923
  success = true;
1507
- options.visited.get(token).delete(syntaxes[i]);
1508
- context.next();
1924
+ visitedSyntaxes[visit] = null;
1925
+ if (result.context.done()) {
1926
+ context.end();
1927
+ return setDoneMatch(out, syntaxes, i + 1, true, null, context, noErrors);
1928
+ }
1929
+ else {
1930
+ context.update(result.context.current());
1931
+ }
1509
1932
  break;
1510
1933
  }
1511
- }
1512
- return setMatch(out, false, true, token, context, syntaxes[i], result.errors);
1513
- case ValidationTokenEnum.Separator:
1514
- if (arena.dataTypes[token] === EnumToken.LiteralTokenType && arena.strings[arena.stringValue[token]] === "/") {
1515
- options.visited.get(token).delete(syntaxes[i]);
1516
- context.next();
1517
- break;
1518
- }
1519
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1520
- case ValidationTokenEnum.FunctionDefinition:
1521
- if (arena.strings[arena.stringValue[token]] &&
1522
- equalsIgnoreCase(arena.strings[arena.stringValue[token]], syntaxes[i].val)) {
1523
- if (tokensfuncDefMap.has(arena.dataTypes[token])) {
1524
- const children = trimArray(arena, context.peekRange());
1525
- result = matchSyntax(arena, (getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, syntaxes[i].val + "()")?.[0]).chi ?? [], createValidationContext(arena, children.slice(1, -1)), options, out);
1934
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1935
+ case ValidationTokenEnum.AmpersandToken:
1936
+ result = matchAmpersandSyntax(arena, syntaxes[i], context.slice(), options);
1937
+ if (result.success) {
1938
+ success = true;
1939
+ visitedSyntaxes[visit] = null;
1940
+ if (result.context.done()) {
1941
+ context.end();
1942
+ return setDoneMatch(out, syntaxes, i + 1, true, null, context, noErrors);
1943
+ }
1944
+ else {
1945
+ context.update(result.context.current());
1946
+ }
1947
+ break;
1948
+ }
1949
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1950
+ case ValidationTokenEnum.Function:
1951
+ if ((!isFunctionDefType(arena.dataTypes[token]) &&
1952
+ !funcLike.includes(arena.dataTypes[token]) &&
1953
+ !funcTypes.includes(arena.dataTypes[token])) ||
1954
+ !equalsIgnoreCase(syntaxes[i].val, arena.resolveString(arena.stringValue[token]).toLowerCase())) {
1955
+ return setMatch(out, false, true, token, context, syntaxes[i], [
1956
+ {
1957
+ action: "drop",
1958
+ message: `Expecting a function ${EnumToken[arena.dataTypes[token]]}`,
1959
+ node: token,
1960
+ },
1961
+ ]);
1962
+ }
1963
+ if (isFunctionDefType(arena.dataTypes[token])) {
1964
+ const range = context.peekRange();
1965
+ result = matchSyntax(arena, syntaxes[i].chi, createValidationContext(arena, range.slice(1, -1)), options, out);
1526
1966
  if (result.success && result.context.done()) {
1527
- success = true;
1528
- options.visited.get(token).delete(syntaxes[i]);
1529
- context.update(children.at(-1));
1967
+ context.update(range.at(-1));
1968
+ visitedSyntaxes[visit] = null;
1530
1969
  break;
1531
1970
  }
1532
1971
  }
1533
- else if (tokensfuncSet.has(arena.dataTypes[token])) {
1534
- result = matchSyntax(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, syntaxes[i].val + "()") ?? [], createValidationContext(arena, [token]), options, out);
1972
+ else {
1973
+ result = matchSyntax(arena, syntaxes[i].chi, createValidationContext(arena, arena.tokens(token)), options, out);
1535
1974
  if (result.success && result.context.done()) {
1536
1975
  success = true;
1537
- options.visited.get(token).delete(syntaxes[i]);
1976
+ visitedSyntaxes[visit] = null;
1538
1977
  context.next();
1539
1978
  break;
1540
1979
  }
1541
1980
  }
1542
- }
1543
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1544
- case ValidationTokenEnum.OpenParenthesis:
1545
- if (arena.dataTypes[token] === EnumToken.StartParensTokenType) {
1546
- options.visited.get(token).delete(syntaxes[i]);
1547
- context.next();
1548
- break;
1549
- }
1550
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1551
- case ValidationTokenEnum.OpenCurlyBrace:
1552
- if (arena.dataTypes[token] === EnumToken.BlockStartTokenType) {
1553
- options.visited.get(token).delete(syntaxes[i]);
1554
- context.next();
1555
- break;
1556
- }
1557
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1558
- case ValidationTokenEnum.CloseParenthesis:
1559
- if (arena.dataTypes[token] === EnumToken.EndParensTokenType) {
1560
- options.visited.get(token).delete(syntaxes[i]);
1561
- context.next();
1562
- break;
1563
- }
1564
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1565
- default:
1566
- throw new Error(`Unexpected syntax ${ValidationTokenEnum[syntaxes[i].typ]}`);
1567
- }
1568
- if (success &&
1569
- "range" in syntaxes[i] &&
1570
- !(arena.dataTypes[token] === EnumToken.MathFunctionTokenType ||
1571
- arena.dataTypes[token] === EnumToken.MathFunctionTokenDefType ||
1572
- arena.dataTypes[token] === EnumToken.WildCardFunctionTokenDefType)) {
1573
- let success = false;
1574
- success =
1575
- !arena.hasFraction(token) &&
1576
- arena.numericValue[token] >=
1577
- syntaxes[i].range.min.val;
1578
- if (syntaxes[i].range.min.typ === ValidationTokenEnum.Dimension) {
1579
- success =
1580
- arena.dataTypes[token] ===
1581
- EnumToken[
1582
- // @ts-expect-error
1583
- syntaxes[i].range.min
1584
- .unit];
1585
- }
1586
- if (success &&
1587
- syntaxes[i].range.max != null) {
1588
- success =
1589
- arena.numericValue[token] <=
1590
- syntaxes[i].range.max.val;
1591
- }
1592
- if (!success) {
1593
- return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1981
+ return setMatch(out, false, true, token, context, syntaxes[i], result.errors);
1982
+ case ValidationTokenEnum.Number:
1983
+ case ValidationTokenEnum.Dimension:
1984
+ // a literal number or dimension: `-moz-force-broken-image-icon: 0 | 1`,
1985
+ // `glyph-orientation-vertical: auto | 0deg | 90deg | 0 | 90`
1986
+ if (isLiteralNumber(arena, token, syntaxes[i])) {
1987
+ success = true;
1988
+ visitedSyntaxes[visit] = null;
1989
+ context.next();
1990
+ break;
1991
+ }
1992
+ if (isOptional) {
1993
+ break;
1994
+ }
1995
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1996
+ case ValidationTokenEnum.Separator:
1997
+ if (arena.dataTypes[token] === EnumToken.LiteralTokenType &&
1998
+ arena.resolveString(arena.stringValue[token]) === "/") {
1999
+ visitedSyntaxes[visit] = null;
2000
+ context.next();
2001
+ break;
2002
+ }
2003
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
2004
+ case ValidationTokenEnum.FunctionDefinition:
2005
+ if (arena.resolveString(arena.stringValue[token]) &&
2006
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[token]), syntaxes[i].val)) {
2007
+ if (isFunctionDefType(arena.dataTypes[token])) {
2008
+ const children = trimArray(arena, context.peekRange());
2009
+ result = matchSyntax(arena, (getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, syntaxes[i].val + "()")?.[0]).chi ?? [], createValidationContext(arena, children.slice(1, -1)), options, out);
2010
+ if (result.success && result.context.done()) {
2011
+ success = true;
2012
+ visitedSyntaxes[visit] = null;
2013
+ context.update(children.at(-1));
2014
+ break;
2015
+ }
2016
+ }
2017
+ else if (isFunctionType(arena.dataTypes[token])) {
2018
+ result = matchSyntax(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, syntaxes[i].val + "()") ?? [], createValidationContext(arena, [token]), options, out);
2019
+ if (result.success && result.context.done()) {
2020
+ success = true;
2021
+ visitedSyntaxes[visit] = null;
2022
+ context.next();
2023
+ break;
2024
+ }
2025
+ }
2026
+ }
2027
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
2028
+ case ValidationTokenEnum.OpenParenthesis:
2029
+ if (arena.dataTypes[token] === EnumToken.StartParensTokenType) {
2030
+ visitedSyntaxes[visit] = null;
2031
+ context.next();
2032
+ break;
2033
+ }
2034
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
2035
+ case ValidationTokenEnum.OpenCurlyBrace:
2036
+ if (arena.dataTypes[token] === EnumToken.BlockStartTokenType) {
2037
+ visitedSyntaxes[visit] = null;
2038
+ context.next();
2039
+ break;
2040
+ }
2041
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
2042
+ case ValidationTokenEnum.CloseParenthesis:
2043
+ if (arena.dataTypes[token] === EnumToken.EndParensTokenType) {
2044
+ visitedSyntaxes[visit] = null;
2045
+ context.next();
2046
+ break;
2047
+ }
2048
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
2049
+ default:
2050
+ throw new Error(`Unexpected syntax ${ValidationTokenEnum[syntaxes[i].typ]}`);
1594
2051
  }
1595
2052
  }
2053
+ finally {
2054
+ // the syntax is no longer being matched: a token may be matched again with the same syntax (the
2055
+ // alternatives of an optional group, ...). only the syntaxes being matched detect a cyclic definition
2056
+ visitedSyntaxes[visit] = null;
2057
+ visitedLength = visit;
2058
+ }
2059
+ if (success && !isInRange(arena, token, syntaxes[i])) {
2060
+ return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
2061
+ }
1596
2062
  }
1597
- return result?.success ? result : setMatch(out, true, true, token, context, syntaxes[i] ?? null, noErrors);
2063
+ // the context of this list of syntaxes: the last result may not include the tokens matched after it (`,`, ...)
2064
+ return setMatch(out, true, result?.success ? result.valid : true, token, context, syntaxes[i] ?? null, result?.success ? result.errors : noErrors);
1598
2065
  }
1599
2066
  /**
1600
- * matches a column of syntaxes
2067
+ * matches a column of syntaxes: `<a> || <b> || <c>`, one or more syntaxes in any order. the syntaxes are matched in
2068
+ * the order that matches the most tokens: `[ none | <single-transition-property> ] || <time> || <easing-function>`
2069
+ * matches `.2s ease-in-out transform` (`ease-in-out` is also a <custom-ident>)
2070
+ * @param arena
1601
2071
  * @param syntax
1602
2072
  * @param context
1603
2073
  * @param options
1604
2074
  * @returns
1605
2075
  */
1606
2076
  function matchColumnSyntax(arena, syntax, context, options) {
1607
- let syntaxes = syntax.chi.slice();
1608
- let i = 0;
1609
- let success = false;
1610
- let result;
1611
- syntaxes = syntaxes.slice();
1612
- for (; i < syntaxes.length; i++) {
1613
- result = matchSyntax(arena, syntaxes[i], context.slice(), options);
1614
- if (result.success) {
1615
- const curr = result.context.current();
1616
- if (curr == null && !result.context.done()) {
1617
- continue;
1618
- }
1619
- if (result.context.done()) {
1620
- context.end();
1621
- return {
1622
- ...result,
1623
- context,
1624
- syntaxToken: syntaxes[i + 1]?.[0],
1625
- };
1626
- }
1627
- context.update(curr);
1628
- success = true;
1629
- syntaxes.splice(i, 1);
1630
- i = -1;
2077
+ return matchUnorderedSyntaxes(arena, syntax, syntax.chi, false, context, options);
2078
+ }
2079
+ /**
2080
+ * matches syntaxes in any order: all the syntaxes (`&&`) or one or more syntaxes (`||`), see {@link searchUnorderedSyntaxes}
2081
+ * @param arena
2082
+ * @param syntax
2083
+ * @param syntaxes
2084
+ * @param all all the syntaxes must be matched, except the syntaxes that match no token
2085
+ * @param context
2086
+ * @param options
2087
+ */
2088
+ function matchUnorderedSyntaxes(arena, syntax, syntaxes, all, context, options) {
2089
+ const best = searchUnorderedSyntaxes(arena, syntaxes, syntaxes.map((_, index) => index), all, context, context, options, new Map());
2090
+ if (best != null) {
2091
+ if (best.done()) {
2092
+ context.end();
2093
+ }
2094
+ else {
2095
+ context.update(best.current());
1631
2096
  }
1632
2097
  }
1633
2098
  return {
1634
- success,
2099
+ // `<a>? && <b>?` matches no token
2100
+ success: best != null ||
2101
+ (all && syntaxes.every((syntaxes) => findMandatorySyntax(syntaxes, 0) === -1)),
1635
2102
  valid: true,
1636
2103
  token: context.current(),
1637
2104
  context,
@@ -1640,32 +2107,139 @@ function matchColumnSyntax(arena, syntax, context, options) {
1640
2107
  };
1641
2108
  }
1642
2109
  /**
1643
- * matches an ampersand of syntaxes
2110
+ * position of a context in the tokens of the unordered syntaxes, see {@link searchUnorderedSyntaxes}
2111
+ * @param base
2112
+ * @param context
2113
+ */
2114
+ function unorderedPosition(base, context) {
2115
+ return context.done() ? Number.POSITIVE_INFINITY : base.tokens.indexOf(context.current());
2116
+ }
2117
+ /**
2118
+ * the context after the unordered syntaxes that match the most tokens. null if the syntaxes do not match
2119
+ * @param arena
2120
+ * @param syntaxes the unordered syntaxes
2121
+ * @param remaining the indexes of the syntaxes that are not matched
2122
+ * @param all all the syntaxes must be matched, except the syntaxes that match no token
2123
+ * @param base the context of the syntaxes
2124
+ * @param context
2125
+ * @param options
2126
+ * @param cache the context after a syntax, by syntax index and position
2127
+ */
2128
+ function searchUnorderedSyntaxes(arena, syntaxes, remaining, all, base, context, options, cache) {
2129
+ const position = unorderedPosition(base, context);
2130
+ let best = null;
2131
+ for (let k = 0; k < remaining.length; k++) {
2132
+ const key = remaining[k] + ":" + position;
2133
+ let next = cache.get(key);
2134
+ if (next === undefined) {
2135
+ next = null;
2136
+ // a syntax that cannot start with the token matches no token (see mayStartWith())
2137
+ const result = mayStartWith(arena, syntaxes[remaining[k]], context.peek())
2138
+ ? matchSyntax(arena, syntaxes[remaining[k]], context.slice(), options)
2139
+ : null;
2140
+ // the syntax matched some tokens
2141
+ if (result != null &&
2142
+ result.success &&
2143
+ (result.context.done() || result.context.current() !== context.current())) {
2144
+ next = context.slice();
2145
+ if (result.context.done()) {
2146
+ next.end();
2147
+ }
2148
+ else {
2149
+ next.update(result.context.current());
2150
+ }
2151
+ }
2152
+ cache.set(key, next);
2153
+ }
2154
+ if (next == null) {
2155
+ continue;
2156
+ }
2157
+ const rest = remaining.slice(0, k).concat(remaining.slice(k + 1));
2158
+ // the match may stop after this syntax
2159
+ const complete = !all || rest.every((index) => findMandatorySyntax(syntaxes[index], 0) === -1);
2160
+ const candidates = [];
2161
+ if (!next.done() && rest.length > 0) {
2162
+ const deeper = searchUnorderedSyntaxes(arena, syntaxes, rest, all, base, next, options, cache);
2163
+ if (deeper != null) {
2164
+ candidates.push(deeper);
2165
+ }
2166
+ }
2167
+ if (complete) {
2168
+ candidates.push(next);
2169
+ }
2170
+ for (const candidate of candidates) {
2171
+ if (candidate.done()) {
2172
+ return candidate;
2173
+ }
2174
+ if (best == null || unorderedPosition(base, candidate) > unorderedPosition(base, best)) {
2175
+ best = candidate;
2176
+ }
2177
+ }
2178
+ }
2179
+ return best;
2180
+ }
2181
+ /**
2182
+ * the operands of chained ampersands: `<a> && <b> && <c>` is parsed as `[ <a> && <b> ] && <c>`
2183
+ * @param syntax
2184
+ * @param operands
2185
+ */
2186
+ function ampersandOperands(syntax, operands = []) {
2187
+ for (const side of [syntax.l, syntax.r]) {
2188
+ const tokens = side.filter((token) => token.typ !== ValidationTokenEnum.Whitespace);
2189
+ if (tokens.length === 1 &&
2190
+ tokens[0].typ === ValidationTokenEnum.AmpersandToken &&
2191
+ !tokens[0].isOptional &&
2192
+ !tokens[0].isRepeatable &&
2193
+ !tokens[0].isRepeatableAtLeastOnce &&
2194
+ !tokens[0].isList &&
2195
+ tokens[0].match == null) {
2196
+ ampersandOperands(tokens[0], operands);
2197
+ }
2198
+ else {
2199
+ operands.push(side);
2200
+ }
2201
+ }
2202
+ return operands;
2203
+ }
2204
+ /**
2205
+ * matches an ampersand of syntaxes: `<a> && <b> && <c>`, all the syntaxes in any order, except the optional syntaxes
2206
+ * that match no token. `[ <ident> && <length> ]` matches `thin 10px` and `10px thin`
2207
+ * @param arena
1644
2208
  * @param syntax
1645
2209
  * @param context
1646
2210
  * @param options
1647
2211
  * @returns
1648
2212
  */
1649
2213
  function matchAmpersandSyntax(arena, syntax, context, options) {
1650
- const syntaxes = [syntax.l, syntax.r];
1651
- let result;
1652
- let i;
1653
- for (i = 0; i < syntaxes.length; i++) {
1654
- result = matchSyntax(arena, syntaxes[i], context.slice(), options);
1655
- if (result.success) {
1656
- if (result.context.done()) {
1657
- context.end();
1658
- break;
1659
- }
1660
- context.update(result.context.current());
1661
- syntaxes.splice(i, 1);
1662
- i = -1;
2214
+ return matchUnorderedSyntaxes(arena, syntax, ampersandOperands(syntax), true, context, options);
2215
+ }
2216
+ /**
2217
+ * the tokens of a media / container feature: the tokens before the closing parenthesis, `width >= 700px` in
2218
+ * `(width >= 700px)`. the whitespace and the comments are skipped
2219
+ * @param arena
2220
+ * @param context
2221
+ */
2222
+ function peekFeatureTokens(arena, context) {
2223
+ const tokens = [];
2224
+ let depth = 0;
2225
+ for (let index = context.index + 1; index < context.tokens.length; index++) {
2226
+ const token = context.tokens[index];
2227
+ if (arena.dataTypes[token] === EnumToken.StartParensTokenType || isFunctionDefType(arena.dataTypes[token])) {
2228
+ depth++;
2229
+ }
2230
+ else if (arena.dataTypes[token] === EnumToken.EndParensTokenType && depth-- === 0) {
2231
+ break;
2232
+ }
2233
+ if (arena.dataTypes[token] !== EnumToken.WhitespaceTokenType &&
2234
+ arena.dataTypes[token] !== EnumToken.CommentTokenType) {
2235
+ tokens.push(token);
1663
2236
  }
1664
2237
  }
1665
- return result;
2238
+ return tokens;
1666
2239
  }
1667
2240
  /**
1668
2241
  * matches a property
2242
+ * @param arena
1669
2243
  * @param property
1670
2244
  * @param context
1671
2245
  * @param options
@@ -1676,7 +2250,7 @@ function matchProperty(arena, property, context, options) {
1676
2250
  const token = context.peek();
1677
2251
  // inlined: arena.typeOf(token)
1678
2252
  let t = (token == null ? null : arena.dataTypes[token]);
1679
- let checkCalc = (t == EnumToken.MathFunctionTokenDefType || t == EnumToken.MathFunctionTokenType) &&
2253
+ let checkCalc = (t === EnumToken.MathFunctionTokenDefType || t === EnumToken.MathFunctionTokenType) &&
1680
2254
  [
1681
2255
  "number",
1682
2256
  "zero",
@@ -1690,11 +2264,11 @@ function matchProperty(arena, property, context, options) {
1690
2264
  "calc-product",
1691
2265
  ].includes(property.val);
1692
2266
  if (checkCalc && !["number", "zero", "integer", "percentage", "length-percentage"].includes(property.val)) {
1693
- checkCalc = arena.strings[arena.stringValue[token]] === "calc";
2267
+ checkCalc = arena.resolveString(arena.stringValue[token]) === "calc";
1694
2268
  }
1695
2269
  if (checkCalc) {
1696
2270
  let result;
1697
- const syntax = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, arena.strings[arena.stringValue[token]] + "()")?.[0]?.chi;
2271
+ const syntax = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, arena.resolveString(arena.stringValue[token]) + "()")?.[0]?.chi;
1698
2272
  if (t === EnumToken.MathFunctionTokenType) {
1699
2273
  result = matchSyntax(arena, syntax, createValidationContext(arena, arena.tokens(token)), options);
1700
2274
  if (result.success && result.context.done()) {
@@ -1734,7 +2308,6 @@ function matchProperty(arena, property, context, options) {
1734
2308
  };
1735
2309
  }
1736
2310
  switch (property.val) {
1737
- case "function-parameter":
1738
2311
  case "declaration-value":
1739
2312
  {
1740
2313
  // consume a declaration value
@@ -1744,21 +2317,24 @@ function matchProperty(arena, property, context, options) {
1744
2317
  // if semicolon or end curly brace is encountered, stop
1745
2318
  let expectToken = true;
1746
2319
  let token;
2320
+ // one or more tokens
2321
+ let consumed = 0;
1747
2322
  const stack = [];
1748
2323
  token = context.peek();
1749
2324
  if (token != null) {
1750
2325
  success = false;
1751
2326
  while ((token = context.peek()) != null) {
1752
- if (arena.dataTypes[token] == EnumToken.StartParensTokenType || tokensfuncDefMap.has(arena.dataTypes[token])) {
2327
+ if (arena.dataTypes[token] === EnumToken.StartParensTokenType ||
2328
+ isFunctionDefType(arena.dataTypes[token])) {
1753
2329
  stack.push(token);
1754
2330
  }
1755
2331
  if (expectToken) {
1756
2332
  expectToken = false;
1757
2333
  }
1758
- if (arena.dataTypes[token] == EnumToken.CommaTokenType) {
2334
+ if (arena.dataTypes[token] === EnumToken.CommaTokenType) {
1759
2335
  expectToken = true;
1760
2336
  }
1761
- if (arena.dataTypes[token] == EnumToken.EndParensTokenType) {
2337
+ if (arena.dataTypes[token] === EnumToken.EndParensTokenType) {
1762
2338
  if (stack.length > 0) {
1763
2339
  stack.pop();
1764
2340
  }
@@ -1768,9 +2344,10 @@ function matchProperty(arena, property, context, options) {
1768
2344
  }
1769
2345
  }
1770
2346
  context.next();
2347
+ consumed++;
1771
2348
  }
1772
2349
  }
1773
- success = !expectToken;
2350
+ success = consumed > 0 && !expectToken;
1774
2351
  if (success) {
1775
2352
  return {
1776
2353
  success,
@@ -1820,8 +2397,8 @@ function matchProperty(arena, property, context, options) {
1820
2397
  if (
1821
2398
  // inlined: arena.typeOf(token)
1822
2399
  arena.dataTypes[token] === EnumToken.IdenTokenType &&
1823
- ("even" === arena.strings[arena.stringValue[token]] ||
1824
- "odd" === arena.strings[arena.stringValue[token]])) {
2400
+ ("even" === arena.resolveString(arena.stringValue[token]) ||
2401
+ "odd" === arena.resolveString(arena.stringValue[token]))) {
1825
2402
  success = true;
1826
2403
  break;
1827
2404
  }
@@ -1833,26 +2410,20 @@ function matchProperty(arena, property, context, options) {
1833
2410
  break;
1834
2411
  }
1835
2412
  // inlined: arena.typeOf(token)
1836
- if (arena.dataTypes[token] === EnumToken.LiteralTokenType || arena.dataTypes[token] === EnumToken.IdenTokenType) {
1837
- let match = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(arena.strings[arena.stringValue[token]]);
2413
+ if (arena.dataTypes[token] === EnumToken.LiteralTokenType ||
2414
+ arena.dataTypes[token] === EnumToken.IdenTokenType) {
2415
+ let match = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(arena.resolveString(arena.stringValue[token]));
1838
2416
  success = match != null;
1839
- if (match?.[3] == null) {
1840
- token = context.peek(1);
1841
- if (
1842
- // inlined: arena.typeOf(token)
1843
- arena.dataTypes[token] === EnumToken.Add ||
1844
- // inlined: arena.typeOf(token)
1845
- arena.dataTypes[token] === EnumToken.NextSiblingCombinatorTokenType ||
1846
- // inlined: arena.typeOf(token)
1847
- arena.dataTypes[token] === EnumToken.Sub) {
1848
- token = context.peek(2);
1849
- success =
1850
- // inlined: arena.typeOf(token)
1851
- arena.dataTypes[token] === EnumToken.NumberTokenType &&
1852
- Number.isInteger(arena.numericValue[token]);
1853
- }
1854
- if (success) {
1855
- context.update(token);
2417
+ // `n + 1`: the sign and the number follow the ident
2418
+ if (success && match?.[3] == null) {
2419
+ const sign = context.peek(1);
2420
+ // `n+1`: the sign is a part of the number
2421
+ if (arena.dataTypes[sign] === EnumToken.NumberTokenType) {
2422
+ if (!Number.isInteger(arena.numericValue[sign])) {
2423
+ success = false;
2424
+ break;
2425
+ }
2426
+ context.update(sign);
1856
2427
  return {
1857
2428
  success: true,
1858
2429
  valid: true,
@@ -1862,20 +2433,50 @@ function matchProperty(arena, property, context, options) {
1862
2433
  errors: [],
1863
2434
  };
1864
2435
  }
2436
+ if (
2437
+ // inlined: arena.typeOf(sign)
2438
+ arena.dataTypes[sign] === EnumToken.Add ||
2439
+ // inlined: arena.typeOf(sign)
2440
+ arena.dataTypes[sign] === EnumToken.NextSiblingCombinatorTokenType ||
2441
+ // inlined: arena.typeOf(sign)
2442
+ arena.dataTypes[sign] === EnumToken.Sub) {
2443
+ token = context.peek(2);
2444
+ success =
2445
+ // inlined: arena.typeOf(token)
2446
+ arena.dataTypes[token] === EnumToken.NumberTokenType &&
2447
+ Number.isInteger(arena.numericValue[token]);
2448
+ if (success) {
2449
+ context.update(token);
2450
+ return {
2451
+ success: true,
2452
+ valid: true,
2453
+ token: null,
2454
+ context,
2455
+ syntaxToken: property,
2456
+ errors: [],
2457
+ };
2458
+ }
2459
+ }
1865
2460
  }
1866
2461
  break;
1867
2462
  }
1868
2463
  if (
1869
2464
  // inlined: arena.typeOf(token)
1870
2465
  arena.dataTypes[token] === EnumToken.DimensionTokenType &&
1871
- arena.strings[arena.unit[token]] === "n") {
2466
+ arena.resolveString(arena.unit[token]) === "n") {
1872
2467
  success = Number.isInteger(arena.numericValue[token]);
1873
2468
  token = context.peek(1);
1874
2469
  if (token == null) {
1875
2470
  break;
1876
2471
  }
1877
2472
  if (arena.dataTypes[token] === EnumToken.NumberTokenType) {
1878
- // the sign of the number is not tracked by the tokenizer
2473
+ // `2n+1`, `-2n -0`: the sign is a part of the number
2474
+ if (Number.isInteger(arena.numericValue[token])) {
2475
+ context.next();
2476
+ }
2477
+ else {
2478
+ success = false;
2479
+ }
1879
2480
  break;
1880
2481
  }
1881
2482
  if (arena.dataTypes[token] === EnumToken.NextSiblingCombinatorTokenType) {
@@ -1903,11 +2504,13 @@ function matchProperty(arena, property, context, options) {
1903
2504
  case "content-position": {
1904
2505
  let values = propertyKeywordValues.get(property.val);
1905
2506
  if (values == null) {
1906
- values = new Set(config.syntaxes[property.val].syntax.split(/[\s|]+/));
2507
+ values = new Set((getSyntax(property.val) ?? "").split(/[\s|]+/));
1907
2508
  propertyKeywordValues.set(property.val, values);
1908
2509
  }
1909
2510
  // inlined: arena.typeOf(token)
1910
- success = arena.dataTypes[token] == EnumToken.IdenTokenType && values.has(arena.strings[arena.stringValue[token]].toLowerCase());
2511
+ success =
2512
+ arena.dataTypes[token] === EnumToken.IdenTokenType &&
2513
+ values.has(arena.resolveString(arena.stringValue[token]).toLowerCase());
1911
2514
  break;
1912
2515
  }
1913
2516
  case "mf-name":
@@ -1916,14 +2519,14 @@ function matchProperty(arena, property, context, options) {
1916
2519
  success =
1917
2520
  // inlined: arena.typeOf(token)
1918
2521
  arena.dataTypes[token] === EnumToken.IdenTokenType &&
1919
- arena.strings[arena.stringValue[token]].toLowerCase() in config.mediaFeatures;
2522
+ arena.resolveString(arena.stringValue[token]).toLowerCase() in config.mediaFeatures;
1920
2523
  }
1921
2524
  break;
1922
2525
  case "dashed-ident":
1923
2526
  case "extension-name":
1924
2527
  case "custom-property-name":
1925
2528
  // inlined: arena.typeOf(context.peek())
1926
- success = arena.dataTypes[context.peek()] == EnumToken.DashedIdenTokenType;
2529
+ success = arena.dataTypes[context.peek()] === EnumToken.DashedIdenTokenType;
1927
2530
  break;
1928
2531
  case "namespace-prefix":
1929
2532
  case "ident":
@@ -1932,10 +2535,12 @@ function matchProperty(arena, property, context, options) {
1932
2535
  case "counter-name":
1933
2536
  case "counter-style-name":
1934
2537
  // inlined: arena.typeOf(token)
1935
- success = arena.dataTypes[token] == EnumToken.IdenTokenType || arena.dataTypes[token] == EnumToken.DashedIdenTokenType;
2538
+ success =
2539
+ arena.dataTypes[token] === EnumToken.IdenTokenType ||
2540
+ arena.dataTypes[token] === EnumToken.DashedIdenTokenType;
1936
2541
  // inlined: arena.typeOf(token)
1937
2542
  if (success && arena.dataTypes[token] === EnumToken.IdenTokenType) {
1938
- const val = arena.strings[arena.stringValue[token]].toLowerCase();
2543
+ const val = arena.resolveString(arena.stringValue[token]).toLowerCase();
1939
2544
  success = "none" !== val && !allValues.includes(val);
1940
2545
  }
1941
2546
  break;
@@ -1958,6 +2563,21 @@ function matchProperty(arena, property, context, options) {
1958
2563
  ].includes(arena.dataTypes[token]);
1959
2564
  break;
1960
2565
  }
2566
+ case "unicode-range-token":
2567
+ // `U+0025-00FF`
2568
+ // inlined: arena.typeOf(context.peek())
2569
+ success = arena.dataTypes[context.peek()] === EnumToken.UniCodeRange;
2570
+ break;
2571
+ case "hash-token": {
2572
+ // <id-selector>: `#foo`. a valid hex color such as `#fade` is tokenized as a color
2573
+ const token = context.peek();
2574
+ success =
2575
+ // inlined: arena.typeOf(token)
2576
+ arena.dataTypes[token] === EnumToken.HashTokenType ||
2577
+ (arena.dataTypes[token] === EnumToken.ColorTokenType &&
2578
+ arena.colorType[token] === ColorType.HEX);
2579
+ break;
2580
+ }
1961
2581
  case "string": {
1962
2582
  const token = context.peek();
1963
2583
  // inlined: arena.typeOf(token)
@@ -1966,7 +2586,7 @@ function matchProperty(arena, property, context, options) {
1966
2586
  }
1967
2587
  case "wq-name": {
1968
2588
  // inlined: arena.typeOf(context.peek())
1969
- const success = arena.dataTypes[context.peek()] == EnumToken.IdenTokenType;
2589
+ const success = arena.dataTypes[context.peek()] === EnumToken.IdenTokenType;
1970
2590
  return {
1971
2591
  success,
1972
2592
  valid: true,
@@ -1980,13 +2600,15 @@ function matchProperty(arena, property, context, options) {
1980
2600
  {
1981
2601
  const token = context.peek();
1982
2602
  // inlined: arena.typeOf(token)
1983
- success = arena.dataTypes[token] == EnumToken.NumberTokenType;
2603
+ success = arena.dataTypes[token] === EnumToken.NumberTokenType;
1984
2604
  }
1985
2605
  break;
1986
2606
  case "zero": {
1987
2607
  const token = context.peek();
1988
2608
  // inlined: arena.typeOf(token)
1989
- success = arena.dataTypes[token] == EnumToken.NumberTokenType && arena.numericValue[token] === 0;
2609
+ success =
2610
+ arena.dataTypes[token] === EnumToken.NumberTokenType &&
2611
+ arena.numericValue[token] === 0;
1990
2612
  break;
1991
2613
  }
1992
2614
  case "integer":
@@ -1994,44 +2616,50 @@ function matchProperty(arena, property, context, options) {
1994
2616
  const token = context.peek();
1995
2617
  success =
1996
2618
  // inlined: arena.typeOf(token)
1997
- arena.dataTypes[token] == EnumToken.NumberTokenType && Number.isInteger(Number(arena.numericValue[token]));
2619
+ arena.dataTypes[token] === EnumToken.NumberTokenType &&
2620
+ Number.isInteger(Number(arena.numericValue[token]));
1998
2621
  }
1999
2622
  break;
2000
2623
  case "dimension": {
2001
2624
  const token = context.peek();
2002
2625
  success =
2003
2626
  // inlined: arena.typeOf(token)
2004
- arena.dataTypes[token] == EnumToken.DimensionTokenType ||
2627
+ arena.dataTypes[token] === EnumToken.DimensionTokenType ||
2005
2628
  // inlined: arena.typeOf(token)
2006
- arena.dataTypes[token] == EnumToken.AngleTokenType ||
2629
+ arena.dataTypes[token] === EnumToken.AngleTokenType ||
2007
2630
  // inlined: arena.typeOf(token)
2008
- arena.dataTypes[token] == EnumToken.LengthTokenType ||
2631
+ arena.dataTypes[token] === EnumToken.LengthTokenType ||
2009
2632
  // inlined: arena.typeOf(token)
2010
- arena.dataTypes[token] == EnumToken.PercentageTokenType ||
2633
+ arena.dataTypes[token] === EnumToken.PercentageTokenType ||
2011
2634
  // inlined: arena.typeOf(token)
2012
- arena.dataTypes[token] == EnumToken.ResolutionTokenType ||
2635
+ arena.dataTypes[token] === EnumToken.ResolutionTokenType ||
2013
2636
  // inlined: arena.typeOf(token)
2014
- arena.dataTypes[token] == EnumToken.TimeTokenType ||
2637
+ arena.dataTypes[token] === EnumToken.TimeTokenType ||
2015
2638
  // inlined: arena.typeOf(token)
2016
- arena.dataTypes[token] == EnumToken.FrequencyTokenType ||
2639
+ arena.dataTypes[token] === EnumToken.FrequencyTokenType ||
2017
2640
  // inlined: arena.typeOf(token)
2018
- arena.dataTypes[token] == EnumToken.UserDefinedDimensionTokenType ||
2641
+ arena.dataTypes[token] === EnumToken.UserDefinedDimensionTokenType ||
2019
2642
  // inlined: arena.typeOf(token)
2020
- arena.dataTypes[token] == EnumToken.FlexTokenType;
2643
+ arena.dataTypes[token] === EnumToken.FlexTokenType;
2021
2644
  break;
2022
2645
  }
2023
2646
  case "angle":
2024
2647
  {
2025
2648
  const token = context.peek();
2026
2649
  // inlined: arena.typeOf(token)
2027
- success = arena.dataTypes[token] === EnumToken.AngleTokenType || arena.dataTypes[token] === EnumToken.UserDefinedDimensionTokenType;
2650
+ success =
2651
+ arena.dataTypes[token] === EnumToken.AngleTokenType ||
2652
+ arena.dataTypes[token] === EnumToken.UserDefinedDimensionTokenType;
2028
2653
  }
2029
2654
  break;
2030
2655
  case "angle-percentage":
2031
2656
  {
2032
2657
  const token = context.peek();
2033
2658
  // inlined: arena.typeOf(token)
2034
- success = arena.dataTypes[token] === EnumToken.AngleTokenType || arena.dataTypes[token] === EnumToken.UserDefinedDimensionTokenType || arena.dataTypes[token] === EnumToken.PercentageTokenType;
2659
+ success =
2660
+ arena.dataTypes[token] === EnumToken.AngleTokenType ||
2661
+ arena.dataTypes[token] === EnumToken.UserDefinedDimensionTokenType ||
2662
+ arena.dataTypes[token] === EnumToken.PercentageTokenType;
2035
2663
  }
2036
2664
  break;
2037
2665
  case "percentage":
@@ -2048,28 +2676,49 @@ function matchProperty(arena, property, context, options) {
2048
2676
  const token = context.peek();
2049
2677
  success =
2050
2678
  // inlined: arena.typeOf(token)
2051
- arena.dataTypes[token] == EnumToken.PercentageTokenType ||
2679
+ arena.dataTypes[token] === EnumToken.PercentageTokenType ||
2052
2680
  // inlined: arena.typeOf(token)
2053
- arena.dataTypes[token] == EnumToken.LengthTokenType ||
2681
+ arena.dataTypes[token] === EnumToken.LengthTokenType ||
2054
2682
  // inlined: arena.typeOf(token)
2055
- arena.dataTypes[token] == EnumToken.UserDefinedDimensionTokenType ||
2683
+ arena.dataTypes[token] === EnumToken.UserDefinedDimensionTokenType ||
2056
2684
  // inlined: arena.typeOf(token)
2057
- (arena.dataTypes[token] == EnumToken.NumberTokenType && arena.numericValue[token] === 0);
2685
+ (arena.dataTypes[token] === EnumToken.NumberTokenType &&
2686
+ arena.numericValue[token] === 0);
2058
2687
  }
2059
2688
  break;
2060
2689
  case "flex":
2061
2690
  // inlined: arena.typeOf(context.peek())
2062
- success = arena.dataTypes[context.peek()] == EnumToken.FlexTokenType || arena.dataTypes[context.peek()] == EnumToken.UserDefinedDimensionTokenType;
2691
+ success =
2692
+ arena.dataTypes[context.peek()] === EnumToken.FlexTokenType ||
2693
+ arena.dataTypes[context.peek()] === EnumToken.UserDefinedDimensionTokenType;
2063
2694
  break;
2064
2695
  case "length": {
2065
2696
  const token = context.peek();
2066
2697
  success =
2067
2698
  // inlined: arena.typeOf(token)
2068
- arena.dataTypes[token] == EnumToken.LengthTokenType ||
2699
+ arena.dataTypes[token] === EnumToken.LengthTokenType ||
2069
2700
  // inlined: arena.typeOf(token)
2070
- arena.dataTypes[token] == EnumToken.UserDefinedDimensionTokenType ||
2701
+ arena.dataTypes[token] === EnumToken.UserDefinedDimensionTokenType ||
2071
2702
  // inlined: arena.typeOf(token)
2072
- (arena.dataTypes[token] == EnumToken.NumberTokenType && arena.numericValue[token] === 0);
2703
+ (arena.dataTypes[token] === EnumToken.NumberTokenType &&
2704
+ arena.numericValue[token] === 0);
2705
+ break;
2706
+ }
2707
+ case "resolution":
2708
+ // inlined: arena.typeOf(context.peek())
2709
+ success = arena.dataTypes[context.peek()] === EnumToken.ResolutionTokenType;
2710
+ break;
2711
+ case "frequency":
2712
+ // inlined: arena.typeOf(context.peek())
2713
+ success = arena.dataTypes[context.peek()] === EnumToken.FrequencyTokenType;
2714
+ break;
2715
+ case "decibel":
2716
+ case "semitones": {
2717
+ // `6dB`, `2st`
2718
+ const token = context.peek();
2719
+ success =
2720
+ arena.dataTypes[token] === EnumToken.DimensionTokenType &&
2721
+ equalsIgnoreCase(arena.resolveString(arena.unit[token]), property.val === "decibel" ? "dB" : "st");
2073
2722
  break;
2074
2723
  }
2075
2724
  case "time":
@@ -2078,7 +2727,9 @@ function matchProperty(arena, property, context, options) {
2078
2727
  if (token == null) {
2079
2728
  break;
2080
2729
  }
2081
- success = arena.dataTypes[token] == EnumToken.TimeTokenType || arena.dataTypes[token] == EnumToken.UserDefinedDimensionTokenType;
2730
+ success =
2731
+ arena.dataTypes[token] === EnumToken.TimeTokenType ||
2732
+ arena.dataTypes[token] === EnumToken.UserDefinedDimensionTokenType;
2082
2733
  }
2083
2734
  break;
2084
2735
  case "color":
@@ -2087,11 +2738,11 @@ function matchProperty(arena, property, context, options) {
2087
2738
  const token = context.peek();
2088
2739
  const errors = [];
2089
2740
  // inlined: arena.typeOf(token)
2090
- if (tokensfuncDefMap.has((token == null ? null : arena.dataTypes[token]))) {
2741
+ if (isFunctionDefType((token == null ? null : arena.dataTypes[token]))) {
2091
2742
  const range = context.peekRange();
2092
2743
  const newRange = range.map((t) => arena.clone(t, true));
2093
2744
  parseTokens(arena, newRange, { parseColor: true }, errors);
2094
- success = newRange.length == 1 && isColor(arena, newRange[0], errors);
2745
+ success = newRange.length === 1 && isColor(arena, newRange[0], errors);
2095
2746
  if (success) {
2096
2747
  context.update(range.at(-1));
2097
2748
  return {
@@ -2136,11 +2787,11 @@ function matchProperty(arena, property, context, options) {
2136
2787
  case "complex-selector-list": {
2137
2788
  const tokens = context.getRemainingTokens();
2138
2789
  const result = matchSelectorSyntax(arena, tokens, [], options, property.val.startsWith("forgiving-relative-selector-list"));
2139
- if (result.success || property.val == "forgiving-relative-selector-list") {
2790
+ if (result.success || property.val === "forgiving-relative-selector-list") {
2140
2791
  context.update(tokens.at(-1));
2141
2792
  }
2142
2793
  return {
2143
- success: result.success || property.val == "forgiving-relative-selector-list",
2794
+ success: result.success || property.val === "forgiving-relative-selector-list",
2144
2795
  valid: true,
2145
2796
  token: context.peek(),
2146
2797
  context,
@@ -2150,9 +2801,9 @@ function matchProperty(arena, property, context, options) {
2150
2801
  }
2151
2802
  case "function-token":
2152
2803
  // inlined: arena.typeOf(context.peek()). the type of a null id is undefined: not a key of the map either
2153
- if (tokensfuncDefMap.has(arena.dataTypes[context.peek()])) {
2804
+ if (isFunctionDefType(arena.dataTypes[context.peek()])) {
2805
+ // the token is consumed below
2154
2806
  success = true;
2155
- context.next();
2156
2807
  break;
2157
2808
  }
2158
2809
  return {
@@ -2167,68 +2818,54 @@ function matchProperty(arena, property, context, options) {
2167
2818
  const token = context.peek();
2168
2819
  success =
2169
2820
  // inlined: arena.typeOf(token)
2170
- arena.dataTypes[token] == EnumToken.IdenTokenType &&
2821
+ arena.dataTypes[token] === EnumToken.IdenTokenType &&
2171
2822
  // @ts-expect-error
2172
- config.languages.some((t) => t === arena.strings[arena.stringValue[token]] || arena.strings[arena.stringValue[token]].startsWith(t + "-"));
2823
+ config.languages.some((t) => t === arena.resolveString(arena.stringValue[token]) ||
2824
+ arena.resolveString(arena.stringValue[token]).startsWith(t + "-"));
2173
2825
  break;
2174
2826
  }
2175
2827
  case "size-feature": {
2176
- const range = context.peekRange();
2177
- const filteredRange = range.filter((token) => arena.dataTypes[token] !== EnumToken.WhitespaceTokenType && arena.dataTypes[token] !== EnumToken.CommentTokenType);
2178
- if (filteredRange.length === 1) {
2179
- success =
2180
- arena.dataTypes[filteredRange[0]] === EnumToken.IdenTokenType ||
2181
- arena.dataTypes[filteredRange[0]] === EnumToken.DashedIdenTokenType;
2182
- context.update(filteredRange[0]);
2183
- return {
2184
- success,
2185
- valid: true,
2186
- token: filteredRange[0],
2187
- context,
2188
- syntaxToken: property,
2189
- errors: [],
2190
- };
2191
- }
2192
- if (!mFLT.has(arena.dataTypes[filteredRange[1]]) &&
2193
- !mFGT.has(arena.dataTypes[filteredRange[1]]) &&
2194
- arena.dataTypes[filteredRange[1]] !== EnumToken.ColonTokenType &&
2195
- arena.dataTypes[filteredRange[1]] !== EnumToken.DelimTokenType) {
2196
- return {
2197
- success: false,
2198
- valid: true,
2199
- token: filteredRange[1],
2200
- context,
2201
- syntaxToken: property,
2202
- errors: [],
2203
- };
2828
+ // <mf-name> | <mf-name> : <mf-value> | <mf-range>. the feature is validated by the at-rule parser
2829
+ const filteredRange = peekFeatureTokens(arena, context);
2830
+ success =
2831
+ filteredRange.length === 1
2832
+ ? arena.dataTypes[filteredRange[0]] === EnumToken.IdenTokenType ||
2833
+ arena.dataTypes[filteredRange[0]] === EnumToken.DashedIdenTokenType
2834
+ : filteredRange.length > 2 &&
2835
+ (mFLT.has(arena.dataTypes[filteredRange[1]]) ||
2836
+ mFGT.has(arena.dataTypes[filteredRange[1]]) ||
2837
+ arena.dataTypes[filteredRange[1]] === EnumToken.ColonTokenType ||
2838
+ arena.dataTypes[filteredRange[1]] === EnumToken.DelimTokenType);
2839
+ if (success) {
2840
+ context.update(filteredRange.at(-1));
2204
2841
  }
2205
- break;
2842
+ return {
2843
+ success,
2844
+ valid: true,
2845
+ token: success ? null : (filteredRange[1] ?? context.peek()),
2846
+ context,
2847
+ syntaxToken: property,
2848
+ errors: [],
2849
+ };
2206
2850
  }
2207
2851
  case "style-feature": {
2208
- // case 'style-feature-value':
2209
- // match <style-feature-plain>
2210
- // match <style-feature-boolean>
2211
- // match <style-range>
2212
- const range = context.peekRange();
2213
- const filteredRange = range.filter((token) => arena.dataTypes[token] !== EnumToken.WhitespaceTokenType && arena.dataTypes[token] !== EnumToken.CommentTokenType);
2214
- if (filteredRange.length === 1) {
2215
- success =
2216
- arena.dataTypes[filteredRange[0]] === EnumToken.IdenTokenType ||
2217
- arena.dataTypes[filteredRange[0]] === EnumToken.DashedIdenTokenType;
2218
- context.update(filteredRange[0]);
2219
- return {
2220
- success,
2221
- valid: true,
2222
- token: filteredRange[0],
2223
- context,
2224
- syntaxToken: property,
2225
- errors: [],
2226
- };
2852
+ // <style-feature-plain> | <style-feature-boolean> | <style-range>
2853
+ const filteredRange = peekFeatureTokens(arena, context);
2854
+ success =
2855
+ filteredRange.length === 1
2856
+ ? arena.dataTypes[filteredRange[0]] === EnumToken.IdenTokenType ||
2857
+ arena.dataTypes[filteredRange[0]] === EnumToken.DashedIdenTokenType
2858
+ : filteredRange.length > 2 &&
2859
+ (mFLT.has(arena.dataTypes[filteredRange[1]]) ||
2860
+ mFGT.has(arena.dataTypes[filteredRange[1]]) ||
2861
+ arena.dataTypes[filteredRange[1]] === EnumToken.ColonTokenType);
2862
+ if (success) {
2863
+ context.update(filteredRange.at(-1));
2227
2864
  }
2228
2865
  return {
2229
- success: false,
2230
- valid: false,
2231
- token: context.peek(),
2866
+ success,
2867
+ valid: success,
2868
+ token: success ? null : context.peek(),
2232
2869
  context,
2233
2870
  syntaxToken: property,
2234
2871
  errors: [],
@@ -2238,25 +2875,27 @@ function matchProperty(arena, property, context, options) {
2238
2875
  const token = context.peek();
2239
2876
  let l;
2240
2877
  // inlined: arena.typeOf(token)
2241
- if (arena.dataTypes[token] == EnumToken.UrlFunctionTokenDefType) {
2878
+ if (arena.dataTypes[token] === EnumToken.UrlFunctionTokenDefType) {
2242
2879
  l = 0;
2243
2880
  let token;
2244
2881
  while ((token = context.peek(++l))) {
2245
- if (arena.dataTypes[token] !== EnumToken.WhitespaceTokenType && arena.dataTypes[token] !== EnumToken.CommentTokenType) {
2882
+ if (arena.dataTypes[token] !== EnumToken.WhitespaceTokenType &&
2883
+ arena.dataTypes[token] !== EnumToken.CommentTokenType) {
2246
2884
  break;
2247
2885
  }
2248
2886
  }
2249
2887
  if (token != null &&
2250
- (arena.dataTypes[token] == EnumToken.StringTokenType ||
2251
- arena.dataTypes[token] == EnumToken.IdenTokenType ||
2252
- arena.dataTypes[token] == EnumToken.UrlTokenTokenType)) {
2888
+ (arena.dataTypes[token] === EnumToken.StringTokenType ||
2889
+ arena.dataTypes[token] === EnumToken.IdenTokenType ||
2890
+ arena.dataTypes[token] === EnumToken.UrlTokenTokenType)) {
2253
2891
  while ((token = context.peek(++l))) {
2254
- if (arena.dataTypes[token] !== EnumToken.WhitespaceTokenType && arena.dataTypes[token] !== EnumToken.CommentTokenType) {
2892
+ if (arena.dataTypes[token] !== EnumToken.WhitespaceTokenType &&
2893
+ arena.dataTypes[token] !== EnumToken.CommentTokenType) {
2255
2894
  break;
2256
2895
  }
2257
2896
  }
2258
2897
  // inlined: arena.typeOf(token)
2259
- if (arena.dataTypes[token] == EnumToken.EndParensTokenType) {
2898
+ if (arena.dataTypes[token] === EnumToken.EndParensTokenType) {
2260
2899
  success = true;
2261
2900
  context.update(token);
2262
2901
  return {
@@ -2272,7 +2911,7 @@ function matchProperty(arena, property, context, options) {
2272
2911
  break;
2273
2912
  }
2274
2913
  // inlined: arena.typeOf(token)
2275
- if (arena.dataTypes[token] != EnumToken.UrlFunctionTokenType) {
2914
+ if (arena.dataTypes[token] !== EnumToken.UrlFunctionTokenType) {
2276
2915
  success = false;
2277
2916
  break;
2278
2917
  }
@@ -2286,9 +2925,9 @@ function matchProperty(arena, property, context, options) {
2286
2925
  }
2287
2926
  if (l < urlTokens.length) {
2288
2927
  success =
2289
- arena.dataTypes[urlTokens[l]] == EnumToken.StringTokenType ||
2290
- arena.dataTypes[urlTokens[l]] == EnumToken.IdenTokenType ||
2291
- arena.dataTypes[urlTokens[l]] == EnumToken.UrlTokenTokenType;
2928
+ arena.dataTypes[urlTokens[l]] === EnumToken.StringTokenType ||
2929
+ arena.dataTypes[urlTokens[l]] === EnumToken.IdenTokenType ||
2930
+ arena.dataTypes[urlTokens[l]] === EnumToken.UrlTokenTokenType;
2292
2931
  context.next();
2293
2932
  return {
2294
2933
  success: true,
@@ -2302,10 +2941,38 @@ function matchProperty(arena, property, context, options) {
2302
2941
  break;
2303
2942
  }
2304
2943
  case "css-type": {
2305
- // inlined: arena.typeOf(context.peek())
2306
- success = arena.dataTypes[context.peek()] == EnumToken.CssTypeTokenType;
2944
+ const token = context.peek();
2945
+ // `type(<number> | <transform-function>)`: the css type syntax
2946
+ if ((arena.dataTypes[token] === EnumToken.FunctionTokenDefType ||
2947
+ arena.dataTypes[token] === EnumToken.FunctionTokenType) &&
2948
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[token]), "type")) {
2949
+ if (arena.dataTypes[token] === EnumToken.FunctionTokenDefType) {
2950
+ context.update(context.peekRange().at(-1));
2951
+ }
2952
+ else {
2953
+ context.next();
2954
+ }
2955
+ return {
2956
+ success: true,
2957
+ valid: true,
2958
+ token: null,
2959
+ context,
2960
+ syntaxToken: null,
2961
+ errors: [],
2962
+ };
2963
+ }
2964
+ // inlined: arena.typeOf(token)
2965
+ success = arena.dataTypes[token] === EnumToken.CssTypeTokenType;
2307
2966
  if (success) {
2308
2967
  context.next();
2968
+ // the multiplier follows the type: `<length>#`, `<length>+`
2969
+ const multiplier = context.tokens[context.index + 1];
2970
+ if (multiplier != null &&
2971
+ (arena.dataTypes[multiplier] === EnumToken.Plus ||
2972
+ (arena.dataTypes[multiplier] === EnumToken.LiteralTokenType &&
2973
+ arena.resolveString(arena.stringValue[multiplier]) === "#"))) {
2974
+ context.next();
2975
+ }
2309
2976
  return {
2310
2977
  success: true,
2311
2978
  valid: true,
@@ -2320,9 +2987,17 @@ function matchProperty(arena, property, context, options) {
2320
2987
  case "dashed-function": {
2321
2988
  const token = context.peek();
2322
2989
  // inlined: arena.typeOf(token)
2323
- success = arena.dataTypes[token] == EnumToken.CustomFunctionTokenDefType;
2990
+ success =
2991
+ arena.dataTypes[token] === EnumToken.CustomFunctionTokenDefType ||
2992
+ arena.dataTypes[token] === EnumToken.CustomFunctionTokenType;
2324
2993
  if (success) {
2325
- context.next();
2994
+ // `--name(<arguments>)`: the function and its arguments
2995
+ if (arena.dataTypes[token] === EnumToken.CustomFunctionTokenDefType) {
2996
+ context.update(context.peekRange().at(-1));
2997
+ }
2998
+ else {
2999
+ context.next();
3000
+ }
2326
3001
  return {
2327
3002
  success: true,
2328
3003
  valid: true,
@@ -2336,7 +3011,7 @@ function matchProperty(arena, property, context, options) {
2336
3011
  }
2337
3012
  case "block-contents":
2338
3013
  // inlined: arena.typeOf(context.peek())
2339
- success = arena.dataTypes[context.peek()] == EnumToken.StartParensTokenType;
3014
+ success = arena.dataTypes[context.peek()] === EnumToken.StartParensTokenType;
2340
3015
  if (success) {
2341
3016
  context.next();
2342
3017
  return {
@@ -2357,7 +3032,7 @@ function matchProperty(arena, property, context, options) {
2357
3032
  errors: [],
2358
3033
  };
2359
3034
  default:
2360
- if (!(property.val in config.syntaxes)) {
3035
+ if (!hasSyntax(property.val)) {
2361
3036
  console.debug("Unknown syntax", context.peek(), property);
2362
3037
  throw new Error(`Unexpected validation property ${property.val}`);
2363
3038
  }
@@ -2365,8 +3040,8 @@ function matchProperty(arena, property, context, options) {
2365
3040
  }
2366
3041
  if (!success &&
2367
3042
  // inlined: arena.typeOf(context.peek())
2368
- arena.dataTypes[context.peek()] == EnumToken.IdenTokenType &&
2369
- allValues.includes(arena.strings[arena.stringValue[context.peek()]])) {
3043
+ arena.dataTypes[context.peek()] === EnumToken.IdenTokenType &&
3044
+ allValues.includes(arena.resolveString(arena.stringValue[context.peek()]))) {
2370
3045
  success = true;
2371
3046
  }
2372
3047
  if (success) {
@@ -2391,6 +3066,7 @@ function matchProperty(arena, property, context, options) {
2391
3066
  }
2392
3067
  /**
2393
3068
  * matches a repeatable syntax
3069
+ * @param arena
2394
3070
  * @param syntax
2395
3071
  * @param context
2396
3072
  * @param options
@@ -2410,7 +3086,8 @@ function matchRepeatableSyntax(arena, syntax, context, options) {
2410
3086
  context.end();
2411
3087
  break;
2412
3088
  }
2413
- if (tmpResult.context.index === context.index) {
3089
+ // no token matched. the indexes of the contexts are not comparable: the result context is a slice
3090
+ if (tmpResult.context.current() === context.current()) {
2414
3091
  break;
2415
3092
  }
2416
3093
  context.update(result.context.current());