@tbela99/css-parser 1.4.6 → 1.4.8
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.
- package/CHANGELOG.md +23 -0
- package/README.md +1 -0
- package/dist/index-umd-web.js +6578 -6969
- package/dist/index.cjs +7808 -8197
- package/dist/index.d.ts +923 -51
- package/dist/lib/ast/clone.js +16 -23
- package/dist/lib/ast/expand.js +2 -2
- package/dist/lib/ast/features/if.js +88 -87
- package/dist/lib/ast/features/inlinecssvariables.js +2 -2
- package/dist/lib/ast/features/prefix.js +26 -22
- package/dist/lib/ast/features/transform.js +3 -3
- package/dist/lib/ast/find.js +17 -14
- package/dist/lib/ast/minify.js +97 -94
- package/dist/lib/ast/types.js +10 -2
- package/dist/lib/ast/walk.js +1 -1
- package/dist/lib/fs/resolve.js +47 -45
- package/dist/lib/parser/arena.js +206 -0
- package/dist/lib/parser/declaration/list.js +9 -10
- package/dist/lib/parser/declaration/map.js +13 -18
- package/dist/lib/parser/linemap.js +62 -0
- package/dist/lib/parser/parse.js +331 -504
- package/dist/lib/parser/source.js +72 -0
- package/dist/lib/parser/tokenize.js +39 -33
- package/dist/lib/parser/utils/at-rule-container.js +244 -247
- package/dist/lib/parser/utils/at-rule-generic.js +25 -25
- package/dist/lib/parser/utils/at-rule-import.js +194 -234
- package/dist/lib/parser/utils/at-rule-media.js +284 -283
- package/dist/lib/parser/utils/at-rule-support.js +279 -251
- package/dist/lib/parser/utils/at-rule-when-else.js +256 -259
- package/dist/lib/parser/utils/at-rule.js +19 -16
- package/dist/lib/parser/utils/cache.js +33 -5
- package/dist/lib/parser/utils/declaration.js +260 -298
- package/dist/lib/parser/utils/hash.js +15 -15
- package/dist/lib/parser/utils/selector.js +182 -222
- package/dist/lib/parser/utils/token.js +4 -7
- package/dist/lib/renderer/render.js +152 -121
- package/dist/lib/syntax/color/alpha.js +1 -4
- package/dist/lib/syntax/color/color-mix.js +8 -1
- package/dist/lib/syntax/color/color.js +17 -9
- package/dist/lib/syntax/color/hex.js +1 -1
- package/dist/lib/syntax/color/p3.js +9 -1
- package/dist/lib/syntax/color/utils/colorspace.js +3 -3
- package/dist/lib/syntax/color/utils/components.js +21 -22
- package/dist/lib/syntax/color/utils/distance.js +6 -1
- package/dist/lib/syntax/color/utils/matrix.js +12 -11
- package/dist/lib/syntax/constants.js +25 -3
- package/dist/lib/syntax/syntax.js +240 -311
- package/dist/lib/validation/config.js +16 -17
- package/dist/lib/validation/match.js +1118 -1131
- package/dist/lib/validation/parser/parse.js +130 -286
- package/dist/node.js +18 -14
- package/dist/web.js +12 -10
- package/package.json +4 -4
- package/dist/lib/validation/utils/whitespace.js +0 -17
- /package/dist/lib/syntax/color/{relativecolor.js → relative-color.js} +0 -0
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import { EnumToken } from '../../ast/types.js';
|
|
2
2
|
import { isIdentStart, isIdent, parseDimension, isPseudo } from '../../syntax/syntax.js';
|
|
3
3
|
import { ValidationTokenEnum } from './typedef.js';
|
|
4
|
+
import { LOC } from '../../syntax/constants.js';
|
|
4
5
|
|
|
5
|
-
const objectProperties = {
|
|
6
|
-
enumerable: false,
|
|
7
|
-
writable: true,
|
|
8
|
-
configurable: true,
|
|
9
|
-
};
|
|
10
6
|
const SymbolsMapTokens = {
|
|
11
7
|
"&&": ValidationTokenEnum.Ampersand,
|
|
12
8
|
"|": ValidationTokenEnum.Pipe,
|
|
@@ -51,49 +47,56 @@ function getTokenType(token, position, currentPosition) {
|
|
|
51
47
|
const pos = { ...position };
|
|
52
48
|
Object.assign(position, currentPosition);
|
|
53
49
|
if (token === "<an+b>") {
|
|
54
|
-
return
|
|
50
|
+
return {
|
|
55
51
|
typ: ValidationTokenEnum.PropertyType,
|
|
56
52
|
val: token.slice(1, -1),
|
|
57
|
-
|
|
53
|
+
[LOC]: pos,
|
|
54
|
+
};
|
|
58
55
|
}
|
|
59
56
|
if (token.charAt(0) == '"' || token.charAt(0) == "'") {
|
|
60
|
-
return
|
|
57
|
+
return {
|
|
61
58
|
typ: ValidationTokenEnum.StringToken,
|
|
62
59
|
val: token,
|
|
63
|
-
|
|
60
|
+
[LOC]: pos,
|
|
61
|
+
};
|
|
64
62
|
}
|
|
65
63
|
if (token.match(/^\d+$/)) {
|
|
66
|
-
return
|
|
64
|
+
return {
|
|
67
65
|
typ: ValidationTokenEnum.Number,
|
|
68
66
|
val: Number(token),
|
|
69
|
-
|
|
67
|
+
[LOC]: pos,
|
|
68
|
+
};
|
|
70
69
|
}
|
|
71
70
|
let dimension = parseDimension(token);
|
|
72
71
|
if (dimension) {
|
|
73
72
|
// @ts-expect-error
|
|
74
|
-
return
|
|
73
|
+
return {
|
|
75
74
|
typ: ValidationTokenEnum.Dimension,
|
|
76
75
|
unit: EnumToken[dimension.typ],
|
|
77
76
|
val: dimension.val,
|
|
78
77
|
unitText: dimension.unit,
|
|
79
|
-
|
|
78
|
+
[LOC]: pos,
|
|
79
|
+
};
|
|
80
80
|
}
|
|
81
81
|
if (isPseudo(token)) {
|
|
82
|
-
return
|
|
82
|
+
return {
|
|
83
83
|
typ: ValidationTokenEnum.PseudoClassToken,
|
|
84
84
|
val: token,
|
|
85
|
-
|
|
85
|
+
[LOC]: pos,
|
|
86
|
+
};
|
|
86
87
|
}
|
|
87
88
|
if (token.startsWith("@") && isIdent(token.slice(1))) {
|
|
88
|
-
return
|
|
89
|
+
return {
|
|
89
90
|
typ: ValidationTokenEnum.AtRule,
|
|
90
91
|
val: token.slice(1),
|
|
91
|
-
|
|
92
|
+
[LOC]: pos,
|
|
93
|
+
};
|
|
92
94
|
}
|
|
93
|
-
return
|
|
95
|
+
return {
|
|
94
96
|
typ: ValidationTokenEnum.Keyword,
|
|
95
97
|
val: token,
|
|
96
|
-
|
|
98
|
+
[LOC]: pos,
|
|
99
|
+
};
|
|
97
100
|
}
|
|
98
101
|
function trimSyntaxArray(tokens) {
|
|
99
102
|
while (tokens[0]?.typ === ValidationTokenEnum.Whitespace) {
|
|
@@ -131,10 +134,10 @@ function* tokenizeSyntax(syntax, position = {
|
|
|
131
134
|
let chr = syntax.charAt(i);
|
|
132
135
|
move(currentPosition, chr);
|
|
133
136
|
if (chr === "<" && "an+b>" === syntax.slice(i + 1, i + 6)) {
|
|
134
|
-
if (buffer.length > 0) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
137
|
+
// if (buffer.length > 0) {
|
|
138
|
+
// yield getTokenType(buffer, position, currentPosition);
|
|
139
|
+
// buffer = "";
|
|
140
|
+
// }
|
|
138
141
|
yield getTokenType("<an+b>", position, currentPosition);
|
|
139
142
|
move(currentPosition, "an+b>");
|
|
140
143
|
i += 5;
|
|
@@ -149,9 +152,10 @@ function* tokenizeSyntax(syntax, position = {
|
|
|
149
152
|
yield getTokenType(buffer, position, currentPosition);
|
|
150
153
|
buffer = "";
|
|
151
154
|
}
|
|
152
|
-
yield
|
|
155
|
+
yield {
|
|
153
156
|
typ: SymbolsMapTokens[chr],
|
|
154
|
-
|
|
157
|
+
[LOC]: pos,
|
|
158
|
+
};
|
|
155
159
|
continue;
|
|
156
160
|
}
|
|
157
161
|
if (chr === ":") {
|
|
@@ -173,9 +177,7 @@ function* tokenizeSyntax(syntax, position = {
|
|
|
173
177
|
if (SymbolsMapTokens[chr] === ValidationTokenEnum.Whitespace) {
|
|
174
178
|
let ch;
|
|
175
179
|
let k = i;
|
|
176
|
-
while ((ch = syntax.charAt(k + 1)) &&
|
|
177
|
-
ch in SymbolsMapTokens &&
|
|
178
|
-
SymbolsMapTokens[ch] === ValidationTokenEnum.Whitespace) {
|
|
180
|
+
while ((ch = syntax.charAt(k + 1)) && SymbolsMapTokens[ch] === ValidationTokenEnum.Whitespace) {
|
|
179
181
|
k++;
|
|
180
182
|
}
|
|
181
183
|
if (k != i) {
|
|
@@ -189,28 +191,30 @@ function* tokenizeSyntax(syntax, position = {
|
|
|
189
191
|
yield getTokenType(buffer, position, currentPosition);
|
|
190
192
|
buffer = "";
|
|
191
193
|
}
|
|
192
|
-
yield
|
|
194
|
+
yield {
|
|
193
195
|
typ: SymbolsMapTokens[chr],
|
|
194
|
-
|
|
196
|
+
[LOC]: pos,
|
|
197
|
+
};
|
|
195
198
|
continue;
|
|
196
199
|
}
|
|
197
200
|
switch (chr) {
|
|
198
|
-
case "\\":
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
201
|
+
// case "\\":
|
|
202
|
+
// if (buffer.length > 0) {
|
|
203
|
+
// yield getTokenType(buffer, position, currentPosition);
|
|
204
|
+
// buffer = "";
|
|
205
|
+
// }
|
|
206
|
+
// buffer += chr;
|
|
207
|
+
// chr = syntax.charAt(++i);
|
|
208
|
+
// buffer += chr;
|
|
209
|
+
// move(currentPosition, chr);
|
|
210
|
+
// break;
|
|
208
211
|
case ":":
|
|
209
212
|
if (isIdent(buffer)) {
|
|
210
|
-
yield
|
|
213
|
+
yield {
|
|
211
214
|
typ: ValidationTokenEnum.DeclarationNameToken,
|
|
212
215
|
val: buffer,
|
|
213
|
-
|
|
216
|
+
[LOC]: { ...position },
|
|
217
|
+
};
|
|
214
218
|
buffer = "";
|
|
215
219
|
move(currentPosition, chr);
|
|
216
220
|
break;
|
|
@@ -219,19 +223,19 @@ function* tokenizeSyntax(syntax, position = {
|
|
|
219
223
|
break;
|
|
220
224
|
case '"':
|
|
221
225
|
case "'":
|
|
222
|
-
if (buffer.length > 0) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
+
// if (buffer.length > 0) {
|
|
227
|
+
// yield getTokenType(buffer, position, currentPosition);
|
|
228
|
+
// buffer = "";
|
|
229
|
+
// }
|
|
226
230
|
buffer += chr;
|
|
227
231
|
while (i + 1 < syntax.length) {
|
|
228
232
|
chr = syntax.charAt(++i);
|
|
229
233
|
buffer += chr;
|
|
230
|
-
if (chr == "\\") {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
}
|
|
234
|
+
// if (chr == "\\") {
|
|
235
|
+
// chr = syntax.charAt(++i);
|
|
236
|
+
// buffer += chr;
|
|
237
|
+
// continue;
|
|
238
|
+
// }
|
|
235
239
|
if (chr == buffer.charAt(0)) {
|
|
236
240
|
break;
|
|
237
241
|
}
|
|
@@ -248,9 +252,10 @@ function* tokenizeSyntax(syntax, position = {
|
|
|
248
252
|
if (buffer.length > 0) {
|
|
249
253
|
yield getTokenType(buffer, position, currentPosition);
|
|
250
254
|
}
|
|
251
|
-
yield
|
|
255
|
+
yield {
|
|
252
256
|
typ: ValidationTokenEnum.EOF,
|
|
253
|
-
|
|
257
|
+
[LOC]: { ...position },
|
|
258
|
+
};
|
|
254
259
|
}
|
|
255
260
|
function parseSyntax(syntax) {
|
|
256
261
|
const stack = [];
|
|
@@ -318,15 +323,19 @@ function parseSyntax(syntax) {
|
|
|
318
323
|
r: [],
|
|
319
324
|
}));
|
|
320
325
|
}
|
|
321
|
-
else {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
326
|
+
// else {
|
|
327
|
+
// (stack.at(-1) as ValidationAmpersandToken).r = trimSyntaxArray(
|
|
328
|
+
// tokens.splice(index + 1, tokens.length - index - 2),
|
|
329
|
+
// );
|
|
330
|
+
// stack.pop();
|
|
331
|
+
// stack.push(
|
|
332
|
+
// Object.assign(token, {
|
|
333
|
+
// typ: makeBinaryOp.get(token!.typ),
|
|
334
|
+
// l: tokens.splice(index, 1),
|
|
335
|
+
// r: [],
|
|
336
|
+
// }) as ValidationAmpersandToken,
|
|
337
|
+
// );
|
|
338
|
+
// }
|
|
330
339
|
}
|
|
331
340
|
else {
|
|
332
341
|
stack.push(Object.assign(token, {
|
|
@@ -390,11 +399,13 @@ function parseSyntax(syntax) {
|
|
|
390
399
|
stack.at(-1).val === "function-token")) {
|
|
391
400
|
stack.pop();
|
|
392
401
|
}
|
|
393
|
-
else if (stack.at(-1)?.typ === ValidationTokenEnum.PipeToken) {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
402
|
+
// else if ((stack.at(-1) as ValidationToken)?.typ === ValidationTokenEnum.PipeToken) {
|
|
403
|
+
// let index: number = tokens.lastIndexOf(stack.at(-1) as ValidationToken);
|
|
404
|
+
// (stack.at(-1) as ValidationPipeToken).chi.push(
|
|
405
|
+
// trimSyntaxArray(tokens.splice(index + 1, tokens.length - index - 2)),
|
|
406
|
+
// );
|
|
407
|
+
// stack.pop();
|
|
408
|
+
// }
|
|
398
409
|
else if (stack.at(-1)?.typ === ValidationTokenEnum.AmpersandToken) {
|
|
399
410
|
let index = tokens.lastIndexOf(stack.at(-1));
|
|
400
411
|
stack.at(-1).r = trimSyntaxArray(tokens.splice(index + 1, tokens.length - index - 2));
|
|
@@ -467,10 +478,10 @@ function parseSyntax(syntax) {
|
|
|
467
478
|
break;
|
|
468
479
|
}
|
|
469
480
|
currentToken = stack.at(-1);
|
|
470
|
-
if (currentToken?.typ !== ValidationTokenEnum.OpenBracket) {
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
}
|
|
481
|
+
// if (currentToken?.typ !== ValidationTokenEnum.OpenBracket) {
|
|
482
|
+
// console.debug(JSON.stringify(tokens, null, 1));
|
|
483
|
+
// throw new SyntaxError(`Unexpected token: ']' at line ${token![LOC].lin}:${token![LOC].col} `);
|
|
484
|
+
// }
|
|
474
485
|
{
|
|
475
486
|
let index = tokens.lastIndexOf(stack.at(-1));
|
|
476
487
|
if ((tokens[index - 2]?.typ === ValidationTokenEnum.Keyword ||
|
|
@@ -478,17 +489,17 @@ function parseSyntax(syntax) {
|
|
|
478
489
|
tokens[index - 1]?.typ === ValidationTokenEnum.Whitespace &&
|
|
479
490
|
(tokens[index + 1]?.typ === ValidationTokenEnum.Number ||
|
|
480
491
|
tokens[index + 1]?.typ === ValidationTokenEnum.Dimension)) {
|
|
481
|
-
if (tokens[index + 2]?.typ === ValidationTokenEnum.CloseBracket) {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
}
|
|
492
|
+
// if (tokens[index + 2]?.typ === ValidationTokenEnum.CloseBracket) {
|
|
493
|
+
// stack.pop();
|
|
494
|
+
// Object.assign(tokens[index - 2], {
|
|
495
|
+
// range: {
|
|
496
|
+
// min: <ValidationNumberToken>tokens[index + 1],
|
|
497
|
+
// max: <ValidationNumberToken>tokens[index + 1],
|
|
498
|
+
// },
|
|
499
|
+
// });
|
|
500
|
+
// tokens.splice(index - 1, 5);
|
|
501
|
+
// break;
|
|
502
|
+
// }
|
|
492
503
|
if (tokens[index + 2]?.typ === ValidationTokenEnum.Comma &&
|
|
493
504
|
(tokens[index + 3]?.typ === tokens[index + 1]?.typ ||
|
|
494
505
|
tokens[index + 3]?.typ === ValidationTokenEnum.InfinityToken) &&
|
|
@@ -523,13 +534,15 @@ function parseSyntax(syntax) {
|
|
|
523
534
|
// }
|
|
524
535
|
case ValidationTokenEnum.CloseCurlyBrace:
|
|
525
536
|
while (true) {
|
|
526
|
-
if (stack.at(-1).typ == ValidationTokenEnum.AmpersandToken) {
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
537
|
+
// if ((stack.at(-1) as ValidationToken).typ == ValidationTokenEnum.AmpersandToken) {
|
|
538
|
+
// let index: number = tokens.lastIndexOf(stack.at(-1) as ValidationToken);
|
|
539
|
+
// (stack.at(-1) as ValidationAmpersandToken).r = trimSyntaxArray(
|
|
540
|
+
// tokens.splice(index + 1, tokens.length - index - 2),
|
|
541
|
+
// );
|
|
542
|
+
// stack.pop();
|
|
543
|
+
// continue;
|
|
544
|
+
// } else
|
|
545
|
+
if (stack.at(-1).typ == ValidationTokenEnum.ColumnToken) {
|
|
533
546
|
let index = tokens.lastIndexOf(stack.at(-1));
|
|
534
547
|
stack.at(-1).chi.push(trimSyntaxArray(tokens.splice(index + 1, tokens.length - index - 2)));
|
|
535
548
|
stack.pop();
|
|
@@ -543,9 +556,9 @@ function parseSyntax(syntax) {
|
|
|
543
556
|
}
|
|
544
557
|
break;
|
|
545
558
|
}
|
|
546
|
-
if (stack.at(-1)?.typ != ValidationTokenEnum.OpenCurlyBrace) {
|
|
547
|
-
|
|
548
|
-
}
|
|
559
|
+
// if (stack.at(-1)?.typ != ValidationTokenEnum.OpenCurlyBrace) {
|
|
560
|
+
// throw new SyntaxError(`Unexpected token } as ${token![LOC].lin}:${token![LOC].col}`);
|
|
561
|
+
// }
|
|
549
562
|
{
|
|
550
563
|
let index = tokens.lastIndexOf(stack.at(-1));
|
|
551
564
|
const slice = tokens.slice(index + 1, -1);
|
|
@@ -558,13 +571,17 @@ function parseSyntax(syntax) {
|
|
|
558
571
|
},
|
|
559
572
|
});
|
|
560
573
|
if (slice.length === 3) {
|
|
561
|
-
if (slice[1].typ != ValidationTokenEnum.Comma) {
|
|
562
|
-
|
|
563
|
-
}
|
|
564
|
-
if (
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
574
|
+
// if (slice[1].typ != ValidationTokenEnum.Comma) {
|
|
575
|
+
// throw new SyntaxError(`Expecting ',' at ${slice[1][LOC].lin}:${slice[1][LOC].col}`);
|
|
576
|
+
// }
|
|
577
|
+
// if (
|
|
578
|
+
// slice[2].typ != ValidationTokenEnum.Number &&
|
|
579
|
+
// slice[2].typ != ValidationTokenEnum.InfinityToken
|
|
580
|
+
// ) {
|
|
581
|
+
// throw new SyntaxError(
|
|
582
|
+
// `Expecting number or infinity at ${slice[2][LOC].lin}:${slice[2][LOC].col}`,
|
|
583
|
+
// );
|
|
584
|
+
// }
|
|
568
585
|
Object.assign(tokens[index - 1], {
|
|
569
586
|
match: {
|
|
570
587
|
min: slice[0],
|
|
@@ -591,16 +608,16 @@ function parseSyntax(syntax) {
|
|
|
591
608
|
// if the previous token is a comma, we wrap them in a virtual group and mark the group as optional
|
|
592
609
|
{
|
|
593
610
|
let sliceIndex = -1;
|
|
594
|
-
let isGroup = tokens.at(-2)?.typ == ValidationTokenEnum.Comma;
|
|
611
|
+
let isGroup; // = tokens.at(-2)?.typ == ValidationTokenEnum.Comma;
|
|
612
|
+
// if (isGroup) {
|
|
613
|
+
// sliceIndex = tokens.length - 2;
|
|
614
|
+
// } else
|
|
615
|
+
// {
|
|
616
|
+
isGroup = tokens.at(-3)?.typ == ValidationTokenEnum.Comma;
|
|
595
617
|
if (isGroup) {
|
|
596
|
-
sliceIndex = tokens.length -
|
|
597
|
-
}
|
|
598
|
-
else {
|
|
599
|
-
isGroup = tokens.at(-3)?.typ == ValidationTokenEnum.Comma;
|
|
600
|
-
if (isGroup) {
|
|
601
|
-
sliceIndex = tokens.length - 3;
|
|
602
|
-
}
|
|
618
|
+
sliceIndex = tokens.length - 3;
|
|
603
619
|
}
|
|
620
|
+
// }
|
|
604
621
|
if (isGroup) {
|
|
605
622
|
tokens.splice(sliceIndex, tokens.length - sliceIndex, {
|
|
606
623
|
typ: ValidationTokenEnum.OptionalGroupToken,
|
|
@@ -616,10 +633,10 @@ function parseSyntax(syntax) {
|
|
|
616
633
|
case ValidationTokenEnum.Comma:
|
|
617
634
|
{
|
|
618
635
|
let sliceIndex = -1;
|
|
619
|
-
if (tokens.at(-2)?.isOptional && tokens.at(-2)
|
|
620
|
-
|
|
621
|
-
}
|
|
622
|
-
|
|
636
|
+
// if (tokens.at(-2)?.isOptional && tokens.at(-2)!.typ !== ValidationTokenEnum.OptionalGroupToken) {
|
|
637
|
+
// sliceIndex = tokens.length - 2;
|
|
638
|
+
// } else
|
|
639
|
+
if (tokens.at(-2).typ == ValidationTokenEnum.Whitespace &&
|
|
623
640
|
tokens.at(-3)?.isOptional &&
|
|
624
641
|
tokens.at(-3).typ !== ValidationTokenEnum.OptionalGroupToken) {
|
|
625
642
|
sliceIndex = tokens.length - 3;
|
|
@@ -661,182 +678,9 @@ function parseSyntax(syntax) {
|
|
|
661
678
|
}
|
|
662
679
|
}
|
|
663
680
|
if (stack.length > 0) {
|
|
664
|
-
throw new SyntaxError(`Unexpected token ${ValidationTokenEnum[stack.at(-1)?.typ]} at ${stack.at(-1)?.
|
|
681
|
+
throw new SyntaxError(`Unexpected token ${ValidationTokenEnum[stack.at(-1)?.typ]} at ${stack.at(-1)?.[LOC]?.lin}:${stack.at(-1)?.[LOC]?.col}`);
|
|
665
682
|
}
|
|
666
683
|
return trimSyntaxArray(tokens);
|
|
667
684
|
}
|
|
668
|
-
function renderSyntax(token, options = { minify: true, indent: 1 }) {
|
|
669
|
-
options.indent ??= 1;
|
|
670
|
-
let glue = options.minify ? "" : " ".repeat(options.indent);
|
|
671
|
-
switch (token.typ) {
|
|
672
|
-
case ValidationTokenEnum.InfinityToken:
|
|
673
|
-
// '∞'
|
|
674
|
-
return "\u221e";
|
|
675
|
-
case ValidationTokenEnum.Root:
|
|
676
|
-
return token.chi.reduce((acc, curr) => acc + renderSyntax(curr, options), "");
|
|
677
|
-
case ValidationTokenEnum.Whitespace:
|
|
678
|
-
return " ";
|
|
679
|
-
case ValidationTokenEnum.FunctionDefinition:
|
|
680
|
-
return "<" + token.val + "()>";
|
|
681
|
-
case ValidationTokenEnum.HashMark:
|
|
682
|
-
return "#";
|
|
683
|
-
case ValidationTokenEnum.Colon:
|
|
684
|
-
return ":";
|
|
685
|
-
case ValidationTokenEnum.Pipe:
|
|
686
|
-
return `${glue}|${glue}`;
|
|
687
|
-
case ValidationTokenEnum.Column:
|
|
688
|
-
return `${glue}||${glue}`;
|
|
689
|
-
case ValidationTokenEnum.PipeToken:
|
|
690
|
-
return token.chi.reduce((acc, curr) => acc +
|
|
691
|
-
(acc.trim().length > 0 ? `${glue}|${glue}` : "") +
|
|
692
|
-
curr.reduce((acc, curr) => acc + renderSyntax(curr, options), ""), "");
|
|
693
|
-
case ValidationTokenEnum.ColumnToken:
|
|
694
|
-
glue = `${glue}||${glue}`;
|
|
695
|
-
return token.chi.reduce((acc, curr) => acc +
|
|
696
|
-
(acc.length > 0 ? glue : "") +
|
|
697
|
-
curr.reduce((acc, curr) => acc + renderSyntax(curr, options), ""), "");
|
|
698
|
-
case ValidationTokenEnum.AmpersandToken:
|
|
699
|
-
glue = `${glue}&&${glue}`;
|
|
700
|
-
return (token.l.reduce((acc, curr) => acc + renderSyntax(curr, options), "") +
|
|
701
|
-
glue +
|
|
702
|
-
token.r.reduce((acc, curr) => acc + renderSyntax(curr, options), ""));
|
|
703
|
-
case ValidationTokenEnum.Function:
|
|
704
|
-
case ValidationTokenEnum.PseudoClassFunctionToken:
|
|
705
|
-
case ValidationTokenEnum.Parens:
|
|
706
|
-
return (token.val +
|
|
707
|
-
"(" +
|
|
708
|
-
token.chi.reduce((acc, curr) => acc + renderSyntax(curr, options), "") +
|
|
709
|
-
")" +
|
|
710
|
-
renderAttributes(token));
|
|
711
|
-
case ValidationTokenEnum.Comma:
|
|
712
|
-
return `,${glue}`;
|
|
713
|
-
case ValidationTokenEnum.Keyword:
|
|
714
|
-
return token.val + renderAttributes(token);
|
|
715
|
-
case ValidationTokenEnum.OpenBracket:
|
|
716
|
-
return `[${glue}`;
|
|
717
|
-
case ValidationTokenEnum.Ampersand:
|
|
718
|
-
return `${glue}&&${glue}`;
|
|
719
|
-
case ValidationTokenEnum.Plus:
|
|
720
|
-
return "+";
|
|
721
|
-
case ValidationTokenEnum.QuestionMark:
|
|
722
|
-
return "?";
|
|
723
|
-
case ValidationTokenEnum.Separator:
|
|
724
|
-
return "/";
|
|
725
|
-
case ValidationTokenEnum.Bracket:
|
|
726
|
-
return (`[${glue}` +
|
|
727
|
-
token.chi.reduce((acc, curr) => acc + renderSyntax(curr, options), "") +
|
|
728
|
-
`${glue}]` +
|
|
729
|
-
renderAttributes(token));
|
|
730
|
-
case ValidationTokenEnum.PropertyType:
|
|
731
|
-
case ValidationTokenEnum.DeclarationType: {
|
|
732
|
-
const q = token.typ ===
|
|
733
|
-
ValidationTokenEnum.DeclarationType
|
|
734
|
-
? "'"
|
|
735
|
-
: "";
|
|
736
|
-
return ("<" +
|
|
737
|
-
q +
|
|
738
|
-
token.val +
|
|
739
|
-
q +
|
|
740
|
-
(token.range == null
|
|
741
|
-
? ""
|
|
742
|
-
: ` [${renderSyntax(token?.range.min)},${token?.range?.max == null
|
|
743
|
-
? "\u221e"
|
|
744
|
-
: renderSyntax(token?.range.max)}]`) +
|
|
745
|
-
">" +
|
|
746
|
-
renderAttributes(token));
|
|
747
|
-
}
|
|
748
|
-
case ValidationTokenEnum.Number:
|
|
749
|
-
case ValidationTokenEnum.PseudoClassToken:
|
|
750
|
-
case ValidationTokenEnum.StringToken:
|
|
751
|
-
return token.val + "";
|
|
752
|
-
case ValidationTokenEnum.SemiColon:
|
|
753
|
-
return ";";
|
|
754
|
-
case ValidationTokenEnum.AtRule:
|
|
755
|
-
return "@" + token.val;
|
|
756
|
-
case ValidationTokenEnum.AtRuleDefinition:
|
|
757
|
-
return ("@" +
|
|
758
|
-
token.val +
|
|
759
|
-
(token.prelude == null
|
|
760
|
-
? ""
|
|
761
|
-
: " " +
|
|
762
|
-
token.prelude.reduce((acc, curr) => acc + renderSyntax(curr, options), "")) +
|
|
763
|
-
(token.chi == null
|
|
764
|
-
? ""
|
|
765
|
-
: " {\n" +
|
|
766
|
-
token.chi.reduce((acc, curr) => acc + renderSyntax(curr, options), "")).slice(1, -1) +
|
|
767
|
-
"\n}");
|
|
768
|
-
case ValidationTokenEnum.Block:
|
|
769
|
-
return (`{${options.minify ? "" : "\n"}` +
|
|
770
|
-
token.chi.reduce((acc, t) => acc + renderSyntax(t, options), "") +
|
|
771
|
-
`${options.minify ? "" : "\n"}}`);
|
|
772
|
-
case ValidationTokenEnum.DeclarationDefinitionToken:
|
|
773
|
-
return (token.nam +
|
|
774
|
-
": " +
|
|
775
|
-
renderSyntax(token.val, options));
|
|
776
|
-
case ValidationTokenEnum.LessThan:
|
|
777
|
-
return "<";
|
|
778
|
-
case ValidationTokenEnum.GreaterThan:
|
|
779
|
-
return ">";
|
|
780
|
-
case ValidationTokenEnum.CloseParenthesis:
|
|
781
|
-
return ")";
|
|
782
|
-
case ValidationTokenEnum.OpenParenthesis:
|
|
783
|
-
return "(";
|
|
784
|
-
case ValidationTokenEnum.DisallowWhitespace:
|
|
785
|
-
return "† ";
|
|
786
|
-
case ValidationTokenEnum.Dimension:
|
|
787
|
-
return token.val + token.unitText;
|
|
788
|
-
case ValidationTokenEnum.OpenCurlyBrace:
|
|
789
|
-
options.indent++;
|
|
790
|
-
return `{${options.minify ? "" : "\n"}}`;
|
|
791
|
-
case ValidationTokenEnum.CloseCurlyBrace:
|
|
792
|
-
options.indent--;
|
|
793
|
-
return `${options.minify ? "" : "\n"}}`;
|
|
794
|
-
case ValidationTokenEnum.DeclarationNameToken:
|
|
795
|
-
return token.val + ":";
|
|
796
|
-
case ValidationTokenEnum.OptionalGroupToken:
|
|
797
|
-
return token.chi.reduce((acc, curr, index, array) => acc +
|
|
798
|
-
(index == array.length - 1
|
|
799
|
-
? curr.typ === ValidationTokenEnum.Comma
|
|
800
|
-
? "?,"
|
|
801
|
-
: renderSyntax(curr, options) + "?"
|
|
802
|
-
: renderSyntax(curr, options)), "");
|
|
803
|
-
default:
|
|
804
|
-
throw new Error("Unhandled token: " + JSON.stringify({ token }, null, 1));
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
function renderAttributes(token) {
|
|
808
|
-
let result = "";
|
|
809
|
-
if (token.isList) {
|
|
810
|
-
result += "#";
|
|
811
|
-
}
|
|
812
|
-
if (token.isOptional) {
|
|
813
|
-
result += "?";
|
|
814
|
-
}
|
|
815
|
-
if (token.isMandatatoryGroup) {
|
|
816
|
-
result += "!";
|
|
817
|
-
}
|
|
818
|
-
if (token.isRepeatable) {
|
|
819
|
-
result += "*";
|
|
820
|
-
}
|
|
821
|
-
if (token.isRepeatableAtLeastOnce) {
|
|
822
|
-
result += "+";
|
|
823
|
-
}
|
|
824
|
-
if (token.match != null) {
|
|
825
|
-
if (token.match.max == null ||
|
|
826
|
-
token.match.max.val === token.match.min.val ||
|
|
827
|
-
Number.isNaN(token.match.max.val)) {
|
|
828
|
-
result += "{" + renderSyntax(token.match.min) + "}";
|
|
829
|
-
}
|
|
830
|
-
else {
|
|
831
|
-
result +=
|
|
832
|
-
"{" +
|
|
833
|
-
renderSyntax(token.match.min) +
|
|
834
|
-
"," +
|
|
835
|
-
(Number.isFinite(token.match.max.val) ? renderSyntax(token.match.max) : "\u221e") +
|
|
836
|
-
"}";
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
return result;
|
|
840
|
-
}
|
|
841
685
|
|
|
842
|
-
export { parseSyntax,
|
|
686
|
+
export { parseSyntax, tokenizeSyntax, trimSyntaxArray };
|