@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
@@ -0,0 +1,571 @@
1
+ /*
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.2.0
6
+ License: MIT
7
+ */
8
+ import { EnumToken } from '../ast/types.js';
9
+ import { getSyntaxConfig, getParsedSyntax } from './config.js';
10
+ import { ValidationTokenEnum, ValidationSyntaxGroupEnum } from './parser/typedef.js';
11
+ import { tokensfuncDefMap, isFunctionDefType, isFunctionType, funcLike } from '../syntax/constants.js';
12
+ import { equalsIgnoreCase, lowercase } from '../parser/utils/text.js';
13
+
14
+ /**
15
+ * the syntaxes that may match no token, see {@link isNullable}
16
+ */
17
+ const nullableSyntaxes = new WeakMap();
18
+ /**
19
+ * true if the syntax may match no token: `<a>?`, `<a>*`, `[ <a>? <b>? ]`, `[ [ and <a> ]* | [ or <a> ]* ]`
20
+ * @param syntax
21
+ */
22
+ function isNullable(syntax) {
23
+ if (syntax.typ === ValidationTokenEnum.Whitespace) {
24
+ return true;
25
+ }
26
+ // `[ <a>? <b>? ]!`: at least one token
27
+ if (syntax.isMandatatoryGroup) {
28
+ return false;
29
+ }
30
+ if (syntax.isOptional || syntax.isRepeatable || syntax.match?.min?.val === 0) {
31
+ return true;
32
+ }
33
+ let nullable = nullableSyntaxes.get(syntax);
34
+ if (nullable == null) {
35
+ switch (syntax.typ) {
36
+ case ValidationTokenEnum.Bracket:
37
+ nullable = syntax.chi.every(isNullable);
38
+ break;
39
+ case ValidationTokenEnum.PipeToken:
40
+ nullable = syntax.chi.some((syntaxes) => syntaxes.every(isNullable));
41
+ break;
42
+ case ValidationTokenEnum.AmpersandToken:
43
+ nullable =
44
+ syntax.l.every(isNullable) &&
45
+ syntax.r.every(isNullable);
46
+ break;
47
+ default:
48
+ nullable = false;
49
+ }
50
+ nullableSyntaxes.set(syntax, nullable);
51
+ }
52
+ return nullable;
53
+ }
54
+ /**
55
+ * the types of the function tokens (`name(`, `name(...)`): the types whose name is a key of the sets
56
+ */
57
+ const functionTokenTypes = new Set();
58
+ /**
59
+ * the types of the function definition tokens (`name(` followed by the arguments and `)`)
60
+ */
61
+ const functionDefTypes = [];
62
+ /**
63
+ * the function types accepted by the `Function` syntaxes of the matcher (see `funcTypes` in match.ts)
64
+ */
65
+ const funcTypes = [
66
+ ...tokensfuncDefMap.values(),
67
+ EnumToken.FunctionTokenType,
68
+ EnumToken.PseudoClassFuncTokenType,
69
+ ];
70
+ for (const value of Object.values(EnumToken)) {
71
+ if (typeof value !== "number") {
72
+ continue;
73
+ }
74
+ if (isFunctionDefType(value)) {
75
+ functionDefTypes.push(value);
76
+ }
77
+ if (isFunctionDefType(value) || isFunctionType(value) || funcLike.includes(value) || funcTypes.includes(value)) {
78
+ functionTokenTypes.add(value);
79
+ }
80
+ }
81
+ /**
82
+ * the types of the tokens accepted by the built-in types of the matcher (`matchProperty()`). the built-in types that
83
+ * are not listed accept any token (complex checks: `<declaration-value>`, `<an+b>`, the selectors, the media features,
84
+ * ...).
85
+ * the css wide keywords, which are accepted by the built-in types, are not looked up
86
+ */
87
+ const builtinTypes = new Map([
88
+ ["dashed-ident", [EnumToken.DashedIdenTokenType]],
89
+ ["extension-name", [EnumToken.DashedIdenTokenType]],
90
+ ["custom-property-name", [EnumToken.DashedIdenTokenType]],
91
+ ["namespace-prefix", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
92
+ ["ident", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
93
+ ["ident-token", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
94
+ ["custom-ident", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
95
+ ["counter-name", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
96
+ ["counter-style-name", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
97
+ ["unicode-range-token", [EnumToken.UniCodeRange]],
98
+ ["hash-token", [EnumToken.HashTokenType, EnumToken.ColorTokenType]],
99
+ ["string", [EnumToken.StringTokenType]],
100
+ ["wq-name", [EnumToken.IdenTokenType]],
101
+ ["number", [EnumToken.NumberTokenType]],
102
+ ["zero", [EnumToken.NumberTokenType]],
103
+ ["integer", [EnumToken.NumberTokenType]],
104
+ [
105
+ "dimension",
106
+ [
107
+ EnumToken.DimensionTokenType,
108
+ EnumToken.AngleTokenType,
109
+ EnumToken.LengthTokenType,
110
+ EnumToken.PercentageTokenType,
111
+ EnumToken.ResolutionTokenType,
112
+ EnumToken.TimeTokenType,
113
+ EnumToken.FrequencyTokenType,
114
+ EnumToken.UserDefinedDimensionTokenType,
115
+ EnumToken.FlexTokenType,
116
+ ],
117
+ ],
118
+ ["angle", [EnumToken.AngleTokenType, EnumToken.UserDefinedDimensionTokenType]],
119
+ [
120
+ "angle-percentage",
121
+ [EnumToken.AngleTokenType, EnumToken.UserDefinedDimensionTokenType, EnumToken.PercentageTokenType],
122
+ ],
123
+ ["percentage", [EnumToken.PercentageTokenType]],
124
+ [
125
+ "length-percentage",
126
+ [
127
+ EnumToken.PercentageTokenType,
128
+ EnumToken.LengthTokenType,
129
+ EnumToken.UserDefinedDimensionTokenType,
130
+ EnumToken.NumberTokenType,
131
+ ],
132
+ ],
133
+ ["flex", [EnumToken.FlexTokenType, EnumToken.UserDefinedDimensionTokenType]],
134
+ ["length", [EnumToken.LengthTokenType, EnumToken.UserDefinedDimensionTokenType, EnumToken.NumberTokenType]],
135
+ ["resolution", [EnumToken.ResolutionTokenType]],
136
+ ["frequency", [EnumToken.FrequencyTokenType]],
137
+ ["decibel", [EnumToken.DimensionTokenType]],
138
+ ["semitones", [EnumToken.DimensionTokenType]],
139
+ ["time", [EnumToken.TimeTokenType, EnumToken.UserDefinedDimensionTokenType]],
140
+ ["quote", [EnumToken.IdenTokenType]],
141
+ ["display-box", [EnumToken.IdenTokenType]],
142
+ ["display-inside", [EnumToken.IdenTokenType]],
143
+ ["display-internal", [EnumToken.IdenTokenType]],
144
+ ["display-outside", [EnumToken.IdenTokenType]],
145
+ ["display-legacy", [EnumToken.IdenTokenType]],
146
+ ["content-position", [EnumToken.IdenTokenType]],
147
+ ["mf-name", [EnumToken.IdenTokenType]],
148
+ ["language-code", [EnumToken.IdenTokenType]],
149
+ ["url", [EnumToken.UrlFunctionTokenDefType, EnumToken.UrlFunctionTokenType]],
150
+ // `isColor()`: the color tokens, the color names and the color functions
151
+ ["color", [EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.FunctionTokenType, ...functionDefTypes]],
152
+ [
153
+ "color-function",
154
+ [EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.FunctionTokenType, ...functionDefTypes],
155
+ ],
156
+ ["function-token", functionDefTypes],
157
+ ["dashed-function", [EnumToken.CustomFunctionTokenDefType, EnumToken.CustomFunctionTokenType]],
158
+ ["block-contents", [EnumToken.StartParensTokenType]],
159
+ ["css-type", [EnumToken.FunctionTokenDefType, EnumToken.FunctionTokenType, EnumToken.CssTypeTokenType]],
160
+ // any token
161
+ ["declaration-value", null],
162
+ ["calc-sum", null],
163
+ ["an+b", null],
164
+ ["any-value", null],
165
+ ["type-selector", null],
166
+ ["compound-selector", null],
167
+ ["complex-selector", null],
168
+ ["subclass-selector", null],
169
+ ["attribute-selector", null],
170
+ ["complex-selector-unit", null],
171
+ ["pseudo-compound-selector", null],
172
+ ["forgiving-relative-selector-list", null],
173
+ ["complex-selector-list", null],
174
+ ["size-feature", null],
175
+ ["style-feature", null],
176
+ ]);
177
+ /**
178
+ * a set that accepts any token
179
+ */
180
+ const anyToken = Object.freeze({
181
+ any: true,
182
+ nullable: true,
183
+ types: new Set(),
184
+ keywords: new Set(),
185
+ functions: new Set(),
186
+ });
187
+ /**
188
+ * first token sets of the syntax tokens
189
+ */
190
+ const tokenSets = new WeakMap();
191
+ /**
192
+ * first token sets of the syntax sequences
193
+ */
194
+ const sequenceSets = new WeakMap();
195
+ /**
196
+ * the sequences whose set is being computed: a cyclic reference accepts any token
197
+ */
198
+ const computing = new Set();
199
+ /**
200
+ * an empty set
201
+ */
202
+ function createSet() {
203
+ return {
204
+ any: false,
205
+ nullable: false,
206
+ types: new Set(),
207
+ keywords: new Set(),
208
+ functions: new Set(),
209
+ };
210
+ }
211
+ /**
212
+ * add the tokens of a set to another set
213
+ * @param target
214
+ * @param source
215
+ */
216
+ function merge(target, source) {
217
+ target.any ||= source.any;
218
+ for (const type of source.types) {
219
+ target.types.add(type);
220
+ }
221
+ for (const keyword of source.keywords) {
222
+ target.keywords.add(keyword);
223
+ }
224
+ for (const name of source.functions) {
225
+ target.functions.add(name);
226
+ }
227
+ }
228
+ /**
229
+ * first tokens of a sequence of syntaxes: the first tokens of its syntaxes until a syntax that must match a token
230
+ * @param syntaxes
231
+ */
232
+ function firstOfSequence(syntaxes) {
233
+ if (syntaxes == null) {
234
+ // no syntax: the matcher accepts the value
235
+ return anyToken;
236
+ }
237
+ let set = sequenceSets.get(syntaxes);
238
+ if (set !== undefined) {
239
+ return set;
240
+ }
241
+ if (computing.has(syntaxes)) {
242
+ return anyToken;
243
+ }
244
+ computing.add(syntaxes);
245
+ try {
246
+ set = createSet();
247
+ set.nullable = true;
248
+ for (const syntax of syntaxes) {
249
+ const first = firstOf(syntax);
250
+ merge(set, first);
251
+ if (set.any || !first.nullable) {
252
+ set.nullable = false;
253
+ break;
254
+ }
255
+ }
256
+ if (set.any) {
257
+ set = anyToken;
258
+ }
259
+ }
260
+ finally {
261
+ computing.delete(syntaxes);
262
+ }
263
+ sequenceSets.set(syntaxes, set);
264
+ return set;
265
+ }
266
+ /**
267
+ * first tokens of a syntax token
268
+ * @param syntax
269
+ */
270
+ function firstOf(syntax) {
271
+ let set = tokenSets.get(syntax);
272
+ if (set !== undefined) {
273
+ return set;
274
+ }
275
+ set = createSet();
276
+ // the syntax may match no token through its content: an empty alternative (`| -moz-center`), a reference to a
277
+ // syntax that may match no token, ...
278
+ let nullable = false;
279
+ let first;
280
+ switch (syntax.typ) {
281
+ case ValidationTokenEnum.Whitespace:
282
+ nullable = true;
283
+ break;
284
+ case ValidationTokenEnum.Keyword:
285
+ set.keywords.add(lowercase(syntax.val));
286
+ break;
287
+ case ValidationTokenEnum.Comma:
288
+ set.types.add(EnumToken.CommaTokenType);
289
+ break;
290
+ case ValidationTokenEnum.Colon:
291
+ set.types.add(EnumToken.ColonTokenType);
292
+ break;
293
+ case ValidationTokenEnum.Separator:
294
+ set.types.add(EnumToken.LiteralTokenType);
295
+ break;
296
+ case ValidationTokenEnum.OpenParenthesis:
297
+ set.types.add(EnumToken.StartParensTokenType);
298
+ break;
299
+ case ValidationTokenEnum.CloseParenthesis:
300
+ set.types.add(EnumToken.EndParensTokenType);
301
+ break;
302
+ case ValidationTokenEnum.OpenCurlyBrace:
303
+ set.types.add(EnumToken.BlockStartTokenType);
304
+ break;
305
+ case ValidationTokenEnum.Number:
306
+ set.types.add(EnumToken.NumberTokenType);
307
+ break;
308
+ case ValidationTokenEnum.Dimension:
309
+ set.types.add(EnumToken[syntax.unit]);
310
+ break;
311
+ case ValidationTokenEnum.StringToken:
312
+ // `'+'`, `'-'`, `'/'`
313
+ set.types.add(EnumToken.Add);
314
+ set.types.add(EnumToken.Sub);
315
+ set.types.add(EnumToken.LiteralTokenType);
316
+ set.types.add(EnumToken.Plus);
317
+ break;
318
+ case ValidationTokenEnum.Function:
319
+ case ValidationTokenEnum.FunctionDefinition:
320
+ set.functions.add(lowercase(syntax.val));
321
+ break;
322
+ case ValidationTokenEnum.Bracket:
323
+ first = firstOfSequence(syntax.chi);
324
+ merge(set, first);
325
+ nullable = first.nullable;
326
+ break;
327
+ case ValidationTokenEnum.OptionalGroupToken:
328
+ // the group is skipped when it does not match
329
+ merge(set, firstOfSequence(syntax.chi));
330
+ nullable = true;
331
+ break;
332
+ case ValidationTokenEnum.PipeToken:
333
+ for (const syntaxes of syntax.chi) {
334
+ first = firstOfSequence(syntaxes);
335
+ merge(set, first);
336
+ nullable ||= first.nullable;
337
+ }
338
+ break;
339
+ case ValidationTokenEnum.ColumnToken:
340
+ // one or more operands, in any order: at least one token
341
+ for (const syntaxes of syntax.chi) {
342
+ merge(set, firstOfSequence(syntaxes));
343
+ }
344
+ break;
345
+ case ValidationTokenEnum.AmpersandToken:
346
+ // all the operands, in any order
347
+ nullable = true;
348
+ for (const syntaxes of [syntax.l, syntax.r]) {
349
+ first = firstOfSequence(syntaxes);
350
+ merge(set, first);
351
+ nullable &&= first.nullable;
352
+ }
353
+ break;
354
+ case ValidationTokenEnum.DeclarationType:
355
+ first = firstOfSequence(getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, syntax.val));
356
+ merge(set, first);
357
+ nullable = first.nullable;
358
+ break;
359
+ case ValidationTokenEnum.PropertyType: {
360
+ const name = syntax.val;
361
+ if (builtinTypes.has(name)) {
362
+ const types = builtinTypes.get(name);
363
+ if (types == null) {
364
+ set.any = true;
365
+ nullable = true;
366
+ }
367
+ else {
368
+ for (const type of types) {
369
+ set.types.add(type);
370
+ }
371
+ }
372
+ }
373
+ else {
374
+ first = firstOfSequence(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, name));
375
+ merge(set, first);
376
+ nullable = first.nullable;
377
+ }
378
+ break;
379
+ }
380
+ default:
381
+ set.any = true;
382
+ nullable = true;
383
+ }
384
+ // the syntaxes that may match no token: `<a>?`, `<a>*`, `<a>{0,2}`, `[ <a>? ]`, ...
385
+ set.nullable =
386
+ nullable ||
387
+ syntax.isOptional === true ||
388
+ syntax.isRepeatable === true ||
389
+ syntax.match?.min?.val === 0 ||
390
+ isNullable(syntax);
391
+ tokenSets.set(syntax, set);
392
+ return set;
393
+ }
394
+ /**
395
+ * the css wide keywords, accepted by every syntax
396
+ */
397
+ const cssWideKeywords = getSyntaxConfig().declarations.all.syntax.split(/[\s|]+/g);
398
+ /**
399
+ * the token may match a syntax that does not accept its type (`var()`, `env()`, `attr()`, the math functions, the css
400
+ * wide keywords, ...): the first token sets do not apply
401
+ * @param arena
402
+ * @param token
403
+ * @param typ
404
+ */
405
+ function matchesAnySyntax(arena, token, typ) {
406
+ switch (typ) {
407
+ case EnumToken.WildCardFunctionTokenType:
408
+ case EnumToken.WildCardFunctionTokenDefType:
409
+ case EnumToken.MathFunctionTokenType:
410
+ case EnumToken.MathFunctionTokenDefType:
411
+ return true;
412
+ case EnumToken.IdenTokenType: {
413
+ const value = arena.resolveString(arena.stringValue[token]);
414
+ for (let i = 0; i < cssWideKeywords.length; i++) {
415
+ if (equalsIgnoreCase(cssWideKeywords[i], value)) {
416
+ return true;
417
+ }
418
+ }
419
+ }
420
+ }
421
+ return false;
422
+ }
423
+ /**
424
+ * true if a set may start with a token
425
+ * @param set
426
+ * @param typ token type
427
+ * @param name lowercase name of an identifier or of a function, null for the other tokens
428
+ */
429
+ function accepts(set, typ, name) {
430
+ if (set.any || set.types.has(typ)) {
431
+ return true;
432
+ }
433
+ if (name === null) {
434
+ return false;
435
+ }
436
+ return typ === EnumToken.IdenTokenType ? set.keywords.has(name) : set.functions.has(name);
437
+ }
438
+ /**
439
+ * lowercase name of an identifier or of a function token, null for the other tokens
440
+ * @param arena
441
+ * @param token
442
+ * @param typ
443
+ */
444
+ function tokenName(arena, token, typ) {
445
+ return typ === EnumToken.IdenTokenType || functionTokenTypes.has(typ)
446
+ ? arena.resolveString(arena.lowerStringId(arena.stringValue[token]))
447
+ : null;
448
+ }
449
+ /**
450
+ * maximum number of named lookups memoized by an index
451
+ */
452
+ const MAX_NAMES = 512;
453
+ /**
454
+ * the alternatives of a syntax indexed by their first tokens
455
+ */
456
+ class AlternativeIndex {
457
+ /**
458
+ * first tokens of each alternative
459
+ */
460
+ sets;
461
+ /**
462
+ * the alternatives cannot be filtered: all of them accept any token
463
+ */
464
+ filtered;
465
+ /**
466
+ * the alternatives that may start with a token, by token type (the tokens without a name)
467
+ */
468
+ byType = new Map();
469
+ /**
470
+ * the alternatives that may start with an identifier or a function, by token type and by name
471
+ */
472
+ byName = new Map();
473
+ /**
474
+ * number of memoized named lookups
475
+ */
476
+ names = 0;
477
+ constructor(alternatives) {
478
+ this.sets = alternatives.map((syntaxes) => firstOfSequence(syntaxes));
479
+ this.filtered = this.sets.some((set) => !set.any && !set.nullable);
480
+ }
481
+ /**
482
+ * the indexes of the alternatives that may match a token, in order. null when all the alternatives may match
483
+ * @param typ
484
+ * @param name
485
+ */
486
+ lookup(typ, name) {
487
+ if (name === null) {
488
+ let result = this.byType.get(typ);
489
+ if (result === undefined) {
490
+ result = this.compute(typ, null);
491
+ this.byType.set(typ, result);
492
+ }
493
+ return result;
494
+ }
495
+ let names = this.byName.get(typ);
496
+ if (names === undefined) {
497
+ names = new Map();
498
+ this.byName.set(typ, names);
499
+ }
500
+ let result = names.get(name);
501
+ if (result === undefined) {
502
+ result = this.compute(typ, name);
503
+ if (this.names < MAX_NAMES) {
504
+ this.names++;
505
+ names.set(name, result);
506
+ }
507
+ }
508
+ return result;
509
+ }
510
+ /**
511
+ * the alternatives that may match a token. null when all the alternatives may match
512
+ * @param typ
513
+ * @param name
514
+ */
515
+ compute(typ, name) {
516
+ const result = [];
517
+ for (let i = 0; i < this.sets.length; i++) {
518
+ if (this.sets[i].nullable || accepts(this.sets[i], typ, name)) {
519
+ result.push(i);
520
+ }
521
+ }
522
+ return result.length === this.sets.length ? null : result;
523
+ }
524
+ }
525
+ /**
526
+ * the indexes of the alternatives, by list of alternatives (`ValidationPipeToken.chi`: the lists are shared by the
527
+ * copies of the syntax tokens)
528
+ */
529
+ const alternativeIndexes = new WeakMap();
530
+ /**
531
+ * the alternatives of a syntax (`<a> | <b> | <c>`) that may match the first token of a value, in order. the other
532
+ * alternatives cannot match the value. null when all the alternatives may match
533
+ * @param arena
534
+ * @param alternatives
535
+ * @param token the first token of the value
536
+ */
537
+ function matchingAlternatives(arena, alternatives, token) {
538
+ let index = alternativeIndexes.get(alternatives);
539
+ if (index === undefined) {
540
+ index = new AlternativeIndex(alternatives);
541
+ alternativeIndexes.set(alternatives, index);
542
+ }
543
+ if (!index.filtered || token == null) {
544
+ return null;
545
+ }
546
+ const typ = arena.dataTypes[token];
547
+ if (matchesAnySyntax(arena, token, typ)) {
548
+ return null;
549
+ }
550
+ return index.lookup(typ, tokenName(arena, token, typ));
551
+ }
552
+ /**
553
+ * true if a sequence of syntaxes may match tokens starting with a token. a sequence that does not accept the token
554
+ * matches no token
555
+ * @param arena
556
+ * @param syntaxes
557
+ * @param token
558
+ */
559
+ function mayStartWith(arena, syntaxes, token) {
560
+ if (token == null) {
561
+ return true;
562
+ }
563
+ const set = firstOfSequence(syntaxes);
564
+ if (set.any) {
565
+ return true;
566
+ }
567
+ const typ = arena.dataTypes[token];
568
+ return matchesAnySyntax(arena, token, typ) || accepts(set, typ, tokenName(arena, token, typ));
569
+ }
570
+
571
+ export { isNullable, matchingAlternatives, mayStartWith };