@tbela99/css-parser 1.4.2 → 1.4.4

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 (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -0,0 +1,3120 @@
1
+ import { getParsedSyntax, getSyntaxConfig } from './config.js';
2
+ import { EnumToken, ColorType } from '../ast/types.js';
3
+ import { ValidationSyntaxGroupEnum, ValidationTokenEnum, MediaFeatureType } from './parser/typedef.js';
4
+ import { tokensfuncDefMap, tokensfuncSet, funcLike, mFLT, mFGT } from '../syntax/constants.js';
5
+ import { pseudoElements, isValue, isColor } from '../syntax/syntax.js';
6
+ import { isDeclarationValue } from '../parser/utils/declaration.js';
7
+ import { renderSyntax } from './parser/parse.js';
8
+ import { equalsIgnoreCase } from '../parser/utils/text.js';
9
+ import { cloneNode } from '../ast/clone.js';
10
+ import { parseTokens } from '../parser/parse.js';
11
+
12
+ const config = getSyntaxConfig();
13
+ // @ts-expect-error
14
+ const allValues = config.declarations.all.syntax.split(/[\s|]+/g);
15
+ const funcTypes = [
16
+ ...tokensfuncDefMap.values(),
17
+ EnumToken.FunctionTokenType,
18
+ EnumToken.PseudoClassFuncTokenType,
19
+ ];
20
+ function trimArray(tokens) {
21
+ while (tokens[0]?.typ === EnumToken.WhitespaceTokenType) {
22
+ tokens.shift();
23
+ }
24
+ while (tokens[tokens.length - 1]?.typ === EnumToken.WhitespaceTokenType) {
25
+ tokens.pop();
26
+ }
27
+ return tokens;
28
+ }
29
+ function isMFName(featureName) {
30
+ return featureName.startsWith("--") || featureName.toLowerCase() in config.mediaFeatures;
31
+ }
32
+ /**
33
+ *
34
+ * @param featureName
35
+ * @returns
36
+ */
37
+ function getMFInfo(featureName) {
38
+ // @ts-expect-error
39
+ return config.mediaFeatures[featureName.toLowerCase()] ?? null;
40
+ }
41
+ /**
42
+ *
43
+ * @param featureName
44
+ * @param tokens
45
+ * @returns object with:
46
+ * - valid: boolean. true the media feaure is known or is a custom property. false otherwise
47
+ * - success: boolean. validation result
48
+ */
49
+ function isMFValue(featureName, tokens, isMFRange) {
50
+ // mf-value: <number> | <dimension> | <ident> | <ratio>
51
+ tokens = tokens.filter((token) => token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType);
52
+ if (tokens.length === 0) {
53
+ return { valid: true, success: false };
54
+ }
55
+ // https://www.w3.org/TR/mediaqueries-5/#:~:text=Attempting%20to%20evaluate%20a%20min%2Fmax%20prefixed
56
+ // https://www.w3.org/TR/mediaqueries-5/#custom-mq
57
+ if (featureName.startsWith("--") || (isMFRange && /^((min)|(max))-/g.test(featureName))) {
58
+ return { valid: true, success: false, isValueAllowed: false };
59
+ }
60
+ featureName = featureName.toLowerCase();
61
+ if (!(featureName in config.mediaFeatures)) {
62
+ return { valid: false, success: false };
63
+ }
64
+ // @ts-expect-error
65
+ const mediaFeature = config.mediaFeatures[featureName];
66
+ if (tokens.length === 1 &&
67
+ tokens[0].typ === EnumToken.MathFunctionTokenType &&
68
+ tokens[0].val === "calc") {
69
+ // todo: check calc tokens are of compatible types : resolution, ratio, length, integer, number?
70
+ // https://github.com/web-platform-tests/wpt/blob/master/css/mediaqueries/mq-calc-sign-function-003.html
71
+ return {
72
+ valid: true,
73
+ success: mediaFeature.type !== MediaFeatureType.KeywordType && mediaFeature.type !== MediaFeatureType.StringType,
74
+ };
75
+ }
76
+ switch (mediaFeature.type) {
77
+ case MediaFeatureType.BooleanType:
78
+ return {
79
+ valid: true,
80
+ success: tokens.length === 1 &&
81
+ tokens[0].typ === EnumToken.NumberTokenType &&
82
+ (tokens[0].val === 0 || tokens[0].val === 1),
83
+ };
84
+ case MediaFeatureType.KeywordType:
85
+ return {
86
+ valid: true,
87
+ success: tokens.length === 1 &&
88
+ tokens[0].typ === EnumToken.IdenTokenType &&
89
+ mediaFeature.values.includes(tokens[0].val.toLowerCase()),
90
+ };
91
+ case MediaFeatureType.LengthType:
92
+ return { valid: true, success: tokens.length === 1 && tokens[0].typ == EnumToken.LengthTokenType };
93
+ case MediaFeatureType.IntergerType:
94
+ return {
95
+ valid: true,
96
+ success: tokens.length === 1 &&
97
+ tokens[0].typ == EnumToken.NumberTokenType &&
98
+ !tokens[0].val.toString().includes("."),
99
+ };
100
+ case MediaFeatureType.NumberType:
101
+ return {
102
+ valid: true,
103
+ success: tokens.length === 1 &&
104
+ tokens[0].typ == EnumToken.NumberTokenType &&
105
+ typeof tokens[0].val === "number",
106
+ };
107
+ case MediaFeatureType.StringType:
108
+ return { valid: true, success: tokens.length === 1 && tokens[0].typ == EnumToken.StringTokenType };
109
+ case MediaFeatureType.ResolutionType:
110
+ return { valid: true, success: tokens.length === 1 && tokens[0].typ == EnumToken.ResolutionTokenType };
111
+ case MediaFeatureType.RatioType:
112
+ return {
113
+ valid: true,
114
+ success: (tokens.length == 1 &&
115
+ tokens[0].typ == EnumToken.NumberTokenType &&
116
+ tokens[0].val.typ == EnumToken.FractionTokenType) ||
117
+ (tokens.length === 3 &&
118
+ tokens[0].typ == EnumToken.NumberTokenType &&
119
+ typeof tokens[0].val === "number" &&
120
+ tokens[1].typ == EnumToken.LiteralTokenType &&
121
+ tokens[1].val === "/" &&
122
+ tokens[2].typ == EnumToken.NumberTokenType &&
123
+ typeof tokens[2].val === "number"),
124
+ };
125
+ default:
126
+ console.debug("Unknown media feature type " + mediaFeature.type);
127
+ }
128
+ return {
129
+ valid: true,
130
+ success: true,
131
+ };
132
+ }
133
+ function isStyleRangeValue(tokens) {
134
+ const filtered = tokens.filter((token) => token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType);
135
+ const result = isDeclarationValue(tokens);
136
+ if (result.success) {
137
+ result.success = filtered.length > 0;
138
+ }
139
+ return result;
140
+ }
141
+ function createValidationContext(tokens) {
142
+ tokens = trimArray(tokens.filter((t) => t.typ !== EnumToken.CommentTokenType));
143
+ if (tokens.at(-1)?.typ === EnumToken.ImportantTokenType) {
144
+ tokens.pop();
145
+ trimArray(tokens);
146
+ }
147
+ const token = {
148
+ tokens,
149
+ index: -1,
150
+ current() {
151
+ if (this.index < 0) {
152
+ return null;
153
+ }
154
+ while (this.tokens[this.index]?.typ == EnumToken.WhitespaceTokenType ||
155
+ this.tokens[this.index]?.typ == EnumToken.CommentTokenType ||
156
+ this.tokens[this.index]?.typ == EnumToken.CDOCOMMTokenType) {
157
+ this.index++;
158
+ }
159
+ return this.tokens[this.index];
160
+ },
161
+ peek(offset = 0, skipWhitespace = true) {
162
+ let index = this.index;
163
+ let token = this.tokens[++index];
164
+ if (!skipWhitespace) {
165
+ while (offset >= 0 && index < this.tokens.length) {
166
+ while (token != null && token.typ === EnumToken.CommentTokenType) {
167
+ token = this.tokens[++index];
168
+ }
169
+ if (offset === 0 || token == null) {
170
+ return token;
171
+ }
172
+ offset--;
173
+ token = this.tokens[++index];
174
+ }
175
+ return token;
176
+ }
177
+ while (offset >= 0 && index < this.tokens.length) {
178
+ while (token?.typ == EnumToken.WhitespaceTokenType ||
179
+ token?.typ == EnumToken.CommentTokenType ||
180
+ token?.typ == EnumToken.CDOCOMMTokenType) {
181
+ token = this.tokens[++index];
182
+ }
183
+ if (token == null || offset === 0) {
184
+ return token;
185
+ }
186
+ offset--;
187
+ token = this.tokens[++index];
188
+ }
189
+ return token;
190
+ },
191
+ /**
192
+ *
193
+ * @param stopCondition
194
+ * @param matchCount
195
+ * @returns
196
+ */
197
+ peekRange(open = EnumToken.StartParensTokenType, close = EnumToken.EndParensTokenType, counter = 0) {
198
+ let index = this.index;
199
+ let token = this.tokens[index];
200
+ // track balanced parens
201
+ let matchCount = 0;
202
+ const tokens = [];
203
+ while (index + 1 < this.tokens.length && this.tokens[index + 1]?.typ === EnumToken.WhitespaceTokenType) {
204
+ index++;
205
+ }
206
+ while (index + 1 < this.tokens.length) {
207
+ token = this.tokens[++index];
208
+ if (token?.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(token?.typ)) {
209
+ matchCount++;
210
+ }
211
+ else if (token?.typ === EnumToken.EndParensTokenType) {
212
+ matchCount--;
213
+ }
214
+ if (matchCount >= 0) {
215
+ tokens.push(token);
216
+ }
217
+ if (matchCount === 0) {
218
+ if (close !== EnumToken.EndParensTokenType && token?.typ === close) {
219
+ counter--;
220
+ }
221
+ else if (open !== EnumToken.StartParensTokenType && token?.typ === open) {
222
+ counter++;
223
+ }
224
+ }
225
+ if (matchCount <= 0 && counter <= 0) {
226
+ break;
227
+ }
228
+ }
229
+ if (tokens[0]?.typ === EnumToken.WhitespaceTokenType) {
230
+ tokens.shift();
231
+ }
232
+ return tokens;
233
+ },
234
+ // 2
235
+ split(split = EnumToken.CommaTokenType) {
236
+ let index = this.index;
237
+ let token = this.tokens[index];
238
+ // track balanced parens
239
+ let matchCount = 0;
240
+ const tokens = [[]];
241
+ while (index + 1 < this.tokens.length) {
242
+ token = this.tokens[++index];
243
+ if (token?.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(token?.typ)) {
244
+ matchCount++;
245
+ }
246
+ else if (token?.typ === EnumToken.EndParensTokenType) {
247
+ matchCount--;
248
+ }
249
+ if (matchCount === 0 && token.typ === split) {
250
+ tokens.at(-1).push(token);
251
+ tokens.push([]);
252
+ }
253
+ else {
254
+ tokens.at(-1).push(token);
255
+ }
256
+ }
257
+ return tokens;
258
+ },
259
+ getRemainingTokens() {
260
+ return this.tokens.slice(this.index + 1);
261
+ },
262
+ last() {
263
+ let index = this.tokens.length - 1;
264
+ let token = this.tokens[index];
265
+ while ((this.index >= 0 && token?.typ === EnumToken.WhitespaceTokenType) ||
266
+ token?.typ === EnumToken.CommentTokenType ||
267
+ token?.typ === EnumToken.CDOCOMMTokenType ||
268
+ token?.typ === EnumToken.InvalidCommentTokenType ||
269
+ token?.typ === EnumToken.BadCommentTokenType ||
270
+ token?.typ === EnumToken.BadStringTokenType) {
271
+ token = this.tokens[--index];
272
+ if (token == null) {
273
+ break;
274
+ }
275
+ }
276
+ return token;
277
+ },
278
+ end() {
279
+ this.index = this.tokens.length + 1;
280
+ return this;
281
+ },
282
+ next() {
283
+ let token = this.tokens[++this.index];
284
+ while (token?.typ === EnumToken.WhitespaceTokenType ||
285
+ token?.typ === EnumToken.CommentTokenType ||
286
+ token?.typ === EnumToken.CDOCOMMTokenType ||
287
+ token?.typ === EnumToken.InvalidCommentTokenType ||
288
+ token?.typ === EnumToken.BadCommentTokenType ||
289
+ token?.typ === EnumToken.BadStringTokenType) {
290
+ token = this.tokens[++this.index];
291
+ }
292
+ return token;
293
+ },
294
+ slice() {
295
+ return {
296
+ ...token,
297
+ index: this.index == -1 ? -1 : 0,
298
+ tokens: this.index == -1 ? this.tokens.slice() : this.tokens.slice(this.index),
299
+ };
300
+ },
301
+ update(token) {
302
+ const index = this.tokens.indexOf(token);
303
+ if (index != -1) {
304
+ this.index = index;
305
+ }
306
+ return this;
307
+ },
308
+ done() {
309
+ if (this.index + 1 < this.tokens.length) {
310
+ let index = this.index + 1;
311
+ while (index < this.tokens.length) {
312
+ if (this.tokens[index].typ === EnumToken.WhitespaceTokenType ||
313
+ this.tokens[index].typ === EnumToken.CommentTokenType) {
314
+ index++;
315
+ }
316
+ else {
317
+ return false;
318
+ }
319
+ }
320
+ }
321
+ return true;
322
+ },
323
+ };
324
+ return token;
325
+ }
326
+ function matchSelectorSyntax(stream, errors, options, nested = true) {
327
+ const stack = [];
328
+ const tokens = [];
329
+ const nodes = [
330
+ EnumToken.CommaTokenType,
331
+ EnumToken.ColumnCombinatorTokenType,
332
+ EnumToken.ChildCombinatorTokenType,
333
+ EnumToken.NextSiblingCombinatorTokenType,
334
+ EnumToken.SubsequentSiblingCombinatorTokenType,
335
+ ];
336
+ const trimWhitespaceBefore = nodes.concat(EnumToken.DelimTokenType, EnumToken.DashMatchTokenType, EnumToken.IncludeMatchTokenType, EnumToken.ContainMatchTokenType, EnumToken.StartMatchTokenType, EnumToken.EndMatchTokenType, EnumToken.AttrEndTokenType);
337
+ const trimWhitespaceAfter = nodes.concat(EnumToken.DelimTokenType, EnumToken.DashMatchTokenType, EnumToken.IncludeMatchTokenType, EnumToken.ContainMatchTokenType, EnumToken.StartMatchTokenType, EnumToken.EndMatchTokenType, EnumToken.AttrStartTokenType);
338
+ const enumMap = new Map([
339
+ [EnumToken.Tilda, EnumToken.SubsequentSiblingCombinatorTokenType],
340
+ [EnumToken.GtTokenType, EnumToken.ChildCombinatorTokenType],
341
+ ]);
342
+ let token;
343
+ let i = 0;
344
+ let success = true;
345
+ while (i < stream.length &&
346
+ (stream[i].typ === EnumToken.WhitespaceTokenType || stream[i].typ === EnumToken.CommentTokenType)) {
347
+ i++;
348
+ }
349
+ if (i < stream.length) {
350
+ switch (stream[i].typ) {
351
+ case EnumToken.Plus:
352
+ case EnumToken.Tilda:
353
+ case EnumToken.GtTokenType:
354
+ case EnumToken.ColumnCombinatorTokenType:
355
+ case EnumToken.ChildCombinatorTokenType:
356
+ case EnumToken.NextSiblingCombinatorTokenType:
357
+ case EnumToken.SubsequentSiblingCombinatorTokenType:
358
+ if (!nested) {
359
+ return {
360
+ success: false,
361
+ errors: [
362
+ {
363
+ action: "drop",
364
+ message: `Unexpected token ${EnumToken[stream[i].typ]} at ${stream[i].loc.src}:${stream[i].loc.sta.lin}:${stream[i].loc.sta.col}`,
365
+ node: stream[i],
366
+ location: stream[i].loc,
367
+ },
368
+ ],
369
+ };
370
+ }
371
+ }
372
+ }
373
+ for (; i < stream.length; i++) {
374
+ token = stream[i];
375
+ if (token.typ === EnumToken.EOF) {
376
+ break;
377
+ }
378
+ if (token.typ === EnumToken.Star) {
379
+ token.typ = EnumToken.UniversalSelectorTokenType;
380
+ }
381
+ tokens.push(token);
382
+ if (tokensfuncDefMap.has(token.typ)) {
383
+ if (stack.length > 0 && nodes.includes(stack.at(-1).typ)) {
384
+ stack.pop();
385
+ }
386
+ stack.push(token);
387
+ continue;
388
+ }
389
+ if (stack.length > 0 &&
390
+ nodes.includes(stack.at(-1).typ) &&
391
+ !nodes.includes(token.typ) &&
392
+ token.typ !== EnumToken.WhitespaceTokenType &&
393
+ token.typ !== EnumToken.CommentTokenType &&
394
+ token.typ !== EnumToken.CDOCOMMTokenType) {
395
+ stack.pop();
396
+ }
397
+ if (token.typ === EnumToken.LiteralTokenType && "+" === token.val) {
398
+ Object.assign(token, { typ: EnumToken.NextSiblingCombinatorTokenType });
399
+ continue;
400
+ }
401
+ switch (token.typ) {
402
+ case EnumToken.InvalidCommentTokenType:
403
+ case EnumToken.BadCommentTokenType:
404
+ case EnumToken.BadStringTokenType:
405
+ break;
406
+ case EnumToken.PseudoClassFuncTokenType: {
407
+ const result = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, token.val + "()")?.[0]?.chi ?? [], createValidationContext(token.chi), options);
408
+ if (!result.success) {
409
+ success = false;
410
+ if (result.errors.length > 0) {
411
+ errors.push(...result.errors);
412
+ }
413
+ }
414
+ }
415
+ case EnumToken.WhitespaceTokenType:
416
+ case EnumToken.CommentTokenType:
417
+ case EnumToken.CDOCOMMTokenType:
418
+ break;
419
+ case EnumToken.NestingSelectorTokenType:
420
+ if (nested === false && !options.nestedRule) {
421
+ return {
422
+ success: false,
423
+ errors: [
424
+ {
425
+ action: "drop",
426
+ message: `Nesting selector is not allowed at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
427
+ node: token,
428
+ location: token.loc,
429
+ },
430
+ ],
431
+ };
432
+ }
433
+ break;
434
+ case EnumToken.Plus:
435
+ Object.assign(token, { typ: EnumToken.NextSiblingCombinatorTokenType });
436
+ if (stack.length > 0 && nodes.includes(stack.at(-1)?.typ)) {
437
+ return {
438
+ success: false,
439
+ errors: [
440
+ {
441
+ action: "drop",
442
+ message: `Unexpected combinator ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
443
+ node: token,
444
+ location: token.loc,
445
+ },
446
+ ],
447
+ };
448
+ }
449
+ stack.push(token);
450
+ break;
451
+ case EnumToken.Tilda:
452
+ case EnumToken.GtTokenType:
453
+ Object.assign(token, { typ: enumMap.get(token.typ) });
454
+ case EnumToken.ColumnCombinatorTokenType:
455
+ case EnumToken.ChildCombinatorTokenType:
456
+ case EnumToken.UniversalSelectorTokenType:
457
+ case EnumToken.DescendantCombinatorTokenType:
458
+ case EnumToken.NextSiblingCombinatorTokenType:
459
+ case EnumToken.SubsequentSiblingCombinatorTokenType:
460
+ if (tokens.at(-1)?.typ === EnumToken.WhitespaceTokenType) {
461
+ tokens.pop();
462
+ }
463
+ if (stream[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
464
+ i++;
465
+ }
466
+ if (stack.length > 0 && stack.at(-1)?.typ === EnumToken.UniversalSelectorTokenType) {
467
+ stack.pop();
468
+ }
469
+ if (stack.length > 0 && nodes.includes(stack.at(-1)?.typ)) {
470
+ return {
471
+ success: false,
472
+ errors: [
473
+ {
474
+ action: "drop",
475
+ message: `Unexpected combinator ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
476
+ node: token,
477
+ location: token.loc,
478
+ },
479
+ ],
480
+ };
481
+ }
482
+ stack.push(token);
483
+ break;
484
+ case EnumToken.CommaTokenType:
485
+ if (stack.length > 0 && stack.at(-1).typ === EnumToken.UniversalSelectorTokenType) {
486
+ stack.pop();
487
+ }
488
+ if (tokens.length === 0 || stack.at(-1)?.typ == EnumToken.CommaTokenType) {
489
+ return {
490
+ success: false,
491
+ errors: [
492
+ {
493
+ action: "drop",
494
+ message: `Unexpected token ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
495
+ node: token,
496
+ location: token.loc,
497
+ },
498
+ ],
499
+ };
500
+ }
501
+ stack.push(token);
502
+ if (!nested) {
503
+ let k = i + 1;
504
+ while (k < stream.length &&
505
+ (stream[k].typ === EnumToken.WhitespaceTokenType ||
506
+ stream[k].typ === EnumToken.CommentTokenType)) {
507
+ k++;
508
+ }
509
+ // :has(> a, ~b, +c)
510
+ if (!tokensfuncDefMap.has(stack.at(-2)?.typ) && k < stream.length) {
511
+ switch (stream[k].typ) {
512
+ case EnumToken.Plus:
513
+ case EnumToken.Tilda:
514
+ case EnumToken.GtTokenType:
515
+ case EnumToken.ColumnCombinatorTokenType:
516
+ case EnumToken.ChildCombinatorTokenType:
517
+ case EnumToken.NextSiblingCombinatorTokenType:
518
+ case EnumToken.SubsequentSiblingCombinatorTokenType:
519
+ return {
520
+ success: false,
521
+ errors: [
522
+ {
523
+ action: "drop",
524
+ message: `Unexpected token ${EnumToken[stream[k].typ]} at ${stream[k].loc.src}:${stream[k].loc.sta.lin}:${stream[k].loc.sta.col}`,
525
+ node: stream[k],
526
+ location: stream[k].loc,
527
+ },
528
+ ],
529
+ };
530
+ }
531
+ }
532
+ }
533
+ break;
534
+ case EnumToken.Pipe:
535
+ case EnumToken.DelimTokenType:
536
+ case EnumToken.StringTokenType:
537
+ case EnumToken.IncludeMatchTokenType:
538
+ case EnumToken.ContainMatchTokenType:
539
+ case EnumToken.StartMatchTokenType:
540
+ case EnumToken.EndMatchTokenType:
541
+ case EnumToken.DashMatchTokenType:
542
+ if (stack.at(-1)?.typ !== EnumToken.AttrStartTokenType &&
543
+ !(stack.at(-1)?.typ === EnumToken.UniversalSelectorTokenType &&
544
+ stack.at(-2)?.typ === EnumToken.AttrStartTokenType)) {
545
+ return {
546
+ success: false,
547
+ errors: [
548
+ {
549
+ action: "drop",
550
+ message: `Unexpected token ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
551
+ node: token,
552
+ location: token.loc,
553
+ },
554
+ ],
555
+ };
556
+ }
557
+ break;
558
+ // case EnumToken.Star:
559
+ // Object.assign(token, { typ: EnumToken.UniversalSelectorTokenType });
560
+ // break;
561
+ case EnumToken.IdenTokenType:
562
+ case EnumToken.HashTokenType:
563
+ case EnumToken.PseudoElementTokenType:
564
+ case EnumToken.PseudoClassTokenType:
565
+ case EnumToken.ClassSelectorTokenType:
566
+ if (stack.at(-1)?.typ === EnumToken.CommaTokenType) {
567
+ stack.pop();
568
+ }
569
+ break;
570
+ case EnumToken.AttrStartTokenType:
571
+ if (stack.at(-1)?.typ === EnumToken.CommaTokenType) {
572
+ stack.pop();
573
+ }
574
+ stack.push(token);
575
+ break;
576
+ case EnumToken.AttrEndTokenType:
577
+ if (stack.length > 0 && stack.at(-1).typ === EnumToken.UniversalSelectorTokenType) {
578
+ stack.pop();
579
+ }
580
+ if (stack.at(-1)?.typ !== EnumToken.AttrStartTokenType) {
581
+ return {
582
+ success: false,
583
+ errors: [
584
+ {
585
+ action: "drop",
586
+ message: `Unexpected token ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
587
+ node: token,
588
+ location: token.loc,
589
+ },
590
+ ],
591
+ };
592
+ }
593
+ {
594
+ const k = tokens.length - 1;
595
+ let index = tokens.indexOf(stack.at(-1));
596
+ const slice = [];
597
+ for (let n = index + 1; n < k; n++) {
598
+ if (tokens[n].typ === EnumToken.WhitespaceTokenType ||
599
+ tokens[n].typ === EnumToken.CommentTokenType ||
600
+ tokens[n].typ === EnumToken.CDOCOMMTokenType) {
601
+ continue;
602
+ }
603
+ slice.push(tokens[n]);
604
+ }
605
+ if (slice.length === 0) {
606
+ return {
607
+ success: false,
608
+ errors: [
609
+ {
610
+ action: "drop",
611
+ message: `Invalid selector attribute at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
612
+ node: token,
613
+ location: token.loc,
614
+ },
615
+ ],
616
+ };
617
+ }
618
+ if (slice[1]?.typ === EnumToken.Pipe) {
619
+ if (slice[0].typ !== EnumToken.UniversalSelectorTokenType &&
620
+ slice[0].typ !== EnumToken.Star &&
621
+ slice[0].typ !== EnumToken.IdenTokenType) {
622
+ return {
623
+ success: false,
624
+ errors: [
625
+ {
626
+ action: "drop",
627
+ message: `Invalid selector attribute at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
628
+ node: token,
629
+ location: token.loc,
630
+ },
631
+ ],
632
+ };
633
+ }
634
+ slice.shift();
635
+ }
636
+ if (slice[0].typ === EnumToken.Pipe) {
637
+ if (slice.length === 1) {
638
+ return {
639
+ success: false,
640
+ errors: [
641
+ {
642
+ action: "drop",
643
+ message: `Invalid selector attribute at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
644
+ node: token,
645
+ location: token.loc,
646
+ },
647
+ ],
648
+ };
649
+ }
650
+ if (slice[1]?.typ !== EnumToken.IdenTokenType) {
651
+ return {
652
+ success: false,
653
+ errors: [
654
+ {
655
+ action: "drop",
656
+ message: `Invalid selector attribute at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
657
+ node: token,
658
+ location: token.loc,
659
+ },
660
+ ],
661
+ };
662
+ }
663
+ slice.shift();
664
+ }
665
+ if (slice.length === 1) {
666
+ if (slice[0].typ !== EnumToken.IdenTokenType) {
667
+ return {
668
+ success: false,
669
+ errors: [
670
+ {
671
+ action: "drop",
672
+ message: `Invalid selector attribute at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
673
+ node: token,
674
+ location: token.loc,
675
+ },
676
+ ],
677
+ };
678
+ }
679
+ stack.pop();
680
+ break;
681
+ }
682
+ slice.shift();
683
+ if (slice[0].typ != EnumToken.DelimTokenType &&
684
+ slice[0].typ !== EnumToken.DashMatchTokenType &&
685
+ slice[0].typ !== EnumToken.EndMatchTokenType &&
686
+ slice[0].typ !== EnumToken.IncludeMatchTokenType &&
687
+ slice[0].typ !== EnumToken.StartMatchTokenType &&
688
+ slice[0].typ !== EnumToken.ContainMatchTokenType) {
689
+ return {
690
+ success: false,
691
+ errors: [
692
+ {
693
+ action: "drop",
694
+ message: `Unexpected token ${EnumToken[slice[0].typ]} at ${slice[0].loc.src}:${slice[0].loc.sta.lin}:${slice[0].loc.sta.col}`,
695
+ node: slice[0],
696
+ location: slice[0].loc,
697
+ },
698
+ ],
699
+ };
700
+ }
701
+ slice.shift();
702
+ if (slice.length === 0) {
703
+ // expect iden or string
704
+ return {
705
+ success: false,
706
+ errors: [
707
+ {
708
+ action: "drop",
709
+ message: `Invalid selector attribute at ${slice[0].loc.src}:${slice[0].loc.sta.lin}:${slice[0].loc.sta.col}`,
710
+ node: slice[0],
711
+ location: slice[0].loc,
712
+ },
713
+ ],
714
+ };
715
+ }
716
+ if (slice[0].typ !== EnumToken.IdenTokenType && slice[0].typ !== EnumToken.StringTokenType) {
717
+ return {
718
+ success: false,
719
+ errors: [
720
+ {
721
+ action: "drop",
722
+ message: `Unexpected token ${EnumToken[slice[0].typ]} at ${slice[0].loc.src}:${slice[0].loc.sta.lin}:${slice[0].loc.sta.col}`,
723
+ node: slice[0],
724
+ location: slice[0].loc,
725
+ },
726
+ ],
727
+ };
728
+ }
729
+ if (slice[0]?.typ === EnumToken.StringTokenType &&
730
+ /^[a-zA-Z0-9_-]+$/.test(slice[0].val.slice(1, -1))) {
731
+ Object.assign(slice[0], {
732
+ typ: EnumToken.IdenTokenType,
733
+ val: slice[0].val.slice(1, -1),
734
+ });
735
+ }
736
+ slice.shift();
737
+ if (slice.length === 0) {
738
+ stack.pop();
739
+ break;
740
+ }
741
+ if (slice[0].typ !== EnumToken.IdenTokenType ||
742
+ (slice[0].val != "i" && slice[0].val != "s")) {
743
+ return {
744
+ success: false,
745
+ errors: [
746
+ {
747
+ action: "drop",
748
+ message: `Unexpected token ${EnumToken[slice[0].typ]} at ${slice[0].loc.src}:${slice[0].loc.sta.lin}:${slice[0].loc.sta.col}`,
749
+ node: slice[0],
750
+ location: slice[0].loc,
751
+ },
752
+ ],
753
+ };
754
+ }
755
+ slice.shift();
756
+ if (slice.length > 0) {
757
+ return {
758
+ success: false,
759
+ errors: [
760
+ {
761
+ action: "drop",
762
+ message: `Unexpected token ${EnumToken[slice[0].typ]} at ${slice[0].loc.src}:${slice[0].loc.sta.lin}:${slice[0].loc.sta.col}`,
763
+ node: slice[0],
764
+ location: slice[0].loc,
765
+ },
766
+ ],
767
+ };
768
+ }
769
+ stack.pop();
770
+ break;
771
+ }
772
+ case EnumToken.ColonTokenType:
773
+ if (stream[i + 1]?.typ === EnumToken.IdenTokenType) {
774
+ Object.assign(token, {
775
+ typ: stream[i + 1].val === "page"
776
+ ? EnumToken.PseudoPageTokenType
777
+ : pseudoElements.includes(token.val)
778
+ ? EnumToken.PseudoElementTokenType
779
+ : EnumToken.PseudoClassTokenType,
780
+ val: ":" + stream[i + 1].val,
781
+ });
782
+ token.loc.end = stream[++i].loc.end;
783
+ break;
784
+ }
785
+ else if (stream[i + 1]?.typ === EnumToken.FunctionTokenDefType) {
786
+ Object.assign(token, {
787
+ typ: EnumToken.PseudoClassFunctionTokenDefType,
788
+ val: ":" + stream[i + 1].val,
789
+ });
790
+ token.loc.end = stream[++i].loc.end;
791
+ stack.push(token);
792
+ break;
793
+ }
794
+ return {
795
+ success: false,
796
+ errors: [
797
+ {
798
+ action: "drop",
799
+ message: `Unexpected token ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
800
+ node: token,
801
+ location: token.loc,
802
+ },
803
+ ],
804
+ };
805
+ case EnumToken.DoubleColonTokenType:
806
+ if (stream[i + 1]?.typ === EnumToken.IdenTokenType) {
807
+ Object.assign(token, {
808
+ typ: stream[i + 1].val === "page"
809
+ ? EnumToken.PseudoPageTokenType
810
+ : EnumToken.PseudoElementTokenType,
811
+ val: "::" + stream[i + 1].val,
812
+ });
813
+ token.loc.end = stream[++i].loc.end;
814
+ break;
815
+ }
816
+ else if (stream[i + 1]?.typ === EnumToken.FunctionTokenDefType) {
817
+ Object.assign(token, {
818
+ typ: EnumToken.PseudoClassFunctionTokenDefType,
819
+ val: "::" + stream[i + 1].val,
820
+ });
821
+ token.loc.end = stream[++i].loc.end;
822
+ stack.push(token);
823
+ break;
824
+ }
825
+ return {
826
+ success: false,
827
+ errors: [
828
+ {
829
+ action: "drop",
830
+ message: `Unexpected token ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
831
+ node: token,
832
+ location: token.loc,
833
+ },
834
+ ],
835
+ };
836
+ case EnumToken.StartParensTokenType:
837
+ if (tokens.at(-2)?.typ === EnumToken.PseudoClassTokenType ||
838
+ tokens.at(-2)?.typ === EnumToken.PseudoElementTokenType) {
839
+ stack.push(Object.assign(tokens.at(-2), {
840
+ typ: EnumToken.PseudoClassFunctionTokenDefType,
841
+ chi: [],
842
+ }));
843
+ // tokens.pop();
844
+ break;
845
+ }
846
+ return {
847
+ success: false,
848
+ errors: [
849
+ {
850
+ action: "drop",
851
+ message: `Unexpected token ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
852
+ node: token,
853
+ location: token.loc,
854
+ },
855
+ ],
856
+ };
857
+ case EnumToken.EndParensTokenType:
858
+ if (stack.length > 0 && stack.at(-1)?.typ === EnumToken.UniversalSelectorTokenType) {
859
+ stack.pop();
860
+ }
861
+ if (stack.at(-1)?.typ === EnumToken.PseudoClassFunctionTokenDefType ||
862
+ stack.at(-1)?.typ === EnumToken.PseudoElementTokenType) {
863
+ const token = stack.at(-1);
864
+ if (!(stack.at(-1).val + "()" in config.selectors)) {
865
+ return {
866
+ errors: [
867
+ {
868
+ action: "drop",
869
+ message: `Unknown class element ${token.val}`,
870
+ node: token,
871
+ location: token.loc,
872
+ },
873
+ ],
874
+ success: false,
875
+ };
876
+ }
877
+ const index = tokens.indexOf(token);
878
+ const result = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, token.val + "()")?.[0]?.chi ?? [], createValidationContext(tokens.slice(index + 1, tokens.length - 1)), options);
879
+ if (!result.success) {
880
+ success = false;
881
+ if (result.errors.length > 0) {
882
+ errors.push(...result.errors);
883
+ }
884
+ }
885
+ stack.pop();
886
+ break;
887
+ }
888
+ return {
889
+ success: false,
890
+ errors: [
891
+ {
892
+ action: "drop",
893
+ message: `Unexpected token ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
894
+ node: token,
895
+ location: token.loc,
896
+ },
897
+ ],
898
+ };
899
+ case EnumToken.NumberTokenType:
900
+ case EnumToken.LiteralTokenType:
901
+ case EnumToken.DimensionTokenType:
902
+ if (stack.at(-1)?.typ === EnumToken.CommaTokenType) {
903
+ stack.pop();
904
+ }
905
+ if (stack.at(-1)?.typ === EnumToken.PseudoClassFunctionTokenDefType) {
906
+ break;
907
+ }
908
+ default:
909
+ return {
910
+ success: false,
911
+ errors: [
912
+ {
913
+ action: "drop",
914
+ message: `Unsupported selector token ${EnumToken[token.typ]} at ${token.loc.src}:${token.loc.sta.lin}:${token.loc.sta.col}`,
915
+ node: token,
916
+ location: token.loc,
917
+ },
918
+ ],
919
+ };
920
+ }
921
+ if (token.typ === EnumToken.WhitespaceTokenType &&
922
+ trimWhitespaceAfter.includes(tokens.at(-2)?.typ) &&
923
+ tokens.at(-1)?.typ === EnumToken.WhitespaceTokenType) {
924
+ tokens.pop();
925
+ }
926
+ else if (trimWhitespaceBefore.includes(token.typ) && tokens.at(-2)?.typ === EnumToken.WhitespaceTokenType) {
927
+ tokens.splice(tokens.length - 2, 1);
928
+ }
929
+ }
930
+ if (stack.length > 0 && stack.at(-1).typ === EnumToken.UniversalSelectorTokenType) {
931
+ stack.pop();
932
+ }
933
+ if (stack.length > 0) {
934
+ return {
935
+ success: false,
936
+ errors: [
937
+ {
938
+ action: "drop",
939
+ message: `Unmatched token ${EnumToken[stack.at(-1).typ]} at ${stack.at(-1).loc.src}:${stack.at(-1).loc.sta.lin}:${stack.at(-1).loc.sta.col}`,
940
+ node: stack.at(-1),
941
+ location: stack.at(-1).loc,
942
+ },
943
+ ],
944
+ };
945
+ }
946
+ stream.length = 0;
947
+ stream.push(...tokens);
948
+ if (!success && errors.length === 0) {
949
+ errors.push({
950
+ action: "drop",
951
+ message: "Invalid selector",
952
+ node: tokens[0],
953
+ location: tokens[0].loc,
954
+ });
955
+ }
956
+ return { success, errors };
957
+ }
958
+ function matchAllSyntaxes(syntaxes, context, options) {
959
+ const result = matchSyntax(syntaxes, context, {
960
+ ...options,
961
+ visited: new Map(),
962
+ });
963
+ if (result.success && !result.context.done()) {
964
+ const node = result.context.peek();
965
+ return {
966
+ ...result,
967
+ success: false,
968
+ token: context.peek(),
969
+ errors: [
970
+ ...result.errors,
971
+ {
972
+ action: "drop",
973
+ message: `Unexpected token ${EnumToken[node?.typ]} at ${node.loc?.src}:${node.loc?.sta?.lin}:${node.loc?.sta?.col}`,
974
+ node,
975
+ syntax: result.syntaxToken ??
976
+ syntaxes?.reduce?.((acc, b) => acc + renderSyntax(b), "")?.trim?.() ??
977
+ null,
978
+ },
979
+ ],
980
+ };
981
+ }
982
+ if (syntaxes != null && result.success && result.syntaxToken != null) {
983
+ const index = syntaxes.indexOf(result.syntaxToken);
984
+ if (index != -1) {
985
+ for (let i = index; i < syntaxes.length; i++) {
986
+ if (syntaxes[i].typ == ValidationTokenEnum.Whitespace ||
987
+ syntaxes[i].isOptional ||
988
+ syntaxes[i].isRepeatable) {
989
+ continue;
990
+ }
991
+ if (syntaxes[i].typ == ValidationTokenEnum.SemiColon && i === syntaxes.length - 1) {
992
+ continue;
993
+ }
994
+ return {
995
+ ...result,
996
+ success: false,
997
+ syntaxToken: syntaxes[i],
998
+ };
999
+ }
1000
+ }
1001
+ }
1002
+ return {
1003
+ ...result,
1004
+ errors: !result.success && result.errors.length === 0
1005
+ ? [
1006
+ {
1007
+ action: "drop",
1008
+ message: result.errors[0]?.message || "could not match syntax",
1009
+ node: result.token,
1010
+ syntax: result.syntaxToken,
1011
+ location: result.token?.loc ?? context.tokens.at(-1)?.loc,
1012
+ },
1013
+ ]
1014
+ : result.errors,
1015
+ syntaxToken: !result.success ? result.syntaxToken : null,
1016
+ };
1017
+ }
1018
+ function matchListSyntax(syntax, context, options) {
1019
+ const { isList, match, isOptional, ...rest } = syntax;
1020
+ let success = true;
1021
+ let result = null;
1022
+ let tmpResult;
1023
+ let count = 0;
1024
+ let range;
1025
+ success = false;
1026
+ do {
1027
+ range = context.peekRange(EnumToken.CommaTokenType, EnumToken.CommaTokenType, 1);
1028
+ tmpResult = matchSyntax([rest], createValidationContext(range.at(-1)?.typ === EnumToken.CommaTokenType ? range.slice(0, -1) : range), options);
1029
+ if (tmpResult.success) {
1030
+ count++;
1031
+ success = true;
1032
+ result = tmpResult;
1033
+ if (Number.isFinite(match?.max?.val) && count === match.max.val) {
1034
+ context.update(range.at(-1)?.typ === EnumToken.CommaTokenType ? range.at(-2) : range.at(-1));
1035
+ break;
1036
+ }
1037
+ else {
1038
+ context.update(range.at(-1));
1039
+ }
1040
+ if (context.done()) {
1041
+ // context.end();
1042
+ break;
1043
+ }
1044
+ }
1045
+ } while (tmpResult.success && !context.done());
1046
+ if (result?.success && match != null) {
1047
+ if (count < match.min.val || (Number.isFinite(match.max) && count > match.max.val)) {
1048
+ return {
1049
+ ...result,
1050
+ success: false,
1051
+ errors: [
1052
+ {
1053
+ action: "drop",
1054
+ message: "could not match syntax",
1055
+ node: context.peek(),
1056
+ location: context.peek()?.loc,
1057
+ },
1058
+ ],
1059
+ };
1060
+ }
1061
+ }
1062
+ return result == null
1063
+ ? {
1064
+ success: false,
1065
+ valid: true,
1066
+ context,
1067
+ token: context.peek(),
1068
+ syntaxToken: syntax,
1069
+ errors: [],
1070
+ }
1071
+ : {
1072
+ ...result,
1073
+ success,
1074
+ context,
1075
+ token: context.peek(),
1076
+ };
1077
+ }
1078
+ function matchOccurenceSyntax(syntax, context, options) {
1079
+ const { match, ...rest } = syntax;
1080
+ let result = null;
1081
+ let tmpResult;
1082
+ let count = 0;
1083
+ do {
1084
+ tmpResult = matchSyntax([rest], context.slice(), options);
1085
+ if (tmpResult.success) {
1086
+ count++;
1087
+ result = tmpResult;
1088
+ if (tmpResult.context.done()) {
1089
+ context.end();
1090
+ break;
1091
+ }
1092
+ context.update(tmpResult.context.current());
1093
+ if (match?.max?.val != null && Number.isFinite(match?.max?.val) && count === match.max.val) {
1094
+ break;
1095
+ }
1096
+ }
1097
+ } while (tmpResult.success && !context.done());
1098
+ if (match != null &&
1099
+ (count < match.min.val || (Number.isFinite(match.max?.val) && count > match.max.val))) {
1100
+ return {
1101
+ success: false,
1102
+ errors: [
1103
+ {
1104
+ action: "drop",
1105
+ message: "could not match syntax",
1106
+ node: context.peek(),
1107
+ location: context.peek()?.loc,
1108
+ },
1109
+ ],
1110
+ syntaxToken: null,
1111
+ valid: true,
1112
+ context,
1113
+ token: null,
1114
+ };
1115
+ }
1116
+ return (result ?? {
1117
+ success: false,
1118
+ errors: [
1119
+ {
1120
+ action: "drop",
1121
+ message: "could not match syntax",
1122
+ node: context.peek(),
1123
+ location: context.peek().loc,
1124
+ },
1125
+ ],
1126
+ syntaxToken: syntax,
1127
+ valid: true,
1128
+ context,
1129
+ token: context.peek(),
1130
+ });
1131
+ }
1132
+ function matchSyntax(syntaxes, context, options) {
1133
+ if (syntaxes == null) {
1134
+ return {
1135
+ syntaxToken: null,
1136
+ token: context.peek(),
1137
+ success: true,
1138
+ valid: false,
1139
+ context,
1140
+ errors: [],
1141
+ };
1142
+ }
1143
+ syntaxes = syntaxes.slice();
1144
+ let i = -1;
1145
+ let success = false;
1146
+ let token = null;
1147
+ let result = null;
1148
+ let isOptional;
1149
+ if (context.tokens.length == 1 &&
1150
+ context.tokens[0].typ == EnumToken.IdenTokenType &&
1151
+ allValues.some((v) => equalsIgnoreCase(v, context.tokens[0].val))) {
1152
+ context.end();
1153
+ return {
1154
+ syntaxToken: null,
1155
+ token: context.peek(),
1156
+ success: true,
1157
+ valid: true,
1158
+ context,
1159
+ errors: [],
1160
+ };
1161
+ }
1162
+ while (++i < syntaxes.length) {
1163
+ if (syntaxes[i].typ == ValidationTokenEnum.Whitespace) {
1164
+ continue;
1165
+ }
1166
+ isOptional =
1167
+ (syntaxes[i].isOptional === true ||
1168
+ syntaxes[i].isRepeatable === true ||
1169
+ syntaxes[i].isRepeatable === true) &&
1170
+ !syntaxes[i].isMandatatoryGroup;
1171
+ token = context.peek();
1172
+ if (token == null) {
1173
+ if (!isOptional) {
1174
+ return {
1175
+ success: false,
1176
+ errors: [
1177
+ {
1178
+ action: "drop",
1179
+ message: "could not match syntax",
1180
+ node: null,
1181
+ syntax: syntaxes[i],
1182
+ },
1183
+ ],
1184
+ syntaxToken: syntaxes[i],
1185
+ valid: true,
1186
+ context,
1187
+ token: null,
1188
+ };
1189
+ }
1190
+ break;
1191
+ }
1192
+ if (token.typ === EnumToken.WildCardFunctionTokenType && token.val === "var") {
1193
+ result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, token.val + "()")?.[0]?.chi, createValidationContext(token.chi), options);
1194
+ if ((result.success && result.context.done()) || isOptional) {
1195
+ context.next();
1196
+ continue;
1197
+ }
1198
+ return {
1199
+ ...result,
1200
+ success: false,
1201
+ token,
1202
+ syntaxToken: syntaxes[i],
1203
+ errors: [
1204
+ {
1205
+ action: "drop",
1206
+ message: "could not match syntax",
1207
+ node: context.peek(),
1208
+ location: context.peek().loc,
1209
+ },
1210
+ ],
1211
+ };
1212
+ }
1213
+ // custom function token
1214
+ if (token.typ === EnumToken.CustomFunctionTokenDefType) {
1215
+ if (syntaxes[i].typ != ValidationTokenEnum.PropertyType ||
1216
+ syntaxes[i + 1].typ != ValidationTokenEnum.OpenParenthesis ||
1217
+ syntaxes[i + 2].typ == null) {
1218
+ if (!isOptional) {
1219
+ return {
1220
+ success: false,
1221
+ errors: [
1222
+ {
1223
+ action: "drop",
1224
+ message: "could not match syntax",
1225
+ node: token,
1226
+ syntax: syntaxes[i],
1227
+ },
1228
+ ],
1229
+ syntaxToken: syntaxes[i],
1230
+ valid: true,
1231
+ context,
1232
+ token: null,
1233
+ };
1234
+ }
1235
+ break;
1236
+ }
1237
+ const range = trimArray(context.peekRange());
1238
+ context.next();
1239
+ i++;
1240
+ result = matchSyntax([syntaxes[i + 1]], createValidationContext(range.slice(1, -1)), options);
1241
+ if (result.success) {
1242
+ context.update(range.at(-1));
1243
+ i += 2;
1244
+ }
1245
+ return result;
1246
+ }
1247
+ if (tokensfuncDefMap.has(token.typ) &&
1248
+ token.typ === EnumToken.WildCardFunctionTokenDefType) {
1249
+ const range = trimArray(context.peekRange());
1250
+ result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, token.val + "()")?.[0]?.chi, createValidationContext(range.slice(1, -1)), options);
1251
+ if (result.success) {
1252
+ context.update(range.at(-1));
1253
+ if (context.done()) {
1254
+ return {
1255
+ ...result,
1256
+ context,
1257
+ syntaxToken: syntaxes[i + 1],
1258
+ };
1259
+ }
1260
+ continue;
1261
+ }
1262
+ if (isOptional) {
1263
+ continue;
1264
+ }
1265
+ return result;
1266
+ }
1267
+ if (syntaxes[i].isRepeatableAtLeastOnce || syntaxes[i].isRepeatable) {
1268
+ result = matchRepeatableSyntax(syntaxes[i], context.slice(), options);
1269
+ if (result.success) {
1270
+ if (result.context.done()) {
1271
+ context.end();
1272
+ return { ...result, syntaxToken: syntaxes[i + 1] };
1273
+ }
1274
+ context.update(result.context.current());
1275
+ continue;
1276
+ }
1277
+ if (isOptional) {
1278
+ continue;
1279
+ }
1280
+ return result;
1281
+ }
1282
+ if (syntaxes[i].isList) {
1283
+ result = matchListSyntax(syntaxes[i], context.slice(), options);
1284
+ if (result.success) {
1285
+ options.visited.get(token).delete(syntaxes[i]);
1286
+ if (result.context.done()) {
1287
+ context.end();
1288
+ return {
1289
+ ...result,
1290
+ success: true,
1291
+ valid: true,
1292
+ context,
1293
+ token: null,
1294
+ syntaxToken: syntaxes[i + 1],
1295
+ };
1296
+ }
1297
+ context.update(result.context.current());
1298
+ let k = i + 1;
1299
+ for (; k < syntaxes.length; k++) {
1300
+ if (syntaxes[k].typ !== ValidationTokenEnum.Whitespace) {
1301
+ break;
1302
+ }
1303
+ if (syntaxes[k]?.typ === ValidationTokenEnum.Comma) {
1304
+ i = k;
1305
+ }
1306
+ }
1307
+ continue;
1308
+ }
1309
+ if (isOptional) {
1310
+ // eat the next ','
1311
+ if (syntaxes[i + 1]?.typ === ValidationTokenEnum.Whitespace ||
1312
+ syntaxes[i + 1]?.typ === ValidationTokenEnum.Comma) {
1313
+ i++;
1314
+ }
1315
+ continue;
1316
+ }
1317
+ return {
1318
+ success: false,
1319
+ valid: true,
1320
+ token,
1321
+ context,
1322
+ syntaxToken: syntaxes[i],
1323
+ errors: result.errors,
1324
+ };
1325
+ }
1326
+ if (syntaxes[i].match != null) {
1327
+ result = matchOccurenceSyntax(syntaxes[i], context.slice(), options);
1328
+ if (result.success) {
1329
+ options.visited.get(token).delete(syntaxes[i]);
1330
+ if (result.context.done()) {
1331
+ context.end();
1332
+ return {
1333
+ ...result,
1334
+ context,
1335
+ syntaxToken: syntaxes[++i],
1336
+ };
1337
+ }
1338
+ context.update(result.context.current());
1339
+ continue;
1340
+ }
1341
+ if (isOptional) {
1342
+ // eat the next ','
1343
+ if (syntaxes[i + 1]?.typ === ValidationTokenEnum.Whitespace) {
1344
+ i++;
1345
+ }
1346
+ if (syntaxes[i + 1]?.typ === ValidationTokenEnum.Comma) {
1347
+ i++;
1348
+ }
1349
+ continue;
1350
+ }
1351
+ return {
1352
+ success: false,
1353
+ valid: true,
1354
+ token,
1355
+ context,
1356
+ syntaxToken: syntaxes[i],
1357
+ errors: result.errors,
1358
+ };
1359
+ }
1360
+ if (!options.visited.has(token)) {
1361
+ options.visited.set(token, new Set());
1362
+ }
1363
+ if (options.visited.get(token).has(syntaxes[i])) {
1364
+ // cyclic definition
1365
+ return {
1366
+ success: false,
1367
+ valid: true,
1368
+ token,
1369
+ context,
1370
+ syntaxToken: syntaxes[i],
1371
+ errors: [],
1372
+ };
1373
+ }
1374
+ options.visited.get(token).add(syntaxes[i]);
1375
+ success = false;
1376
+ switch (syntaxes[i].typ) {
1377
+ case ValidationTokenEnum.Comma:
1378
+ if (token.typ == EnumToken.CommaTokenType) {
1379
+ success = true;
1380
+ options.visited.get(token).delete(syntaxes[i]);
1381
+ context.next();
1382
+ if (context.done()) {
1383
+ return {
1384
+ success: true,
1385
+ valid: true,
1386
+ token: null,
1387
+ context,
1388
+ syntaxToken: syntaxes[i + 1],
1389
+ errors: [],
1390
+ };
1391
+ }
1392
+ break;
1393
+ }
1394
+ if (isOptional) {
1395
+ break;
1396
+ }
1397
+ return {
1398
+ success: false,
1399
+ valid: true,
1400
+ token,
1401
+ context,
1402
+ syntaxToken: syntaxes[i],
1403
+ errors: [],
1404
+ };
1405
+ case ValidationTokenEnum.Colon:
1406
+ if (token.typ == EnumToken.ColonTokenType) {
1407
+ success = true;
1408
+ options.visited.get(token).delete(syntaxes[i]);
1409
+ context.next();
1410
+ if (context.done()) {
1411
+ return {
1412
+ success: true,
1413
+ valid: true,
1414
+ token: null,
1415
+ context,
1416
+ syntaxToken: syntaxes[i + 1],
1417
+ errors: [],
1418
+ };
1419
+ }
1420
+ break;
1421
+ }
1422
+ if (isOptional) {
1423
+ break;
1424
+ }
1425
+ return {
1426
+ success: false,
1427
+ valid: true,
1428
+ token,
1429
+ context,
1430
+ syntaxToken: syntaxes[i],
1431
+ errors: [],
1432
+ };
1433
+ case ValidationTokenEnum.Keyword:
1434
+ if (token.typ == EnumToken.IdenTokenType &&
1435
+ token.val.toLowerCase() ===
1436
+ syntaxes[i].val.toLowerCase()) {
1437
+ success = true;
1438
+ options.visited.get(token).delete(syntaxes[i]);
1439
+ context.next();
1440
+ break;
1441
+ }
1442
+ if (isOptional) {
1443
+ break;
1444
+ }
1445
+ return {
1446
+ success: false,
1447
+ valid: true,
1448
+ token,
1449
+ context,
1450
+ syntaxToken: syntaxes[i],
1451
+ errors: [],
1452
+ };
1453
+ case ValidationTokenEnum.DeclarationType:
1454
+ result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, syntaxes[i].val), context, options);
1455
+ if (result.success) {
1456
+ success = true;
1457
+ options.visited.get(token).delete(syntaxes[i]);
1458
+ if (result.context.done()) {
1459
+ context.end();
1460
+ return {
1461
+ ...result,
1462
+ token,
1463
+ context,
1464
+ syntaxToken: syntaxes[1 + i],
1465
+ };
1466
+ }
1467
+ else {
1468
+ if (result.context.current() != null) {
1469
+ context.update(result.context.current());
1470
+ }
1471
+ }
1472
+ }
1473
+ if (success || isOptional) {
1474
+ break;
1475
+ }
1476
+ return {
1477
+ success: false,
1478
+ valid: true,
1479
+ token,
1480
+ context,
1481
+ syntaxToken: syntaxes[i],
1482
+ errors: result.errors,
1483
+ };
1484
+ case ValidationTokenEnum.OptionalGroupToken: {
1485
+ //
1486
+ const tokens = context.split();
1487
+ let j = 0;
1488
+ for (; j < tokens.length; j++) {
1489
+ result = matchSyntax(syntaxes[i].chi, context.slice(), options);
1490
+ if (result.success) {
1491
+ context.update(tokens[j].at(-1));
1492
+ }
1493
+ break;
1494
+ }
1495
+ if (result?.context?.done?.()) {
1496
+ return result;
1497
+ }
1498
+ break;
1499
+ }
1500
+ case ValidationTokenEnum.AtRule:
1501
+ if (token.typ == EnumToken.AtRuleTokenType) {
1502
+ success = true;
1503
+ options.visited.get(token).delete(syntaxes[i]);
1504
+ context.next();
1505
+ break;
1506
+ }
1507
+ if (isOptional) {
1508
+ break;
1509
+ }
1510
+ return {
1511
+ success: false,
1512
+ valid: true,
1513
+ token,
1514
+ context,
1515
+ syntaxToken: syntaxes[i],
1516
+ errors: [],
1517
+ };
1518
+ case ValidationTokenEnum.StringToken:
1519
+ if (token.typ == EnumToken.StringTokenType || token.typ == EnumToken.UrlFunctionTokenType) {
1520
+ success = true;
1521
+ }
1522
+ else if (token.typ === EnumToken.StringTokenType) {
1523
+ success = token.val === syntaxes[i].val.slice(1, -1);
1524
+ }
1525
+ else if (token.typ === EnumToken.Add) {
1526
+ success = syntaxes[i].val.slice(1, -1) === "+";
1527
+ }
1528
+ else if (token.typ === EnumToken.Mul) {
1529
+ success = syntaxes[i].val.slice(1, -1) === "*";
1530
+ }
1531
+ else if (token.typ === EnumToken.Div) {
1532
+ success = syntaxes[i].val.slice(1, -1) === "/";
1533
+ }
1534
+ else if (token.typ === EnumToken.Sub) {
1535
+ success = syntaxes[i].val.slice(1, -1) === "-";
1536
+ }
1537
+ else if (token.typ === EnumToken.LiteralTokenType) {
1538
+ success = token.val === syntaxes[i].val.slice(1, -1);
1539
+ }
1540
+ else if (token.typ == EnumToken.Plus) {
1541
+ success = true;
1542
+ }
1543
+ if (success) {
1544
+ context.next();
1545
+ break;
1546
+ }
1547
+ if (isOptional) {
1548
+ break;
1549
+ }
1550
+ return {
1551
+ success: false,
1552
+ valid: true,
1553
+ token,
1554
+ context,
1555
+ syntaxToken: syntaxes[i],
1556
+ errors: [],
1557
+ };
1558
+ case ValidationTokenEnum.SemiColon:
1559
+ if (token.typ == EnumToken.SemiColonTokenType) {
1560
+ success = true;
1561
+ options.visited.get(token).delete(syntaxes[i]);
1562
+ context.next();
1563
+ break;
1564
+ }
1565
+ if (isOptional) {
1566
+ break;
1567
+ }
1568
+ return {
1569
+ success: false,
1570
+ valid: true,
1571
+ token,
1572
+ context,
1573
+ syntaxToken: syntaxes[i],
1574
+ errors: [],
1575
+ };
1576
+ case ValidationTokenEnum.PropertyType:
1577
+ result = matchProperty(syntaxes[i], context, options);
1578
+ if (!result.success) {
1579
+ if (isOptional) {
1580
+ break;
1581
+ }
1582
+ return {
1583
+ success: false,
1584
+ valid: true,
1585
+ token: context.current() ?? token,
1586
+ context,
1587
+ syntaxToken: syntaxes[i],
1588
+ errors: [],
1589
+ };
1590
+ }
1591
+ success = true;
1592
+ options.visited.get(token).delete(syntaxes[i]);
1593
+ if (result.context.done()) {
1594
+ context.end();
1595
+ return {
1596
+ success,
1597
+ valid: true,
1598
+ token: null,
1599
+ context,
1600
+ syntaxToken: syntaxes[i + 1],
1601
+ errors: [],
1602
+ };
1603
+ }
1604
+ else {
1605
+ context.update(result.context.current());
1606
+ }
1607
+ break;
1608
+ case ValidationTokenEnum.PipeToken:
1609
+ {
1610
+ result = null;
1611
+ const results = [];
1612
+ let tmp = null;
1613
+ for (const syntax of syntaxes[i].chi) {
1614
+ tmp = matchSyntax(syntax, context.slice(), options);
1615
+ if (tmp.success) {
1616
+ results.push(tmp);
1617
+ if (tmp.context.done()) {
1618
+ context.end();
1619
+ return { ...tmp, context, syntaxToken: syntaxes[i + 1] };
1620
+ }
1621
+ }
1622
+ }
1623
+ if (results.length > 0) {
1624
+ result = results.reduce((a, b) => (a.context.index > b.context.index ? a : b));
1625
+ }
1626
+ if (result?.success) {
1627
+ success = true;
1628
+ options.visited.get(token).delete(syntaxes[i]);
1629
+ if (result.context.done()) {
1630
+ context.end();
1631
+ return {
1632
+ success: true,
1633
+ valid: true,
1634
+ token: null,
1635
+ context,
1636
+ syntaxToken: syntaxes[i + 1],
1637
+ errors: [],
1638
+ };
1639
+ }
1640
+ else {
1641
+ context.update(result.context.current());
1642
+ }
1643
+ break;
1644
+ }
1645
+ }
1646
+ if (isOptional) {
1647
+ break;
1648
+ }
1649
+ return {
1650
+ success: false,
1651
+ valid: true,
1652
+ token,
1653
+ context,
1654
+ syntaxToken: syntaxes[i],
1655
+ errors: [
1656
+ {
1657
+ action: "drop",
1658
+ message: "could not match syntax",
1659
+ node: token,
1660
+ location: token.loc,
1661
+ syntax: syntaxes[i],
1662
+ },
1663
+ ],
1664
+ };
1665
+ case ValidationTokenEnum.Bracket:
1666
+ result = matchSyntax(syntaxes[i].chi, context.slice(), options);
1667
+ if (result.success) {
1668
+ success = true;
1669
+ options.visited.get(token).delete(syntaxes[i]);
1670
+ if (result.context.done()) {
1671
+ context.end();
1672
+ return {
1673
+ success,
1674
+ valid: true,
1675
+ token: null,
1676
+ context,
1677
+ syntaxToken: syntaxes[1 + i],
1678
+ errors: [],
1679
+ };
1680
+ }
1681
+ else {
1682
+ context.update(result.context.current());
1683
+ }
1684
+ break;
1685
+ }
1686
+ if (isOptional) {
1687
+ break;
1688
+ }
1689
+ return {
1690
+ success: false,
1691
+ valid: true,
1692
+ token,
1693
+ context,
1694
+ syntaxToken: syntaxes[i],
1695
+ errors: [],
1696
+ };
1697
+ case ValidationTokenEnum.ColumnToken:
1698
+ result = matchColumnSyntax(syntaxes[i], context.slice(), options);
1699
+ if (result.success) {
1700
+ success = true;
1701
+ options.visited.get(token).delete(syntaxes[i]);
1702
+ if (result.context.done()) {
1703
+ context.end();
1704
+ return {
1705
+ success,
1706
+ valid: true,
1707
+ token: null,
1708
+ context,
1709
+ syntaxToken: syntaxes[1 + i],
1710
+ errors: [],
1711
+ };
1712
+ }
1713
+ else {
1714
+ context.update(result.context.current());
1715
+ }
1716
+ break;
1717
+ }
1718
+ if (isOptional) {
1719
+ break;
1720
+ }
1721
+ return {
1722
+ success: false,
1723
+ valid: true,
1724
+ token,
1725
+ context,
1726
+ syntaxToken: syntaxes[i],
1727
+ errors: [],
1728
+ };
1729
+ case ValidationTokenEnum.AmpersandToken:
1730
+ result = matchAmpersandSyntax(syntaxes[i], context.slice(), options);
1731
+ if (result.success) {
1732
+ success = true;
1733
+ options.visited.get(token).delete(syntaxes[i]);
1734
+ if (result.context.done()) {
1735
+ context.end();
1736
+ return {
1737
+ success,
1738
+ valid: true,
1739
+ token: null,
1740
+ context,
1741
+ syntaxToken: syntaxes[1 + i],
1742
+ errors: [],
1743
+ };
1744
+ }
1745
+ else {
1746
+ context.update(result.context.current());
1747
+ }
1748
+ break;
1749
+ }
1750
+ if (isOptional) {
1751
+ break;
1752
+ }
1753
+ return {
1754
+ success: false,
1755
+ valid: true,
1756
+ token,
1757
+ context,
1758
+ syntaxToken: syntaxes[i],
1759
+ errors: [],
1760
+ };
1761
+ case ValidationTokenEnum.PseudoClassFunctionToken:
1762
+ if (!(token.val + "()" in config.selectors)) {
1763
+ return {
1764
+ success: false,
1765
+ valid: true,
1766
+ token,
1767
+ context,
1768
+ syntaxToken: syntaxes[i],
1769
+ errors: [
1770
+ {
1771
+ action: "drop",
1772
+ message: `Unknown pseudo-class selector ${token.val}()`,
1773
+ node: token,
1774
+ },
1775
+ ],
1776
+ };
1777
+ }
1778
+ result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, token.val + "()")?.[0]?.chi ?? [], createValidationContext(token.chi), options);
1779
+ if (result.success) {
1780
+ success = true;
1781
+ options.visited.get(token).delete(syntaxes[i]);
1782
+ context.next();
1783
+ break;
1784
+ }
1785
+ return {
1786
+ success: false,
1787
+ valid: true,
1788
+ token,
1789
+ context,
1790
+ syntaxToken: syntaxes[i],
1791
+ errors: result.errors,
1792
+ };
1793
+ case ValidationTokenEnum.Function:
1794
+ if ((!tokensfuncDefMap.has(token.typ) &&
1795
+ !funcLike.includes(token.typ) &&
1796
+ !funcTypes.includes(token.typ)) ||
1797
+ !("val" in token) ||
1798
+ !equalsIgnoreCase(syntaxes[i].val, token.val.toLowerCase())) {
1799
+ return {
1800
+ success: false,
1801
+ valid: true,
1802
+ token,
1803
+ context,
1804
+ syntaxToken: syntaxes[i],
1805
+ errors: [
1806
+ {
1807
+ action: "drop",
1808
+ message: `Expecting a function ${EnumToken[token.typ]}`,
1809
+ node: token,
1810
+ },
1811
+ ],
1812
+ };
1813
+ }
1814
+ if (tokensfuncDefMap.has(token.typ)) {
1815
+ const range = context.peekRange();
1816
+ if (range.at(-1)?.typ !== EnumToken.EndParensTokenType) {
1817
+ const tk = range.at(-1);
1818
+ return {
1819
+ success: false,
1820
+ valid: true,
1821
+ token,
1822
+ context,
1823
+ syntaxToken: syntaxes[i],
1824
+ errors: [
1825
+ {
1826
+ action: "drop",
1827
+ message: `Expecting ')' at ${tk.loc?.src}:${tk.loc?.sta?.lin}:${tk.loc?.sta?.col}`,
1828
+ node: token,
1829
+ },
1830
+ ],
1831
+ };
1832
+ }
1833
+ result = matchSyntax(syntaxes[i].chi, createValidationContext(range.slice(1, -1)), options);
1834
+ if (result.success && result.context.done()) {
1835
+ context.update(range.at(-1));
1836
+ options.visited.get(token).delete(syntaxes[i]);
1837
+ break;
1838
+ }
1839
+ }
1840
+ else {
1841
+ result = matchSyntax(syntaxes[i].chi, createValidationContext(token.chi), options);
1842
+ if (result.success && result.context.done()) {
1843
+ success = true;
1844
+ options.visited.get(token).delete(syntaxes[i]);
1845
+ context.next();
1846
+ break;
1847
+ }
1848
+ }
1849
+ if (isOptional) {
1850
+ break;
1851
+ }
1852
+ return {
1853
+ success: false,
1854
+ valid: true,
1855
+ token,
1856
+ context,
1857
+ syntaxToken: syntaxes[i],
1858
+ errors: result.errors,
1859
+ };
1860
+ case ValidationTokenEnum.LessThan:
1861
+ if (token.typ === EnumToken.LtTokenType) {
1862
+ options.visited.get(token).delete(syntaxes[i]);
1863
+ context.next();
1864
+ break;
1865
+ }
1866
+ return {
1867
+ success: false,
1868
+ valid: true,
1869
+ token,
1870
+ context,
1871
+ syntaxToken: syntaxes[i],
1872
+ errors: [],
1873
+ };
1874
+ case ValidationTokenEnum.Separator:
1875
+ if (token.typ === EnumToken.LiteralTokenType && token.val === "/") {
1876
+ options.visited.get(token).delete(syntaxes[i]);
1877
+ context.next();
1878
+ break;
1879
+ }
1880
+ return {
1881
+ success: false,
1882
+ valid: true,
1883
+ token,
1884
+ context,
1885
+ syntaxToken: syntaxes[i],
1886
+ errors: [],
1887
+ };
1888
+ case ValidationTokenEnum.FunctionDefinition:
1889
+ if (equalsIgnoreCase(token.val, syntaxes[i].val)) {
1890
+ if (tokensfuncDefMap.has(token.typ)) {
1891
+ const children = trimArray(context.peekRange());
1892
+ result = matchSyntax((getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, syntaxes[i].val + "()")?.[0]).chi ?? [], createValidationContext(children.slice(1, -1)), options);
1893
+ if (result.success && result.context.done()) {
1894
+ success = true;
1895
+ options.visited.get(token).delete(syntaxes[i]);
1896
+ context.update(children.at(-1));
1897
+ break;
1898
+ }
1899
+ }
1900
+ else if (tokensfuncSet.has(token.typ)) {
1901
+ result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, syntaxes[i].val + "()") ?? [], createValidationContext([token]), options);
1902
+ if (result.success && result.context.done()) {
1903
+ success = true;
1904
+ options.visited.get(token).delete(syntaxes[i]);
1905
+ context.next();
1906
+ break;
1907
+ }
1908
+ }
1909
+ }
1910
+ return {
1911
+ success: false,
1912
+ valid: true,
1913
+ token,
1914
+ context,
1915
+ syntaxToken: syntaxes[i],
1916
+ errors: [],
1917
+ };
1918
+ case ValidationTokenEnum.OpenParenthesis:
1919
+ if (token.typ === EnumToken.StartParensTokenType) {
1920
+ options.visited.get(token).delete(syntaxes[i]);
1921
+ context.next();
1922
+ break;
1923
+ }
1924
+ if (isOptional) {
1925
+ break;
1926
+ }
1927
+ return {
1928
+ success: false,
1929
+ valid: true,
1930
+ token,
1931
+ context,
1932
+ syntaxToken: syntaxes[i],
1933
+ errors: [],
1934
+ };
1935
+ case ValidationTokenEnum.CloseParenthesis:
1936
+ if (token.typ === EnumToken.EndParensTokenType) {
1937
+ options.visited.get(token).delete(syntaxes[i]);
1938
+ context.next();
1939
+ break;
1940
+ }
1941
+ if (isOptional) {
1942
+ break;
1943
+ }
1944
+ return {
1945
+ success: false,
1946
+ valid: true,
1947
+ token,
1948
+ context,
1949
+ syntaxToken: syntaxes[i],
1950
+ errors: [],
1951
+ };
1952
+ case ValidationTokenEnum.DeclarationNameToken:
1953
+ result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, syntaxes[i].val) ?? [], context.slice(), options);
1954
+ if (!result.success) {
1955
+ return {
1956
+ success: false,
1957
+ valid: true,
1958
+ token,
1959
+ context,
1960
+ syntaxToken: syntaxes[i],
1961
+ errors: result.errors,
1962
+ };
1963
+ }
1964
+ success = true;
1965
+ options.visited.get(token).delete(syntaxes[i]);
1966
+ if (result.context.done()) {
1967
+ context.end();
1968
+ return {
1969
+ success,
1970
+ valid: true,
1971
+ token: null,
1972
+ context,
1973
+ syntaxToken: syntaxes[1 + i],
1974
+ errors: [],
1975
+ };
1976
+ }
1977
+ else {
1978
+ context.update(result.context.current());
1979
+ }
1980
+ break;
1981
+ case ValidationTokenEnum.DisallowWhitespace:
1982
+ if (context.peek(0, false)?.typ === EnumToken.WhitespaceTokenType) {
1983
+ return {
1984
+ success: false,
1985
+ valid: true,
1986
+ token,
1987
+ context,
1988
+ syntaxToken: syntaxes[i],
1989
+ errors: [],
1990
+ };
1991
+ }
1992
+ break;
1993
+ default:
1994
+ throw new Error(`Unexpected syntax ${ValidationTokenEnum[syntaxes[i].typ]}`);
1995
+ }
1996
+ if (success &&
1997
+ "range" in syntaxes[i] &&
1998
+ !(token?.typ === EnumToken.MathFunctionTokenType ||
1999
+ token?.typ === EnumToken.MathFunctionTokenDefType ||
2000
+ token?.typ === EnumToken.WildCardFunctionTokenDefType)) {
2001
+ let success = false;
2002
+ success =
2003
+ typeof token.val === "number" &&
2004
+ token.val >=
2005
+ syntaxes[i].range.min.val;
2006
+ if (syntaxes[i].range.min.typ === ValidationTokenEnum.Dimension) {
2007
+ success =
2008
+ token.typ ===
2009
+ EnumToken[
2010
+ // @ts-expect-error
2011
+ syntaxes[i].range.min
2012
+ .unit];
2013
+ }
2014
+ if (success &&
2015
+ syntaxes[i].range.max != null) {
2016
+ success =
2017
+ token.val <=
2018
+ syntaxes[i].range.max.val;
2019
+ }
2020
+ if (!success) {
2021
+ return {
2022
+ success: false,
2023
+ valid: true,
2024
+ token,
2025
+ context,
2026
+ syntaxToken: syntaxes[i],
2027
+ errors: [],
2028
+ };
2029
+ }
2030
+ }
2031
+ }
2032
+ return result?.success
2033
+ ? result
2034
+ : {
2035
+ success: true,
2036
+ context,
2037
+ syntaxToken: syntaxes[i] ?? null,
2038
+ token,
2039
+ valid: true,
2040
+ errors: [],
2041
+ };
2042
+ }
2043
+ function matchColumnSyntax(syntax, context, options) {
2044
+ let syntaxes = syntax.chi.slice();
2045
+ let i = 0;
2046
+ let success = false;
2047
+ let result;
2048
+ syntaxes = syntaxes.slice();
2049
+ for (; i < syntaxes.length; i++) {
2050
+ result = matchSyntax(syntaxes[i], context.slice(), options);
2051
+ if (result.success) {
2052
+ const curr = result.context.current();
2053
+ if (curr == null && !result.context.done()) {
2054
+ continue;
2055
+ }
2056
+ if (result.context.done()) {
2057
+ context.end();
2058
+ return {
2059
+ ...result,
2060
+ context,
2061
+ syntaxToken: syntaxes[i + 1]?.[0],
2062
+ };
2063
+ }
2064
+ context.update(curr);
2065
+ success = true;
2066
+ syntaxes.splice(i, 1);
2067
+ i = -1;
2068
+ }
2069
+ }
2070
+ return {
2071
+ success,
2072
+ valid: true,
2073
+ token: context.current(),
2074
+ context,
2075
+ syntaxToken: syntax,
2076
+ errors: [],
2077
+ };
2078
+ }
2079
+ function matchAmpersandSyntax(syntax, context, options) {
2080
+ const syntaxes = [syntax.l, syntax.r];
2081
+ let result;
2082
+ let i;
2083
+ let success = false;
2084
+ for (i = 0; i < syntaxes.length; i++) {
2085
+ result = matchSyntax(syntaxes[i], context.slice(), options);
2086
+ if (result.success) {
2087
+ success = true;
2088
+ if (result.context.done()) {
2089
+ context.end();
2090
+ break;
2091
+ }
2092
+ context.update(result.context.current());
2093
+ syntaxes.splice(i, 1);
2094
+ i = -1;
2095
+ }
2096
+ }
2097
+ return (
2098
+ /* syntaxes.length === 0 ? */ result ?? {
2099
+ success,
2100
+ valid: true,
2101
+ token: context.peek(),
2102
+ context,
2103
+ syntaxToken: syntax,
2104
+ errors: [],
2105
+ });
2106
+ }
2107
+ function matchProperty(property, context, options) {
2108
+ let success = false;
2109
+ let t = context.peek()?.typ;
2110
+ let checkCalc = (t == EnumToken.MathFunctionTokenDefType || t == EnumToken.MathFunctionTokenType) &&
2111
+ [
2112
+ "number",
2113
+ "zero",
2114
+ "integer",
2115
+ "percentage",
2116
+ "dimension",
2117
+ "length-percentage",
2118
+ "length",
2119
+ "time",
2120
+ "calc-sum",
2121
+ "calc-product",
2122
+ ].includes(property.val);
2123
+ if (checkCalc && !["number", "zero", "integer", "percentage", "length-percentage"].includes(property.val)) {
2124
+ checkCalc = context.peek().val === "calc";
2125
+ }
2126
+ if (checkCalc) {
2127
+ let result;
2128
+ const syntax = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, context.peek().val + "()")?.[0]?.chi;
2129
+ if (t === EnumToken.MathFunctionTokenType) {
2130
+ result = matchSyntax(syntax, createValidationContext(context.peek().chi), options);
2131
+ if (result.success && result.context.done()) {
2132
+ context.next();
2133
+ return {
2134
+ success: true,
2135
+ valid: true,
2136
+ token: context.peek(),
2137
+ context,
2138
+ syntaxToken: null,
2139
+ errors: [],
2140
+ };
2141
+ }
2142
+ }
2143
+ else {
2144
+ const range = context.peekRange();
2145
+ result = matchSyntax(syntax, createValidationContext(range.slice(1, -1)), options);
2146
+ if (result.success) {
2147
+ context.update(range.at(-1));
2148
+ return {
2149
+ success: true,
2150
+ valid: true,
2151
+ token: range.at(-1),
2152
+ context,
2153
+ syntaxToken: null,
2154
+ errors: [],
2155
+ };
2156
+ }
2157
+ }
2158
+ return {
2159
+ success: false,
2160
+ valid: true,
2161
+ token: context.peek(),
2162
+ context,
2163
+ syntaxToken: property,
2164
+ errors: [],
2165
+ };
2166
+ }
2167
+ switch (property.val) {
2168
+ case "combinator":
2169
+ {
2170
+ const token = context.peek();
2171
+ success =
2172
+ token != null &&
2173
+ (token.typ == EnumToken.NextSiblingCombinatorTokenType ||
2174
+ token.typ == EnumToken.ChildCombinatorTokenType ||
2175
+ token.typ == EnumToken.SubsequentSiblingCombinatorTokenType ||
2176
+ token.typ == EnumToken.ColumnCombinatorTokenType);
2177
+ }
2178
+ break;
2179
+ case "declaration-value":
2180
+ {
2181
+ // consume a declaration value
2182
+ // consume a token,
2183
+ // if the next token is a comma consume it and expect a token
2184
+ // check the parenthises are balanced
2185
+ // if semicolon or end curly brace is encountered, stop
2186
+ let expectToken = true;
2187
+ let token;
2188
+ const stack = [];
2189
+ token = context.peek();
2190
+ if (token?.typ == EnumToken.WhitespaceTokenType) {
2191
+ context.next();
2192
+ token = context.peek();
2193
+ }
2194
+ if (token?.typ == EnumToken.CommentTokenType) {
2195
+ context.next();
2196
+ token = context.peek();
2197
+ }
2198
+ if (token?.typ == EnumToken.CommaTokenType) {
2199
+ context.next();
2200
+ expectToken = true;
2201
+ }
2202
+ while ((token = context.peek()) != null) {
2203
+ if (token.typ == EnumToken.StartParensTokenType || tokensfuncDefMap.has(token?.typ)) {
2204
+ stack.push(token);
2205
+ }
2206
+ if (token.typ == EnumToken.WhitespaceTokenType || token.typ == EnumToken.CommentTokenType) {
2207
+ context.next();
2208
+ }
2209
+ if (expectToken) {
2210
+ if (token?.typ == EnumToken.CommaTokenType || token.typ == EnumToken.EndParensTokenType) {
2211
+ return {
2212
+ success: false,
2213
+ valid: true,
2214
+ token,
2215
+ context,
2216
+ syntaxToken: property,
2217
+ errors: [
2218
+ {
2219
+ action: "drop",
2220
+ message: `Unexpected token ${EnumToken[token?.typ]} at ${token.loc?.src}:${token.loc?.sta?.lin}:${token.loc?.sta?.col}`,
2221
+ node: token,
2222
+ location: token.loc,
2223
+ },
2224
+ ],
2225
+ };
2226
+ }
2227
+ expectToken = false;
2228
+ }
2229
+ if (token.typ == EnumToken.CommaTokenType) {
2230
+ expectToken = true;
2231
+ }
2232
+ if (token.typ == EnumToken.EndParensTokenType) {
2233
+ if (stack.length > 0) {
2234
+ stack.pop();
2235
+ }
2236
+ else {
2237
+ return {
2238
+ success: false,
2239
+ valid: true,
2240
+ token,
2241
+ context,
2242
+ syntaxToken: property,
2243
+ errors: [
2244
+ {
2245
+ action: "drop",
2246
+ message: `Unexpected token ${EnumToken[token?.typ]} at ${token.loc?.src}:${token.loc?.sta?.lin}:${token.loc?.sta?.col}`,
2247
+ node: token,
2248
+ location: token.loc,
2249
+ },
2250
+ ],
2251
+ };
2252
+ }
2253
+ }
2254
+ context.next();
2255
+ }
2256
+ if (stack.length > 0) {
2257
+ token = stack.at(-1);
2258
+ return {
2259
+ success: false,
2260
+ valid: true,
2261
+ token,
2262
+ context,
2263
+ syntaxToken: property,
2264
+ errors: [
2265
+ {
2266
+ action: "drop",
2267
+ message: `unbalanced parentheses ${EnumToken[token?.typ]} at ${token.loc?.src}:${token.loc?.sta?.lin}:${token.loc?.sta?.col}`,
2268
+ node: token,
2269
+ location: token.loc,
2270
+ },
2271
+ ],
2272
+ };
2273
+ }
2274
+ success = !expectToken;
2275
+ if (!success) {
2276
+ return {
2277
+ success: false,
2278
+ valid: true,
2279
+ token: token ?? context.current(),
2280
+ context,
2281
+ syntaxToken: property,
2282
+ errors: [],
2283
+ };
2284
+ }
2285
+ }
2286
+ break;
2287
+ case "calc-sum": {
2288
+ const token = context.peek();
2289
+ if (token?.typ === EnumToken.ParensTokenType) {
2290
+ const result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "calc()")[0].chi, createValidationContext(token.chi), options);
2291
+ success = result.success && result.context.done();
2292
+ break;
2293
+ }
2294
+ if (token?.typ === EnumToken.BinaryExpressionTokenType) {
2295
+ let result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "calc-sum"), createValidationContext([token.l]), options);
2296
+ if (result.success) {
2297
+ result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "calc-sum"), createValidationContext([token.r]), options);
2298
+ }
2299
+ success = result.success;
2300
+ break;
2301
+ }
2302
+ const result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "calc-sum"), context.slice(), options);
2303
+ success = result.success;
2304
+ if (success) {
2305
+ if (result.context.done()) {
2306
+ context.end();
2307
+ }
2308
+ else {
2309
+ context.update(result.context.current());
2310
+ }
2311
+ return {
2312
+ success: true,
2313
+ valid: true,
2314
+ token: null,
2315
+ context,
2316
+ syntaxToken: null,
2317
+ errors: [],
2318
+ };
2319
+ }
2320
+ break;
2321
+ }
2322
+ case "at-keyword-token":
2323
+ success = context.peek()?.typ === EnumToken.AtRuleTokenType;
2324
+ break;
2325
+ case "an+b":
2326
+ {
2327
+ let token = context.peek();
2328
+ if (token == null) {
2329
+ return {
2330
+ success: false,
2331
+ valid: true,
2332
+ token: null,
2333
+ context,
2334
+ syntaxToken: property,
2335
+ errors: [],
2336
+ };
2337
+ }
2338
+ if (token.typ === EnumToken.IdenTokenType &&
2339
+ ("even" === token.val || "odd" === token.val)) {
2340
+ success = true;
2341
+ break;
2342
+ }
2343
+ if (token.typ === EnumToken.NumberTokenType &&
2344
+ Number.isInteger(token.val) &&
2345
+ !token.toString().includes(".")) {
2346
+ success = true;
2347
+ break;
2348
+ }
2349
+ if (token.typ === EnumToken.LiteralTokenType || token.typ === EnumToken.IdenTokenType) {
2350
+ let match = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(token.val);
2351
+ success = match != null;
2352
+ if (!success) {
2353
+ success = /^(([+-]?[0-9]*)?n)?([+-])?$/.test(token.val);
2354
+ if (success) {
2355
+ token = context.peek(1);
2356
+ success =
2357
+ token?.typ === EnumToken.NumberTokenType &&
2358
+ Number.isInteger(token.val) &&
2359
+ !token.toString().includes(".") &&
2360
+ token.sign == null;
2361
+ context.next();
2362
+ }
2363
+ }
2364
+ else if (match?.[3] == null) {
2365
+ token = context.peek(1);
2366
+ if (token?.typ === EnumToken.Add ||
2367
+ token?.typ === EnumToken.NextSiblingCombinatorTokenType ||
2368
+ token?.typ === EnumToken.Sub) {
2369
+ token = context.peek(2);
2370
+ success =
2371
+ token?.typ === EnumToken.NumberTokenType &&
2372
+ Number.isInteger(token.val) &&
2373
+ !token.toString().includes(".") &&
2374
+ token.sign == null;
2375
+ }
2376
+ else if (token?.typ === EnumToken.NumberTokenType) {
2377
+ success =
2378
+ Number.isInteger(token.val) &&
2379
+ !token.toString().includes(".") &&
2380
+ token.sign != null;
2381
+ }
2382
+ if (success) {
2383
+ context.update(token);
2384
+ return {
2385
+ success: true,
2386
+ valid: true,
2387
+ token: null,
2388
+ context,
2389
+ syntaxToken: property,
2390
+ errors: [],
2391
+ };
2392
+ }
2393
+ }
2394
+ break;
2395
+ }
2396
+ if (token.typ === EnumToken.DimensionTokenType && token.unit === "n") {
2397
+ success =
2398
+ Number.isInteger(token.val) &&
2399
+ !token.toString().includes(".");
2400
+ if (!success) {
2401
+ return {
2402
+ success: false,
2403
+ valid: true,
2404
+ token: null,
2405
+ context,
2406
+ syntaxToken: property,
2407
+ errors: [
2408
+ {
2409
+ action: "drop",
2410
+ message: `expecting <dimension-token>`,
2411
+ node: token,
2412
+ location: token.loc,
2413
+ },
2414
+ ],
2415
+ };
2416
+ }
2417
+ if (token == null) {
2418
+ break;
2419
+ }
2420
+ token = context.peek(1);
2421
+ if (token == null) {
2422
+ break;
2423
+ }
2424
+ if (token.typ === EnumToken.NumberTokenType) {
2425
+ if (Number.isInteger(token.val) &&
2426
+ !token.toString().includes(".") &&
2427
+ ["+", "-"].includes(token.sign)) {
2428
+ success = true;
2429
+ context.next();
2430
+ }
2431
+ break;
2432
+ }
2433
+ if (token?.typ === EnumToken.NextSiblingCombinatorTokenType) {
2434
+ Object.assign(token, { typ: EnumToken.Add });
2435
+ }
2436
+ token = context.peek(2);
2437
+ if (token == null) {
2438
+ break;
2439
+ }
2440
+ if (token?.typ === EnumToken.NumberTokenType &&
2441
+ Number.isInteger(token.val) &&
2442
+ !token.toString().includes(".") &&
2443
+ token.sign == null) {
2444
+ success = true;
2445
+ context.next();
2446
+ context.next();
2447
+ }
2448
+ break;
2449
+ }
2450
+ }
2451
+ break;
2452
+ case "number-token":
2453
+ {
2454
+ const token = context.peek();
2455
+ success =
2456
+ token?.typ === EnumToken.NumberTokenType &&
2457
+ !token.val.toString().includes(".") &&
2458
+ Number.isInteger(token.val);
2459
+ }
2460
+ break;
2461
+ case "ndashdigit-ident":
2462
+ {
2463
+ const token = context.peek();
2464
+ success =
2465
+ (token?.typ === EnumToken.IdenTokenType || token?.typ === EnumToken.LiteralTokenType) &&
2466
+ /^-?n[-+]\d+$/.test(token.val);
2467
+ }
2468
+ break;
2469
+ case "dimension-token":
2470
+ {
2471
+ const token = context.peek();
2472
+ success =
2473
+ token?.typ === EnumToken.DimensionTokenType &&
2474
+ token.unit === "n" &&
2475
+ !token.val.toString().includes(".") &&
2476
+ Number.isInteger(token.val);
2477
+ }
2478
+ break;
2479
+ case "quote":
2480
+ case "display-box":
2481
+ case "display-inside":
2482
+ case "display-internal":
2483
+ case "display-outside":
2484
+ case "display-legacy":
2485
+ case "content-position":
2486
+ success =
2487
+ context.peek()?.typ == EnumToken.IdenTokenType &&
2488
+ // @ts-expect-error
2489
+ config.syntaxes[property.val].syntax
2490
+ .split(/[\s|]+/)
2491
+ .includes(context.peek().val.toLowerCase());
2492
+ break;
2493
+ case "mf-name":
2494
+ {
2495
+ const token = context.peek();
2496
+ success =
2497
+ token?.typ === EnumToken.IdenTokenType &&
2498
+ token.val.toLowerCase() in config.mediaFeatures;
2499
+ }
2500
+ break;
2501
+ case "mf-comparison":
2502
+ {
2503
+ const token = context.peek();
2504
+ success =
2505
+ token?.typ === EnumToken.LtTokenType ||
2506
+ token?.typ === EnumToken.LteTokenType ||
2507
+ token?.typ === EnumToken.GtTokenType ||
2508
+ token?.typ === EnumToken.GteTokenType ||
2509
+ token?.typ === EnumToken.DelimTokenType;
2510
+ }
2511
+ break;
2512
+ case "mf-gt":
2513
+ {
2514
+ const token = context.peek();
2515
+ success = token?.typ === EnumToken.GtTokenType || token?.typ === EnumToken.GteTokenType;
2516
+ }
2517
+ break;
2518
+ case "mf-lt":
2519
+ {
2520
+ const token = context.peek();
2521
+ success = token?.typ === EnumToken.LtTokenType || token?.typ === EnumToken.LteTokenType;
2522
+ }
2523
+ break;
2524
+ case "dashed-ident":
2525
+ case "extension-name":
2526
+ case "custom-property-name":
2527
+ success = context.peek()?.typ == EnumToken.DashedIdenTokenType;
2528
+ break;
2529
+ case "namespace-prefix":
2530
+ case "ident":
2531
+ case "ident-token":
2532
+ case "custom-ident":
2533
+ case "counter-name":
2534
+ case "counter-style-name":
2535
+ success =
2536
+ context.peek()?.typ == EnumToken.IdenTokenType || context.peek()?.typ == EnumToken.DashedIdenTokenType;
2537
+ if (success && context.peek()?.typ === EnumToken.IdenTokenType) {
2538
+ const val = context.peek().val.toLowerCase();
2539
+ success = "none" !== val && !allValues.includes(val);
2540
+ }
2541
+ break;
2542
+ case "any-value": {
2543
+ const token = context.peek();
2544
+ success =
2545
+ token == null ||
2546
+ [
2547
+ EnumToken.String,
2548
+ EnumToken.NumberTokenType,
2549
+ EnumToken.DashedIdenTokenType,
2550
+ EnumToken.IdenTokenType,
2551
+ EnumToken.DashedIdenTokenType,
2552
+ EnumToken.FunctionTokenType,
2553
+ EnumToken.ImageFunctionTokenType,
2554
+ EnumToken.UrlFunctionTokenType,
2555
+ EnumToken.TimingFunctionTokenType,
2556
+ EnumToken.TimelineFunctionTokenType,
2557
+ EnumToken.TransformFunctionTokenType,
2558
+ ].includes(token?.typ);
2559
+ break;
2560
+ }
2561
+ case "string": {
2562
+ const token = context.peek();
2563
+ if (token == null) {
2564
+ break;
2565
+ }
2566
+ success = token.typ === EnumToken.StringTokenType;
2567
+ break;
2568
+ }
2569
+ case "wq-name":
2570
+ // wq-name matches
2571
+ // a
2572
+ // |a
2573
+ // *|a
2574
+ // a|b
2575
+ if (context.peek()?.typ == EnumToken.Pipe) {
2576
+ const success = context.peek(1)?.typ == EnumToken.IdenTokenType;
2577
+ return {
2578
+ success,
2579
+ valid: true,
2580
+ token: success ? context.update(context.peek(1)).current() : context.peek(),
2581
+ context,
2582
+ syntaxToken: property,
2583
+ errors: [],
2584
+ };
2585
+ }
2586
+ else if (context.peek(1)?.typ == EnumToken.Pipe) {
2587
+ const success = context.peek()?.typ === EnumToken.Star && context.peek(2)?.typ === EnumToken.IdenTokenType;
2588
+ return {
2589
+ success,
2590
+ valid: true,
2591
+ token: success ? context.update(context.peek(2)).current() : context.peek(),
2592
+ context,
2593
+ syntaxToken: property,
2594
+ errors: [],
2595
+ };
2596
+ }
2597
+ else {
2598
+ const success = context.peek()?.typ == EnumToken.IdenTokenType;
2599
+ return {
2600
+ success,
2601
+ valid: true,
2602
+ token: success ? context.next() : context.peek(),
2603
+ context,
2604
+ syntaxToken: property,
2605
+ errors: [],
2606
+ };
2607
+ }
2608
+ case "id-selector":
2609
+ success = context.peek()?.typ == EnumToken.HashTokenType;
2610
+ break;
2611
+ case "class-selector":
2612
+ success = context.peek()?.typ == EnumToken.ClassSelectorTokenType;
2613
+ break;
2614
+ case "pseudo-class-selector":
2615
+ {
2616
+ const token = context.peek();
2617
+ success =
2618
+ token?.typ == EnumToken.PseudoClassTokenType ||
2619
+ token?.typ == EnumToken.PseudoClassFuncTokenType ||
2620
+ token?.typ == EnumToken.PseudoElementTokenType;
2621
+ }
2622
+ break;
2623
+ case "number":
2624
+ {
2625
+ const token = context.peek();
2626
+ if (token == null) {
2627
+ break;
2628
+ }
2629
+ success = token.typ == EnumToken.NumberTokenType;
2630
+ }
2631
+ break;
2632
+ case "zero": {
2633
+ const token = context.peek();
2634
+ success = token?.typ == EnumToken.NumberTokenType && token.val === 0;
2635
+ break;
2636
+ }
2637
+ case "integer":
2638
+ {
2639
+ const token = context.peek();
2640
+ success =
2641
+ token?.typ == EnumToken.NumberTokenType && Number.isInteger(Number(token.val));
2642
+ }
2643
+ break;
2644
+ case "dimension": {
2645
+ const token = context.peek();
2646
+ if (token == null) {
2647
+ break;
2648
+ }
2649
+ success =
2650
+ token?.typ == EnumToken.DimensionTokenType ||
2651
+ token?.typ == EnumToken.AngleTokenType ||
2652
+ token?.typ == EnumToken.LengthTokenType ||
2653
+ token?.typ == EnumToken.PercentageTokenType ||
2654
+ token?.typ == EnumToken.ResolutionTokenType ||
2655
+ token?.typ == EnumToken.TimeTokenType ||
2656
+ token?.typ == EnumToken.FrequencyTokenType ||
2657
+ token?.typ == EnumToken.FlexTokenType;
2658
+ break;
2659
+ }
2660
+ case "angle":
2661
+ {
2662
+ const token = context.peek();
2663
+ if (token == null) {
2664
+ break;
2665
+ }
2666
+ success = token.typ === EnumToken.AngleTokenType;
2667
+ }
2668
+ break;
2669
+ case "angle-percentage":
2670
+ {
2671
+ const token = context.peek();
2672
+ if (token == null) {
2673
+ break;
2674
+ }
2675
+ success = token.typ === EnumToken.AngleTokenType || token.typ === EnumToken.PercentageTokenType;
2676
+ }
2677
+ break;
2678
+ case "percentage":
2679
+ {
2680
+ const token = context.peek();
2681
+ if (token == null) {
2682
+ break;
2683
+ }
2684
+ success = token.typ === EnumToken.PercentageTokenType;
2685
+ }
2686
+ break;
2687
+ case "length-percentage":
2688
+ {
2689
+ const token = context.peek();
2690
+ if (token == null) {
2691
+ break;
2692
+ }
2693
+ success =
2694
+ token.typ == EnumToken.PercentageTokenType ||
2695
+ token.typ == EnumToken.LengthTokenType ||
2696
+ (token.typ == EnumToken.NumberTokenType && token.val === 0);
2697
+ }
2698
+ break;
2699
+ case "flex":
2700
+ success = context.peek()?.typ == EnumToken.FlexTokenType;
2701
+ break;
2702
+ case "length": {
2703
+ const token = context.peek();
2704
+ if (token == null) {
2705
+ break;
2706
+ }
2707
+ success =
2708
+ token.typ == EnumToken.LengthTokenType ||
2709
+ (token.typ == EnumToken.NumberTokenType && token.val === 0);
2710
+ break;
2711
+ }
2712
+ case "time":
2713
+ {
2714
+ const token = context.peek();
2715
+ if (token == null) {
2716
+ break;
2717
+ }
2718
+ success = token.typ == EnumToken.TimeTokenType;
2719
+ }
2720
+ break;
2721
+ case "hex-color": {
2722
+ const token = context.peek();
2723
+ success =
2724
+ token != null && token.typ == EnumToken.ColorTokenType && token.kin == ColorType.HEX;
2725
+ break;
2726
+ }
2727
+ case "color":
2728
+ case "color-function":
2729
+ {
2730
+ const token = context.peek();
2731
+ const errors = [];
2732
+ if (tokensfuncDefMap.has(token?.typ)) {
2733
+ const range = context.peekRange();
2734
+ // if (
2735
+ // range.some(
2736
+ // (token) =>
2737
+ // token.typ == EnumToken.IdenTokenType &&
2738
+ // "hslrgbchwmyk".split("").some((t) => equalsIgnoreCase(t, (token as IdentToken).val)),
2739
+ // )
2740
+ // ) {
2741
+ const newRange = range.map((t) => cloneNode(t, true));
2742
+ parseTokens(newRange, { parseColor: true }, errors);
2743
+ success = newRange.length == 1 && isColor(newRange[0], errors);
2744
+ if (success) {
2745
+ context.update(range.at(-1));
2746
+ return {
2747
+ success: true,
2748
+ valid: true,
2749
+ token: context.peek(),
2750
+ context,
2751
+ syntaxToken: null,
2752
+ errors,
2753
+ };
2754
+ }
2755
+ return {
2756
+ success: false,
2757
+ valid: true,
2758
+ token: context.peek(),
2759
+ context,
2760
+ syntaxToken: null,
2761
+ errors,
2762
+ };
2763
+ // }
2764
+ // const result = matchSyntax(
2765
+ // getParsedSyntax(
2766
+ // ValidationSyntaxGroupEnum.Syntaxes,
2767
+ // (token as FunctionToken).val + "()",
2768
+ // ) as ValidationFunctionToken[],
2769
+ // createValidationContext(range),
2770
+ // options,
2771
+ // );
2772
+ // if (result.success) {
2773
+ // context.update(range.at(-1) as Token);
2774
+ // return {
2775
+ // success: true,
2776
+ // valid: true,
2777
+ // token: context.peek(),
2778
+ // context,
2779
+ // syntaxToken: null,
2780
+ // errors: [],
2781
+ // };
2782
+ // }
2783
+ // return result;
2784
+ }
2785
+ // @ts-expect-error
2786
+ else if ("chi" in token) {
2787
+ success = isColor(token, errors);
2788
+ }
2789
+ success = token != null && isColor(token, errors);
2790
+ if (!success) {
2791
+ return {
2792
+ success,
2793
+ valid: false,
2794
+ token,
2795
+ context,
2796
+ syntaxToken: property,
2797
+ errors,
2798
+ };
2799
+ }
2800
+ }
2801
+ break;
2802
+ case "url-token":
2803
+ success =
2804
+ context.peek()?.typ == EnumToken.StringTokenType || context.peek()?.typ == EnumToken.UrlTokenTokenType;
2805
+ break;
2806
+ case "type-selector":
2807
+ case "compound-selector":
2808
+ case "complex-selector":
2809
+ case "subclass-selector":
2810
+ case "attribute-selector":
2811
+ case "complex-selector-unit":
2812
+ case "pseudo-compound-selector":
2813
+ case "forgiving-relative-selector-list":
2814
+ case "complex-selector-list": {
2815
+ const tokens = context.getRemainingTokens();
2816
+ const result = matchSelectorSyntax(tokens, [], options, property.val.startsWith("forgiving-relative-selector-list"));
2817
+ if (result.success || property.val == "forgiving-relative-selector-list") {
2818
+ context.update(tokens.at(-1));
2819
+ }
2820
+ return {
2821
+ success: result.success || property.val == "forgiving-relative-selector-list",
2822
+ valid: true,
2823
+ token: context.peek(),
2824
+ context,
2825
+ syntaxToken: property,
2826
+ errors: result.errors,
2827
+ };
2828
+ }
2829
+ case "function-token":
2830
+ if (context.peek()?.typ == EnumToken.IdenTokenType &&
2831
+ context.peek(1, false)?.typ == EnumToken.StartParensTokenType) {
2832
+ context.next();
2833
+ context.next();
2834
+ success = true;
2835
+ break;
2836
+ }
2837
+ if (tokensfuncDefMap.has(context.peek()?.typ)) {
2838
+ success = true;
2839
+ context.next();
2840
+ break;
2841
+ }
2842
+ return {
2843
+ success: false,
2844
+ valid: true,
2845
+ token: context.peek(),
2846
+ context,
2847
+ syntaxToken: property,
2848
+ errors: [],
2849
+ };
2850
+ case "language-code": {
2851
+ const token = context.peek();
2852
+ success =
2853
+ token?.typ == EnumToken.IdenTokenType &&
2854
+ // @ts-expect-error
2855
+ config.languages.some((t) => t === token.val || token.val.startsWith(t + "-"));
2856
+ break;
2857
+ }
2858
+ case "named-color": {
2859
+ const token = context.peek();
2860
+ success =
2861
+ token != null &&
2862
+ token.typ == EnumToken.ColorTokenType &&
2863
+ (token.kin == ColorType.LIT ||
2864
+ token.kin == ColorType.SYS ||
2865
+ token.kin == ColorType.DPSYS ||
2866
+ token.kin == ColorType.NON_STD);
2867
+ break;
2868
+ }
2869
+ case "size-feature": {
2870
+ const range = context.peekRange();
2871
+ const filteredRange = range.filter((token) => token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType);
2872
+ if (filteredRange.length === 1) {
2873
+ success =
2874
+ filteredRange[0].typ === EnumToken.IdenTokenType ||
2875
+ filteredRange[0].typ === EnumToken.DashedIdenTokenType;
2876
+ context.update(filteredRange[0]);
2877
+ return {
2878
+ success,
2879
+ valid: true,
2880
+ token: filteredRange[0],
2881
+ context,
2882
+ syntaxToken: property,
2883
+ errors: [],
2884
+ };
2885
+ }
2886
+ if (!mFLT.has(filteredRange[1]?.typ) &&
2887
+ !mFGT.has(filteredRange[1]?.typ) &&
2888
+ filteredRange[1]?.typ !== EnumToken.ColonTokenType &&
2889
+ filteredRange[1]?.typ !== EnumToken.DelimTokenType) {
2890
+ return {
2891
+ success: false,
2892
+ valid: true,
2893
+ token: filteredRange[1],
2894
+ context,
2895
+ syntaxToken: property,
2896
+ errors: [],
2897
+ };
2898
+ }
2899
+ if (filteredRange.length === 3) {
2900
+ success = isValue(filteredRange[2]);
2901
+ if (success) {
2902
+ context.update(filteredRange[2]);
2903
+ return {
2904
+ success,
2905
+ valid: true,
2906
+ token: filteredRange[2],
2907
+ context,
2908
+ syntaxToken: property,
2909
+ errors: [],
2910
+ };
2911
+ }
2912
+ }
2913
+ break;
2914
+ }
2915
+ case "style-feature": {
2916
+ // case 'style-feature-value':
2917
+ // match <style-feature-plain>
2918
+ // match <style-feature-boolean>
2919
+ // match <style-range>
2920
+ const range = context.peekRange();
2921
+ const filteredRange = range.filter((token) => token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType);
2922
+ if (filteredRange.length === 0) {
2923
+ return {
2924
+ success: false,
2925
+ valid: true,
2926
+ token: context.peek(),
2927
+ context,
2928
+ syntaxToken: property,
2929
+ errors: [],
2930
+ };
2931
+ }
2932
+ if (filteredRange.length === 1) {
2933
+ success =
2934
+ filteredRange[0].typ === EnumToken.IdenTokenType ||
2935
+ filteredRange[0].typ === EnumToken.DashedIdenTokenType;
2936
+ context.update(filteredRange[0]);
2937
+ return {
2938
+ success,
2939
+ valid: true,
2940
+ token: filteredRange[0],
2941
+ context,
2942
+ syntaxToken: property,
2943
+ errors: [],
2944
+ };
2945
+ }
2946
+ if (filteredRange[1]?.typ === EnumToken.ColonTokenType ||
2947
+ filteredRange[1]?.typ === EnumToken.DelimTokenType) {
2948
+ const result = isDeclarationValue(filteredRange.slice(2));
2949
+ if (result.success) {
2950
+ context.update(filteredRange.at(-1));
2951
+ return {
2952
+ success: true,
2953
+ valid: true,
2954
+ token: filteredRange.at(-1),
2955
+ context,
2956
+ syntaxToken: property,
2957
+ errors: [],
2958
+ };
2959
+ }
2960
+ return {
2961
+ success: false,
2962
+ valid: false,
2963
+ token: context.peek(),
2964
+ context,
2965
+ syntaxToken: property,
2966
+ errors: result.errors,
2967
+ };
2968
+ }
2969
+ return {
2970
+ success: false,
2971
+ valid: false,
2972
+ token: context.peek(),
2973
+ context,
2974
+ syntaxToken: property,
2975
+ errors: [],
2976
+ };
2977
+ }
2978
+ case "url": {
2979
+ const token = context.peek();
2980
+ if (token == null) {
2981
+ success = false;
2982
+ break;
2983
+ }
2984
+ let l;
2985
+ if (token.typ == EnumToken.UrlFunctionTokenDefType) {
2986
+ l = 0;
2987
+ let token;
2988
+ while ((token = context.peek(++l))) {
2989
+ if (token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType) {
2990
+ break;
2991
+ }
2992
+ }
2993
+ if (token != null &&
2994
+ (token.typ == EnumToken.StringTokenType ||
2995
+ token.typ == EnumToken.IdenTokenType ||
2996
+ token.typ == EnumToken.UrlTokenTokenType)) {
2997
+ if (token.typ === EnumToken.IdenTokenType) {
2998
+ while (context.peek(l + 1)?.typ === EnumToken.ClassSelectorTokenType) {
2999
+ l++;
3000
+ }
3001
+ }
3002
+ while ((token = context.peek(++l))) {
3003
+ if (token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType) {
3004
+ break;
3005
+ }
3006
+ }
3007
+ if (token?.typ == EnumToken.EndParensTokenType) {
3008
+ success = true;
3009
+ context.update(token);
3010
+ return {
3011
+ success: true,
3012
+ valid: true,
3013
+ token: null,
3014
+ context,
3015
+ syntaxToken: null,
3016
+ errors: [],
3017
+ };
3018
+ }
3019
+ }
3020
+ break;
3021
+ }
3022
+ if (token?.typ != EnumToken.UrlFunctionTokenType) {
3023
+ success = false;
3024
+ break;
3025
+ }
3026
+ l = -1;
3027
+ while (++l < token.chi.length) {
3028
+ if (token.chi[l].typ !== EnumToken.WhitespaceTokenType &&
3029
+ token.chi[l].typ !== EnumToken.CommentTokenType) {
3030
+ break;
3031
+ }
3032
+ }
3033
+ if (l < token.chi.length) {
3034
+ success =
3035
+ token.chi[l].typ == EnumToken.StringTokenType ||
3036
+ token.chi[l].typ == EnumToken.IdenTokenType ||
3037
+ token.chi[l].typ == EnumToken.UrlTokenTokenType;
3038
+ context.next();
3039
+ return {
3040
+ success: true,
3041
+ valid: true,
3042
+ token: null,
3043
+ context,
3044
+ syntaxToken: null,
3045
+ errors: [],
3046
+ };
3047
+ }
3048
+ break;
3049
+ }
3050
+ default:
3051
+ if (!(property.val in config.syntaxes)) {
3052
+ console.debug("Unknown syntax", context.peek(), property);
3053
+ console.debug(context.getRemainingTokens());
3054
+ throw new Error(`Unexpected validation property ${property.val}`);
3055
+ }
3056
+ return matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, property.val), context, options);
3057
+ }
3058
+ if (!success &&
3059
+ context.peek()?.typ == EnumToken.IdenTokenType &&
3060
+ allValues.includes(context.peek().val)) {
3061
+ success = true;
3062
+ }
3063
+ if (success) {
3064
+ context.next();
3065
+ return {
3066
+ success: true,
3067
+ valid: true,
3068
+ token: null,
3069
+ context,
3070
+ syntaxToken: null,
3071
+ errors: [],
3072
+ };
3073
+ }
3074
+ return {
3075
+ success: false,
3076
+ valid: true,
3077
+ token: context.peek(),
3078
+ context,
3079
+ syntaxToken: property,
3080
+ errors: [],
3081
+ };
3082
+ }
3083
+ function matchRepeatableSyntax(syntax, context, options) {
3084
+ const { isRepeatable, isOptional, isMandatatoryGroup, isRepeatableAtLeastOnce, ...rest } = syntax;
3085
+ let result = null;
3086
+ let tmpResult;
3087
+ let success = !!isRepeatable;
3088
+ let index = context.index;
3089
+ do {
3090
+ tmpResult = matchSyntax([rest], context.slice(), options);
3091
+ if (tmpResult.success) {
3092
+ result = tmpResult;
3093
+ success = true;
3094
+ if (tmpResult.context.done()) {
3095
+ context.end();
3096
+ break;
3097
+ }
3098
+ if (context.current() === tmpResult.context.current()) {
3099
+ context.next();
3100
+ }
3101
+ else {
3102
+ context.update(result.context.current());
3103
+ }
3104
+ if (index === context.index) {
3105
+ break;
3106
+ }
3107
+ index = context.index;
3108
+ }
3109
+ } while (tmpResult.success && !context.done());
3110
+ return {
3111
+ success,
3112
+ valid: true,
3113
+ token: context.peek(),
3114
+ context,
3115
+ syntaxToken: syntax,
3116
+ errors: result?.errors ?? [],
3117
+ };
3118
+ }
3119
+
3120
+ export { createValidationContext, funcTypes, getMFInfo, isMFName, isMFValue, isStyleRangeValue, matchAllSyntaxes, matchOccurenceSyntax, matchSelectorSyntax, trimArray };