@tbela99/css-parser 1.4.8 → 1.4.10
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 +32 -3
- package/README.md +1 -1
- package/dist/index-umd-web.js +12950 -13310
- package/dist/index.cjs +16447 -16809
- package/dist/index.d.ts +2481 -1754
- package/dist/lib/ast/features/calc.js +3 -1
- package/dist/lib/ast/features/if.js +2 -96
- package/dist/lib/ast/features/prefix.js +7 -2
- package/dist/lib/ast/features/shorthand.js +1 -1
- package/dist/lib/ast/features/transform.js +1 -1
- package/dist/lib/ast/find.js +6 -6
- package/dist/lib/ast/math/expression.js +30 -12
- package/dist/lib/ast/math/math.js +2 -1
- package/dist/lib/ast/minify.js +34 -5
- package/dist/lib/ast/transform/compute.js +2 -1
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/utils.js +29 -21
- package/dist/lib/ast/types.js +9 -66
- package/dist/lib/fs/resolve.js +1 -16
- package/dist/lib/parser/declaration/list.js +6 -1
- package/dist/lib/parser/{linemap.js → linesmap.js} +25 -9
- package/dist/lib/parser/parse.js +1264 -151
- package/dist/lib/parser/source.js +48 -17
- package/dist/lib/parser/tokenize.js +112 -133
- package/dist/lib/parser/utils/at-rule-container.js +12 -44
- package/dist/lib/parser/utils/at-rule-generic.js +12 -24
- package/dist/lib/parser/utils/at-rule-import.js +4 -172
- package/dist/lib/parser/utils/at-rule-media.js +14 -251
- package/dist/lib/parser/utils/at-rule-support.js +8 -258
- package/dist/lib/parser/utils/at-rule-when-else.js +4 -214
- package/dist/lib/parser/utils/declaration.js +5 -95
- package/dist/lib/parser/utils/hash.js +54 -1
- package/dist/lib/parser/utils/selector.js +1 -1
- package/dist/lib/parser/utils/token.js +1 -0
- package/dist/lib/renderer/render.js +79 -87
- package/dist/lib/renderer/sourcemap/sourcemap.js +47 -43
- package/dist/lib/syntax/color/cmyk.js +3 -3
- package/dist/lib/syntax/color/color-mix.js +7 -7
- package/dist/lib/syntax/color/color.js +9 -27
- package/dist/lib/syntax/color/hsl.js +6 -6
- package/dist/lib/syntax/color/hwb.js +6 -6
- package/dist/lib/syntax/color/lab.js +5 -5
- package/dist/lib/syntax/color/lch.js +6 -6
- package/dist/lib/syntax/color/oklab.js +5 -5
- package/dist/lib/syntax/color/oklch.js +5 -5
- package/dist/lib/syntax/color/relative-color.js +8 -1
- package/dist/lib/syntax/color/utils/distance.js +2 -1
- package/dist/lib/syntax/constants.js +2 -1
- package/dist/lib/syntax/syntax.js +96 -9
- package/dist/lib/validation/config.json.js +27 -6
- package/dist/lib/validation/match.js +42 -845
- package/dist/node.js +134 -9
- package/dist/web.js +133 -6
- package/llms.txt +73 -0
- package/package.json +13 -16
- package/dist/lib/parser/arena.js +0 -206
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import { LineMap } from './
|
|
1
|
+
import { LineMap } from './linesmap.js';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Source file ID
|
|
5
|
+
*/
|
|
6
|
+
let sourceId = 0;
|
|
3
7
|
/**
|
|
4
8
|
* Source file helper class
|
|
5
9
|
*/
|
|
@@ -8,10 +12,6 @@ class SourceFile {
|
|
|
8
12
|
* Source file ID
|
|
9
13
|
*/
|
|
10
14
|
id;
|
|
11
|
-
/**
|
|
12
|
-
* Source file content
|
|
13
|
-
*/
|
|
14
|
-
content;
|
|
15
15
|
/**
|
|
16
16
|
* Source file path
|
|
17
17
|
*/
|
|
@@ -20,6 +20,10 @@ class SourceFile {
|
|
|
20
20
|
* Line map
|
|
21
21
|
*/
|
|
22
22
|
lineStarts;
|
|
23
|
+
/**
|
|
24
|
+
* Source file content
|
|
25
|
+
*/
|
|
26
|
+
content;
|
|
23
27
|
/**
|
|
24
28
|
* Constructor
|
|
25
29
|
* @param id
|
|
@@ -27,8 +31,8 @@ class SourceFile {
|
|
|
27
31
|
* @param lines
|
|
28
32
|
* @param file
|
|
29
33
|
*/
|
|
30
|
-
constructor(
|
|
31
|
-
this.id =
|
|
34
|
+
constructor(content, lines, file = null) {
|
|
35
|
+
this.id = sourceId++;
|
|
32
36
|
this.content = content;
|
|
33
37
|
this.file = file;
|
|
34
38
|
this.lineStarts = new LineMap(lines);
|
|
@@ -38,27 +42,47 @@ class SourceFile {
|
|
|
38
42
|
* @param content
|
|
39
43
|
* @param lines
|
|
40
44
|
*/
|
|
41
|
-
|
|
42
|
-
this.content
|
|
43
|
-
this.lineStarts = new LineMap(lines);
|
|
45
|
+
append(content) {
|
|
46
|
+
this.content += content;
|
|
44
47
|
}
|
|
45
|
-
|
|
48
|
+
/**
|
|
49
|
+
* get file name
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
getFileName() {
|
|
46
53
|
return this.file;
|
|
47
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* get content
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
getContent() {
|
|
60
|
+
return this.content;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* get text
|
|
64
|
+
* @param start
|
|
65
|
+
* @param length
|
|
66
|
+
* @returns
|
|
67
|
+
*/
|
|
68
|
+
getText(start, length) {
|
|
69
|
+
return this.content.slice(start, start + length);
|
|
70
|
+
}
|
|
48
71
|
/**
|
|
49
72
|
* Compute line and column of the offset
|
|
50
73
|
* @param offset
|
|
51
74
|
* @returns
|
|
52
75
|
*/
|
|
53
|
-
|
|
54
|
-
return this.lineStarts.
|
|
76
|
+
getOffsets(offset) {
|
|
77
|
+
return this.lineStarts.getOffsets(offset);
|
|
55
78
|
}
|
|
56
79
|
/**
|
|
57
|
-
*
|
|
58
|
-
* @param
|
|
80
|
+
* get source location
|
|
81
|
+
* @param offset
|
|
82
|
+
* @returns
|
|
59
83
|
*/
|
|
60
|
-
|
|
61
|
-
this.
|
|
84
|
+
getSourceLocation(offset) {
|
|
85
|
+
return [this.file, ...this.getOffsets(offset)];
|
|
62
86
|
}
|
|
63
87
|
/**
|
|
64
88
|
* get line starts
|
|
@@ -67,6 +91,13 @@ class SourceFile {
|
|
|
67
91
|
getLineStarts() {
|
|
68
92
|
return this.lineStarts.getLineStarts();
|
|
69
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* add line start
|
|
96
|
+
* @param lineStart
|
|
97
|
+
*/
|
|
98
|
+
addLineStart(lineStart) {
|
|
99
|
+
this.lineStarts.addLineStart(lineStart);
|
|
100
|
+
}
|
|
70
101
|
}
|
|
71
102
|
|
|
72
103
|
export { SourceFile };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { EnumToken, ColorType } from '../ast/types.js';
|
|
2
2
|
import { LOC, wildCardFuncs, whenElseFunc, transformFunctions, mathFuncs, colorsFunc, timingFunc, supportFunc, timelineFunc, imageFunc, gridTemplateFunc, urlFunc, containerFunc, pseudoElements } from '../syntax/constants.js';
|
|
3
3
|
import { isDigit, isPseudo, isIdent, isWhiteSpace, isNumber, isHexColor, isHash, isPercentage, parseDimension, isNewLine } from '../syntax/syntax.js';
|
|
4
|
+
import { SourceFile } from './source.js';
|
|
4
5
|
import { equalsIgnoreCase } from './utils/text.js';
|
|
5
6
|
|
|
6
7
|
const SymbolsMapTokens = {
|
|
@@ -134,18 +135,14 @@ var TokenMap;
|
|
|
134
135
|
TokenMap[TokenMap["MINUS"] = 45] = "MINUS";
|
|
135
136
|
TokenMap[TokenMap["GREATERTHAN"] = 62] = "GREATERTHAN";
|
|
136
137
|
})(TokenMap || (TokenMap = {}));
|
|
137
|
-
function consumeString(
|
|
138
|
-
const quote =
|
|
138
|
+
function consumeString(parseInfo) {
|
|
139
|
+
const quote = next(parseInfo);
|
|
139
140
|
let value;
|
|
141
|
+
let buffer = quote;
|
|
140
142
|
const result = [];
|
|
141
|
-
|
|
142
|
-
result.push(yieldResult(buffer, parseInfo));
|
|
143
|
-
buffer = "";
|
|
144
|
-
}
|
|
145
|
-
buffer += quoteStr;
|
|
146
|
-
while ((value = parseInfo.stream.charAt(parseInfo.currentPosition.ind - parseInfo.offset + 1))) {
|
|
143
|
+
while ((value = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1))) {
|
|
147
144
|
if (value == "\\") {
|
|
148
|
-
if ("\\" == parseInfo.stream.charAt(parseInfo.currentPosition
|
|
145
|
+
if ("\\" == parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 2)) {
|
|
149
146
|
buffer += next(parseInfo, 2);
|
|
150
147
|
continue;
|
|
151
148
|
}
|
|
@@ -181,9 +178,7 @@ function consumeString(quoteStr, buffer, parseInfo) {
|
|
|
181
178
|
}
|
|
182
179
|
next(parseInfo, escapeSequence.length +
|
|
183
180
|
1 +
|
|
184
|
-
(isWhiteSpace(parseInfo.stream
|
|
185
|
-
.charAt(parseInfo.currentPosition.ind - parseInfo.offset + 1)
|
|
186
|
-
?.charCodeAt(0))
|
|
181
|
+
(isWhiteSpace(parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1)?.charCodeAt(0))
|
|
187
182
|
? 1
|
|
188
183
|
: 0));
|
|
189
184
|
continue;
|
|
@@ -213,6 +208,7 @@ function consumeString(quoteStr, buffer, parseInfo) {
|
|
|
213
208
|
function yieldResult(val, parseInfo, hint) {
|
|
214
209
|
let token = null;
|
|
215
210
|
let dimension;
|
|
211
|
+
// console.debug(`Yield result: ${val}, ${hint}`);
|
|
216
212
|
if (hint != null) {
|
|
217
213
|
let searchArray = null;
|
|
218
214
|
switch (hint) {
|
|
@@ -334,17 +330,15 @@ function yieldResult(val, parseInfo, hint) {
|
|
|
334
330
|
}
|
|
335
331
|
// return token;
|
|
336
332
|
token[LOC] = {
|
|
337
|
-
|
|
338
|
-
sta:
|
|
339
|
-
end:
|
|
333
|
+
srcId: parseInfo.source.id,
|
|
334
|
+
sta: parseInfo.position,
|
|
335
|
+
end: parseInfo.currentPosition,
|
|
340
336
|
};
|
|
341
|
-
parseInfo.position
|
|
342
|
-
|
|
343
|
-
parseInfo.position.col = parseInfo.currentPosition.col;
|
|
344
|
-
return { token, bytesIn: parseInfo.currentPosition.ind + 1 };
|
|
337
|
+
parseInfo.position = parseInfo.currentPosition;
|
|
338
|
+
return { token, bytesIn: parseInfo.currentPosition + 1 };
|
|
345
339
|
}
|
|
346
340
|
function match(parseInfo, input) {
|
|
347
|
-
let position = parseInfo.currentPosition
|
|
341
|
+
let position = parseInfo.currentPosition - parseInfo.offset;
|
|
348
342
|
for (let i = 0; i < input.length; i++) {
|
|
349
343
|
if (parseInfo.stream[position + i + 1] != input.charAt(i)) {
|
|
350
344
|
return false;
|
|
@@ -354,31 +348,33 @@ function match(parseInfo, input) {
|
|
|
354
348
|
}
|
|
355
349
|
function peek(parseInfo, count = 1) {
|
|
356
350
|
if (count == 1) {
|
|
357
|
-
return parseInfo.stream.charAt(parseInfo.currentPosition
|
|
351
|
+
return parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1);
|
|
358
352
|
}
|
|
359
|
-
const position = parseInfo.currentPosition
|
|
353
|
+
const position = parseInfo.currentPosition - parseInfo.offset;
|
|
360
354
|
return parseInfo.stream.slice(position + 1, position + count + 1);
|
|
361
355
|
}
|
|
362
356
|
function next(parseInfo, count = 1) {
|
|
363
|
-
let position = parseInfo.currentPosition
|
|
357
|
+
let position = parseInfo.currentPosition - parseInfo.offset;
|
|
364
358
|
let char = count == 1 ? parseInfo.stream.charAt(position + 1) : parseInfo.stream.slice(position + 1, position + 1 + count);
|
|
365
359
|
let i = 0;
|
|
360
|
+
let codepoint;
|
|
366
361
|
for (; i < char.length; i++) {
|
|
367
|
-
|
|
368
|
-
if (codepoint == 0xa ||
|
|
369
|
-
codepoint == 0xb ||
|
|
370
|
-
codepoint == 0xc ||
|
|
371
|
-
codepoint == 0xd ||
|
|
372
|
-
codepoint == 0x2028 ||
|
|
373
|
-
codepoint == 0x2029
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
362
|
+
codepoint = char[i].charCodeAt(0);
|
|
363
|
+
if (codepoint == 0xa || // \n
|
|
364
|
+
codepoint == 0xb || // \v
|
|
365
|
+
codepoint == 0xc || // \f
|
|
366
|
+
codepoint == 0xd || // \r
|
|
367
|
+
codepoint == 0x2028 || // \u2028
|
|
368
|
+
codepoint == 0x2029 // \u2029
|
|
369
|
+
) {
|
|
370
|
+
// \r\n
|
|
371
|
+
if (codepoint == 0xa && i > 0 && char.charCodeAt(i - 1) == 0xd) ;
|
|
372
|
+
else {
|
|
373
|
+
parseInfo.source.lineStarts.lineStarts.push(position + i);
|
|
374
|
+
}
|
|
379
375
|
}
|
|
380
376
|
}
|
|
381
|
-
parseInfo.currentPosition
|
|
377
|
+
parseInfo.currentPosition += char.length;
|
|
382
378
|
return char;
|
|
383
379
|
}
|
|
384
380
|
/**
|
|
@@ -389,17 +385,16 @@ function next(parseInfo, count = 1) {
|
|
|
389
385
|
function tokenize(parseInfo, yieldEOFToken = true) {
|
|
390
386
|
if (typeof parseInfo == "string") {
|
|
391
387
|
parseInfo = {
|
|
392
|
-
stream: parseInfo,
|
|
393
388
|
buffer: "",
|
|
394
|
-
|
|
389
|
+
stream: parseInfo,
|
|
390
|
+
source: new SourceFile(parseInfo, [], ""),
|
|
395
391
|
offset: 0,
|
|
396
392
|
time: 0,
|
|
397
|
-
position:
|
|
398
|
-
currentPosition:
|
|
393
|
+
position: 0,
|
|
394
|
+
currentPosition: -1,
|
|
399
395
|
};
|
|
400
396
|
}
|
|
401
397
|
let value;
|
|
402
|
-
let nextValue;
|
|
403
398
|
let buffer = parseInfo.buffer;
|
|
404
399
|
let charCode;
|
|
405
400
|
let nextCharCode;
|
|
@@ -408,22 +403,23 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
408
403
|
// allow 10 characters buffer for the streaming parser to avoid incomplete tokens
|
|
409
404
|
const endPosition = parseInfo.stream.length - 10;
|
|
410
405
|
parseInfo.buffer = "";
|
|
411
|
-
while ((value =
|
|
412
|
-
nextValue = parseInfo.stream.charAt(parseInfo.currentPosition.ind - parseInfo.offset + 1);
|
|
406
|
+
while ((value = peek(parseInfo))) {
|
|
413
407
|
charCode = value.charCodeAt(0);
|
|
414
|
-
nextCharCode = nextValue.charCodeAt(0);
|
|
408
|
+
// nextCharCode = nextValue.charCodeAt(0);
|
|
409
|
+
// console.debug({value, buffer});
|
|
415
410
|
switch (charCode) {
|
|
416
411
|
case 61 /* TokenMap.EQUALS */:
|
|
417
412
|
if (buffer.length > 0) {
|
|
418
413
|
result.push(yieldResult(buffer, parseInfo));
|
|
419
414
|
buffer = "";
|
|
420
415
|
}
|
|
421
|
-
result.push(yieldResult(
|
|
416
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.DelimTokenType));
|
|
422
417
|
break;
|
|
423
418
|
// '+' or '-'
|
|
424
419
|
case 43 /* TokenMap.PLUS */:
|
|
425
420
|
case 45 /* TokenMap.MINUS */:
|
|
426
|
-
|
|
421
|
+
next(parseInfo);
|
|
422
|
+
if (charCode === 43 /* TokenMap.PLUS */ && !isNumber(peek(parseInfo))) {
|
|
427
423
|
if (buffer.length > 0) {
|
|
428
424
|
result.push(yieldResult(buffer, parseInfo));
|
|
429
425
|
buffer = "";
|
|
@@ -439,7 +435,7 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
439
435
|
result.push(yieldResult(buffer, parseInfo));
|
|
440
436
|
buffer = "";
|
|
441
437
|
}
|
|
442
|
-
result.push(yieldResult(
|
|
438
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.BlockStartTokenType));
|
|
443
439
|
break;
|
|
444
440
|
// '}'
|
|
445
441
|
case 125 /* TokenMap.RIGHT_BRACE */:
|
|
@@ -447,12 +443,13 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
447
443
|
result.push(yieldResult(buffer, parseInfo));
|
|
448
444
|
buffer = "";
|
|
449
445
|
}
|
|
450
|
-
result.push(yieldResult(
|
|
446
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.BlockEndTokenType));
|
|
451
447
|
break;
|
|
452
448
|
// '('
|
|
453
449
|
case 40 /* TokenMap.LEFT_PARENTHESIS */:
|
|
454
450
|
if (buffer.length > 0) {
|
|
455
451
|
if (buffer[0] === ":" && isPseudo(buffer)) {
|
|
452
|
+
next(parseInfo);
|
|
456
453
|
result.push(yieldResult(buffer, parseInfo, EnumToken.PseudoClassFunctionTokenDefType));
|
|
457
454
|
buffer = "";
|
|
458
455
|
break;
|
|
@@ -462,8 +459,10 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
462
459
|
? EnumToken.CustomFunctionTokenDefType
|
|
463
460
|
: (SymbolsMapTokens[buffer.toLowerCase() + "("] ?? EnumToken.FunctionTokenDefType);
|
|
464
461
|
result.push(yieldResult(buffer, parseInfo, hint));
|
|
462
|
+
next(parseInfo);
|
|
465
463
|
buffer = "";
|
|
466
464
|
if (hint === EnumToken.UrlFunctionTokenDefType) {
|
|
465
|
+
buffer = "";
|
|
467
466
|
value = peek(parseInfo);
|
|
468
467
|
// consume an <url>
|
|
469
468
|
while (isWhiteSpace((charCode = value.charCodeAt(0)))) {
|
|
@@ -518,10 +517,11 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
518
517
|
buffer = "";
|
|
519
518
|
}
|
|
520
519
|
}
|
|
520
|
+
// console.debug({value: peek(parseInfo)});
|
|
521
521
|
break;
|
|
522
522
|
}
|
|
523
523
|
}
|
|
524
|
-
result.push(yieldResult(
|
|
524
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.StartParensTokenType));
|
|
525
525
|
buffer = "";
|
|
526
526
|
break;
|
|
527
527
|
// ')'
|
|
@@ -530,7 +530,7 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
530
530
|
result.push(yieldResult(buffer, parseInfo));
|
|
531
531
|
buffer = "";
|
|
532
532
|
}
|
|
533
|
-
result.push(yieldResult(
|
|
533
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.EndParensTokenType));
|
|
534
534
|
break;
|
|
535
535
|
// '['
|
|
536
536
|
case 91 /* TokenMap.LEFT_BRACKETS */:
|
|
@@ -538,7 +538,7 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
538
538
|
result.push(yieldResult(buffer, parseInfo));
|
|
539
539
|
buffer = "";
|
|
540
540
|
}
|
|
541
|
-
result.push(yieldResult(
|
|
541
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.AttrStartTokenType));
|
|
542
542
|
break;
|
|
543
543
|
// ']'
|
|
544
544
|
case 93 /* TokenMap.RIGHT_BRACKETS */:
|
|
@@ -546,21 +546,22 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
546
546
|
result.push(yieldResult(buffer, parseInfo));
|
|
547
547
|
buffer = "";
|
|
548
548
|
}
|
|
549
|
-
result.push(yieldResult(
|
|
549
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.AttrEndTokenType));
|
|
550
550
|
break;
|
|
551
551
|
case 59 /* TokenMap.SEMICOLON */:
|
|
552
552
|
if (buffer.length > 0) {
|
|
553
553
|
result.push(yieldResult(buffer, parseInfo));
|
|
554
554
|
buffer = "";
|
|
555
555
|
}
|
|
556
|
-
result.push(yieldResult(
|
|
556
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.SemiColonTokenType));
|
|
557
557
|
break;
|
|
558
558
|
case 58 /* TokenMap.COLON */:
|
|
559
559
|
if (buffer.length > 0) {
|
|
560
560
|
result.push(yieldResult(buffer, parseInfo));
|
|
561
561
|
buffer = "";
|
|
562
562
|
}
|
|
563
|
-
|
|
563
|
+
next(parseInfo);
|
|
564
|
+
if (peek(parseInfo).charCodeAt(0) == 58 /* TokenMap.COLON */) {
|
|
564
565
|
result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.DoubleColonTokenType));
|
|
565
566
|
break;
|
|
566
567
|
}
|
|
@@ -579,17 +580,15 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
579
580
|
result.push(yieldResult(buffer, parseInfo));
|
|
580
581
|
buffer = "";
|
|
581
582
|
}
|
|
582
|
-
buffer +=
|
|
583
|
-
nextCharCode = parseInfo.stream
|
|
584
|
-
.charAt(parseInfo.currentPosition.ind - parseInfo.offset + 1)
|
|
585
|
-
.charCodeAt(0);
|
|
583
|
+
buffer += next(parseInfo);
|
|
584
|
+
nextCharCode = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1).charCodeAt(0);
|
|
586
585
|
while (nextCharCode == 0x20 ||
|
|
587
586
|
(nextCharCode >= 0x9 && nextCharCode <= 0xd) ||
|
|
588
587
|
nextCharCode == 0x2028 ||
|
|
589
588
|
nextCharCode == 0x2029) {
|
|
590
589
|
value += next(parseInfo);
|
|
591
590
|
nextCharCode = parseInfo.stream
|
|
592
|
-
.charAt(parseInfo.currentPosition
|
|
591
|
+
.charAt(parseInfo.currentPosition - parseInfo.offset + 1)
|
|
593
592
|
.charCodeAt(0);
|
|
594
593
|
}
|
|
595
594
|
result.push(yieldResult(value, parseInfo, EnumToken.WhitespaceTokenType));
|
|
@@ -600,29 +599,29 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
600
599
|
result.push(yieldResult(buffer, parseInfo));
|
|
601
600
|
buffer = "";
|
|
602
601
|
}
|
|
603
|
-
result.push(yieldResult(
|
|
602
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.CommaTokenType));
|
|
604
603
|
break;
|
|
605
604
|
case 36 /* TokenMap.DOLLAR */:
|
|
606
605
|
if (buffer.length > 0) {
|
|
607
606
|
result.push(yieldResult(buffer, parseInfo));
|
|
608
607
|
buffer = "";
|
|
609
608
|
}
|
|
610
|
-
if (
|
|
611
|
-
result.push(yieldResult(
|
|
609
|
+
if (match(parseInfo, "$=")) {
|
|
610
|
+
result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.EndMatchTokenType));
|
|
612
611
|
break;
|
|
613
612
|
}
|
|
614
|
-
buffer +=
|
|
613
|
+
buffer += next(parseInfo);
|
|
615
614
|
break;
|
|
616
615
|
case 126 /* TokenMap.TILDA */:
|
|
617
616
|
if (buffer.length > 0) {
|
|
618
617
|
result.push(yieldResult(buffer, parseInfo));
|
|
619
618
|
buffer = "";
|
|
620
619
|
}
|
|
621
|
-
if (
|
|
622
|
-
result.push(yieldResult(
|
|
620
|
+
if (match(parseInfo, "~=")) {
|
|
621
|
+
result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.IncludeMatchTokenType));
|
|
623
622
|
break;
|
|
624
623
|
}
|
|
625
|
-
result.push(yieldResult(
|
|
624
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.Tilda));
|
|
626
625
|
buffer = "";
|
|
627
626
|
break;
|
|
628
627
|
// case '^':
|
|
@@ -631,22 +630,22 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
631
630
|
result.push(yieldResult(buffer, parseInfo));
|
|
632
631
|
buffer = "";
|
|
633
632
|
}
|
|
634
|
-
if (
|
|
635
|
-
result.push(yieldResult(
|
|
633
|
+
if (match(parseInfo, "^=")) {
|
|
634
|
+
result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.StartMatchTokenType));
|
|
636
635
|
break;
|
|
637
636
|
}
|
|
638
|
-
buffer +=
|
|
637
|
+
buffer += next(parseInfo);
|
|
639
638
|
break;
|
|
640
639
|
case 42 /* TokenMap.STAR */:
|
|
641
640
|
if (buffer.length > 0) {
|
|
642
641
|
result.push(yieldResult(buffer, parseInfo));
|
|
643
642
|
buffer = "";
|
|
644
643
|
}
|
|
645
|
-
if (
|
|
646
|
-
result.push(yieldResult(
|
|
644
|
+
if (match(parseInfo, "*=")) {
|
|
645
|
+
result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.ContainMatchTokenType));
|
|
647
646
|
break;
|
|
648
647
|
}
|
|
649
|
-
result.push(yieldResult(
|
|
648
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.Star));
|
|
650
649
|
buffer = "";
|
|
651
650
|
break;
|
|
652
651
|
case 38 /* TokenMap.AMPERSAND */:
|
|
@@ -654,7 +653,7 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
654
653
|
result.push(yieldResult(buffer, parseInfo));
|
|
655
654
|
buffer = "";
|
|
656
655
|
}
|
|
657
|
-
result.push(yieldResult(
|
|
656
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.NestingSelectorTokenType));
|
|
658
657
|
buffer = "";
|
|
659
658
|
break;
|
|
660
659
|
case 124 /* TokenMap.PIPE */:
|
|
@@ -663,15 +662,15 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
663
662
|
buffer = "";
|
|
664
663
|
}
|
|
665
664
|
// '||'
|
|
666
|
-
if (
|
|
667
|
-
result.push(yieldResult(
|
|
665
|
+
if (match(parseInfo, "||")) {
|
|
666
|
+
result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.ColumnCombinatorTokenType));
|
|
668
667
|
break;
|
|
669
668
|
}
|
|
670
|
-
else if (
|
|
671
|
-
result.push(yieldResult(
|
|
669
|
+
else if (match(parseInfo, "|=")) {
|
|
670
|
+
result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.DashMatchTokenType));
|
|
672
671
|
break;
|
|
673
672
|
}
|
|
674
|
-
result.push(yieldResult(
|
|
673
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.Pipe));
|
|
675
674
|
buffer = "";
|
|
676
675
|
break;
|
|
677
676
|
case 33 /* TokenMap.EXCLAMATION */:
|
|
@@ -679,23 +678,23 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
679
678
|
result.push(yieldResult(buffer, parseInfo));
|
|
680
679
|
buffer = "";
|
|
681
680
|
}
|
|
682
|
-
if (match(parseInfo, "important")) {
|
|
683
|
-
result.push(yieldResult(
|
|
681
|
+
if (match(parseInfo, "!important")) {
|
|
682
|
+
result.push(yieldResult(next(parseInfo, 10), parseInfo, EnumToken.ImportantTokenType));
|
|
684
683
|
buffer = "";
|
|
685
684
|
break;
|
|
686
685
|
}
|
|
687
|
-
buffer +=
|
|
686
|
+
buffer += next(parseInfo);
|
|
688
687
|
break;
|
|
689
688
|
case 47 /* TokenMap.SLASH */:
|
|
690
689
|
if (buffer.length > 0) {
|
|
691
690
|
result.push(yieldResult(buffer, parseInfo));
|
|
692
691
|
buffer = "";
|
|
693
692
|
}
|
|
694
|
-
if (!match(parseInfo, "
|
|
695
|
-
result.push(yieldResult(
|
|
693
|
+
if (!match(parseInfo, "/*")) {
|
|
694
|
+
result.push(yieldResult(next(parseInfo), parseInfo, SymbolsMapTokens[value]));
|
|
696
695
|
break;
|
|
697
696
|
}
|
|
698
|
-
buffer +=
|
|
697
|
+
buffer += next(parseInfo, 2);
|
|
699
698
|
while ((value = next(parseInfo))) {
|
|
700
699
|
if (value == "*") {
|
|
701
700
|
buffer += value;
|
|
@@ -719,11 +718,11 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
719
718
|
result.push(yieldResult(buffer, parseInfo));
|
|
720
719
|
buffer = "";
|
|
721
720
|
}
|
|
722
|
-
if (match(parseInfo, "
|
|
723
|
-
result.push(yieldResult(
|
|
721
|
+
if (match(parseInfo, ">=")) {
|
|
722
|
+
result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.GteTokenType));
|
|
724
723
|
break;
|
|
725
724
|
}
|
|
726
|
-
result.push(yieldResult(
|
|
725
|
+
result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.GtTokenType));
|
|
727
726
|
buffer = "";
|
|
728
727
|
break;
|
|
729
728
|
case 60 /* TokenMap.LOWERTHAN */:
|
|
@@ -731,11 +730,11 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
731
730
|
result.push(yieldResult(buffer, parseInfo));
|
|
732
731
|
buffer = "";
|
|
733
732
|
}
|
|
734
|
-
if (match(parseInfo, "
|
|
735
|
-
result.push(yieldResult(
|
|
733
|
+
if (match(parseInfo, "<=")) {
|
|
734
|
+
result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.LteTokenType));
|
|
736
735
|
break;
|
|
737
736
|
}
|
|
738
|
-
buffer +=
|
|
737
|
+
buffer += next(parseInfo);
|
|
739
738
|
if (match(parseInfo, "!--")) {
|
|
740
739
|
buffer += next(parseInfo, 3);
|
|
741
740
|
while ((value = next(parseInfo))) {
|
|
@@ -758,11 +757,12 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
758
757
|
result.push(yieldResult(buffer, parseInfo));
|
|
759
758
|
buffer = "";
|
|
760
759
|
}
|
|
761
|
-
buffer +=
|
|
760
|
+
buffer += next(parseInfo);
|
|
762
761
|
break;
|
|
763
762
|
case 92 /* TokenMap.REVERSE_SOLIDUS */:
|
|
763
|
+
next(parseInfo);
|
|
764
764
|
// EOF
|
|
765
|
-
if (!(
|
|
765
|
+
if (!(peek(parseInfo))) {
|
|
766
766
|
// end of stream ignore \\
|
|
767
767
|
if (buffer.length > 0) {
|
|
768
768
|
result.push(yieldResult(buffer, parseInfo));
|
|
@@ -770,33 +770,36 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
770
770
|
}
|
|
771
771
|
break;
|
|
772
772
|
}
|
|
773
|
-
buffer +=
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
773
|
+
buffer += value + next(parseInfo);
|
|
774
|
+
// buffer +=
|
|
775
|
+
// (parseInfo.offset == parseInfo.currentPosition
|
|
776
|
+
// ? parseInfo.buffer.slice(-1)
|
|
777
|
+
// : parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset - 1)) + value;
|
|
777
778
|
break;
|
|
778
779
|
case 39 /* TokenMap.SINGLE_QUOTE */:
|
|
779
780
|
case 34 /* TokenMap.DOUBLE_QUOTE */:
|
|
780
|
-
|
|
781
|
-
|
|
781
|
+
if (buffer.length > 0) {
|
|
782
|
+
result.push(yieldResult(buffer, parseInfo));
|
|
783
|
+
buffer = "";
|
|
784
|
+
}
|
|
785
|
+
result.push(...consumeString(parseInfo));
|
|
782
786
|
break;
|
|
783
787
|
case 46 /* TokenMap.DOT */:
|
|
784
788
|
const codepoint = parseInfo.stream
|
|
785
|
-
.charAt(parseInfo.currentPosition
|
|
789
|
+
.charAt(parseInfo.currentPosition - parseInfo.offset + 2)
|
|
786
790
|
.charCodeAt(0);
|
|
787
791
|
if (!isDigit(codepoint) && buffer !== "") {
|
|
788
792
|
result.push(yieldResult(buffer, parseInfo));
|
|
789
|
-
buffer =
|
|
793
|
+
buffer = next(parseInfo, 2);
|
|
790
794
|
break;
|
|
791
795
|
}
|
|
792
|
-
buffer +=
|
|
796
|
+
buffer += next(parseInfo);
|
|
793
797
|
break;
|
|
794
798
|
default:
|
|
795
|
-
buffer +=
|
|
799
|
+
buffer += next(parseInfo);
|
|
796
800
|
break;
|
|
797
801
|
}
|
|
798
|
-
if (!yieldEOFToken &&
|
|
799
|
-
endPosition <= parseInfo.stream.length - parseInfo.currentPosition.ind + parseInfo.offset) {
|
|
802
|
+
if (!yieldEOFToken && endPosition <= parseInfo.stream.length - parseInfo.currentPosition + parseInfo.offset) {
|
|
800
803
|
break;
|
|
801
804
|
}
|
|
802
805
|
}
|
|
@@ -815,31 +818,24 @@ function tokenize(parseInfo, yieldEOFToken = true) {
|
|
|
815
818
|
/**
|
|
816
819
|
* tokenize readable stream
|
|
817
820
|
* @param input
|
|
821
|
+
* @param parseInfo
|
|
818
822
|
*/
|
|
819
823
|
async function* tokenizeStream(input, parseInfo) {
|
|
820
|
-
parseInfo ??= {
|
|
821
|
-
stream: "",
|
|
822
|
-
buffer: "",
|
|
823
|
-
src: "",
|
|
824
|
-
offset: 0,
|
|
825
|
-
time: 0,
|
|
826
|
-
position: { ind: 0, lin: 1, col: 0 },
|
|
827
|
-
currentPosition: { ind: -1, lin: 1, col: 0 },
|
|
828
|
-
};
|
|
829
824
|
const decoder = new TextDecoder("utf-8");
|
|
830
825
|
const reader = input.getReader();
|
|
831
826
|
while (true) {
|
|
832
827
|
const { done, value } = await reader.read();
|
|
833
828
|
const stream = ArrayBuffer.isView(value) ? decoder.decode(value, { stream: true }) : value;
|
|
834
829
|
if (!done) {
|
|
830
|
+
parseInfo.source.append(stream);
|
|
835
831
|
if (typeof parseInfo.stream != "string") {
|
|
836
832
|
parseInfo.stream = stream;
|
|
837
833
|
}
|
|
838
834
|
else {
|
|
839
|
-
parseInfo.stream = (parseInfo.stream.slice(parseInfo.currentPosition
|
|
835
|
+
parseInfo.stream = (parseInfo.stream.slice(parseInfo.currentPosition - parseInfo.offset + 1) +
|
|
840
836
|
stream);
|
|
841
837
|
}
|
|
842
|
-
parseInfo.offset = parseInfo.currentPosition
|
|
838
|
+
parseInfo.offset = parseInfo.currentPosition + 1;
|
|
843
839
|
}
|
|
844
840
|
yield* tokenize(parseInfo, done);
|
|
845
841
|
if (done) {
|
|
@@ -847,22 +843,5 @@ async function* tokenizeStream(input, parseInfo) {
|
|
|
847
843
|
}
|
|
848
844
|
}
|
|
849
845
|
}
|
|
850
|
-
/**
|
|
851
|
-
* Update position
|
|
852
|
-
* @param position
|
|
853
|
-
* @param str
|
|
854
|
-
*/
|
|
855
|
-
function move(position, str) {
|
|
856
|
-
let i = 0;
|
|
857
|
-
for (; i < str.length; i++) {
|
|
858
|
-
if (isNewLine(str[i].charCodeAt(0))) {
|
|
859
|
-
position.lin++;
|
|
860
|
-
position.col = 0;
|
|
861
|
-
}
|
|
862
|
-
else {
|
|
863
|
-
position.col++;
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
}
|
|
867
846
|
|
|
868
|
-
export { SymbolsMapTokens, TokenMap, consumeString, hintsEnum, match,
|
|
847
|
+
export { SymbolsMapTokens, TokenMap, consumeString, hintsEnum, match, next, peek, tokenize, tokenizeStream, yieldResult };
|