@tbela99/css-parser 2.1.0 → 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 (125) hide show
  1. package/CHANGELOG.md +42 -1
  2. package/README.md +14 -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 +5 -2
  6. package/dist/index-umd-web.js +10738 -4359
  7. package/dist/index.d.ts +734 -100
  8. package/dist/{src/lib → lib}/ast/arena-node.js +10 -10
  9. package/dist/{src/lib → lib}/ast/arena.js +304 -139
  10. package/dist/{src/lib → lib}/ast/cache.js +2 -2
  11. package/dist/{src/lib → lib}/ast/features/calc.js +11 -9
  12. package/dist/{src/lib → lib}/ast/features/if.js +6 -6
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +12 -10
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +39 -39
  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 +24 -24
  21. package/dist/{src/lib → lib}/ast/math/math.js +2 -2
  22. package/dist/{src/lib → lib}/ast/minify.js +745 -201
  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 +2 -2
  37. package/dist/{src/lib → lib}/ast/walk.js +2 -2
  38. package/dist/{src/lib → lib}/fs/resolve.js +2 -2
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +25 -8
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +15 -15
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +5 -5
  42. package/dist/{src/lib → lib}/parser/input.js +18 -3
  43. package/dist/{src/lib → lib}/parser/linesmap.js +2 -2
  44. package/dist/{src/lib → lib}/parser/parse.js +425 -258
  45. package/dist/{src/lib → lib}/parser/source.js +2 -2
  46. package/dist/{src/lib → lib}/parser/tokenize.js +480 -312
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +12 -12
  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 +7 -7
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +10 -10
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +14 -14
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +9 -14
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +6 -6
  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 +31 -26
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +2 -2
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +2 -2
  64. package/dist/{src/lib → lib}/parser/utils/selector.js +305 -63
  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 +9 -7
  68. package/dist/{src/lib → lib}/printer/render.js +420 -152
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +2 -2
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +2 -2
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +2 -2
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +10 -10
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +2 -2
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +18 -18
  77. package/dist/{src/lib → lib}/syntax/color/color.js +18 -18
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +3 -3
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +2 -2
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +2 -2
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +2 -2
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +2 -2
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +2 -2
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +3 -3
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +2 -2
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +2 -2
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +14 -14
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +4 -4
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +5 -5
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +4 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +10 -10
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +5 -5
  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 +3 -3
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +2 -2
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +2 -2
  99. package/dist/{src/lib → lib}/syntax/constants.js +26 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +37 -29
  101. package/dist/{src/lib → lib}/validation/config.js +23 -10
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +292 -150
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +2 -2
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +2 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +52 -34
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +10 -5
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +47 -34
  113. package/package.json +12 -12
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -95
  116. package/dist/src/lib/ast/features/transform.js +0 -90
  117. package/dist/src/lib/ast/transform/compute.js +0 -362
  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/utils/cache.js +0 -54
@@ -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.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { TokenField } from '../ast/arena.js';
9
9
  import { getParsedSyntax, hasSyntax, getSyntax, getSyntaxConfig } from './config.js';
10
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
@@ -165,7 +166,7 @@ function isMFValue(arena, featureName, tokens, isMFRange) {
165
166
  valid: true,
166
167
  success: tokens.length === 1 &&
167
168
  arena.dataTypes[tokens[0]] === EnumToken.IdenTokenType &&
168
- mediaFeature.values.includes(arena.strings[arena.stringValue[tokens[0]]].toLowerCase()),
169
+ mediaFeature.values.includes(arena.resolveString(arena.stringValue[tokens[0]]).toLowerCase()),
169
170
  };
170
171
  case MediaFeatureType.LengthType:
171
172
  return {
@@ -209,7 +210,7 @@ function isMFValue(arena, featureName, tokens, isMFRange) {
209
210
  arena.dataTypes[tokens[0]] === EnumToken.NumberTokenType &&
210
211
  !arena.hasFraction(tokens[0]) &&
211
212
  arena.dataTypes[tokens[1]] === EnumToken.LiteralTokenType &&
212
- arena.strings[arena.stringValue[tokens[1]]] === "/" &&
213
+ arena.resolveString(arena.stringValue[tokens[1]]) === "/" &&
213
214
  arena.dataTypes[tokens[2]] === EnumToken.NumberTokenType &&
214
215
  !arena.hasFraction(tokens[2])),
215
216
  };
@@ -267,6 +268,8 @@ class ValidationTokenContext {
267
268
  let token = this.tokens[index];
268
269
  // track balanced parens
269
270
  let matchCount = 0;
271
+ // track balanced curly braces: `{a, b}` is a single comma containing value (mixin arguments)
272
+ let blockCount = 0;
270
273
  const tokens = [];
271
274
  while (index + 1 < this.tokens.length &&
272
275
  this.arena.dataTypes[this.tokens[index + 1]] === EnumToken.WhitespaceTokenType) {
@@ -275,16 +278,22 @@ class ValidationTokenContext {
275
278
  while (index + 1 < this.tokens.length) {
276
279
  token = this.tokens[++index];
277
280
  if (this.arena.dataTypes[token] === EnumToken.StartParensTokenType ||
278
- tokensfuncDefMap.has(this.arena.dataTypes[token])) {
281
+ isFunctionDefType(this.arena.dataTypes[token])) {
279
282
  matchCount++;
280
283
  }
281
284
  else if (this.arena.dataTypes[token] === EnumToken.EndParensTokenType) {
282
285
  matchCount--;
283
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
+ }
284
293
  if (matchCount >= 0) {
285
294
  tokens.push(token);
286
295
  }
287
- if (matchCount === 0) {
296
+ if (matchCount === 0 && blockCount === 0) {
288
297
  if (close !== EnumToken.EndParensTokenType && this.arena.dataTypes[token] === close) {
289
298
  counter--;
290
299
  }
@@ -305,7 +314,7 @@ class ValidationTokenContext {
305
314
  while (index + 1 < this.tokens.length) {
306
315
  token = this.tokens[++index];
307
316
  if (this.arena.dataTypes[token] === EnumToken.StartParensTokenType ||
308
- tokensfuncDefMap.has(this.arena.dataTypes[token])) {
317
+ isFunctionDefType(this.arena.dataTypes[token])) {
309
318
  matchCount++;
310
319
  }
311
320
  else if (this.arena.dataTypes[token] === EnumToken.EndParensTokenType) {
@@ -441,7 +450,7 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
441
450
  arena.dataTypes[token] = EnumToken.UniversalSelectorTokenType;
442
451
  }
443
452
  tokens.push(token);
444
- if (tokensfuncDefMap.has(arena.dataTypes[token])) {
453
+ if (isFunctionDefType(arena.dataTypes[token])) {
445
454
  if (stack.length > 0 && selectorNodeTypes.includes(arena.dataTypes[stack[stack.length - 1]])) {
446
455
  stack.pop();
447
456
  }
@@ -459,7 +468,7 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
459
468
  switch (arena.dataTypes[token]) {
460
469
  case EnumToken.PseudoClassFuncTokenType:
461
470
  {
462
- 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);
463
472
  if (!result.success) {
464
473
  success = false;
465
474
  if (result.errors.length > 0) {
@@ -500,9 +509,12 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
500
509
  case EnumToken.Tilda:
501
510
  case EnumToken.GtTokenType:
502
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;
503
516
  case EnumToken.ColumnCombinatorTokenType:
504
517
  case EnumToken.ChildCombinatorTokenType:
505
- case EnumToken.UniversalSelectorTokenType:
506
518
  case EnumToken.DescendantCombinatorTokenType:
507
519
  case EnumToken.NextSiblingCombinatorTokenType:
508
520
  case EnumToken.SubsequentSiblingCombinatorTokenType:
@@ -608,9 +620,9 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
608
620
  slice.shift();
609
621
  slice.shift();
610
622
  if (arena.dataTypes[slice[0]] === EnumToken.StringTokenType &&
611
- /^[a-zA-Z0-9_-]+$/.test(arena.strings[arena.stringValue[slice[0]]].slice(1, -1))) {
612
- // inlined: arena.setStr(slice[0], arena.strings[arena.stringValue[slice[0]]].slice(1, -1))
613
- 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));
614
626
  arena.dataTypes[slice[0]] = EnumToken.IdenTokenType;
615
627
  }
616
628
  slice.shift();
@@ -619,8 +631,8 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
619
631
  break;
620
632
  }
621
633
  if (arena.dataTypes[slice[0]] !== EnumToken.IdenTokenType ||
622
- (arena.strings[arena.stringValue[slice[0]]] !== "i" &&
623
- arena.strings[arena.stringValue[slice[0]]] !== "s")) {
634
+ (arena.resolveString(arena.stringValue[slice[0]]) !== "i" &&
635
+ arena.resolveString(arena.stringValue[slice[0]]) !== "s")) {
624
636
  return {
625
637
  success: false,
626
638
  errors: [
@@ -670,7 +682,7 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
670
682
  arena.dataTypes[stack.at(-1)] === EnumToken.PseudoElementTokenType) {
671
683
  const token = stack.at(-1);
672
684
  const index = tokens.indexOf(token);
673
- 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);
674
686
  if (!result.success) {
675
687
  success = false;
676
688
  if (result.errors.length > 0) {
@@ -762,10 +774,51 @@ function matchSelectorSyntax(arena, stream, errors, options, nested = true) {
762
774
  }
763
775
  /**
764
776
  * declaration validations (successful and failed), by string table of the arena, by syntax and by hash of the value
765
- * encoding (see {@link encodeToken}). the encoding contains interned string ids: the string table is the key, it is
766
- * 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})
767
779
  */
768
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
+ }
769
822
  /**
770
823
  * shared result of a declaration whose validation is cached
771
824
  */
@@ -800,14 +853,18 @@ function encode(value) {
800
853
  }
801
854
  /**
802
855
  * encode a token and its descendants. the validation only depends on these fields: two values with the same encoding
803
- * 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
804
858
  * @param arena
805
859
  * @param token
806
860
  */
807
861
  function encodeToken(arena, token) {
808
- encode(arena.dataTypes[token]);
862
+ const typ = arena.dataTypes[token];
863
+ encode(typ);
809
864
  encode(arena.name[token]);
810
- 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]);
811
868
  encode(arena.numericValue[token]);
812
869
  encode(arena.unit[token]);
813
870
  encode(arena.colorType[token]);
@@ -833,14 +890,11 @@ function encodeToken(arena, token) {
833
890
  * @param create create the bucket when missing
834
891
  */
835
892
  function cachedValidations(arena, syntaxes, create) {
836
- let bySyntax = declarationValidations.get(arena.strings);
837
- if (bySyntax == null) {
838
- if (!create) {
839
- return null;
840
- }
841
- bySyntax = new Map();
842
- declarationValidations.set(arena.strings, bySyntax);
893
+ const cache = validationCache(arena, create);
894
+ if (cache === null) {
895
+ return null;
843
896
  }
897
+ const bySyntax = cache.bySyntax;
844
898
  let byHash = bySyntax.get(syntaxes);
845
899
  if (byHash == null) {
846
900
  if (!create) {
@@ -864,11 +918,12 @@ function cachedValidations(arena, syntaxes, create) {
864
918
  function findValidation(arena, syntaxes) {
865
919
  const bucket = cachedValidations(arena, syntaxes, false);
866
920
  if (bucket != null) {
921
+ const pool = validationCache(arena, false).pool;
867
922
  for (let i = 0; i < bucket.length; i++) {
868
- const candidate = bucket[i].encoding;
869
- if (candidate.length === encodingLength) {
923
+ const offset = bucket[i].offset;
924
+ if (bucket[i].length === encodingLength) {
870
925
  let k = 0;
871
- while (k < encodingLength && candidate[k] === encoding[k]) {
926
+ while (k < encodingLength && pool[offset + k] === encoding[k]) {
872
927
  k++;
873
928
  }
874
929
  if (k === encodingLength) {
@@ -883,14 +938,15 @@ function findValidation(arena, syntaxes) {
883
938
  * cache the validation of an encoding for the syntax
884
939
  * @param arena
885
940
  * @param syntaxes
886
- * @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
887
943
  * @param hash encoding hash
888
944
  * @param valid
889
945
  * @param errors errors of a failed validation, null when unknown
890
946
  */
891
- function cacheValidation(arena, syntaxes, value, hash, valid, errors) {
947
+ function cacheValidation(arena, syntaxes, offset, length, hash, valid, errors) {
892
948
  encodingHash = hash;
893
- cachedValidations(arena, syntaxes, true).push({ encoding: value, valid, errors });
949
+ cachedValidations(arena, syntaxes, true).push({ offset, length, valid, errors });
894
950
  }
895
951
  /**
896
952
  * the value tokens and their descendants, in the encoding order (see {@link encodeToken})
@@ -961,10 +1017,9 @@ function bindErrors(arena, tokens, errors, options) {
961
1017
  }
962
1018
  }
963
1019
  }
964
- const copy = { ...error };
965
- delete copy.node;
966
- delete copy.location;
967
- 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 });
968
1023
  }
969
1024
  return result;
970
1025
  }
@@ -1036,12 +1091,13 @@ function matchDeclarationSyntax(arena, syntaxes, tokens, options) {
1036
1091
  };
1037
1092
  }
1038
1093
  }
1039
- // the encoding is copied before the validation, which may encode other values
1040
- 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);
1041
1097
  const hash = encodingHash;
1042
1098
  const result = matchAllSyntaxes(arena, syntaxes, createValidationContext(arena, tokens), options);
1043
1099
  if (result.success) {
1044
- cacheValidation(arena, syntaxes, value, hash, true, null);
1100
+ cacheValidation(arena, syntaxes, offset, length, hash, true, null);
1045
1101
  }
1046
1102
  else {
1047
1103
  const errors = bindErrors(arena, valueTokens(arena, tokens), result.errors, options);
@@ -1050,7 +1106,7 @@ function matchDeclarationSyntax(arena, syntaxes, tokens, options) {
1050
1106
  cached.errors = errors;
1051
1107
  }
1052
1108
  else {
1053
- cacheValidation(arena, syntaxes, value, hash, false, errors);
1109
+ cacheValidation(arena, syntaxes, offset, length, hash, false, errors);
1054
1110
  }
1055
1111
  }
1056
1112
  return result;
@@ -1074,14 +1130,62 @@ function isValidDeclarationValue(arena, syntaxes, declaration, options) {
1074
1130
  if (cached != null) {
1075
1131
  return cached.valid;
1076
1132
  }
1077
- // the encoding is copied before the validation, which may encode other values
1078
- 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);
1079
1136
  const hash = encodingHash;
1080
1137
  const valid = matchAllSyntaxes(arena, syntaxes, createValidationContext(arena, arena.tokens(declaration)), options).success;
1081
1138
  // the errors are not needed: they are computed if a declaration with the same value is validated by the parser
1082
- cacheValidation(arena, syntaxes, value, hash, valid, null);
1139
+ cacheValidation(arena, syntaxes, offset, length, hash, valid, null);
1083
1140
  return valid;
1084
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
+ }
1085
1189
  /**
1086
1190
  * matches all syntaxes
1087
1191
  * @param arena
@@ -1091,17 +1195,19 @@ function isValidDeclarationValue(arena, syntaxes, declaration, options) {
1091
1195
  * @returns
1092
1196
  */
1093
1197
  function matchAllSyntaxes(arena, syntaxes, context, options) {
1094
- // the visited syntaxes are tracked per call. the options are not copied: the previous map is restored
1095
- 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;
1096
1201
  // the result object is passed to the matching functions, which fill it
1097
1202
  const out = createMatch();
1098
1203
  let result;
1099
- options.visited = new Map();
1204
+ visitedBase = visitedLength;
1100
1205
  try {
1101
1206
  result = matchSyntax(arena, syntaxes, context, options, out);
1102
1207
  }
1103
1208
  finally {
1104
- options.visited = visited;
1209
+ visitedBase = base;
1210
+ visitedLength = length;
1105
1211
  }
1106
1212
  if (syntaxes != null && result.success && result.syntaxToken != null) {
1107
1213
  const index = syntaxes.indexOf(result.syntaxToken);
@@ -1288,46 +1394,6 @@ function matchOccurenceSyntax(arena, syntax, context, options) {
1288
1394
  * @param options
1289
1395
  * @returns
1290
1396
  */
1291
- /**
1292
- * the syntaxes that may match no token, see {@link isNullable}
1293
- */
1294
- const nullableSyntaxes = new WeakMap();
1295
- /**
1296
- * true if the syntax may match no token: `<a>?`, `<a>*`, `[ <a>? <b>? ]`, `[ [ and <a> ]* | [ or <a> ]* ]`
1297
- * @param syntax
1298
- */
1299
- function isNullable(syntax) {
1300
- if (syntax.typ === ValidationTokenEnum.Whitespace) {
1301
- return true;
1302
- }
1303
- // `[ <a>? <b>? ]!`: at least one token
1304
- if (syntax.isMandatatoryGroup) {
1305
- return false;
1306
- }
1307
- if (syntax.isOptional || syntax.isRepeatable || syntax.match?.min?.val === 0) {
1308
- return true;
1309
- }
1310
- let nullable = nullableSyntaxes.get(syntax);
1311
- if (nullable == null) {
1312
- switch (syntax.typ) {
1313
- case ValidationTokenEnum.Bracket:
1314
- nullable = syntax.chi.every(isNullable);
1315
- break;
1316
- case ValidationTokenEnum.PipeToken:
1317
- nullable = syntax.chi.some((syntaxes) => syntaxes.every(isNullable));
1318
- break;
1319
- case ValidationTokenEnum.AmpersandToken:
1320
- nullable =
1321
- syntax.l.every(isNullable) &&
1322
- syntax.r.every(isNullable);
1323
- break;
1324
- default:
1325
- nullable = false;
1326
- }
1327
- nullableSyntaxes.set(syntax, nullable);
1328
- }
1329
- return nullable;
1330
- }
1331
1397
  /**
1332
1398
  * index of the first syntax that must be matched, starting at index. -1 if the remaining syntaxes may match no token
1333
1399
  * @param syntaxes
@@ -1341,6 +1407,23 @@ function findMandatorySyntax(syntaxes, index) {
1341
1407
  }
1342
1408
  return -1;
1343
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
+ }
1344
1427
  /**
1345
1428
  * true if a numeric token is in the range of the syntax: `<integer [1,∞]>`
1346
1429
  * @param arena
@@ -1370,6 +1453,43 @@ function isInRange(arena, token, syntax) {
1370
1453
  }
1371
1454
  return success;
1372
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
+ }
1373
1493
  /**
1374
1494
  * the wildcard functions being matched with a syntax, see {@link matchWildCardFunction}
1375
1495
  */
@@ -1494,7 +1614,7 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1494
1614
  let isOptional;
1495
1615
  if (context.tokens.length === 1 &&
1496
1616
  arena.dataTypes[context.tokens[0]] === EnumToken.IdenTokenType &&
1497
- isCssWideKeyword(arena.strings[arena.stringValue[context.tokens[0]]])) {
1617
+ isCssWideKeyword(arena.resolveString(arena.stringValue[context.tokens[0]]))) {
1498
1618
  context.end();
1499
1619
  return setMatch(out, true, true, context.peek(), context, null, noErrors);
1500
1620
  }
@@ -1524,9 +1644,9 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1524
1644
  break;
1525
1645
  }
1526
1646
  if (arena.dataTypes[token] === EnumToken.WildCardFunctionTokenType &&
1527
- arena.strings[arena.stringValue[token]] === "var" &&
1647
+ arena.resolveString(arena.stringValue[token]) === "var" &&
1528
1648
  wildCardProbes.get(token) !== syntaxes[i]) {
1529
- result = matchSyntax(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, arena.strings[arena.stringValue[token]] + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(token)), options, out);
1649
+ result = matchSyntax(arena, functionSyntax(arena, token)?.[0]?.chi, createValidationContext(arena, arena.tokens(token)), options, out);
1530
1650
  if (result.success && result.context.done()) {
1531
1651
  const wildCardResult = matchWildCardFunction(arena, syntaxes, i, context, token, token, options, out);
1532
1652
  if (wildCardResult == null) {
@@ -1540,7 +1660,7 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1540
1660
  arena.dataTypes[token] === EnumToken.WildCardFunctionTokenDefType &&
1541
1661
  wildCardProbes.get(token) !== syntaxes[i]) {
1542
1662
  const range = trimArray(arena, context.peekRange());
1543
- 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);
1544
1664
  if (result.success) {
1545
1665
  const wildCardResult = matchWildCardFunction(arena, syntaxes, i, context, token, range.at(-1), options, out);
1546
1666
  if (wildCardResult == null) {
@@ -1567,7 +1687,6 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1567
1687
  if (syntaxes[i].isList) {
1568
1688
  result = matchListSyntax(arena, syntaxes[i], context.slice(), options);
1569
1689
  if (result.success) {
1570
- options.visited.get(token)?.delete?.(syntaxes[i]);
1571
1690
  if (result.context.done()) {
1572
1691
  context.end();
1573
1692
  return setDoneMatch(out, syntaxes, i + 1, true, null, context, result.errors);
@@ -1583,7 +1702,6 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1583
1702
  if (syntaxes[i].match != null) {
1584
1703
  result = matchOccurenceSyntax(arena, syntaxes[i], context.slice(), options);
1585
1704
  if (result.success) {
1586
- options.visited.get(token).delete(syntaxes[i]);
1587
1705
  if (result.context.done()) {
1588
1706
  context.end();
1589
1707
  return setDoneMatch(out, syntaxes, i + 1, result.valid, result.token, context, result.errors);
@@ -1596,14 +1714,12 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1596
1714
  }
1597
1715
  return setMatch(out, false, true, token, context, syntaxes[i], result.errors);
1598
1716
  }
1599
- if (!options.visited.has(token)) {
1600
- options.visited.set(token, new Set());
1601
- }
1602
- if (options.visited.get(token).has(syntaxes[i])) {
1717
+ if (isVisited(token, syntaxes[i])) {
1603
1718
  // cyclic definition
1604
1719
  return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1605
1720
  }
1606
- 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]);
1607
1723
  success = false;
1608
1724
  try {
1609
1725
  switch (syntaxes[i].typ) {
@@ -1621,17 +1737,17 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1621
1737
  case ValidationTokenEnum.Comma:
1622
1738
  if (arena.dataTypes[token] === EnumToken.CommaTokenType) {
1623
1739
  success = true;
1624
- options.visited.get(token).delete(syntaxes[i]);
1740
+ visitedSyntaxes[visit] = null;
1625
1741
  context.next();
1626
1742
  break;
1627
1743
  }
1628
1744
  return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1629
1745
  case ValidationTokenEnum.Keyword:
1630
1746
  if (arena.dataTypes[token] === EnumToken.IdenTokenType &&
1631
- lowercase(arena.strings[arena.stringValue[token]]) ===
1747
+ lowercase(arena.resolveString(arena.stringValue[token])) ===
1632
1748
  lowercase(syntaxes[i].val)) {
1633
1749
  success = true;
1634
- options.visited.get(token).delete(syntaxes[i]);
1750
+ visitedSyntaxes[visit] = null;
1635
1751
  context.next();
1636
1752
  break;
1637
1753
  }
@@ -1643,7 +1759,7 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1643
1759
  result = matchSyntax(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, syntaxes[i].val), context, options, out);
1644
1760
  if (result.success) {
1645
1761
  success = true;
1646
- options.visited.get(token).delete(syntaxes[i]);
1762
+ visitedSyntaxes[visit] = null;
1647
1763
  if (result.context.done()) {
1648
1764
  context.end();
1649
1765
  return setDoneMatch(out, syntaxes, i + 1, result.valid, token, context, result.errors);
@@ -1702,7 +1818,7 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1702
1818
  }
1703
1819
  else if (arena.dataTypes[token] === EnumToken.LiteralTokenType) {
1704
1820
  success =
1705
- arena.strings[arena.stringValue[token]] ===
1821
+ arena.resolveString(arena.stringValue[token]) ===
1706
1822
  syntaxes[i].val.slice(1, -1);
1707
1823
  }
1708
1824
  else if (arena.dataTypes[token] === EnumToken.Plus) {
@@ -1725,7 +1841,7 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1725
1841
  return setMatch(out, false, true, context.current() ?? token, context, syntaxes[i], noErrors);
1726
1842
  }
1727
1843
  success = true;
1728
- options.visited.get(token).delete(syntaxes[i]);
1844
+ visitedSyntaxes[visit] = null;
1729
1845
  if (result.context.done()) {
1730
1846
  if (!isInRange(arena, token, syntaxes[i])) {
1731
1847
  return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
@@ -1741,9 +1857,15 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1741
1857
  {
1742
1858
  // each alternative is matched in the same scratch result, the best match is copied
1743
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;
1744
1865
  let best = null;
1745
1866
  let tmp = null;
1746
- for (const syntax of syntaxes[i].chi) {
1867
+ for (let k = 0; k < count; k++) {
1868
+ const syntax = alternatives[candidates === null ? k : candidates[k]];
1747
1869
  tmp = matchSyntax(arena, syntax, context.slice(), options, scratch);
1748
1870
  if (tmp.success) {
1749
1871
  // an alternative that consumes the tokens is skipped if mandatory syntaxes remain
@@ -1762,7 +1884,7 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1762
1884
  result = best;
1763
1885
  if (result?.success) {
1764
1886
  success = true;
1765
- options.visited.get(token).delete(syntaxes[i]);
1887
+ visitedSyntaxes[visit] = null;
1766
1888
  context.update(result.context.current());
1767
1889
  break;
1768
1890
  }
@@ -1781,7 +1903,7 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1781
1903
  result = matchSyntax(arena, syntaxes[i].chi, context.slice(), options, out);
1782
1904
  if (result.success) {
1783
1905
  success = true;
1784
- options.visited.get(token).delete(syntaxes[i]);
1906
+ visitedSyntaxes[visit] = null;
1785
1907
  if (result.context.done()) {
1786
1908
  context.end();
1787
1909
  return setDoneMatch(out, syntaxes, i + 1, true, null, context, noErrors);
@@ -1799,7 +1921,7 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1799
1921
  result = matchColumnSyntax(arena, syntaxes[i], context.slice(), options);
1800
1922
  if (result.success) {
1801
1923
  success = true;
1802
- options.visited.get(token).delete(syntaxes[i]);
1924
+ visitedSyntaxes[visit] = null;
1803
1925
  if (result.context.done()) {
1804
1926
  context.end();
1805
1927
  return setDoneMatch(out, syntaxes, i + 1, true, null, context, noErrors);
@@ -1814,7 +1936,7 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1814
1936
  result = matchAmpersandSyntax(arena, syntaxes[i], context.slice(), options);
1815
1937
  if (result.success) {
1816
1938
  success = true;
1817
- options.visited.get(token).delete(syntaxes[i]);
1939
+ visitedSyntaxes[visit] = null;
1818
1940
  if (result.context.done()) {
1819
1941
  context.end();
1820
1942
  return setDoneMatch(out, syntaxes, i + 1, true, null, context, noErrors);
@@ -1826,10 +1948,10 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1826
1948
  }
1827
1949
  return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1828
1950
  case ValidationTokenEnum.Function:
1829
- if ((!tokensfuncDefMap.has(arena.dataTypes[token]) &&
1951
+ if ((!isFunctionDefType(arena.dataTypes[token]) &&
1830
1952
  !funcLike.includes(arena.dataTypes[token]) &&
1831
1953
  !funcTypes.includes(arena.dataTypes[token])) ||
1832
- !equalsIgnoreCase(syntaxes[i].val, arena.strings[arena.stringValue[token]].toLowerCase())) {
1954
+ !equalsIgnoreCase(syntaxes[i].val, arena.resolveString(arena.stringValue[token]).toLowerCase())) {
1833
1955
  return setMatch(out, false, true, token, context, syntaxes[i], [
1834
1956
  {
1835
1957
  action: "drop",
@@ -1838,12 +1960,12 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1838
1960
  },
1839
1961
  ]);
1840
1962
  }
1841
- if (tokensfuncDefMap.has(arena.dataTypes[token])) {
1963
+ if (isFunctionDefType(arena.dataTypes[token])) {
1842
1964
  const range = context.peekRange();
1843
1965
  result = matchSyntax(arena, syntaxes[i].chi, createValidationContext(arena, range.slice(1, -1)), options, out);
1844
1966
  if (result.success && result.context.done()) {
1845
1967
  context.update(range.at(-1));
1846
- options.visited.get(token).delete(syntaxes[i]);
1968
+ visitedSyntaxes[visit] = null;
1847
1969
  break;
1848
1970
  }
1849
1971
  }
@@ -1851,38 +1973,52 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1851
1973
  result = matchSyntax(arena, syntaxes[i].chi, createValidationContext(arena, arena.tokens(token)), options, out);
1852
1974
  if (result.success && result.context.done()) {
1853
1975
  success = true;
1854
- options.visited.get(token).delete(syntaxes[i]);
1976
+ visitedSyntaxes[visit] = null;
1855
1977
  context.next();
1856
1978
  break;
1857
1979
  }
1858
1980
  }
1859
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);
1860
1996
  case ValidationTokenEnum.Separator:
1861
1997
  if (arena.dataTypes[token] === EnumToken.LiteralTokenType &&
1862
- arena.strings[arena.stringValue[token]] === "/") {
1863
- options.visited.get(token).delete(syntaxes[i]);
1998
+ arena.resolveString(arena.stringValue[token]) === "/") {
1999
+ visitedSyntaxes[visit] = null;
1864
2000
  context.next();
1865
2001
  break;
1866
2002
  }
1867
2003
  return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1868
2004
  case ValidationTokenEnum.FunctionDefinition:
1869
- if (arena.strings[arena.stringValue[token]] &&
1870
- equalsIgnoreCase(arena.strings[arena.stringValue[token]], syntaxes[i].val)) {
1871
- if (tokensfuncDefMap.has(arena.dataTypes[token])) {
2005
+ if (arena.resolveString(arena.stringValue[token]) &&
2006
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[token]), syntaxes[i].val)) {
2007
+ if (isFunctionDefType(arena.dataTypes[token])) {
1872
2008
  const children = trimArray(arena, context.peekRange());
1873
2009
  result = matchSyntax(arena, (getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, syntaxes[i].val + "()")?.[0]).chi ?? [], createValidationContext(arena, children.slice(1, -1)), options, out);
1874
2010
  if (result.success && result.context.done()) {
1875
2011
  success = true;
1876
- options.visited.get(token).delete(syntaxes[i]);
2012
+ visitedSyntaxes[visit] = null;
1877
2013
  context.update(children.at(-1));
1878
2014
  break;
1879
2015
  }
1880
2016
  }
1881
- else if (tokensfuncSet.has(arena.dataTypes[token])) {
2017
+ else if (isFunctionType(arena.dataTypes[token])) {
1882
2018
  result = matchSyntax(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, syntaxes[i].val + "()") ?? [], createValidationContext(arena, [token]), options, out);
1883
2019
  if (result.success && result.context.done()) {
1884
2020
  success = true;
1885
- options.visited.get(token).delete(syntaxes[i]);
2021
+ visitedSyntaxes[visit] = null;
1886
2022
  context.next();
1887
2023
  break;
1888
2024
  }
@@ -1891,21 +2027,21 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1891
2027
  return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1892
2028
  case ValidationTokenEnum.OpenParenthesis:
1893
2029
  if (arena.dataTypes[token] === EnumToken.StartParensTokenType) {
1894
- options.visited.get(token).delete(syntaxes[i]);
2030
+ visitedSyntaxes[visit] = null;
1895
2031
  context.next();
1896
2032
  break;
1897
2033
  }
1898
2034
  return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1899
2035
  case ValidationTokenEnum.OpenCurlyBrace:
1900
2036
  if (arena.dataTypes[token] === EnumToken.BlockStartTokenType) {
1901
- options.visited.get(token).delete(syntaxes[i]);
2037
+ visitedSyntaxes[visit] = null;
1902
2038
  context.next();
1903
2039
  break;
1904
2040
  }
1905
2041
  return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
1906
2042
  case ValidationTokenEnum.CloseParenthesis:
1907
2043
  if (arena.dataTypes[token] === EnumToken.EndParensTokenType) {
1908
- options.visited.get(token).delete(syntaxes[i]);
2044
+ visitedSyntaxes[visit] = null;
1909
2045
  context.next();
1910
2046
  break;
1911
2047
  }
@@ -1917,7 +2053,8 @@ function matchSyntax(arena, syntaxes, context, options, out = createMatch()) {
1917
2053
  finally {
1918
2054
  // the syntax is no longer being matched: a token may be matched again with the same syntax (the
1919
2055
  // alternatives of an optional group, ...). only the syntaxes being matched detect a cyclic definition
1920
- options.visited.get(token)?.delete(syntaxes[i]);
2056
+ visitedSyntaxes[visit] = null;
2057
+ visitedLength = visit;
1921
2058
  }
1922
2059
  if (success && !isInRange(arena, token, syntaxes[i])) {
1923
2060
  return setMatch(out, false, true, token, context, syntaxes[i], noErrors);
@@ -1995,10 +2132,15 @@ function searchUnorderedSyntaxes(arena, syntaxes, remaining, all, base, context,
1995
2132
  const key = remaining[k] + ":" + position;
1996
2133
  let next = cache.get(key);
1997
2134
  if (next === undefined) {
1998
- const result = matchSyntax(arena, syntaxes[remaining[k]], context.slice(), options);
1999
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;
2000
2140
  // the syntax matched some tokens
2001
- if (result.success && (result.context.done() || result.context.current() !== context.current())) {
2141
+ if (result != null &&
2142
+ result.success &&
2143
+ (result.context.done() || result.context.current() !== context.current())) {
2002
2144
  next = context.slice();
2003
2145
  if (result.context.done()) {
2004
2146
  next.end();
@@ -2082,7 +2224,7 @@ function peekFeatureTokens(arena, context) {
2082
2224
  let depth = 0;
2083
2225
  for (let index = context.index + 1; index < context.tokens.length; index++) {
2084
2226
  const token = context.tokens[index];
2085
- if (arena.dataTypes[token] === EnumToken.StartParensTokenType || tokensfuncDefMap.has(arena.dataTypes[token])) {
2227
+ if (arena.dataTypes[token] === EnumToken.StartParensTokenType || isFunctionDefType(arena.dataTypes[token])) {
2086
2228
  depth++;
2087
2229
  }
2088
2230
  else if (arena.dataTypes[token] === EnumToken.EndParensTokenType && depth-- === 0) {
@@ -2122,11 +2264,11 @@ function matchProperty(arena, property, context, options) {
2122
2264
  "calc-product",
2123
2265
  ].includes(property.val);
2124
2266
  if (checkCalc && !["number", "zero", "integer", "percentage", "length-percentage"].includes(property.val)) {
2125
- checkCalc = arena.strings[arena.stringValue[token]] === "calc";
2267
+ checkCalc = arena.resolveString(arena.stringValue[token]) === "calc";
2126
2268
  }
2127
2269
  if (checkCalc) {
2128
2270
  let result;
2129
- 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;
2130
2272
  if (t === EnumToken.MathFunctionTokenType) {
2131
2273
  result = matchSyntax(arena, syntax, createValidationContext(arena, arena.tokens(token)), options);
2132
2274
  if (result.success && result.context.done()) {
@@ -2183,7 +2325,7 @@ function matchProperty(arena, property, context, options) {
2183
2325
  success = false;
2184
2326
  while ((token = context.peek()) != null) {
2185
2327
  if (arena.dataTypes[token] === EnumToken.StartParensTokenType ||
2186
- tokensfuncDefMap.has(arena.dataTypes[token])) {
2328
+ isFunctionDefType(arena.dataTypes[token])) {
2187
2329
  stack.push(token);
2188
2330
  }
2189
2331
  if (expectToken) {
@@ -2255,8 +2397,8 @@ function matchProperty(arena, property, context, options) {
2255
2397
  if (
2256
2398
  // inlined: arena.typeOf(token)
2257
2399
  arena.dataTypes[token] === EnumToken.IdenTokenType &&
2258
- ("even" === arena.strings[arena.stringValue[token]] ||
2259
- "odd" === arena.strings[arena.stringValue[token]])) {
2400
+ ("even" === arena.resolveString(arena.stringValue[token]) ||
2401
+ "odd" === arena.resolveString(arena.stringValue[token]))) {
2260
2402
  success = true;
2261
2403
  break;
2262
2404
  }
@@ -2270,7 +2412,7 @@ function matchProperty(arena, property, context, options) {
2270
2412
  // inlined: arena.typeOf(token)
2271
2413
  if (arena.dataTypes[token] === EnumToken.LiteralTokenType ||
2272
2414
  arena.dataTypes[token] === EnumToken.IdenTokenType) {
2273
- let match = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(arena.strings[arena.stringValue[token]]);
2415
+ let match = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(arena.resolveString(arena.stringValue[token]));
2274
2416
  success = match != null;
2275
2417
  // `n + 1`: the sign and the number follow the ident
2276
2418
  if (success && match?.[3] == null) {
@@ -2321,7 +2463,7 @@ function matchProperty(arena, property, context, options) {
2321
2463
  if (
2322
2464
  // inlined: arena.typeOf(token)
2323
2465
  arena.dataTypes[token] === EnumToken.DimensionTokenType &&
2324
- arena.strings[arena.unit[token]] === "n") {
2466
+ arena.resolveString(arena.unit[token]) === "n") {
2325
2467
  success = Number.isInteger(arena.numericValue[token]);
2326
2468
  token = context.peek(1);
2327
2469
  if (token == null) {
@@ -2368,7 +2510,7 @@ function matchProperty(arena, property, context, options) {
2368
2510
  // inlined: arena.typeOf(token)
2369
2511
  success =
2370
2512
  arena.dataTypes[token] === EnumToken.IdenTokenType &&
2371
- values.has(arena.strings[arena.stringValue[token]].toLowerCase());
2513
+ values.has(arena.resolveString(arena.stringValue[token]).toLowerCase());
2372
2514
  break;
2373
2515
  }
2374
2516
  case "mf-name":
@@ -2377,7 +2519,7 @@ function matchProperty(arena, property, context, options) {
2377
2519
  success =
2378
2520
  // inlined: arena.typeOf(token)
2379
2521
  arena.dataTypes[token] === EnumToken.IdenTokenType &&
2380
- arena.strings[arena.stringValue[token]].toLowerCase() in config.mediaFeatures;
2522
+ arena.resolveString(arena.stringValue[token]).toLowerCase() in config.mediaFeatures;
2381
2523
  }
2382
2524
  break;
2383
2525
  case "dashed-ident":
@@ -2398,7 +2540,7 @@ function matchProperty(arena, property, context, options) {
2398
2540
  arena.dataTypes[token] === EnumToken.DashedIdenTokenType;
2399
2541
  // inlined: arena.typeOf(token)
2400
2542
  if (success && arena.dataTypes[token] === EnumToken.IdenTokenType) {
2401
- const val = arena.strings[arena.stringValue[token]].toLowerCase();
2543
+ const val = arena.resolveString(arena.stringValue[token]).toLowerCase();
2402
2544
  success = "none" !== val && !allValues.includes(val);
2403
2545
  }
2404
2546
  break;
@@ -2576,7 +2718,7 @@ function matchProperty(arena, property, context, options) {
2576
2718
  const token = context.peek();
2577
2719
  success =
2578
2720
  arena.dataTypes[token] === EnumToken.DimensionTokenType &&
2579
- equalsIgnoreCase(arena.strings[arena.unit[token]], property.val === "decibel" ? "dB" : "st");
2721
+ equalsIgnoreCase(arena.resolveString(arena.unit[token]), property.val === "decibel" ? "dB" : "st");
2580
2722
  break;
2581
2723
  }
2582
2724
  case "time":
@@ -2596,7 +2738,7 @@ function matchProperty(arena, property, context, options) {
2596
2738
  const token = context.peek();
2597
2739
  const errors = [];
2598
2740
  // inlined: arena.typeOf(token)
2599
- if (tokensfuncDefMap.has((token == null ? null : arena.dataTypes[token]))) {
2741
+ if (isFunctionDefType((token == null ? null : arena.dataTypes[token]))) {
2600
2742
  const range = context.peekRange();
2601
2743
  const newRange = range.map((t) => arena.clone(t, true));
2602
2744
  parseTokens(arena, newRange, { parseColor: true }, errors);
@@ -2659,7 +2801,7 @@ function matchProperty(arena, property, context, options) {
2659
2801
  }
2660
2802
  case "function-token":
2661
2803
  // inlined: arena.typeOf(context.peek()). the type of a null id is undefined: not a key of the map either
2662
- if (tokensfuncDefMap.has(arena.dataTypes[context.peek()])) {
2804
+ if (isFunctionDefType(arena.dataTypes[context.peek()])) {
2663
2805
  // the token is consumed below
2664
2806
  success = true;
2665
2807
  break;
@@ -2678,8 +2820,8 @@ function matchProperty(arena, property, context, options) {
2678
2820
  // inlined: arena.typeOf(token)
2679
2821
  arena.dataTypes[token] === EnumToken.IdenTokenType &&
2680
2822
  // @ts-expect-error
2681
- config.languages.some((t) => t === arena.strings[arena.stringValue[token]] ||
2682
- 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 + "-"));
2683
2825
  break;
2684
2826
  }
2685
2827
  case "size-feature": {
@@ -2803,7 +2945,7 @@ function matchProperty(arena, property, context, options) {
2803
2945
  // `type(<number> | <transform-function>)`: the css type syntax
2804
2946
  if ((arena.dataTypes[token] === EnumToken.FunctionTokenDefType ||
2805
2947
  arena.dataTypes[token] === EnumToken.FunctionTokenType) &&
2806
- equalsIgnoreCase(arena.strings[arena.stringValue[token]], "type")) {
2948
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[token]), "type")) {
2807
2949
  if (arena.dataTypes[token] === EnumToken.FunctionTokenDefType) {
2808
2950
  context.update(context.peekRange().at(-1));
2809
2951
  }
@@ -2828,7 +2970,7 @@ function matchProperty(arena, property, context, options) {
2828
2970
  if (multiplier != null &&
2829
2971
  (arena.dataTypes[multiplier] === EnumToken.Plus ||
2830
2972
  (arena.dataTypes[multiplier] === EnumToken.LiteralTokenType &&
2831
- arena.strings[arena.stringValue[multiplier]] === "#"))) {
2973
+ arena.resolveString(arena.stringValue[multiplier]) === "#"))) {
2832
2974
  context.next();
2833
2975
  }
2834
2976
  return {
@@ -2899,7 +3041,7 @@ function matchProperty(arena, property, context, options) {
2899
3041
  if (!success &&
2900
3042
  // inlined: arena.typeOf(context.peek())
2901
3043
  arena.dataTypes[context.peek()] === EnumToken.IdenTokenType &&
2902
- allValues.includes(arena.strings[arena.stringValue[context.peek()]])) {
3044
+ allValues.includes(arena.resolveString(arena.stringValue[context.peek()]))) {
2903
3045
  success = true;
2904
3046
  }
2905
3047
  if (success) {