@tbela99/css-parser 1.8.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +129 -31
  3. package/dist/index-umd-web.js +27382 -24086
  4. package/dist/index.d.ts +2737 -2200
  5. package/dist/package.json.js +2 -2
  6. package/dist/src/data/properties.json.js +1 -1
  7. package/dist/src/data/validation.json.js +1 -1
  8. package/dist/src/lib/ast/arena-node.js +548 -0
  9. package/dist/src/lib/ast/arena.js +3198 -0
  10. package/dist/src/lib/ast/features/calc.js +85 -76
  11. package/dist/src/lib/ast/features/if.js +155 -115
  12. package/dist/src/lib/ast/features/index.js +1 -1
  13. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  14. package/dist/src/lib/ast/features/prefix.js +184 -183
  15. package/dist/src/lib/ast/features/shorthand.js +40 -46
  16. package/dist/src/lib/ast/features/transform.js +24 -24
  17. package/dist/src/lib/ast/features/type.js +1 -1
  18. package/dist/src/lib/ast/fingerprint.js +333 -0
  19. package/dist/src/lib/ast/math/expression.js +458 -543
  20. package/dist/src/lib/ast/math/math.js +19 -41
  21. package/dist/src/lib/ast/minify.js +590 -647
  22. package/dist/src/lib/ast/transform/compute.js +82 -85
  23. package/dist/src/lib/ast/transform/matrix.js +21 -38
  24. package/dist/src/lib/ast/transform/minify.js +82 -193
  25. package/dist/src/lib/ast/transform/perspective.js +2 -3
  26. package/dist/src/lib/ast/transform/rotate.js +1 -1
  27. package/dist/src/lib/ast/transform/scale.js +1 -1
  28. package/dist/src/lib/ast/transform/skew.js +1 -1
  29. package/dist/src/lib/ast/transform/translate.js +1 -1
  30. package/dist/src/lib/ast/transform/utils.js +2 -2
  31. package/dist/src/lib/ast/types.js +263 -258
  32. package/dist/src/lib/ast/walk.js +246 -610
  33. package/dist/src/lib/fs/resolve.js +1 -1
  34. package/dist/src/lib/parser/declaration/list.js +223 -139
  35. package/dist/src/lib/parser/declaration/map.js +251 -238
  36. package/dist/src/lib/parser/declaration/set.js +74 -66
  37. package/dist/src/lib/parser/linesmap.js +1 -1
  38. package/dist/src/lib/parser/parse.js +1379 -1662
  39. package/dist/src/lib/parser/source.js +7 -4
  40. package/dist/src/lib/parser/tokenize.js +406 -199
  41. package/dist/src/lib/parser/utils/angle.js +11 -13
  42. package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
  43. package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
  44. package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
  45. package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
  46. package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
  47. package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
  48. package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
  49. package/dist/src/lib/parser/utils/at-rule.js +5 -5
  50. package/dist/src/lib/parser/utils/base64.js +2 -2
  51. package/dist/src/lib/parser/utils/cache.js +1 -1
  52. package/dist/src/lib/parser/utils/config.js +1 -1
  53. package/dist/src/lib/parser/utils/declaration.js +233 -234
  54. package/dist/src/lib/parser/utils/eq.js +50 -3
  55. package/dist/src/lib/parser/utils/hash.js +2 -35
  56. package/dist/src/lib/parser/utils/selector.js +309 -387
  57. package/dist/src/lib/parser/utils/text.js +18 -4
  58. package/dist/src/lib/parser/utils/token.js +16 -69
  59. package/dist/src/lib/parser/utils/type.js +37 -24
  60. package/dist/src/lib/printer/render.js +965 -511
  61. package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
  62. package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
  63. package/dist/src/lib/syntax/color/a98rgb.js +1 -1
  64. package/dist/src/lib/syntax/color/alpha.js +36 -37
  65. package/dist/src/lib/syntax/color/cmyk.js +35 -52
  66. package/dist/src/lib/syntax/color/color-mix.js +96 -132
  67. package/dist/src/lib/syntax/color/color.js +222 -231
  68. package/dist/src/lib/syntax/color/hex.js +45 -55
  69. package/dist/src/lib/syntax/color/hsl.js +54 -62
  70. package/dist/src/lib/syntax/color/hwb.js +60 -68
  71. package/dist/src/lib/syntax/color/lab.js +59 -67
  72. package/dist/src/lib/syntax/color/lch.js +59 -67
  73. package/dist/src/lib/syntax/color/oklab.js +60 -68
  74. package/dist/src/lib/syntax/color/oklch.js +60 -67
  75. package/dist/src/lib/syntax/color/p3.js +1 -1
  76. package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
  77. package/dist/src/lib/syntax/color/rec2020.js +1 -1
  78. package/dist/src/lib/syntax/color/relative-color.js +94 -144
  79. package/dist/src/lib/syntax/color/rgb.js +54 -57
  80. package/dist/src/lib/syntax/color/srgb.js +55 -53
  81. package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
  82. package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
  83. package/dist/src/lib/syntax/color/utils/components.js +19 -20
  84. package/dist/src/lib/syntax/color/utils/distance.js +15 -13
  85. package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
  86. package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
  87. package/dist/src/lib/syntax/color/xyz.js +1 -1
  88. package/dist/src/lib/syntax/color/xyzd50.js +1 -1
  89. package/dist/src/lib/syntax/constants.js +2 -16
  90. package/dist/src/lib/syntax/syntax.js +301 -407
  91. package/dist/src/lib/validation/config.js +1 -1
  92. package/dist/src/lib/validation/match.js +959 -723
  93. package/dist/src/lib/validation/parser/parse.js +230 -112
  94. package/dist/src/lib/validation/parser/typedef.js +1 -1
  95. package/dist/src/lib/validation/utils/list.js +27 -12
  96. package/dist/src/node.js +12 -72
  97. package/dist/src/types.js +1 -1
  98. package/dist/src/utils/sync.js +9 -6
  99. package/dist/src/web.js +11 -73
  100. package/package.json +9 -10
  101. package/dist/index.cjs +0 -35874
  102. package/dist/src/lib/ast/clone.js +0 -51
  103. package/dist/src/lib/ast/expand.js +0 -311
  104. package/dist/src/lib/ast/find.js +0 -192
  105. package/dist/src/lib/ast/node.js +0 -59
@@ -2,18 +2,18 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.1
5
+ Version: 2.0.0
6
6
  License: MIT
7
7
  */
8
8
  import { isIdentColor, parseColor, isColor } from '../syntax/syntax.js';
9
9
  import { camelize, equalsIgnoreCase, dasherize } from './utils/text.js';
10
10
  import { renderValue } from '../printer/render.js';
11
- import { EnumToken, EnumAstNodeStatus, ModuleCaseTransformEnum, ModuleScopeEnumOptions } from '../ast/types.js';
11
+ import { EnumToken, EnumAstNodeStatus, ModuleCaseTransformEnum, ModuleScopeEnumOptions, WalkerEvent } from '../ast/types.js';
12
12
  import { minify } from '../ast/minify.js';
13
- import { expand } from '../ast/expand.js';
14
- import { walk, walkValues, WalkerEvent } from '../ast/walk.js';
15
13
  import { Tokenizer } from './tokenize.js';
16
- import { LOCSRCID, LOCSTA, LOCEND, tokensfuncDefMap, STATE, PARENT, TOKENS, ROOT, ERRORS, pageMarginBoxType } from '../syntax/constants.js';
14
+ import { AstArena, TokenField } from '../ast/arena.js';
15
+ import { AstArenaNode, ARENAID } from '../ast/arena-node.js';
16
+ import { tokensfuncDefMap, pageMarginBoxType } from '../syntax/constants.js';
17
17
  import { hashAlgorithms, hash, syncHash } from './utils/hash.js';
18
18
  import { parseSelector } from './utils/selector.js';
19
19
  import { parseDeclaration } from './utils/declaration.js';
@@ -23,7 +23,7 @@ import { ValidationSyntaxGroupEnum } from '../validation/parser/typedef.js';
23
23
  import { matchAtRuleImportSyntax } from './utils/at-rule-import.js';
24
24
  import { matchAtRuleWhenElseSyntax } from './utils/at-rule-when-else.js';
25
25
  import { parseAtRuleSupportSyntax } from './utils/at-rule-support.js';
26
- import { replaceNodeOrValue, trimWhiteSpaceTokens } from './utils/token.js';
26
+ import { trimWhiteSpaceTokens } from './utils/token.js';
27
27
  import { parseAtRuleContainerQueryList } from './utils/at-rule-container.js';
28
28
  import { parseMediaqueryList } from './utils/at-rule-media.js';
29
29
  import { matchAtRuleSyntax } from './utils/at-rule.js';
@@ -32,13 +32,146 @@ import { matchGenericSyntax } from './utils/at-rule-generic.js';
32
32
  import { memoize } from './utils/cache.js';
33
33
  import { SourceFile } from './source.js';
34
34
  import { dirname } from '../fs/resolve.js';
35
+ import { NodeWalker, ValueWalker } from '../ast/walk.js';
35
36
 
36
- function renderTokens(tokens, options) {
37
+ function renderTokens(arena, tokens, options) {
37
38
  if (tokens == null || tokens.length === 0)
38
39
  return "";
39
40
  if (options != null)
40
- return tokens.map((t) => renderValue(t, options)).join("");
41
- return tokens.map((t) => renderValue(t)).join("");
41
+ return tokens.map((t) => renderValue(arena, t, options)).join("");
42
+ return tokens.map((t) => renderValue(arena, t)).join("");
43
+ }
44
+ /**
45
+ * allocate an at-rule node from an at-rule token
46
+ * @param arena
47
+ * @param atRule at-rule token: name and location
48
+ * @param typ
49
+ * @param tokens prelude tokens
50
+ * @param state
51
+ * @param errors
52
+ * @param end last token of the at-rule
53
+ * @param val
54
+ * @param block
55
+ */
56
+ function createAtRuleNode(arena, atRule, typ, tokens, state, errors, end, val, block) {
57
+ const node = arena.createAtRule(typ, arena.strings[arena.name[atRule]], val, block, tokens, state, errors);
58
+ arena.spanLocation(node, atRule, end);
59
+ return node;
60
+ }
61
+ /**
62
+ * convert a visitor result to node ids. the object views of the arena ({@link AstArenaNode}) are not copied, the views
63
+ * of another arena are imported, plain objects are allocated in the arena
64
+ * @param arena
65
+ * @param replacement
66
+ */
67
+ function visitorResult(arena, replacement) {
68
+ if (Array.isArray(replacement)) {
69
+ const result = [];
70
+ for (const item of replacement) {
71
+ if (typeof item == "number") {
72
+ result.push(item);
73
+ }
74
+ else if (item != null && typeof item == "object") {
75
+ result.push(arena.fromObject(item));
76
+ }
77
+ else {
78
+ return null;
79
+ }
80
+ }
81
+ return result;
82
+ }
83
+ if (typeof replacement == "number") {
84
+ return replacement;
85
+ }
86
+ if (replacement != null && typeof replacement == "object") {
87
+ return arena.fromObject(replacement);
88
+ }
89
+ return null;
90
+ }
91
+ /**
92
+ * allocate a comment node from a comment token
93
+ * @param arena
94
+ * @param token
95
+ */
96
+ function createCommentNode(arena, token) {
97
+ const node = arena.createComment(arena.dataTypes[token], arena.strings[arena.stringValue[token]]);
98
+ arena.copyLocation(node, token);
99
+ return node;
100
+ }
101
+ /**
102
+ * initial arena capacity: one node for every 4 bytes of css. the statement tokens that are not attached to the tree
103
+ * are recycled, so a parse + minify allocates 0.07 to 0.22 node per byte (up to 0.5 for keyframes heavy stylesheets
104
+ * or when nesting rules are expanded): the arena seldom grows. the length of a stream is unknown: 4096 nodes
105
+ * @param tokenizer
106
+ */
107
+ function arenaCapacity(tokenizer) {
108
+ const length = tokenizer.streamLength;
109
+ // small inputs (css modules, inline styles, ...) do not allocate a large arena, the arena grows when needed
110
+ return length == 0 ? 4096 : Math.min(1 << 25, Math.ceil(length / 4) + 1024);
111
+ }
112
+ /**
113
+ * allocate the current tokenizer token in the arena
114
+ * @param arena
115
+ * @param tokenizer
116
+ * @param sourceIndex index of the source in the arena sources table. negative when the token has no location
117
+ */
118
+ function allocateToken(arena, tokenizer, sourceIndex) {
119
+ const item = arena.allocate();
120
+ const val = tokenizer.val;
121
+ arena.dataTypes[item] = tokenizer.typ;
122
+ if (tokenizer.unit != null) {
123
+ arena.numericValue[item] = val;
124
+ arena.unit[item] = arena.internString(tokenizer.unit);
125
+ }
126
+ else if (tokenizer.nam != null) {
127
+ arena.name[item] = arena.internString(tokenizer.nam);
128
+ }
129
+ else if (val !== null) {
130
+ if (typeof val == "number") {
131
+ arena.numericValue[item] = val;
132
+ }
133
+ else if (tokenizer.typ != EnumToken.WhitespaceTokenType) {
134
+ // whitespace is always rendered as a single space: the text is not kept
135
+ arena.stringValue[item] = arena.internString(val);
136
+ }
137
+ if (tokenizer.kin != null) {
138
+ // inlined: arena.setColorType(item, tokenizer.kin)
139
+ arena.colorType[item] = tokenizer.kin;
140
+ }
141
+ }
142
+ if (sourceIndex >= 0) {
143
+ arena.setTokenLocation(item, sourceIndex, tokenizer.sta, tokenizer.end);
144
+ }
145
+ return item;
146
+ }
147
+ /**
148
+ * recycle the tokens of a statement that were not attached to the tree: their ids are reused by the next allocations.
149
+ * the tokens referenced by the errors or the invalid nodes of the statement are kept
150
+ * @param arena
151
+ * @param tokens statement tokens
152
+ * @param errors
153
+ * @param errorStart index of the first error of the statement
154
+ * @param invalidNodes
155
+ * @param invalidStart index of the first invalid node of the statement
156
+ */
157
+ function recycleStatementTokens(arena, tokens, errors, errorStart, invalidNodes, invalidStart) {
158
+ for (let i = 0; i < tokens.length; i++) {
159
+ const token = tokens[i];
160
+ // already recycled (a token can be recorded twice), attached or holding other tokens
161
+ if (arena.dataTypes[token] == 0 ||
162
+ arena.parents[token] != 0 ||
163
+ arena.firstChild[token] != 0 ||
164
+ arena.firstToken[token] != 0) {
165
+ continue;
166
+ }
167
+ let referenced = invalidNodes.indexOf(token, invalidStart) != -1;
168
+ for (let j = errorStart; !referenced && j < errors.length; j++) {
169
+ referenced = errors[j].node === token;
170
+ }
171
+ if (!referenced) {
172
+ arena.recycle(token);
173
+ }
174
+ }
42
175
  }
43
176
  const trimWhiteSpace = [
44
177
  EnumToken.CommentTokenType,
@@ -48,12 +181,16 @@ const trimWhiteSpace = [
48
181
  EnumToken.LteTokenType,
49
182
  EnumToken.ColumnCombinatorTokenType,
50
183
  ];
51
- const BadTokensTypes = [
52
- EnumToken.BadCommentTokenType,
53
- EnumToken.BadCdoTokenType,
54
- EnumToken.BadUrlTokenType,
55
- EnumToken.BadStringTokenType,
56
- ];
184
+ /**
185
+ * bad comment, cdo, url or string token
186
+ * @param type
187
+ */
188
+ function isBadToken(type) {
189
+ return (type == EnumToken.BadCommentTokenType ||
190
+ type == EnumToken.BadCdoTokenType ||
191
+ type == EnumToken.BadUrlTokenType ||
192
+ type == EnumToken.BadStringTokenType);
193
+ }
57
194
  let keyNameCounter = 0;
58
195
  /**
59
196
  * Short-scoped name generator.
@@ -327,6 +464,13 @@ function parseVisitors(visitorsDef, errors) {
327
464
  if (typeof value == "function") {
328
465
  key = value.name;
329
466
  }
467
+ else if (value != null && typeof value == "object") {
468
+ // array of visitor maps: flatten. this used to depend on the array index being an EnumToken value
469
+ for (const val of Object.entries(value)) {
470
+ visitors.push(val);
471
+ }
472
+ continue;
473
+ }
330
474
  }
331
475
  if (key in EnumToken) {
332
476
  if (typeof value == "function") {
@@ -497,9 +641,7 @@ function doParseSync(tokenizer, options = {}) {
497
641
  lenient: true,
498
642
  ...options,
499
643
  };
500
- if (typeof options.validation !== "boolean") {
501
- options.validation = !!options.validation;
502
- }
644
+ options.validation = !!options.validation;
503
645
  if (options.module) {
504
646
  options.expandNestingRules = true;
505
647
  }
@@ -523,10 +665,14 @@ function doParseSync(tokenizer, options = {}) {
523
665
  imports: [],
524
666
  };
525
667
  const invalidNodes = [];
526
- let ast = {
527
- typ: EnumToken.StyleSheetNodeType,
528
- chi: [],
529
- };
668
+ // first error / invalid node of the current statement
669
+ let errorStart = errors.length;
670
+ let invalidStart = 0;
671
+ // tokens allocated for the current statement (parseNode() may remove tokens from the statement array)
672
+ const statement = [];
673
+ // nested parses share the arena of the parent
674
+ const arena = options.arena ?? (options.arena = new AstArena(arenaCapacity(tokenizer)));
675
+ let ast = arena.current == 1 ? arena.root : arena.createStyleSheet();
530
676
  let tokens = [];
531
677
  let context = ast;
532
678
  let item;
@@ -534,160 +680,114 @@ function doParseSync(tokenizer, options = {}) {
534
680
  // @ts-ignore ignore error
535
681
  let parensMatch = 0;
536
682
  let curlyBracketMatch = 0;
537
- ast[LOCSRCID] = options.source.id;
538
- ast[LOCSTA] = 0;
539
- while (!tokenizer.done()) {
683
+ // tokens are located in the tokenizer source (the imported file when parsing an @import)
684
+ arena.setLocation(ast, tokenizer.sourceId, 0, 0);
685
+ const sourceIndex = arena.sourceIndex(tokenizer.sourceId);
686
+ let itemType;
687
+ // inlined: tokenizer.done()
688
+ while (!(tokenizer.typ === EnumToken.EOF)) {
540
689
  tokenizer.next();
541
- if (tokenizer.unit != null) {
542
- item = {
543
- typ: tokenizer.typ,
544
- val: tokenizer.val,
545
- unit: tokenizer.unit,
546
- };
547
- }
548
- else if (tokenizer.nam != null) {
549
- item = {
550
- typ: tokenizer.typ,
551
- nam: tokenizer.nam,
552
- };
553
- }
554
- else if (tokenizer.val === null) {
555
- item = {
556
- typ: tokenizer.typ,
557
- };
558
- }
559
- else if (tokenizer.kin != null) {
560
- item = {
561
- typ: tokenizer.typ,
562
- val: tokenizer.val,
563
- kin: tokenizer.kin,
564
- };
565
- }
566
- else {
567
- item = {
568
- typ: tokenizer.typ,
569
- val: tokenizer.val,
570
- };
571
- }
572
- item[LOCSRCID] = tokenizer.srcId;
573
- item[LOCSTA] = tokenizer.sta;
574
- item[LOCEND] = tokenizer.end;
690
+ item = allocateToken(arena, tokenizer, sourceIndex);
691
+ itemType = tokenizer.typ;
575
692
  stats.bytesIn = tokenizer.bytesIn;
576
693
  stats.tokensCount++;
577
- if (BadTokensTypes.includes(item.typ)) {
694
+ if (isBadToken(itemType)) {
578
695
  tokens.push(item);
696
+ statement.push(item);
579
697
  errors.push({
580
698
  action: "drop",
581
699
  message: "Bad token",
582
700
  syntax: null,
583
701
  node: item,
584
- location: options.source.getSourceLocation(item[LOCSTA]),
702
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(item))
703
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(item))],
585
704
  });
586
705
  // bad token
587
706
  continue;
588
707
  }
589
- if (item.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(item.typ)) {
708
+ if (itemType === EnumToken.StartParensTokenType || tokensfuncDefMap.has(itemType)) {
590
709
  parensMatch++;
591
710
  }
592
- else if (item.typ === EnumToken.EndParensTokenType && parensMatch > 0) {
711
+ else if (itemType === EnumToken.EndParensTokenType && parensMatch > 0) {
593
712
  parensMatch--;
594
713
  }
595
- if (item.typ === EnumToken.BlockStartTokenType) {
714
+ if (itemType === EnumToken.BlockStartTokenType) {
596
715
  curlyBracketMatch++;
597
716
  }
598
- else if (item.typ === EnumToken.BlockEndTokenType && curlyBracketMatch > 0) {
717
+ else if (itemType === EnumToken.BlockEndTokenType && curlyBracketMatch > 0) {
599
718
  curlyBracketMatch--;
600
719
  }
601
720
  tokens.push(item);
721
+ statement.push(item);
602
722
  if (parensMatch === 0 &&
603
- (item.typ === EnumToken.SemiColonTokenType ||
604
- item.typ === EnumToken.BlockStartTokenType ||
605
- item.typ === EnumToken.EOFTokenType)) {
606
- node = parseNode(tokens, context, options, errors, stats, invalidNodes);
723
+ (itemType === EnumToken.SemiColonTokenType ||
724
+ itemType === EnumToken.BlockStartTokenType ||
725
+ itemType === EnumToken.EOFTokenType)) {
726
+ node = parseNode(arena, tokens, context, options, errors, stats, invalidNodes, parensMatch);
607
727
  if (node != null) {
608
- if ("chi" in node) {
728
+ if (arena.flags[node] & EnumAstNodeStatus.HasChildren) {
609
729
  stack.push(node);
610
730
  context = node;
611
731
  }
612
732
  }
613
- else if (item.typ == EnumToken.BlockStartTokenType) {
733
+ else if (itemType == EnumToken.BlockStartTokenType) {
614
734
  let inBlock = 1;
615
735
  tokens.length = 0;
616
736
  tokens.push(item);
617
737
  do {
618
738
  tokenizer.next();
619
- if (tokenizer.unit != null) {
620
- item = {
621
- typ: tokenizer.typ,
622
- val: tokenizer.val,
623
- unit: tokenizer.unit,
624
- };
625
- }
626
- else if (tokenizer.nam != null) {
627
- item = {
628
- typ: tokenizer.typ,
629
- nam: tokenizer.nam,
630
- };
631
- }
632
- else if (tokenizer.val === null) {
633
- item = {
634
- typ: tokenizer.typ,
635
- };
636
- }
637
- else if (tokenizer.kin != null) {
638
- item = {
639
- typ: tokenizer.typ,
640
- val: tokenizer.val,
641
- kin: tokenizer.kin,
642
- };
643
- }
644
- else {
645
- item = {
646
- typ: tokenizer.typ,
647
- val: tokenizer.val,
648
- };
649
- }
650
- item[LOCSRCID] = tokenizer.srcId;
651
- item[LOCSTA] = tokenizer.sta;
652
- item[LOCEND] = tokenizer.end;
739
+ item = allocateToken(arena, tokenizer, sourceIndex);
740
+ itemType = tokenizer.typ;
653
741
  tokens.push(item);
654
- if (item.typ === EnumToken.BlockStartTokenType) {
742
+ statement.push(item);
743
+ if (itemType === EnumToken.BlockStartTokenType) {
655
744
  inBlock++;
656
745
  }
657
- else if (item.typ === EnumToken.BlockEndTokenType) {
746
+ else if (itemType === EnumToken.BlockEndTokenType) {
658
747
  inBlock--;
659
748
  }
660
- } while (inBlock != 0 && !tokenizer.done());
749
+ // inlined: tokenizer.done()
750
+ } while (inBlock != 0 && !(tokenizer.typ === EnumToken.EOF));
661
751
  if (tokens.length > 0) {
662
752
  errors.push({
663
753
  action: "drop",
664
754
  message: "invalid block",
665
- location: options.source.getSourceLocation(tokens[0][LOCSTA]),
755
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(tokens[0]))
756
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[0]))],
666
757
  });
667
758
  }
668
759
  }
760
+ recycleStatementTokens(arena, statement, errors, errorStart, invalidNodes, invalidStart);
761
+ statement.length = 0;
669
762
  tokens.length = 0;
763
+ errorStart = errors.length;
764
+ invalidStart = invalidNodes.length;
670
765
  }
671
- else if ((parensMatch === 0 || curlyBracketMatch === 0) && item.typ === EnumToken.BlockEndTokenType) {
672
- parseNode(tokens, context, options, errors, stats, invalidNodes);
673
- context[LOCEND] = item[LOCEND];
766
+ else if ((parensMatch === 0 || curlyBracketMatch === 0) && itemType === EnumToken.BlockEndTokenType) {
767
+ parseNode(arena, tokens, context, options, errors, stats, invalidNodes, parensMatch);
768
+ // inlined: arena.setSpanEnd(context, arena.getSpanEnd(item))
769
+ arena.setSpan(context, arena.getSourceId(context), arena.getSpanStart(context), arena.getSpanEnd(item));
674
770
  const previousNode = stack.pop();
675
- context = (stack[stack.length - 1] ?? ast);
771
+ context = stack[stack.length - 1] ?? ast;
676
772
  if (options.removeEmpty &&
677
773
  previousNode != null &&
678
- previousNode.chi.length == 0 &&
679
- context.chi[context.chi.length - 1] == previousNode) {
680
- context.chi.pop();
774
+ arena.firstChild[previousNode] == 0 &&
775
+ arena.lastChild[context] == previousNode) {
776
+ arena.detach(previousNode);
681
777
  }
778
+ recycleStatementTokens(arena, statement, errors, errorStart, invalidNodes, invalidStart);
779
+ statement.length = 0;
682
780
  tokens.length = 0;
781
+ errorStart = errors.length;
782
+ invalidStart = invalidNodes.length;
683
783
  parensMatch = 0;
684
784
  curlyBracketMatch = 0;
685
785
  }
686
786
  }
687
787
  if (tokens.length > 0) {
688
- node = parseNode(tokens, context, options, errors, stats, invalidNodes);
788
+ node = parseNode(arena, tokens, context, options, errors, stats, invalidNodes, parensMatch);
689
789
  if (node != null) {
690
- if ("chi" in node) {
790
+ if (arena.flags[node] & EnumAstNodeStatus.HasChildren) {
691
791
  stack.push(node);
692
792
  context = node;
693
793
  }
@@ -695,238 +795,57 @@ function doParseSync(tokenizer, options = {}) {
695
795
  }
696
796
  const endParseTime = performance.now();
697
797
  if (options.expandNestingRules) {
698
- ast = expand(ast);
798
+ ast = arena.expand(ast);
699
799
  }
700
800
  let replacement;
701
801
  if (options.visitor != null) {
702
802
  const handlers = [];
703
803
  const visitors = parseVisitors(options.visitor, errors);
704
- let parens;
705
804
  let genericKey;
706
805
  let nodes = [ast];
707
806
  let node;
708
- while (nodes.length > 0) {
709
- node = nodes.pop();
710
- if (visitors.includeTokens) {
711
- switch (node.typ) {
712
- case EnumToken.RuleNodeType:
713
- case EnumToken.AtRuleNodeType:
714
- case EnumToken.KeyframesRuleNodeType:
715
- case EnumToken.KeyframesAtRuleNodeType:
716
- for (let i =
717
- // @ts-ignore
718
- node[TOKENS].length -
719
- 1; i >= 0; i--) {
720
- // @ts-ignore
721
- node[TOKENS][i][PARENT] =
722
- node;
723
- nodes.push(
724
- // @ts-ignore
725
- node[TOKENS][i]);
726
- }
727
- break;
728
- case EnumToken.DeclarationNodeType:
729
- for (let i =
730
- // @ts-ignore
731
- node.val.length - 1; i >= 0; i--) {
732
- node.val[i][PARENT] = node;
733
- // @ts-ignore
734
- nodes.push(node.val[i]);
735
- }
736
- break;
807
+ let nodeType;
808
+ // push the children of a node or the sub tokens of a value token onto the stack
809
+ const enqueue = (node) => {
810
+ if (arena.flags[node] & EnumAstNodeStatus.HasChildren) {
811
+ for (let child = arena.lastChild[node]; child != 0; child = arena.previousSibling[child]) {
812
+ nodes.push(child);
737
813
  }
738
- // @ts-ignore
739
- // if (node.l != null) {
740
- // // @ts-ignore
741
- // if (node.op2 != null) {
742
- // // @ts-ignore
743
- // nodes.push(node.op2);
744
- // // @ts-ignore
745
- // node.op2[PARENT] = node;
746
- // }
747
- // // @ts-ignore
748
- // if (node.op1 != null) {
749
- // // @ts-ignore
750
- // nodes.push(node.op1);
751
- // // @ts-ignore
752
- // node.op1[PARENT] = node;
753
- // }
754
- // // @ts-ignore
755
- // if (nodes.op != null && node.op.typ != null) {
756
- // // @ts-ignore
757
- // nodes.push(node.op);
758
- // // @ts-ignore
759
- // node.op[PARENT] = node;
760
- // }
761
- // // @ts-ignore
762
- // if (node.r != null) {
763
- // // @ts-ignore
764
- // if (Array.isArray(node.r)) {
765
- // // @ts-ignore
766
- // for (let i = node.r.length - 1; i >= 0; i--) {
767
- // // @ts-ignore
768
- // nodes.push(node.r[i]);
769
- // // @ts-ignore
770
- // node.r[i][PARENT] = node;
771
- // }
772
- // } else {
773
- // // @ts-ignore
774
- // nodes.push(node.r);
775
- // // @ts-ignore
776
- // node.r[PARENT] = node;
777
- // }
778
- // }
779
- // // @ts-ignore
780
- // if (Array.isArray(node.l)) {
781
- // // @ts-ignore
782
- // for (let i = node.l.length - 1; i >= 0; i--) {
783
- // // @ts-ignore
784
- // nodes.push(node.l[i]);
785
- // // @ts-ignore
786
- // node.l[i][PARENT] = node;
787
- // }
788
- // } else {
789
- // // @ts-ignore
790
- // nodes.push(node.l);
791
- // // @ts-ignore
792
- // node.l[PARENT] = node;
793
- // }
794
- // }
795
- }
796
- switch (node.typ) {
797
- case EnumToken.BinaryExpressionTokenType:
798
- case EnumToken.IfElseConditionTokenType:
799
- nodes.push(node.r);
800
- node.r[PARENT] = node;
801
- nodes.push(node.l);
802
- node.l[PARENT] = node;
803
- break;
804
- case EnumToken.MatchExpressionTokenType:
805
- nodes.push(node.r);
806
- node.r[PARENT] = node;
807
- nodes.push(node.op);
808
- node.op[PARENT] = node;
809
- nodes.push(node.l);
810
- node.l[PARENT] = node;
811
- break;
812
- case EnumToken.SupportsQueryConditionTokenType:
813
- case EnumToken.MediaQueryConditionTokenType:
814
- for (let i = node.r.length - 1; i >= 0; i--) {
815
- const element = node.r[i];
816
- nodes.push(element);
817
- element[PARENT] = node;
818
- }
819
- nodes.push(node.op);
820
- node.op[PARENT] = node;
821
- for (let i = node.l.length - 1; i >= 0; i--) {
822
- const element = node.l[i];
823
- nodes.push(element);
824
- element[PARENT] = node;
825
- }
826
- break;
827
- case EnumToken.WhenElseQueryConditionTokenType:
828
- for (let i = node.r.length - 1; i >= 0; i--) {
829
- const element = node.r[i];
830
- nodes.push(element);
831
- element[PARENT] = node;
832
- }
833
- nodes.push(node.l);
834
- node.l[PARENT] = node;
835
- break;
836
- case EnumToken.IfConditionTokenType:
837
- for (let i = node.r.length - 1; i >= 0; i--) {
838
- const element = node.r[i];
839
- nodes.push(element);
840
- element[PARENT] = node;
841
- }
842
- for (let i = node.l.length - 1; i >= 0; i--) {
843
- const element = node.l[i];
844
- nodes.push(element);
845
- element[PARENT] = node;
846
- }
847
- break;
848
- case EnumToken.ComposesSelectorNodeType:
849
- if (node.r != null) {
850
- nodes.push(node.r);
851
- node.r[PARENT] = node;
852
- }
853
- for (let i = node.l.length - 1; i >= 0; i--) {
854
- const element = node.l[i];
855
- nodes.push(element);
856
- element[PARENT] = node;
857
- }
858
- break;
859
- case EnumToken.ContainerStyleRangeTokenType:
860
- for (let i = node.r.length - 1; i >= 0; i--) {
861
- const element = node.r[i];
862
- nodes.push(element);
863
- element[PARENT] = node;
864
- }
865
- for (let i = node.op.length - 1; i >= 0; i--) {
866
- const element = node.op[i];
867
- nodes.push(element);
868
- element[PARENT] = node;
869
- }
870
- for (let i = node.l.length - 1; i >= 0; i--) {
871
- const element = node.l[i];
872
- nodes.push(element);
873
- element[PARENT] = node;
874
- }
875
- break;
876
- case EnumToken.MediaRangeQueryTokenType:
877
- for (let i = node.r.length - 1; i >= 0; i--) {
878
- const element = node.r[i];
879
- nodes.push(element);
880
- element[PARENT] = node;
881
- }
882
- nodes.push(node.op2);
883
- node.op2[PARENT] = node;
884
- for (let i = node.val.length - 1; i >= 0; i--) {
885
- const element = node.val[i];
886
- nodes.push(element);
887
- element[PARENT] = node;
888
- }
889
- nodes.push(node.op1);
890
- node.op1[PARENT] = node;
891
- for (let i = node.l.length - 1; i >= 0; i--) {
892
- const element = node.l[i];
893
- nodes.push(element);
894
- element[PARENT] = node;
895
- }
896
- break;
897
814
  }
898
- // @ts-ignore
899
- if (node.chi != null) {
900
- for (
901
- // @ts-ignore
902
- let i = node.chi.length - 1; i >= 0; i--) {
903
- // @ts-ignore
904
- nodes.push(node.chi[i]);
815
+ if (visitors.includeTokens &&
816
+ arena.dataTypes[node] != EnumToken.RuleNodeType &&
817
+ arena.dataTypes[node] != EnumToken.KeyframesRuleNodeType) {
818
+ // at-rule prelude, declaration values or token children. selector tokens are not visited
819
+ const values = arena.subTokens(node);
820
+ for (let i = values.length - 1; i >= 0; i--) {
821
+ nodes.push(values[i]);
905
822
  }
906
823
  }
907
- parens = null;
824
+ };
825
+ while (nodes.length > 0) {
826
+ node = nodes.pop();
827
+ nodeType = arena.dataTypes[node];
828
+ replacement = null;
908
829
  handlers.length = 0;
909
830
  if (visitors.preAllVisitorsHandlers?.size > 0) {
910
831
  handlers.push(...visitors.preAllVisitorsHandlers);
911
832
  }
912
833
  genericKey =
913
- node.typ == EnumToken.DeclarationNodeType
834
+ nodeType == EnumToken.DeclarationNodeType
914
835
  ? "Declaration"
915
- : node.typ == EnumToken.AtRuleNodeType
836
+ : nodeType == EnumToken.AtRuleNodeType
916
837
  ? "AtRule"
917
- : node.typ == EnumToken.KeyframesAtRuleNodeType
838
+ : nodeType == EnumToken.KeyframesAtRuleNodeType
918
839
  ? "KeyframesAtRule"
919
- : node.typ === EnumToken.KeyframesRuleNodeType
840
+ : nodeType === EnumToken.KeyframesRuleNodeType
920
841
  ? "KeyframesRule"
921
- : node.typ == EnumToken.RuleNodeType
842
+ : nodeType == EnumToken.RuleNodeType
922
843
  ? "Rule"
923
- : node.typ == EnumToken.KeyframesRuleNodeType
924
- ? "KeyframesRule"
925
- : null;
926
- let keyName = node.typ == EnumToken.DeclarationNodeType || node.typ == EnumToken.AtRuleNodeType
927
- ? camelize(node.nam)
928
- : node.typ == EnumToken.KeyframesAtRuleNodeType
929
- ? camelize(node.val)
844
+ : null;
845
+ let keyName = nodeType == EnumToken.DeclarationNodeType || nodeType == EnumToken.AtRuleNodeType
846
+ ? camelize(arena.strings[arena.name[node]])
847
+ : nodeType == EnumToken.KeyframesAtRuleNodeType
848
+ ? camelize(arena.strings[arena.stringValue[node]])
930
849
  : null;
931
850
  if (visitors.allVisitorsHandlers?.size > 0) {
932
851
  handlers.push(...visitors.allVisitorsHandlers);
@@ -946,9 +865,9 @@ function doParseSync(tokenizer, options = {}) {
946
865
  }
947
866
  }
948
867
  }
949
- if (map.has(node.typ)) {
868
+ if (map.has(nodeType)) {
950
869
  // @ts-ignore
951
- for (const handler of map.get(node.typ)) {
870
+ for (const handler of map.get(nodeType)) {
952
871
  if (typeof handler == "function") {
953
872
  handlers.push(handler);
954
873
  }
@@ -973,63 +892,79 @@ function doParseSync(tokenizer, options = {}) {
973
892
  if (visitors.postAllVisitorsHandlers?.size > 0) {
974
893
  handlers.push(...visitors.postAllVisitorsHandlers);
975
894
  }
976
- if (handlers.length == 0) {
977
- continue;
978
- }
979
- for (const callable of handlers) {
980
- replacement = callable(node, node[PARENT], ast,
981
- // @ts-expect-error
982
- function* () {
983
- if (parens == null) {
984
- let parent = node[PARENT];
985
- while (parent != null) {
986
- yield parent;
987
- parent = parent[PARENT];
988
- }
895
+ if (handlers.length > 0) {
896
+ for (const callable of handlers) {
897
+ // the parent, the root, ... are read from the arena
898
+ replacement = callable(arena, node);
899
+ if (replacement == null) {
900
+ continue;
901
+ }
902
+ if (replacement == node) {
903
+ continue;
904
+ }
905
+ //
906
+ if (Array.isArray(replacement)) {
907
+ break;
989
908
  }
990
- });
991
- if (replacement == null) {
992
- continue;
993
- }
994
- if (replacement == node) {
995
- continue;
996
909
  }
997
910
  // @ts-ignore
998
- // node = replacement as AstNode;
999
- //
1000
- if (Array.isArray(replacement)) {
1001
- break;
911
+ if (replacement != null && node != replacement) {
912
+ // nodes and tokens are replaced by ids allocated in the arena. the object views of the arena are
913
+ // not copied, plain objects are imported
914
+ replacement = visitorResult(arena, replacement);
915
+ if (replacement == node) ;
916
+ else if (replacement != null) {
917
+ if (arena.flags[node] & (EnumAstNodeStatus.HasChildren | EnumAstNodeStatus.HasValues)) {
918
+ arena.replaceChild(node, ...(Array.isArray(replacement) ? replacement : [replacement]));
919
+ }
920
+ else {
921
+ arena.replaceToken(node, ...(Array.isArray(replacement) ? replacement : [replacement]));
922
+ }
923
+ }
924
+ else {
925
+ errors.push({
926
+ action: "ignore",
927
+ message: "doParse: visitor must return node ids, objects or arrays of node ids and objects",
928
+ node,
929
+ });
930
+ }
1002
931
  }
1003
932
  }
1004
- // @ts-ignore
1005
- if (replacement != null && node != replacement) {
1006
- // @ts-ignore
1007
- replaceNodeOrValue(node[PARENT], node, replacement);
933
+ // visit the children of the node or of its replacement
934
+ if (replacement == null || replacement == node) {
935
+ enqueue(node);
936
+ }
937
+ else if (Array.isArray(replacement)) {
938
+ for (let i = replacement.length - 1; i >= 0; i--) {
939
+ enqueue(replacement[i]);
940
+ }
941
+ }
942
+ else {
943
+ enqueue(replacement);
1008
944
  }
1009
945
  }
1010
946
  nodes = null;
1011
947
  }
1012
948
  if (invalidNodes.length > 0) {
1013
949
  for (const node of invalidNodes) {
1014
- if (options.lenient && node[STATE] & EnumAstNodeStatus.Unknown) {
950
+ if (options.lenient && arena.flags[node] & EnumAstNodeStatus.Unknown) {
1015
951
  continue;
1016
952
  }
1017
- if (node[STATE] &
953
+ if (arena.flags[node] &
1018
954
  (EnumAstNodeStatus.Invalid |
1019
955
  EnumAstNodeStatus.Unknown |
1020
956
  EnumAstNodeStatus.Unparsed |
1021
957
  EnumAstNodeStatus.Malformed |
1022
958
  EnumAstNodeStatus.Disallowed)) {
1023
- // @ts-ignore
1024
- node[PARENT].chi.splice(node[PARENT].chi.indexOf(node), 1);
959
+ arena.detach(node);
1025
960
  }
1026
961
  }
1027
962
  }
1028
963
  if (options.minifyAST) {
1029
- if (ast.chi.length > 0) {
964
+ if (arena.firstChild[ast] != 0) {
1030
965
  let passes = options.pass ?? 1;
1031
966
  while (passes--) {
1032
- minify(ast, options, true, errors, false);
967
+ minify(arena, ast, options, true, errors, false);
1033
968
  }
1034
969
  }
1035
970
  }
@@ -1037,6 +972,7 @@ function doParseSync(tokenizer, options = {}) {
1037
972
  let endTime = performance.now();
1038
973
  const result = {
1039
974
  ast,
975
+ arena,
1040
976
  errors,
1041
977
  stats: {
1042
978
  ...stats,
@@ -1112,15 +1048,22 @@ function doParseSync(tokenizer, options = {}) {
1112
1048
  moduleSettings.filePath = filePath;
1113
1049
  moduleSettings.pattern =
1114
1050
  pattern != null && pattern !== "" ? pattern : filePath === "" ? `[local]_[hash]` : `[local]_[hash]_[name]`;
1115
- for (const { node, parent } of walk(ast)) {
1116
- if (node.typ == EnumToken.CssVariableImportTokenType) {
1051
+ // inlined: arena.walk(ast)
1052
+ for (const node of new NodeWalker(arena, ast)) {
1053
+ const parent = arena.parents[node];
1054
+ const nodeType = arena.dataTypes[node];
1055
+ if (nodeType == EnumToken.CssVariableImportTokenType) {
1117
1056
  throw new Error("css variable import not supported by parseSync() or transformSync(). use parse() or transform() instead.\nat " +
1118
- options.source.getSourceLocation(node[LOCSTA]).join(":"));
1057
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(node as AstNode))
1058
+ [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(node))].join(":"));
1119
1059
  }
1120
1060
  // @ts-ignore
1121
- if (node.typ == EnumToken.CssVariableDeclarationMapTokenType) {
1061
+ if (nodeType == EnumToken.CssVariableDeclarationMapTokenType) {
1122
1062
  // @ts-ignore
1123
- const from = node.from.find((t) => t.typ == EnumToken.IdenTokenType || isIdentColor(t));
1063
+ const fromToken = arena
1064
+ .fieldTokens(node, TokenField.L)
1065
+ .find((t) => arena.dataTypes[t] == EnumToken.IdenTokenType || isIdentColor(arena, t));
1066
+ const from = { val: arena.strings[arena.stringValue[fromToken]] };
1124
1067
  if (!(from.val in cssVariablesMap)) {
1125
1068
  errors.push({
1126
1069
  node,
@@ -1130,63 +1073,66 @@ function doParseSync(tokenizer, options = {}) {
1130
1073
  }
1131
1074
  else {
1132
1075
  // @ts-ignore
1133
- for (const token of node.vars) {
1134
- if (token.typ == EnumToken.IdenTokenType || isIdentColor(token)) {
1135
- if (!(token.val in cssVariablesMap[from.val])) {
1076
+ for (const token of arena.tokens(node)) {
1077
+ if (arena.dataTypes[token] == EnumToken.IdenTokenType || isIdentColor(arena, token)) {
1078
+ if (!(arena.strings[arena.stringValue[token]] in cssVariablesMap[from.val])) {
1136
1079
  errors.push({
1137
1080
  node,
1138
- message: `value '${token.val}' is not exported from '${from.val}'`,
1081
+ message: `value '${arena.strings[arena.stringValue[token]]}' is not exported from '${from.val}'`,
1139
1082
  action: "drop",
1140
1083
  });
1141
1084
  continue;
1142
1085
  }
1143
1086
  result.cssModuleVariables ??= {};
1144
- result.cssModuleVariables[token.val] = importedCssVariables[token.val] = cssVariablesMap[from.val][token.val];
1087
+ result.cssModuleVariables[arena.strings[arena.stringValue[token]]] = importedCssVariables[arena.strings[arena.stringValue[token]]] = cssVariablesMap[from.val][arena.strings[arena.stringValue[token]]];
1145
1088
  }
1146
1089
  }
1147
1090
  }
1148
- parent.chi.splice(parent.chi.indexOf(node), 1);
1091
+ arena.detach(node);
1149
1092
  continue;
1150
1093
  }
1151
- if (node.typ == EnumToken.CssVariableTokenType) {
1152
- if (parent?.typ == EnumToken.StyleSheetNodeType) {
1094
+ if (nodeType == EnumToken.CssVariableTokenType) {
1095
+ if (parent != null && arena.dataTypes[parent] == EnumToken.StyleSheetNodeType) {
1153
1096
  if (result.cssModuleVariables == null) {
1154
1097
  result.cssModuleVariables = {};
1155
1098
  }
1156
- // @ts-ignore
1157
- result.cssModuleVariables[node.nam] = node;
1099
+ // inlined: cssVariableToken(arena, node as AstNode): the object view of the css variable node
1100
+ // inlined: arena.toObject(node as AstNode)
1101
+ result.cssModuleVariables[arena.strings[arena.name[node]]] = new AstArenaNode(arena, node);
1158
1102
  }
1159
- parent.chi.splice(parent.chi.indexOf(node), 1);
1103
+ arena.detach(node);
1160
1104
  continue;
1161
1105
  }
1162
- if (node.typ == EnumToken.DeclarationNodeType) {
1163
- if (node.nam.startsWith("--")) {
1164
- if (!(node.nam in namesMapping)) {
1106
+ if (nodeType == EnumToken.DeclarationNodeType) {
1107
+ if (arena.strings[arena.name[node]].startsWith("--")) {
1108
+ if (!(arena.strings[arena.name[node]] in namesMapping)) {
1165
1109
  let value = moduleSettings.scoped & ModuleScopeEnumOptions.Global
1166
- ? node.nam
1167
- : moduleSettings.generateScopedName(node.nam, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
1168
- mapping[node.nam] =
1110
+ ? arena.strings[arena.name[node]]
1111
+ : moduleSettings.generateScopedName(arena.strings[arena.name[node]], moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
1112
+ mapping[arena.strings[arena.name[node]]] =
1169
1113
  "--" +
1170
1114
  (moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
1171
1115
  moduleSettings.naming & ModuleCaseTransformEnum.CamelCaseOnly
1172
1116
  ? getKeyName(value, moduleSettings.naming)
1173
1117
  : value);
1174
- revMapping[node.nam] = node.nam;
1118
+ revMapping[arena.strings[arena.name[node]]] =
1119
+ arena.strings[arena.name[node]];
1175
1120
  }
1176
- node.nam = mapping[node.nam];
1121
+ // inlined: arena.setNameString(node as AstNode, mapping[arena.strings[arena.name[node as AstNode]]])
1122
+ arena.name[node] = arena.internString(mapping[arena.strings[arena.name[node]]]);
1177
1123
  }
1178
- if (equalsIgnoreCase("composes", node.nam)) {
1124
+ if (equalsIgnoreCase("composes", arena.strings[arena.name[node]])) {
1179
1125
  const composeSelectors = [];
1180
1126
  // let isValid: boolean = true;
1181
- for (const token of node.val) {
1182
- if (token.typ == EnumToken.ComposesSelectorNodeType) {
1127
+ for (let token = arena.firstToken[node]; token != 0; token = arena.nextSibling[token]) {
1128
+ if (arena.dataTypes[token] == EnumToken.ComposesSelectorNodeType) {
1183
1129
  composeSelectors.push(token);
1184
1130
  }
1185
1131
  }
1186
1132
  // find parent rule
1187
- let parentRule = parent;
1188
- while (parentRule != null && parentRule.typ != EnumToken.RuleNodeType) {
1189
- parentRule = parentRule[PARENT];
1133
+ let parentRule = parent ?? 0;
1134
+ while (parentRule != 0 && arena.dataTypes[parentRule] != EnumToken.RuleNodeType) {
1135
+ parentRule = arena.parents[parentRule];
1190
1136
  }
1191
1137
  if ( /* !isValid || */composeSelectors.length == 0) {
1192
1138
  errors.push({
@@ -1194,39 +1140,40 @@ function doParseSync(tokenizer, options = {}) {
1194
1140
  message: `composes is empty`,
1195
1141
  node,
1196
1142
  });
1197
- parentRule.chi.splice(parentRule.chi.indexOf(node), 1);
1143
+ arena.detach(node);
1198
1144
  continue;
1199
1145
  }
1200
1146
  for (const token of composeSelectors) {
1201
1147
  // composes: a b c;
1202
- if (token.r == null) {
1203
- for (const rule of token.l) {
1204
- if (rule.typ == EnumToken.WhitespaceTokenType ||
1205
- rule.typ == EnumToken.CommentTokenType) {
1148
+ if (!arena.hasField(token, TokenField.R)) {
1149
+ for (const rule of arena.fieldTokens(token, TokenField.L)) {
1150
+ if (arena.dataTypes[rule] == EnumToken.WhitespaceTokenType ||
1151
+ arena.dataTypes[rule] == EnumToken.CommentTokenType) {
1206
1152
  continue;
1207
1153
  }
1208
- if (!(rule.val in mapping)) {
1154
+ if (!(arena.strings[arena.stringValue[rule]] in mapping)) {
1209
1155
  let value = moduleSettings.scoped & ModuleScopeEnumOptions.Global
1210
- ? rule.val
1211
- : moduleSettings.generateScopedName(rule.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
1212
- mapping[rule.val] =
1213
- (rule.typ == EnumToken.DashedIdenTokenType ? "--" : "") +
1156
+ ? arena.strings[arena.stringValue[rule]]
1157
+ : moduleSettings.generateScopedName(arena.strings[arena.stringValue[rule]], moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
1158
+ mapping[arena.strings[arena.stringValue[rule]]] =
1159
+ (arena.dataTypes[rule] == EnumToken.DashedIdenTokenType ? "--" : "") +
1214
1160
  (moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
1215
1161
  moduleSettings.naming & ModuleCaseTransformEnum.CamelCaseOnly
1216
1162
  ? getKeyName(value, moduleSettings.naming)
1217
1163
  : value);
1218
- revMapping[mapping[rule.val]] = rule.val;
1164
+ revMapping[mapping[arena.strings[arena.stringValue[rule]]]] =
1165
+ arena.strings[arena.stringValue[rule]];
1219
1166
  }
1220
- if (parentRule != null) {
1221
- for (const tk of parentRule[TOKENS]) {
1222
- if (tk.typ == EnumToken.ClassSelectorTokenType) {
1223
- const val = tk.val.slice(1);
1167
+ if (parentRule != 0) {
1168
+ for (const tk of arena.tokens(parentRule)) {
1169
+ if (arena.dataTypes[tk] == EnumToken.ClassSelectorTokenType) {
1170
+ const val = arena.strings[arena.stringValue[tk]].slice(1);
1224
1171
  if (val in revMapping) {
1225
1172
  const key = revMapping[val];
1226
1173
  mapping[key] = [
1227
1174
  ...new Set([
1228
1175
  ...mapping[key].split(" "),
1229
- mapping[rule.val],
1176
+ mapping[arena.strings[arena.stringValue[rule]]],
1230
1177
  ]),
1231
1178
  ].join(" ");
1232
1179
  }
@@ -1236,28 +1183,31 @@ function doParseSync(tokenizer, options = {}) {
1236
1183
  }
1237
1184
  }
1238
1185
  // composes: a b c from 'file.css';
1239
- else if (token.r.typ == EnumToken.String) {
1240
- throw new Error(`composes from file is not supported using parseSync() or transformSync(). Use parse() or transform() instead.\nat ${options.source.getSourceLocation(node[LOCSTA]).join(":")}`);
1186
+ else if (arena.dataTypes[arena.field(token, TokenField.R)] == EnumToken.StringTokenType) {
1187
+ throw new Error(
1188
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(node as AstNode))
1189
+ `composes from file is not supported using parseSync() or transformSync(). Use parse() or transform() instead.\nat ${[options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(node))].join(":")}`);
1241
1190
  }
1242
1191
  // composes: a b c from global;
1243
- else if (token.r.typ == EnumToken.IdenTokenType) {
1192
+ else if (arena.dataTypes[arena.field(token, TokenField.R)] == EnumToken.IdenTokenType) {
1244
1193
  // global
1245
- if (parentRule != null) {
1246
- if (equalsIgnoreCase("global", token.r.val)) {
1247
- for (const tk of parentRule[TOKENS]) {
1248
- if (tk.typ == EnumToken.ClassSelectorTokenType) {
1249
- const val = tk.val.slice(1);
1194
+ if (parentRule != 0) {
1195
+ if (equalsIgnoreCase("global", arena.strings[arena.stringValue[arena.field(token, TokenField.R)]])) {
1196
+ for (const tk of arena.tokens(parentRule)) {
1197
+ if (arena.dataTypes[tk] == EnumToken.ClassSelectorTokenType) {
1198
+ const val = arena.strings[arena.stringValue[tk]].slice(1);
1250
1199
  if (val in revMapping) {
1251
1200
  const key = revMapping[val];
1201
+ const composedNames = [];
1202
+ for (const curr of arena.fieldTokens(token, TokenField.L)) {
1203
+ if (arena.dataTypes[curr] == EnumToken.IdenTokenType) {
1204
+ composedNames.push(arena.strings[arena.stringValue[curr]]);
1205
+ }
1206
+ }
1252
1207
  mapping[key] = [
1253
1208
  ...new Set([
1254
1209
  ...mapping[key].split(" "),
1255
- ...token.l.reduce((acc, curr) => {
1256
- if (curr.typ == EnumToken.IdenTokenType) {
1257
- acc.push(curr.val);
1258
- }
1259
- return acc;
1260
- }, []),
1210
+ ...composedNames,
1261
1211
  ]),
1262
1212
  ].join(" ");
1263
1213
  }
@@ -1267,32 +1217,32 @@ function doParseSync(tokenizer, options = {}) {
1267
1217
  else {
1268
1218
  errors.push({
1269
1219
  action: "drop",
1270
- message: `composes '${token.r.val}' is not supported`,
1220
+ message: `composes '${arena.strings[arena.stringValue[arena.field(token, TokenField.R)]]}' is not supported`,
1271
1221
  node,
1272
1222
  });
1273
1223
  }
1274
1224
  }
1275
1225
  }
1276
1226
  }
1277
- parent.chi.splice(parent.chi.indexOf(node), 1);
1278
- }
1279
- if (node.typ == EnumToken.DeclarationNodeType &&
1280
- [
1281
- "grid-column",
1282
- "grid-column-start",
1283
- "grid-column-end",
1284
- "grid-row",
1285
- "grid-row-start",
1286
- "grid-row-end",
1287
- "grid-template",
1288
- "grid-template-columns",
1289
- "grid-template-rows",
1290
- ].includes(node.nam)) {
1291
- for (const { value } of walkValues(node.val, node)) {
1292
- if (value.typ != EnumToken.IdenTokenType) {
1227
+ arena.detach(node);
1228
+ }
1229
+ if ([
1230
+ "grid-column",
1231
+ "grid-column-start",
1232
+ "grid-column-end",
1233
+ "grid-row",
1234
+ "grid-row-start",
1235
+ "grid-row-end",
1236
+ "grid-template",
1237
+ "grid-template-columns",
1238
+ "grid-template-rows",
1239
+ ].includes(arena.strings[arena.name[node]])) {
1240
+ // inlined: arena.walkValues(node as AstNode)
1241
+ for (const value of new ValueWalker(arena, node)) {
1242
+ if (arena.dataTypes[value] != EnumToken.IdenTokenType) {
1293
1243
  continue;
1294
1244
  }
1295
- let idenToken = value.val;
1245
+ let idenToken = arena.strings[arena.stringValue[value]];
1296
1246
  let suffix = "";
1297
1247
  if (idenToken.endsWith("-start")) {
1298
1248
  suffix = "-start";
@@ -1316,41 +1266,45 @@ function doParseSync(tokenizer, options = {}) {
1316
1266
  }
1317
1267
  }
1318
1268
  }
1319
- value.val = mapping[idenToken];
1320
- }
1321
- }
1322
- else if (node.nam == "grid-template-areas" ||
1323
- node.nam == "grid-template") {
1324
- for (let i = 0; i < node.val.length; i++) {
1325
- if (node.val[i].typ == EnumToken.String) {
1326
- const tokens = parseString(node.val[i].val.slice(1, -1));
1327
- for (const { value } of walkValues(tokens)) {
1328
- if (value.typ == EnumToken.IdenTokenType ||
1329
- value.typ == EnumToken.DashedIdenTokenType) {
1330
- if (value.val in mapping) {
1331
- value.val = mapping[value.val];
1269
+ // inlined: arena.setStr(value, mapping[idenToken])
1270
+ arena.stringValue[value] = arena.internString(mapping[idenToken]);
1271
+ }
1272
+ }
1273
+ else if (arena.strings[arena.name[node]] == "grid-template-areas" ||
1274
+ arena.strings[arena.name[node]] == "grid-template") {
1275
+ for (let token = arena.firstToken[node]; token != 0; token = arena.nextSibling[token]) {
1276
+ if (arena.dataTypes[token] == EnumToken.StringTokenType) {
1277
+ const str = arena.strings[arena.stringValue[token]];
1278
+ const tokens = parseString(arena, str.slice(1, -1));
1279
+ // inlined: arena.walkValues(tokens)
1280
+ for (const value of new ValueWalker(arena, tokens)) {
1281
+ if (arena.dataTypes[value] == EnumToken.IdenTokenType ||
1282
+ arena.dataTypes[value] == EnumToken.DashedIdenTokenType) {
1283
+ if (arena.strings[arena.stringValue[value]] in mapping) {
1284
+ // inlined: arena.setStr(value, mapping[arena.strings[arena.stringValue[value]]])
1285
+ arena.stringValue[value] = arena.internString(mapping[arena.strings[arena.stringValue[value]]]);
1332
1286
  }
1333
1287
  else {
1334
1288
  let result = moduleSettings.scoped & ModuleScopeEnumOptions.Global
1335
- ? value.val
1336
- : moduleSettings.generateScopedName(value.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
1337
- mapping[value.val] = result;
1338
- revMapping[result] = value.val;
1339
- value.val = result;
1289
+ ? arena.strings[arena.stringValue[value]]
1290
+ : moduleSettings.generateScopedName(arena.strings[arena.stringValue[value]], moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
1291
+ mapping[arena.strings[arena.stringValue[value]]] = result;
1292
+ revMapping[result] = arena.strings[arena.stringValue[value]];
1293
+ // inlined: arena.setStr(value, result)
1294
+ arena.stringValue[value] = arena.internString(result);
1340
1295
  }
1341
1296
  }
1342
1297
  }
1343
- node.val[i].val =
1344
- node.val[i].val.charAt(0) +
1345
- renderTokens(tokens) +
1346
- node.val[i].val.charAt(node.val[i].val.length - 1);
1298
+ // inlined: arena.setStr(token, str.charAt(0) + renderTokens(arena, tokens) + str.charAt(str.length - 1))
1299
+ arena.stringValue[token] = arena.internString((str.charAt(0) + renderTokens(arena, tokens) + str.charAt(str.length - 1)));
1347
1300
  }
1348
1301
  }
1349
1302
  }
1350
- else if (node.nam == "animation" ||
1351
- node.nam == "animation-name") {
1352
- for (const { value } of walkValues(node.val, node)) {
1353
- if (value.typ == EnumToken.IdenTokenType &&
1303
+ else if (arena.strings[arena.name[node]] == "animation" ||
1304
+ arena.strings[arena.name[node]] == "animation-name") {
1305
+ // inlined: arena.walkValues(node as AstNode)
1306
+ for (const value of new ValueWalker(arena, node)) {
1307
+ if (arena.dataTypes[value] == EnumToken.IdenTokenType &&
1354
1308
  ![
1355
1309
  "none",
1356
1310
  "infinite",
@@ -1379,83 +1333,89 @@ function doParseSync(tokenizer, options = {}) {
1379
1333
  "inherit",
1380
1334
  "initial",
1381
1335
  "unset",
1382
- ].includes(value.val)) {
1383
- if (!(value.val in mapping)) {
1384
- mapping[value.val] =
1336
+ ].includes(arena.strings[arena.stringValue[value]])) {
1337
+ if (!(arena.strings[arena.stringValue[value]] in mapping)) {
1338
+ mapping[arena.strings[arena.stringValue[value]]] =
1385
1339
  moduleSettings.scoped & ModuleScopeEnumOptions.Global
1386
- ? value.val
1387
- : moduleSettings.generateScopedName(value.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
1388
- revMapping[mapping[value.val]] = value.val;
1340
+ ? arena.strings[arena.stringValue[value]]
1341
+ : moduleSettings.generateScopedName(arena.strings[arena.stringValue[value]], moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
1342
+ revMapping[mapping[arena.strings[arena.stringValue[value]]]] = arena.strings[arena.stringValue[value]];
1389
1343
  }
1390
- value.val = mapping[value.val];
1344
+ // inlined: arena.setStr(value, mapping[arena.strings[arena.stringValue[value]]])
1345
+ arena.stringValue[value] = arena.internString(mapping[arena.strings[arena.stringValue[value]]]);
1391
1346
  }
1392
1347
  }
1393
1348
  }
1394
- for (const r of walkValues(node.val, node)) {
1395
- if (r.value.typ == EnumToken.DashedIdenTokenType) {
1396
- r.value.val = mapping[r.value.val];
1349
+ // inlined: arena.walkValues(node as AstNode)
1350
+ for (const value of new ValueWalker(arena, node)) {
1351
+ if (arena.dataTypes[value] == EnumToken.DashedIdenTokenType) {
1352
+ // inlined: arena.setStr(value, mapping[arena.strings[arena.stringValue[value]]])
1353
+ arena.stringValue[value] = arena.internString(mapping[arena.strings[arena.stringValue[value]]]);
1397
1354
  }
1398
- else if ((r.value.typ == EnumToken.IdenTokenType || isIdentColor(r.value)) &&
1399
- r.value.val in importedCssVariables) {
1400
- // @ts-ignore
1401
- replaceNodeOrValue(r.parent, r.value, importedCssVariables[value.val].val);
1355
+ else if ((arena.dataTypes[value] == EnumToken.IdenTokenType || isIdentColor(arena, value)) &&
1356
+ arena.strings[arena.stringValue[value]] in importedCssVariables) {
1357
+ arena.replaceToken(value, ...arena.tokens(importedCssVariables[arena.strings[arena.stringValue[value]]][ARENAID]).map((token) => arena.clone(token, true)));
1402
1358
  }
1403
1359
  }
1404
1360
  }
1405
- else if (node.typ == EnumToken.RuleNodeType) {
1361
+ else if (nodeType == EnumToken.RuleNodeType) {
1406
1362
  // if (node[TOKENS] == null) {
1407
- const tokens = parseString(node.sel);
1408
- matchSelectorSyntax(tokens, [], options);
1409
- node[TOKENS] = trimArray(tokens);
1363
+ const tokens = parseString(arena, arena.strings[arena.name[node]]);
1364
+ matchSelectorSyntax(arena, tokens, [], options);
1365
+ arena.setTokenList(node, trimArray(arena, tokens));
1410
1366
  let hasIdOrClass = false;
1411
- for (const { value } of walkValues(node[TOKENS], node,
1412
- // @ts-ignore
1413
- (value, parent) => {
1414
- if (value.typ == EnumToken.PseudoClassTokenType ||
1415
- value.typ == EnumToken.PseudoElementTokenType) {
1416
- const val = value.val.toLowerCase();
1367
+ // inlined: arena.walkValues(node as AstNode, filter)
1368
+ for (const value of new ValueWalker(arena, node, (arena, value) => {
1369
+ if (arena.dataTypes[value] == EnumToken.PseudoClassTokenType ||
1370
+ arena.dataTypes[value] == EnumToken.PseudoElementTokenType) {
1371
+ const val = arena.strings[arena.stringValue[value]].toLowerCase();
1417
1372
  switch (val) {
1418
1373
  case ":local":
1419
1374
  case ":global":
1420
1375
  {
1421
- let index = parent[TOKENS].indexOf(value);
1422
- parent[TOKENS].splice(index, 1);
1423
- if (parent[TOKENS][index]?.typ == EnumToken.WhitespaceTokenType ||
1424
- parent[TOKENS][index]?.typ ==
1425
- EnumToken.DescendantCombinatorTokenType) {
1426
- parent[TOKENS].splice(index, 1);
1376
+ // remove the pseudo class and the following whitespace
1377
+ const next = arena.nextSibling[value];
1378
+ arena.detach(value);
1379
+ if (next != 0 &&
1380
+ (arena.dataTypes[next] == EnumToken.WhitespaceTokenType ||
1381
+ arena.dataTypes[next] == EnumToken.DescendantCombinatorTokenType)) {
1382
+ arena.detach(next);
1427
1383
  }
1428
1384
  }
1429
1385
  break;
1430
1386
  }
1431
1387
  }
1432
- else if (value.typ == EnumToken.PseudoClassFuncTokenType) {
1433
- switch (value.val.toLowerCase()) {
1388
+ else if (arena.dataTypes[value] == EnumToken.PseudoClassFuncTokenType) {
1389
+ switch (arena.strings[arena.stringValue[value]].toLowerCase()) {
1434
1390
  case ":global":
1435
- for (const token of value.chi) {
1391
+ for (const token of arena.tokens(value)) {
1436
1392
  global.add(token);
1437
1393
  }
1438
- case ":local":
1439
- parent[TOKENS].splice(parent[TOKENS].indexOf(value), 1, ...value.chi);
1440
- break;
1394
+ case ":local": {
1395
+ // replace the function by its arguments. the arguments are visited
1396
+ const children = arena.tokens(value);
1397
+ arena.replaceToken(value, ...children);
1398
+ return children;
1399
+ }
1441
1400
  }
1442
1401
  }
1402
+ return null;
1443
1403
  })) {
1444
- if (value.typ == EnumToken.HashTokenType || value.typ == EnumToken.ClassSelectorTokenType) {
1404
+ if (arena.dataTypes[value] == EnumToken.HashTokenType || arena.dataTypes[value] == EnumToken.ClassSelectorTokenType) {
1445
1405
  hasIdOrClass = true;
1446
1406
  }
1447
1407
  if (processed.has(value)) {
1448
1408
  continue;
1449
1409
  }
1450
1410
  processed.add(value);
1451
- if (value.typ == EnumToken.PseudoClassTokenType || value.typ == EnumToken.PseudoElementTokenType) ;
1452
- else if (value.typ == EnumToken.PseudoClassFuncTokenType) ;
1411
+ if (arena.dataTypes[value] == EnumToken.PseudoClassTokenType || arena.dataTypes[value] == EnumToken.PseudoElementTokenType) ;
1412
+ else if (arena.dataTypes[value] == EnumToken.PseudoClassFuncTokenType) ;
1453
1413
  else {
1454
1414
  if (global.has(value)) {
1455
1415
  continue;
1456
1416
  }
1457
- if (value.typ == EnumToken.ClassSelectorTokenType) {
1458
- const val = value.val.slice(1);
1417
+ if (arena.dataTypes[value] == EnumToken.ClassSelectorTokenType) {
1418
+ const val = arena.strings[arena.stringValue[value]].slice(1);
1459
1419
  if (!(val in mapping)) {
1460
1420
  let value = moduleSettings.scoped & ModuleScopeEnumOptions.Global
1461
1421
  ? val
@@ -1467,46 +1427,50 @@ function doParseSync(tokenizer, options = {}) {
1467
1427
  : value;
1468
1428
  revMapping[mapping[val]] = val;
1469
1429
  }
1470
- value.val = "." + mapping[val];
1430
+ // inlined: arena.setStr(value, "." + mapping[val])
1431
+ arena.stringValue[value] = arena.internString(("." + mapping[val]));
1471
1432
  }
1472
1433
  }
1473
1434
  }
1474
1435
  if (moduleSettings.scoped & ModuleScopeEnumOptions.Pure) {
1475
1436
  if (!hasIdOrClass) {
1476
- throw new Error(`pure module: No id or class found in selector '${node.sel}' at '${options.source.getOffsets(node[LOCSTA]).join(":")}'`);
1437
+ throw new Error(
1438
+ // inlined: options.source!.getOffsets(arena.getSpanStart(node as AstNode))
1439
+ `pure module: No id or class found in selector '${arena.strings[arena.name[node]]}' at '${options.source.lineStarts.getOffsets(arena.getSpanStart(node)).join(":")}'`);
1477
1440
  }
1478
1441
  }
1479
- node.sel = "";
1480
- for (const token of node[TOKENS]) {
1481
- node.sel += renderValue(token);
1482
- }
1442
+ // inlined: arena.setSelector(node as AstNode, arena.tokens(node as AstNode).reduce((acc: string, token: Token) => acc + renderValue(arena, token), ""))
1443
+ arena.name[node] = arena.internString(arena.tokens(node).reduce((acc, token) => acc + renderValue(arena, token), ""));
1483
1444
  }
1484
- else if (node.typ == EnumToken.AtRuleNodeType || node.typ == EnumToken.KeyframesAtRuleNodeType) {
1485
- const val = node.nam.toLowerCase();
1486
- if (node[TOKENS] == null) {
1487
- node[TOKENS] = parseString(node.val);
1445
+ else if (nodeType == EnumToken.AtRuleNodeType || nodeType == EnumToken.KeyframesAtRuleNodeType) {
1446
+ const val = arena.strings[arena.name[node]].toLowerCase();
1447
+ if (arena.firstToken[node] == 0) {
1448
+ // inlined: arena.setTokens(node as AstNode, ...parseString(arena, arena.strings[arena.stringValue[node as AstNode]]))
1449
+ arena.setTokenList(node, parseString(arena, arena.strings[arena.stringValue[node]]));
1488
1450
  }
1489
1451
  if (val == "property" || val == "keyframes") {
1490
1452
  const prefix = val == "property" ? "--" : "";
1491
- for (const value of node[TOKENS]) {
1492
- if ((prefix == "--" && value.typ == EnumToken.DashedIdenTokenType) ||
1493
- (prefix == "" && value.typ == EnumToken.IdenTokenType)) {
1494
- if (!(value.val in mapping)) {
1453
+ for (const value of arena.tokens(node)) {
1454
+ if ((prefix == "--" && arena.dataTypes[value] == EnumToken.DashedIdenTokenType) ||
1455
+ (prefix == "" && arena.dataTypes[value] == EnumToken.IdenTokenType)) {
1456
+ if (!(arena.strings[arena.stringValue[value]] in mapping)) {
1495
1457
  let val = moduleSettings.scoped & ModuleScopeEnumOptions.Global
1496
- ? value.val
1497
- : moduleSettings.generateScopedName(value.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
1498
- mapping[value.val] =
1458
+ ? arena.strings[arena.stringValue[value]]
1459
+ : moduleSettings.generateScopedName(arena.strings[arena.stringValue[value]], moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
1460
+ mapping[arena.strings[arena.stringValue[value]]] =
1499
1461
  prefix +
1500
1462
  (moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
1501
1463
  moduleSettings.naming & ModuleCaseTransformEnum.CamelCaseOnly
1502
1464
  ? getKeyName(val, moduleSettings.naming)
1503
1465
  : val);
1504
- revMapping[mapping[value.val]] = value.val;
1466
+ revMapping[mapping[arena.strings[arena.stringValue[value]]]] = arena.strings[arena.stringValue[value]];
1505
1467
  }
1506
- value.val = mapping[value.val];
1468
+ // inlined: arena.setStr(value, mapping[arena.strings[arena.stringValue[value]]])
1469
+ arena.stringValue[value] = arena.internString(mapping[arena.strings[arena.stringValue[value]]]);
1507
1470
  }
1508
1471
  }
1509
- node.val = renderTokens(node[TOKENS]);
1472
+ // inlined: arena.setValueString(node as AstNode, renderTokens(arena, arena.tokens(node as AstNode)))
1473
+ arena.stringValue[node] = arena.internString(renderTokens(arena, arena.tokens(node)));
1510
1474
  }
1511
1475
  }
1512
1476
  }
@@ -1570,9 +1534,7 @@ async function doParse(iter, options = {}) {
1570
1534
  ...options,
1571
1535
  };
1572
1536
  options.minifyAST ??= options.minify;
1573
- if (typeof options.validation !== "boolean") {
1574
- options.validation = !!options.validation;
1575
- }
1537
+ options.validation = !!options.validation;
1576
1538
  if (options.module) {
1577
1539
  options.expandNestingRules = true;
1578
1540
  }
@@ -1598,10 +1560,15 @@ async function doParse(iter, options = {}) {
1598
1560
  imports: [],
1599
1561
  };
1600
1562
  const invalidNodes = [];
1601
- let ast = {
1602
- typ: EnumToken.StyleSheetNodeType,
1603
- chi: [],
1604
- };
1563
+ // first error / invalid node of the current statement
1564
+ let errorStart = errors.length;
1565
+ let invalidStart = 0;
1566
+ // tokens allocated for the current statement (parseNode() may remove tokens from the statement array)
1567
+ const statement = [];
1568
+ let tokenizer = iter instanceof Promise ? await iter : iter;
1569
+ // nested parses share the arena of the parent
1570
+ const arena = options.arena ?? (options.arena = new AstArena(arenaCapacity(tokenizer)));
1571
+ let ast = arena.current == 1 ? arena.root : arena.createStyleSheet();
1605
1572
  let tokens = [];
1606
1573
  let context = ast;
1607
1574
  const imports = [];
@@ -1610,169 +1577,123 @@ async function doParse(iter, options = {}) {
1610
1577
  // @ts-ignore ignore error
1611
1578
  let parensMatch = 0;
1612
1579
  let curlyBracketMatch = 0;
1613
- let tokenizer = iter instanceof Promise ? await iter : iter;
1614
- ast[LOCSRCID] = options.source.id;
1615
- ast[LOCSTA] = 0;
1616
- ast[LOCEND] = 0;
1617
- while (!tokenizer.done()) {
1580
+ // tokens are located in the tokenizer source (the imported file when parsing an @import)
1581
+ arena.setLocation(ast, tokenizer.sourceId, 0, 0);
1582
+ const sourceIndex = arena.sourceIndex(tokenizer.sourceId);
1583
+ let itemType;
1584
+ // inlined: tokenizer.done()
1585
+ while (!(tokenizer.typ === EnumToken.EOF)) {
1618
1586
  tokenizer.next();
1619
- if (tokenizer.unit != null) {
1620
- item = {
1621
- typ: tokenizer.typ,
1622
- val: tokenizer.val,
1623
- unit: tokenizer.unit,
1624
- };
1625
- }
1626
- else if (tokenizer.nam != null) {
1627
- item = {
1628
- typ: tokenizer.typ,
1629
- nam: tokenizer.nam,
1630
- };
1631
- }
1632
- else if (tokenizer.val === null) {
1633
- item = {
1634
- typ: tokenizer.typ,
1635
- };
1636
- }
1637
- else if (tokenizer.kin != null) {
1638
- item = {
1639
- typ: tokenizer.typ,
1640
- val: tokenizer.val,
1641
- kin: tokenizer.kin,
1642
- };
1643
- }
1644
- else {
1645
- item = {
1646
- typ: tokenizer.typ,
1647
- val: tokenizer.val,
1648
- };
1649
- }
1650
- item[LOCSRCID] = tokenizer.srcId;
1651
- item[LOCSTA] = tokenizer.sta;
1652
- item[LOCEND] = tokenizer.end;
1587
+ item = allocateToken(arena, tokenizer, sourceIndex);
1588
+ itemType = tokenizer.typ;
1653
1589
  stats.bytesIn = tokenizer.bytesIn;
1654
1590
  stats.tokensCount++;
1655
- if (BadTokensTypes.includes(item.typ)) {
1591
+ if (isBadToken(itemType)) {
1656
1592
  tokens.push(item);
1593
+ statement.push(item);
1657
1594
  errors.push({
1658
1595
  action: "drop",
1659
1596
  message: "Bad token",
1660
1597
  syntax: null,
1661
1598
  node: item,
1662
- location: options.source.getSourceLocation(item[LOCSTA]),
1599
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(item))
1600
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(item))],
1663
1601
  });
1664
1602
  // bad token
1665
1603
  continue;
1666
1604
  }
1667
- if (item.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(item.typ)) {
1605
+ if (itemType === EnumToken.StartParensTokenType || tokensfuncDefMap.has(itemType)) {
1668
1606
  parensMatch++;
1669
1607
  }
1670
- else if (item.typ === EnumToken.EndParensTokenType && parensMatch > 0) {
1608
+ else if (itemType === EnumToken.EndParensTokenType && parensMatch > 0) {
1671
1609
  parensMatch--;
1672
1610
  }
1673
- if (item.typ === EnumToken.BlockStartTokenType) {
1611
+ if (itemType === EnumToken.BlockStartTokenType) {
1674
1612
  curlyBracketMatch++;
1675
1613
  }
1676
- else if (item.typ === EnumToken.BlockEndTokenType && curlyBracketMatch > 0) {
1614
+ else if (itemType === EnumToken.BlockEndTokenType && curlyBracketMatch > 0) {
1677
1615
  curlyBracketMatch--;
1678
1616
  }
1679
1617
  tokens.push(item);
1618
+ statement.push(item);
1680
1619
  if (parensMatch === 0 &&
1681
- (item.typ === EnumToken.SemiColonTokenType ||
1682
- item.typ === EnumToken.BlockStartTokenType ||
1683
- item.typ === EnumToken.EOFTokenType)) {
1684
- node = parseNode(tokens, context, options, errors, stats, invalidNodes);
1620
+ (itemType === EnumToken.SemiColonTokenType ||
1621
+ itemType === EnumToken.BlockStartTokenType ||
1622
+ itemType === EnumToken.EOFTokenType)) {
1623
+ node = parseNode(arena, tokens, context, options, errors, stats, invalidNodes, parensMatch);
1685
1624
  if (node != null) {
1686
- if ("chi" in node) {
1625
+ if (arena.flags[node] & EnumAstNodeStatus.HasChildren) {
1687
1626
  stack.push(node);
1688
1627
  context = node;
1689
1628
  }
1690
- else if (node.typ == EnumToken.AtRuleNodeType && node.nam === "import") {
1629
+ else if (arena.dataTypes[node] == EnumToken.AtRuleNodeType &&
1630
+ arena.strings[arena.name[node]] === "import") {
1691
1631
  imports.push(node);
1692
1632
  }
1693
1633
  }
1694
- else if (item.typ == EnumToken.BlockStartTokenType) {
1634
+ else if (itemType == EnumToken.BlockStartTokenType) {
1695
1635
  let inBlock = 1;
1696
1636
  tokens.length = 0;
1697
1637
  tokens.push(item);
1698
1638
  do {
1699
1639
  tokenizer.next();
1700
- if (tokenizer.unit != null) {
1701
- item = {
1702
- typ: tokenizer.typ,
1703
- val: tokenizer.val,
1704
- unit: tokenizer.unit,
1705
- };
1706
- }
1707
- else if (tokenizer.nam != null) {
1708
- item = {
1709
- typ: tokenizer.typ,
1710
- nam: tokenizer.nam,
1711
- };
1712
- }
1713
- else if (tokenizer.val === null) {
1714
- item = {
1715
- typ: tokenizer.typ,
1716
- };
1717
- }
1718
- else if (tokenizer.kin != null) {
1719
- item = {
1720
- typ: tokenizer.typ,
1721
- val: tokenizer.val,
1722
- kin: tokenizer.kin,
1723
- };
1724
- }
1725
- else {
1726
- item = {
1727
- typ: tokenizer.typ,
1728
- val: tokenizer.val,
1729
- };
1730
- }
1731
- item[LOCSRCID] = tokenizer.srcId;
1732
- item[LOCSTA] = tokenizer.sta;
1733
- item[LOCEND] = tokenizer.end;
1640
+ item = allocateToken(arena, tokenizer, sourceIndex);
1641
+ itemType = tokenizer.typ;
1734
1642
  tokens.push(item);
1735
- if (item.typ === EnumToken.BlockStartTokenType) {
1643
+ statement.push(item);
1644
+ if (itemType === EnumToken.BlockStartTokenType) {
1736
1645
  inBlock++;
1737
1646
  }
1738
- else if (item.typ === EnumToken.BlockEndTokenType) {
1647
+ else if (itemType === EnumToken.BlockEndTokenType) {
1739
1648
  inBlock--;
1740
1649
  }
1741
- } while (inBlock != 0 && !tokenizer.done());
1650
+ // inlined: tokenizer.done()
1651
+ } while (inBlock != 0 && !(tokenizer.typ === EnumToken.EOF));
1742
1652
  if (tokens.length > 0) {
1743
1653
  errors.push({
1744
1654
  action: "drop",
1745
1655
  message: "invalid block",
1746
- location: options.source.getSourceLocation(tokens[0][LOCSTA]),
1656
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(tokens[0]))
1657
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[0]))],
1747
1658
  });
1748
1659
  }
1749
1660
  }
1661
+ recycleStatementTokens(arena, statement, errors, errorStart, invalidNodes, invalidStart);
1662
+ statement.length = 0;
1750
1663
  tokens.length = 0;
1664
+ errorStart = errors.length;
1665
+ invalidStart = invalidNodes.length;
1751
1666
  }
1752
- else if ((parensMatch === 0 || curlyBracketMatch === 0) && item.typ === EnumToken.BlockEndTokenType) {
1753
- parseNode(tokens, context, options, errors, stats, invalidNodes);
1754
- context[LOCEND] = item[LOCEND];
1667
+ else if ((parensMatch === 0 || curlyBracketMatch === 0) && itemType === EnumToken.BlockEndTokenType) {
1668
+ parseNode(arena, tokens, context, options, errors, stats, invalidNodes, parensMatch);
1669
+ // inlined: arena.setSpanEnd(context, arena.getSpanEnd(item))
1670
+ arena.setSpan(context, arena.getSourceId(context), arena.getSpanStart(context), arena.getSpanEnd(item));
1755
1671
  const previousNode = stack.pop();
1756
- context = (stack[stack.length - 1] ?? ast);
1672
+ context = stack[stack.length - 1] ?? ast;
1757
1673
  if (options.removeEmpty &&
1758
1674
  previousNode != null &&
1759
- previousNode.chi.length == 0 &&
1760
- context.chi[context.chi.length - 1] == previousNode) {
1761
- context.chi.pop();
1675
+ arena.firstChild[previousNode] == 0 &&
1676
+ arena.lastChild[context] == previousNode) {
1677
+ arena.detach(previousNode);
1762
1678
  }
1679
+ recycleStatementTokens(arena, statement, errors, errorStart, invalidNodes, invalidStart);
1680
+ statement.length = 0;
1763
1681
  tokens.length = 0;
1682
+ errorStart = errors.length;
1683
+ invalidStart = invalidNodes.length;
1764
1684
  parensMatch = 0;
1765
1685
  curlyBracketMatch = 0;
1766
1686
  }
1767
1687
  // }
1768
1688
  }
1769
1689
  if (tokens.length > 0) {
1770
- node = parseNode(tokens, context, options, errors, stats, invalidNodes);
1690
+ node = parseNode(arena, tokens, context, options, errors, stats, invalidNodes, parensMatch);
1771
1691
  if (node != null) {
1772
- if (node.typ == EnumToken.AtRuleNodeType && "import" === node.val) {
1692
+ if (arena.dataTypes[node] == EnumToken.AtRuleNodeType &&
1693
+ "import" === arena.strings[arena.stringValue[node]]) {
1773
1694
  imports.push(node);
1774
1695
  }
1775
- if ("chi" in node /* && node.typ != EnumToken.InvalidRuleNodeType */) {
1696
+ if (arena.flags[node] & EnumAstNodeStatus.HasChildren) {
1776
1697
  stack.push(node);
1777
1698
  context = node;
1778
1699
  }
@@ -1780,11 +1701,13 @@ async function doParse(iter, options = {}) {
1780
1701
  }
1781
1702
  if (imports.length > 0 && options.resolveImport) {
1782
1703
  await Promise.all(imports.map(async (node) => {
1783
- if (node[STATE] !== EnumAstNodeStatus.Validated) {
1704
+ if ((arena.flags[node] & EnumAstNodeStatus.StateMask) !== EnumAstNodeStatus.Validated) {
1784
1705
  return;
1785
1706
  }
1786
- const token = node[TOKENS][0];
1787
- const url = token.typ == EnumToken.StringTokenType ? token.val.slice(1, -1) : token.val;
1707
+ const token = arena.firstToken[node];
1708
+ const url = arena.dataTypes[token] == EnumToken.StringTokenType
1709
+ ? arena.strings[arena.stringValue[token]].slice(1, -1)
1710
+ : arena.strings[arena.stringValue[token]];
1788
1711
  try {
1789
1712
  const src = options.resolve(url, options.src ? dirname(options.src) : options.cwd);
1790
1713
  const result = options.load(src);
@@ -1812,8 +1735,11 @@ async function doParse(iter, options = {}) {
1812
1735
  stats.nodesCount += root.stats.nodesCount;
1813
1736
  stats.tokensCount += root.stats.tokensCount;
1814
1737
  stats.imports.push(root.stats);
1815
- // @ts-ignore
1816
- node[PARENT].chi.splice(node[PARENT].chi.indexOf(node), 1, ...root.ast.chi);
1738
+ // the imported stylesheet is allocated in the same arena: its children replace the @import rule
1739
+ if (arena.parents[node] != 0) {
1740
+ arena.moveChildren(root.ast, arena.parents[node], node);
1741
+ arena.detach(node);
1742
+ }
1817
1743
  if (root.errors.length > 0) {
1818
1744
  for (const error of root.errors) {
1819
1745
  errors.push(error);
@@ -1828,240 +1754,57 @@ async function doParse(iter, options = {}) {
1828
1754
  }
1829
1755
  const endParseTime = performance.now();
1830
1756
  if (options.expandNestingRules) {
1831
- ast = expand(ast);
1757
+ ast = arena.expand(ast);
1832
1758
  }
1833
1759
  let replacement;
1834
1760
  if (options.visitor != null) {
1835
- let parens;
1836
- let genericKey;
1837
1761
  const handlers = [];
1838
1762
  const visitors = parseVisitors(options.visitor, errors);
1763
+ let genericKey;
1839
1764
  let nodes = [ast];
1840
1765
  let node;
1841
- // nodes[0] = ast;
1842
- while (nodes.length > 0) {
1843
- node = nodes.pop();
1844
- if (visitors.includeTokens) {
1845
- switch (node.typ) {
1846
- case EnumToken.RuleNodeType:
1847
- case EnumToken.AtRuleNodeType:
1848
- case EnumToken.KeyframesRuleNodeType:
1849
- case EnumToken.KeyframesAtRuleNodeType:
1850
- for (let i =
1851
- // @ts-ignore
1852
- node[TOKENS].length -
1853
- 1; i >= 0; i--) {
1854
- // @ts-ignore
1855
- node[TOKENS][i][PARENT] =
1856
- node;
1857
- nodes.push(
1858
- // @ts-ignore
1859
- node[TOKENS][i]);
1860
- }
1861
- break;
1862
- case EnumToken.DeclarationNodeType:
1863
- for (let i =
1864
- // @ts-ignore
1865
- node.val.length - 1; i >= 0; i--) {
1866
- // @ts-ignore
1867
- node.val[i][PARENT] = node;
1868
- // @ts-ignore
1869
- nodes.push(node.val[i]);
1870
- }
1871
- break;
1766
+ let nodeType;
1767
+ // push the children of a node or the sub tokens of a value token onto the stack
1768
+ const enqueue = (node) => {
1769
+ if (arena.flags[node] & EnumAstNodeStatus.HasChildren) {
1770
+ for (let child = arena.lastChild[node]; child != 0; child = arena.previousSibling[child]) {
1771
+ nodes.push(child);
1872
1772
  }
1873
- // @ts-ignore
1874
- // if (node.l != null) {
1875
- // // @ts-ignore
1876
- // if (node.op2 != null) {
1877
- // // @ts-ignore
1878
- // nodes.push(node.op2);
1879
- // // @ts-ignore
1880
- // nodes.op2[PARENT] = node;
1881
- // }
1882
- // // @ts-ignore
1883
- // if (node.op1 != null) {
1884
- // // @ts-ignore
1885
- // nodes.push(node.op1);
1886
- // // @ts-ignore
1887
- // nodes.op1[PARENT] = node;
1888
- // }
1889
- // // @ts-ignore
1890
- // if (nodes.op != null && node.op.typ != null) {
1891
- // // @ts-ignore
1892
- // nodes.push(node.op);
1893
- // // @ts-ignore
1894
- // nodes.op[PARENT] = node;
1895
- // }
1896
- // // @ts-ignore
1897
- // if (node.r != null) {
1898
- // // @ts-ignore
1899
- // if (Array.isArray(node.r)) {
1900
- // // @ts-ignore
1901
- // for (let i = node.r.length - 1; i >= 0; i--) {
1902
- // // @ts-ignore
1903
- // nodes.push(node.r[i]);
1904
- // // @ts-ignore
1905
- // node.r[i][PARENT] = node;
1906
- // }
1907
- // } else {
1908
- // // @ts-ignore
1909
- // nodes.push(node.r);
1910
- // // @ts-ignore
1911
- // node.r[PARENT] = node;
1912
- // }
1913
- // }
1914
- // // @ts-ignore
1915
- // if (Array.isArray(node.l)) {
1916
- // // @ts-ignore
1917
- // for (let i = node.l.length - 1; i >= 0; i--) {
1918
- // // @ts-ignore
1919
- // node.l[i][PARENT] = node;
1920
- // // @ts-ignore
1921
- // nodes.push(node.l[i]);
1922
- // }
1923
- // } else {
1924
- // // @ts-ignore
1925
- // nodes.push(node.l);
1926
- // // @ts-ignore
1927
- // node.l[PARENT] = node;
1928
- // }
1929
- // }
1930
- }
1931
- switch (node.typ) {
1932
- case EnumToken.BinaryExpressionTokenType:
1933
- case EnumToken.IfElseConditionTokenType:
1934
- nodes.push(node.r);
1935
- node.r[PARENT] = node;
1936
- nodes.push(node.l);
1937
- node.l[PARENT] = node;
1938
- break;
1939
- case EnumToken.MatchExpressionTokenType:
1940
- nodes.push(node.r);
1941
- node.r[PARENT] = node;
1942
- nodes.push(node.op);
1943
- node.op[PARENT] = node;
1944
- nodes.push(node.l);
1945
- node.l[PARENT] = node;
1946
- break;
1947
- case EnumToken.SupportsQueryConditionTokenType:
1948
- case EnumToken.MediaQueryConditionTokenType:
1949
- for (let i = node.r.length - 1; i >= 0; i--) {
1950
- const element = node.r[i];
1951
- nodes.push(element);
1952
- element[PARENT] = node;
1953
- }
1954
- nodes.push(node.op);
1955
- node.op[PARENT] = node;
1956
- for (let i = node.l.length - 1; i >= 0; i--) {
1957
- const element = node.l[i];
1958
- nodes.push(element);
1959
- element[PARENT] = node;
1960
- }
1961
- break;
1962
- case EnumToken.WhenElseQueryConditionTokenType:
1963
- for (let i = node.r.length - 1; i >= 0; i--) {
1964
- const element = node.r[i];
1965
- nodes.push(element);
1966
- element[PARENT] = node;
1967
- }
1968
- nodes.push(node.l);
1969
- node.l[PARENT] = node;
1970
- break;
1971
- case EnumToken.IfConditionTokenType:
1972
- for (let i = node.r.length - 1; i >= 0; i--) {
1973
- const element = node.r[i];
1974
- nodes.push(element);
1975
- element[PARENT] = node;
1976
- }
1977
- for (let i = node.l.length - 1; i >= 0; i--) {
1978
- const element = node.l[i];
1979
- nodes.push(element);
1980
- element[PARENT] = node;
1981
- }
1982
- break;
1983
- case EnumToken.ComposesSelectorNodeType:
1984
- if (node.r != null) {
1985
- nodes.push(node.r);
1986
- node.r[PARENT] = node;
1987
- }
1988
- for (let i = node.l.length - 1; i >= 0; i--) {
1989
- const element = node.l[i];
1990
- nodes.push(element);
1991
- element[PARENT] = node;
1992
- }
1993
- break;
1994
- case EnumToken.ContainerStyleRangeTokenType:
1995
- for (let i = node.r.length - 1; i >= 0; i--) {
1996
- const element = node.r[i];
1997
- nodes.push(element);
1998
- element[PARENT] = node;
1999
- }
2000
- for (let i = node.op.length - 1; i >= 0; i--) {
2001
- const element = node.op[i];
2002
- nodes.push(element);
2003
- element[PARENT] = node;
2004
- }
2005
- for (let i = node.l.length - 1; i >= 0; i--) {
2006
- const element = node.l[i];
2007
- nodes.push(element);
2008
- element[PARENT] = node;
2009
- }
2010
- break;
2011
- case EnumToken.MediaRangeQueryTokenType:
2012
- for (let i = node.r.length - 1; i >= 0; i--) {
2013
- const element = node.r[i];
2014
- nodes.push(element);
2015
- element[PARENT] = node;
2016
- }
2017
- nodes.push(node.op2);
2018
- node.op2[PARENT] = node;
2019
- for (let i = node.val.length - 1; i >= 0; i--) {
2020
- const element = node.val[i];
2021
- nodes.push(element);
2022
- element[PARENT] = node;
2023
- }
2024
- nodes.push(node.op1);
2025
- node.op1[PARENT] = node;
2026
- for (let i = node.l.length - 1; i >= 0; i--) {
2027
- const element = node.l[i];
2028
- nodes.push(element);
2029
- element[PARENT] = node;
2030
- }
2031
- break;
2032
1773
  }
2033
- // @ts-ignore
2034
- if (node.chi != null) {
2035
- for (
2036
- // @ts-ignore
2037
- let i = node.chi.length - 1; i >= 0; i--) {
2038
- // @ts-ignore
2039
- nodes.push(node.chi[i]);
1774
+ if (visitors.includeTokens &&
1775
+ arena.dataTypes[node] != EnumToken.RuleNodeType &&
1776
+ arena.dataTypes[node] != EnumToken.KeyframesRuleNodeType) {
1777
+ // at-rule prelude, declaration values or token children. selector tokens are not visited
1778
+ const values = arena.subTokens(node);
1779
+ for (let i = values.length - 1; i >= 0; i--) {
1780
+ nodes.push(values[i]);
2040
1781
  }
2041
1782
  }
2042
- parens = null;
1783
+ };
1784
+ while (nodes.length > 0) {
1785
+ node = nodes.pop();
1786
+ nodeType = arena.dataTypes[node];
1787
+ replacement = null;
2043
1788
  handlers.length = 0;
2044
1789
  if (visitors.preAllVisitorsHandlers?.size > 0) {
2045
1790
  handlers.push(...visitors.preAllVisitorsHandlers);
2046
1791
  }
2047
1792
  genericKey =
2048
- node.typ == EnumToken.DeclarationNodeType
1793
+ nodeType == EnumToken.DeclarationNodeType
2049
1794
  ? "Declaration"
2050
- : node.typ == EnumToken.AtRuleNodeType
1795
+ : nodeType == EnumToken.AtRuleNodeType
2051
1796
  ? "AtRule"
2052
- : node.typ == EnumToken.KeyframesAtRuleNodeType
1797
+ : nodeType == EnumToken.KeyframesAtRuleNodeType
2053
1798
  ? "KeyframesAtRule"
2054
- : node.typ === EnumToken.KeyframesRuleNodeType
1799
+ : nodeType === EnumToken.KeyframesRuleNodeType
2055
1800
  ? "KeyframesRule"
2056
- : node.typ == EnumToken.RuleNodeType
1801
+ : nodeType == EnumToken.RuleNodeType
2057
1802
  ? "Rule"
2058
- : node.typ == EnumToken.KeyframesRuleNodeType
2059
- ? "KeyframesRule"
2060
- : null;
2061
- let keyName = node.typ == EnumToken.DeclarationNodeType || node.typ == EnumToken.AtRuleNodeType
2062
- ? camelize(node.nam)
2063
- : node.typ == EnumToken.KeyframesAtRuleNodeType
2064
- ? camelize(node.val)
1803
+ : null;
1804
+ let keyName = nodeType == EnumToken.DeclarationNodeType || nodeType == EnumToken.AtRuleNodeType
1805
+ ? camelize(arena.strings[arena.name[node]])
1806
+ : nodeType == EnumToken.KeyframesAtRuleNodeType
1807
+ ? camelize(arena.strings[arena.stringValue[node]])
2065
1808
  : null;
2066
1809
  if (visitors.allVisitorsHandlers?.size > 0) {
2067
1810
  handlers.push(...visitors.allVisitorsHandlers);
@@ -2081,9 +1824,9 @@ async function doParse(iter, options = {}) {
2081
1824
  }
2082
1825
  }
2083
1826
  }
2084
- if (map.has(node.typ)) {
1827
+ if (map.has(nodeType)) {
2085
1828
  // @ts-ignore
2086
- for (const handler of map.get(node.typ)) {
1829
+ for (const handler of map.get(nodeType)) {
2087
1830
  if (typeof handler == "function") {
2088
1831
  handlers.push(handler);
2089
1832
  }
@@ -2108,71 +1851,82 @@ async function doParse(iter, options = {}) {
2108
1851
  if (visitors.postAllVisitorsHandlers?.size > 0) {
2109
1852
  handlers.push(...visitors.postAllVisitorsHandlers);
2110
1853
  }
2111
- if (handlers.length == 0) {
2112
- continue;
2113
- }
2114
- for (const callable of handlers) {
2115
- replacement = callable(node, node[PARENT], ast,
2116
- // @ts-expect-error
2117
- function* () {
2118
- if (parens == null) {
2119
- let parent = node[PARENT];
2120
- // let node = node[PARENT] as AstNode;
2121
- while (parent != null) {
2122
- yield parent;
2123
- parent = parent[PARENT];
2124
- }
1854
+ if (handlers.length > 0) {
1855
+ for (const callable of handlers) {
1856
+ // the parent, the root, ... are read from the arena
1857
+ replacement = callable(arena, node);
1858
+ if (replacement == null) {
1859
+ continue;
1860
+ }
1861
+ if (replacement instanceof Promise) {
1862
+ replacement = await replacement;
1863
+ }
1864
+ if (replacement == node) {
1865
+ continue;
1866
+ }
1867
+ //
1868
+ if (Array.isArray(replacement)) {
1869
+ break;
2125
1870
  }
2126
- });
2127
- if (replacement == null) {
2128
- continue;
2129
- }
2130
- if (replacement instanceof Promise) {
2131
- replacement = await replacement;
2132
- }
2133
- if (replacement == null || replacement == node) {
2134
- continue;
2135
1871
  }
2136
1872
  // @ts-ignore
2137
- // node = replacement as AstNode;
2138
- //
2139
- if (Array.isArray(replacement)) {
2140
- break;
1873
+ if (replacement != null && node != replacement) {
1874
+ // nodes and tokens are replaced by ids allocated in the arena. the object views of the arena are
1875
+ // not copied, plain objects are imported
1876
+ replacement = visitorResult(arena, replacement);
1877
+ if (replacement == node) ;
1878
+ else if (replacement != null) {
1879
+ if (arena.flags[node] & (EnumAstNodeStatus.HasChildren | EnumAstNodeStatus.HasValues)) {
1880
+ arena.replaceChild(node, ...(Array.isArray(replacement) ? replacement : [replacement]));
1881
+ }
1882
+ else {
1883
+ arena.replaceToken(node, ...(Array.isArray(replacement) ? replacement : [replacement]));
1884
+ }
1885
+ }
1886
+ else {
1887
+ errors.push({
1888
+ action: "ignore",
1889
+ message: "doParse: visitor must return node ids, objects or arrays of node ids and objects",
1890
+ node,
1891
+ });
1892
+ }
2141
1893
  }
2142
1894
  }
2143
- // @ts-ignore
2144
- if (replacement != null && node != replacement) {
2145
- console.error({
2146
- node,
2147
- replacement,
2148
- });
2149
- // @ts-ignore
2150
- replaceNodeOrValue(node[PARENT], node, replacement);
1895
+ // visit the children of the node or of its replacement
1896
+ if (replacement == null || replacement == node) {
1897
+ enqueue(node);
1898
+ }
1899
+ else if (Array.isArray(replacement)) {
1900
+ for (let i = replacement.length - 1; i >= 0; i--) {
1901
+ enqueue(replacement[i]);
1902
+ }
1903
+ }
1904
+ else {
1905
+ enqueue(replacement);
2151
1906
  }
2152
1907
  }
2153
1908
  nodes = null;
2154
1909
  }
2155
1910
  if (invalidNodes.length > 0) {
2156
1911
  for (const node of invalidNodes) {
2157
- if (options.lenient && node[STATE] & EnumAstNodeStatus.Unknown) {
1912
+ if (options.lenient && arena.flags[node] & EnumAstNodeStatus.Unknown) {
2158
1913
  continue;
2159
1914
  }
2160
- if (node[STATE] &
1915
+ if (arena.flags[node] &
2161
1916
  (EnumAstNodeStatus.Invalid |
2162
1917
  EnumAstNodeStatus.Unknown |
2163
1918
  EnumAstNodeStatus.Unparsed |
2164
1919
  EnumAstNodeStatus.Malformed |
2165
1920
  EnumAstNodeStatus.Disallowed)) {
2166
- // @ts-ignore
2167
- node[PARENT].chi.splice(node[PARENT].chi.indexOf(node), 1);
1921
+ arena.detach(node);
2168
1922
  }
2169
1923
  }
2170
1924
  }
2171
1925
  if (options.minifyAST) {
2172
- if (ast.chi.length > 0) {
1926
+ if (arena.firstChild[ast] != 0) {
2173
1927
  let passes = options.pass ?? 1;
2174
1928
  while (passes--) {
2175
- minify(ast, options, true, errors, false);
1929
+ minify(arena, ast, options, true, errors, false);
2176
1930
  }
2177
1931
  }
2178
1932
  }
@@ -2180,6 +1934,7 @@ async function doParse(iter, options = {}) {
2180
1934
  let endTime = performance.now();
2181
1935
  const result = {
2182
1936
  ast,
1937
+ arena,
2183
1938
  errors,
2184
1939
  stats: {
2185
1940
  ...stats,
@@ -2256,9 +2011,12 @@ async function doParse(iter, options = {}) {
2256
2011
  moduleSettings.filePath = filePath;
2257
2012
  moduleSettings.pattern =
2258
2013
  pattern != null && pattern !== "" ? pattern : filePath === "" ? `[local]_[hash]` : `[local]_[hash]_[name]`;
2259
- for (const { node, parent } of walk(ast)) {
2260
- if (node.typ == EnumToken.CssVariableImportTokenType) {
2261
- const url = node.val.find((t) => t.typ == EnumToken.StringTokenType).val.slice(1, -1);
2014
+ // inlined: arena.walk(ast)
2015
+ for (const node of new NodeWalker(arena, ast)) {
2016
+ const parent = arena.parents[node];
2017
+ const nodeType = arena.dataTypes[node];
2018
+ if (nodeType == EnumToken.CssVariableImportTokenType) {
2019
+ const url = arena.strings[arena.stringValue[arena.findToken(node, (arena, t) => arena.dataTypes[t] == EnumToken.StringTokenType)]].slice(1, -1);
2262
2020
  const src = options.resolve(url, options.dirname(options.src), options.cwd);
2263
2021
  const result = options.load(src, "");
2264
2022
  const stream = result instanceof Promise || Object.getPrototypeOf(result).constructor.name == "AsyncFunction"
@@ -2284,14 +2042,17 @@ async function doParse(iter, options = {}) {
2284
2042
  src: src.relative,
2285
2043
  }));
2286
2044
  // options.parseInfo!.time += parseInfo.time;
2287
- cssVariablesMap[node.nam] = root.cssModuleVariables;
2288
- parent.chi.splice(parent.chi.indexOf(node), 1);
2045
+ cssVariablesMap[arena.strings[arena.name[node]]] = root.cssModuleVariables;
2046
+ arena.detach(node);
2289
2047
  continue;
2290
2048
  }
2291
2049
  // @ts-ignore
2292
- if (node.typ == EnumToken.CssVariableDeclarationMapTokenType) {
2050
+ if (nodeType == EnumToken.CssVariableDeclarationMapTokenType) {
2293
2051
  // @ts-ignore
2294
- const from = node.from.find((t) => t.typ == EnumToken.IdenTokenType || isIdentColor(t));
2052
+ const fromToken = arena
2053
+ .fieldTokens(node, TokenField.L)
2054
+ .find((t) => arena.dataTypes[t] == EnumToken.IdenTokenType || isIdentColor(arena, t));
2055
+ const from = { val: arena.strings[arena.stringValue[fromToken]] };
2295
2056
  if (!(from.val in cssVariablesMap)) {
2296
2057
  errors.push({
2297
2058
  node,
@@ -2301,65 +2062,68 @@ async function doParse(iter, options = {}) {
2301
2062
  }
2302
2063
  else {
2303
2064
  // @ts-ignore
2304
- for (const token of node.vars) {
2305
- if (token.typ == EnumToken.IdenTokenType || isIdentColor(token)) {
2306
- if (!(token.val in cssVariablesMap[from.val])) {
2065
+ for (const token of arena.tokens(node)) {
2066
+ if (arena.dataTypes[token] == EnumToken.IdenTokenType || isIdentColor(arena, token)) {
2067
+ if (!(arena.strings[arena.stringValue[token]] in cssVariablesMap[from.val])) {
2307
2068
  errors.push({
2308
2069
  node,
2309
- message: `value '${token.val}' is not exported from '${from.val}'`,
2070
+ message: `value '${arena.strings[arena.stringValue[token]]}' is not exported from '${from.val}'`,
2310
2071
  action: "drop",
2311
2072
  });
2312
2073
  continue;
2313
2074
  }
2314
2075
  result.cssModuleVariables ??= {};
2315
- result.cssModuleVariables[token.val] = importedCssVariables[token.val] = cssVariablesMap[from.val][token.val];
2076
+ result.cssModuleVariables[arena.strings[arena.stringValue[token]]] = importedCssVariables[arena.strings[arena.stringValue[token]]] = cssVariablesMap[from.val][arena.strings[arena.stringValue[token]]];
2316
2077
  }
2317
2078
  }
2318
2079
  }
2319
- parent.chi.splice(parent.chi.indexOf(node), 1);
2080
+ arena.detach(node);
2320
2081
  continue;
2321
2082
  }
2322
- if (node.typ == EnumToken.CssVariableTokenType) {
2323
- if (parent?.typ == EnumToken.StyleSheetNodeType) {
2083
+ if (nodeType == EnumToken.CssVariableTokenType) {
2084
+ if (parent != null && arena.dataTypes[parent] == EnumToken.StyleSheetNodeType) {
2324
2085
  if (result.cssModuleVariables == null) {
2325
2086
  result.cssModuleVariables = {};
2326
2087
  }
2327
- // @ts-ignore
2328
- result.cssModuleVariables[node.nam] = node;
2088
+ // inlined: cssVariableToken(arena, node as AstNode): the object view of the css variable node
2089
+ // inlined: arena.toObject(node as AstNode)
2090
+ result.cssModuleVariables[arena.strings[arena.name[node]]] = new AstArenaNode(arena, node);
2329
2091
  }
2330
- parent.chi.splice(parent.chi.indexOf(node), 1);
2092
+ arena.detach(node);
2331
2093
  continue;
2332
2094
  }
2333
- if (node.typ == EnumToken.DeclarationNodeType) {
2334
- if (node.nam.startsWith("--")) {
2335
- if (!(node.nam in namesMapping)) {
2095
+ if (nodeType == EnumToken.DeclarationNodeType) {
2096
+ if (arena.strings[arena.name[node]].startsWith("--")) {
2097
+ if (!(arena.strings[arena.name[node]] in namesMapping)) {
2336
2098
  let result = moduleSettings.scoped & ModuleScopeEnumOptions.Global
2337
- ? node.nam
2338
- : moduleSettings.generateScopedName(node.nam, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
2099
+ ? arena.strings[arena.name[node]]
2100
+ : moduleSettings.generateScopedName(arena.strings[arena.name[node]], moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
2339
2101
  // @ts-ignore
2340
2102
  let value = result instanceof Promise ? await result : result;
2341
- mapping[node.nam] =
2103
+ mapping[arena.strings[arena.name[node]]] =
2342
2104
  "--" +
2343
2105
  (moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
2344
2106
  moduleSettings.naming & ModuleCaseTransformEnum.CamelCaseOnly
2345
2107
  ? getKeyName(value, moduleSettings.naming)
2346
2108
  : value);
2347
- revMapping[node.nam] = node.nam;
2109
+ revMapping[arena.strings[arena.name[node]]] =
2110
+ arena.strings[arena.name[node]];
2348
2111
  }
2349
- node.nam = mapping[node.nam];
2112
+ // inlined: arena.setNameString(node as AstNode, mapping[arena.strings[arena.name[node as AstNode]]])
2113
+ arena.name[node] = arena.internString(mapping[arena.strings[arena.name[node]]]);
2350
2114
  }
2351
- if (equalsIgnoreCase("composes", node.nam)) {
2115
+ if (equalsIgnoreCase("composes", arena.strings[arena.name[node]])) {
2352
2116
  const composeSelectors = [];
2353
2117
  // let isValid: boolean = true;
2354
- for (const token of node.val) {
2355
- if (token.typ == EnumToken.ComposesSelectorNodeType) {
2118
+ for (let token = arena.firstToken[node]; token != 0; token = arena.nextSibling[token]) {
2119
+ if (arena.dataTypes[token] == EnumToken.ComposesSelectorNodeType) {
2356
2120
  composeSelectors.push(token);
2357
2121
  }
2358
2122
  }
2359
2123
  // find parent rule
2360
- let parentRule = parent;
2361
- while (parentRule != null && parentRule.typ != EnumToken.RuleNodeType) {
2362
- parentRule = parentRule[PARENT];
2124
+ let parentRule = parent ?? 0;
2125
+ while (parentRule != 0 && arena.dataTypes[parentRule] != EnumToken.RuleNodeType) {
2126
+ parentRule = arena.parents[parentRule];
2363
2127
  }
2364
2128
  if ( /* !isValid || */composeSelectors.length == 0) {
2365
2129
  errors.push({
@@ -2367,42 +2131,43 @@ async function doParse(iter, options = {}) {
2367
2131
  message: `composes is empty`,
2368
2132
  node,
2369
2133
  });
2370
- parentRule.chi.splice(parentRule.chi.indexOf(node), 1);
2134
+ arena.detach(node);
2371
2135
  continue;
2372
2136
  }
2373
2137
  const resolvedSrc = options.resolve(options.src, options.cwd);
2374
2138
  for (const token of composeSelectors) {
2375
2139
  // composes: a b c;
2376
- if (token.r == null) {
2377
- for (const rule of token.l) {
2378
- if (rule.typ == EnumToken.WhitespaceTokenType ||
2379
- rule.typ == EnumToken.CommentTokenType) {
2140
+ if (!arena.hasField(token, TokenField.R)) {
2141
+ for (const rule of arena.fieldTokens(token, TokenField.L)) {
2142
+ if (arena.dataTypes[rule] == EnumToken.WhitespaceTokenType ||
2143
+ arena.dataTypes[rule] == EnumToken.CommentTokenType) {
2380
2144
  continue;
2381
2145
  }
2382
- if (!(rule.val in mapping)) {
2146
+ if (!(arena.strings[arena.stringValue[rule]] in mapping)) {
2383
2147
  let result = moduleSettings.scoped & ModuleScopeEnumOptions.Global
2384
- ? rule.val
2385
- : moduleSettings.generateScopedName(rule.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
2148
+ ? arena.strings[arena.stringValue[rule]]
2149
+ : moduleSettings.generateScopedName(arena.strings[arena.stringValue[rule]], moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
2386
2150
  // @ts-expect-error
2387
2151
  let value = result instanceof Promise ? await result : result;
2388
- mapping[rule.val] =
2389
- (rule.typ == EnumToken.DashedIdenTokenType ? "--" : "") +
2152
+ mapping[arena.strings[arena.stringValue[rule]]] =
2153
+ (arena.dataTypes[rule] == EnumToken.DashedIdenTokenType ? "--" : "") +
2390
2154
  (moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
2391
2155
  moduleSettings.naming & ModuleCaseTransformEnum.CamelCaseOnly
2392
2156
  ? getKeyName(value, moduleSettings.naming)
2393
2157
  : value);
2394
- revMapping[mapping[rule.val]] = rule.val;
2158
+ revMapping[mapping[arena.strings[arena.stringValue[rule]]]] =
2159
+ arena.strings[arena.stringValue[rule]];
2395
2160
  }
2396
- if (parentRule != null) {
2397
- for (const tk of parentRule[TOKENS]) {
2398
- if (tk.typ == EnumToken.ClassSelectorTokenType) {
2399
- const val = tk.val.slice(1);
2161
+ if (parentRule != 0) {
2162
+ for (const tk of arena.tokens(parentRule)) {
2163
+ if (arena.dataTypes[tk] == EnumToken.ClassSelectorTokenType) {
2164
+ const val = arena.strings[arena.stringValue[tk]].slice(1);
2400
2165
  if (val in revMapping) {
2401
2166
  const key = revMapping[val];
2402
2167
  mapping[key] = [
2403
2168
  ...new Set([
2404
2169
  ...mapping[key].split(" "),
2405
- mapping[rule.val],
2170
+ mapping[arena.strings[arena.stringValue[rule]]],
2406
2171
  ]),
2407
2172
  ].join(" ");
2408
2173
  }
@@ -2412,8 +2177,8 @@ async function doParse(iter, options = {}) {
2412
2177
  }
2413
2178
  }
2414
2179
  // composes: a b c from 'file.css';
2415
- else if (token.r.typ == EnumToken.String) {
2416
- const url = token.r.val.slice(1, -1);
2180
+ else if (arena.dataTypes[arena.field(token, TokenField.R)] == EnumToken.StringTokenType) {
2181
+ const url = arena.strings[arena.stringValue[arena.field(token, TokenField.R)]].slice(1, -1);
2417
2182
  const src = options.resolve(url, options.dirname(options.src), options.cwd);
2418
2183
  const result = options.load(src, "");
2419
2184
  const stream = result instanceof Promise ||
@@ -2445,35 +2210,35 @@ async function doParse(iter, options = {}) {
2445
2210
  if (Object.keys(root.mapping).length > 0) {
2446
2211
  importMapping[srcIndex] = {};
2447
2212
  }
2448
- if (parentRule != null) {
2449
- for (const tk of parentRule[TOKENS]) {
2450
- if (tk.typ == EnumToken.ClassSelectorTokenType) {
2451
- const val = tk.val.slice(1);
2213
+ if (parentRule != 0) {
2214
+ for (const tk of arena.tokens(parentRule)) {
2215
+ if (arena.dataTypes[tk] == EnumToken.ClassSelectorTokenType) {
2216
+ const val = arena.strings[arena.stringValue[tk]].slice(1);
2452
2217
  if (val in revMapping) {
2453
2218
  const key = revMapping[val];
2454
2219
  const values = [];
2455
- for (const iden of token.l) {
2456
- if (iden.typ != EnumToken.IdenTokenType &&
2457
- iden.typ != EnumToken.DashedIdenTokenType) {
2220
+ for (const iden of arena.fieldTokens(token, TokenField.L)) {
2221
+ if (arena.dataTypes[iden] != EnumToken.IdenTokenType &&
2222
+ arena.dataTypes[iden] != EnumToken.DashedIdenTokenType) {
2458
2223
  continue;
2459
2224
  }
2460
- if (!(iden.val in root.mapping)) {
2225
+ if (!(arena.strings[arena.stringValue[iden]] in root.mapping)) {
2461
2226
  const result = moduleSettings.scoped & ModuleScopeEnumOptions.Global
2462
- ? iden.val
2463
- : moduleSettings.generateScopedName(iden.val, srcIndex, moduleSettings.pattern, moduleSettings.hashLength);
2227
+ ? arena.strings[arena.stringValue[iden]]
2228
+ : moduleSettings.generateScopedName(arena.strings[arena.stringValue[iden]], srcIndex, moduleSettings.pattern, moduleSettings.hashLength);
2464
2229
  let value =
2465
2230
  // @ts-expect-error
2466
2231
  result instanceof Promise ? await result : result;
2467
- root.mapping[iden.val] =
2232
+ root.mapping[arena.strings[arena.stringValue[iden]]] =
2468
2233
  moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
2469
2234
  moduleSettings.naming & ModuleCaseTransformEnum.CamelCaseOnly
2470
2235
  ? getKeyName(value, moduleSettings.naming)
2471
2236
  : value;
2472
- root.revMapping[root.mapping[iden.val]] = iden.val;
2237
+ root.revMapping[root.mapping[arena.strings[arena.stringValue[iden]]]] = arena.strings[arena.stringValue[iden]];
2473
2238
  }
2474
- importMapping[srcIndex][iden.val] =
2475
- root.mapping[iden.val];
2476
- values.push(root.mapping[iden.val]);
2239
+ importMapping[srcIndex][arena.strings[arena.stringValue[iden]]] =
2240
+ root.mapping[arena.strings[arena.stringValue[iden]]];
2241
+ values.push(root.mapping[arena.strings[arena.stringValue[iden]]]);
2477
2242
  }
2478
2243
  mapping[key] = [...new Set([...mapping[key].split(" "), ...values])].join(" ");
2479
2244
  }
@@ -2482,24 +2247,25 @@ async function doParse(iter, options = {}) {
2482
2247
  }
2483
2248
  }
2484
2249
  // composes: a b c from global;
2485
- else if (token.r.typ == EnumToken.IdenTokenType) {
2250
+ else if (arena.dataTypes[arena.field(token, TokenField.R)] == EnumToken.IdenTokenType) {
2486
2251
  // global
2487
- if (parentRule != null) {
2488
- if (equalsIgnoreCase("global", token.r.val)) {
2489
- for (const tk of parentRule[TOKENS]) {
2490
- if (tk.typ == EnumToken.ClassSelectorTokenType) {
2491
- const val = tk.val.slice(1);
2252
+ if (parentRule != 0) {
2253
+ if (equalsIgnoreCase("global", arena.strings[arena.stringValue[arena.field(token, TokenField.R)]])) {
2254
+ for (const tk of arena.tokens(parentRule)) {
2255
+ if (arena.dataTypes[tk] == EnumToken.ClassSelectorTokenType) {
2256
+ const val = arena.strings[arena.stringValue[tk]].slice(1);
2492
2257
  if (val in revMapping) {
2493
2258
  const key = revMapping[val];
2259
+ const composedNames = [];
2260
+ for (const curr of arena.fieldTokens(token, TokenField.L)) {
2261
+ if (arena.dataTypes[curr] == EnumToken.IdenTokenType) {
2262
+ composedNames.push(arena.strings[arena.stringValue[curr]]);
2263
+ }
2264
+ }
2494
2265
  mapping[key] = [
2495
2266
  ...new Set([
2496
2267
  ...mapping[key].split(" "),
2497
- ...token.l.reduce((acc, curr) => {
2498
- if (curr.typ == EnumToken.IdenTokenType) {
2499
- acc.push(curr.val);
2500
- }
2501
- return acc;
2502
- }, []),
2268
+ ...composedNames,
2503
2269
  ]),
2504
2270
  ].join(" ");
2505
2271
  }
@@ -2509,32 +2275,32 @@ async function doParse(iter, options = {}) {
2509
2275
  else {
2510
2276
  errors.push({
2511
2277
  action: "drop",
2512
- message: `composes '${token.r.val}' is not supported`,
2278
+ message: `composes '${arena.strings[arena.stringValue[arena.field(token, TokenField.R)]]}' is not supported`,
2513
2279
  node,
2514
2280
  });
2515
2281
  }
2516
2282
  }
2517
2283
  }
2518
2284
  }
2519
- parent.chi.splice(parent.chi.indexOf(node), 1);
2520
- }
2521
- if (node.typ == EnumToken.DeclarationNodeType &&
2522
- [
2523
- "grid-column",
2524
- "grid-column-start",
2525
- "grid-column-end",
2526
- "grid-row",
2527
- "grid-row-start",
2528
- "grid-row-end",
2529
- "grid-template",
2530
- "grid-template-columns",
2531
- "grid-template-rows",
2532
- ].includes(node.nam)) {
2533
- for (const { value } of walkValues(node.val, node)) {
2534
- if (value.typ != EnumToken.IdenTokenType) {
2285
+ arena.detach(node);
2286
+ }
2287
+ if ([
2288
+ "grid-column",
2289
+ "grid-column-start",
2290
+ "grid-column-end",
2291
+ "grid-row",
2292
+ "grid-row-start",
2293
+ "grid-row-end",
2294
+ "grid-template",
2295
+ "grid-template-columns",
2296
+ "grid-template-rows",
2297
+ ].includes(arena.strings[arena.name[node]])) {
2298
+ // inlined: arena.walkValues(node as AstNode)
2299
+ for (const value of new ValueWalker(arena, node)) {
2300
+ if (arena.dataTypes[value] != EnumToken.IdenTokenType) {
2535
2301
  continue;
2536
2302
  }
2537
- let idenToken = value.val;
2303
+ let idenToken = arena.strings[arena.stringValue[value]];
2538
2304
  let suffix = "";
2539
2305
  if (idenToken.endsWith("-start")) {
2540
2306
  suffix = "-start";
@@ -2562,45 +2328,49 @@ async function doParse(iter, options = {}) {
2562
2328
  }
2563
2329
  }
2564
2330
  }
2565
- value.val = mapping[idenToken];
2566
- }
2567
- }
2568
- else if (node.nam == "grid-template-areas" ||
2569
- node.nam == "grid-template") {
2570
- for (let i = 0; i < node.val.length; i++) {
2571
- if (node.val[i].typ == EnumToken.String) {
2572
- const tokens = parseString(node.val[i].val.slice(1, -1));
2573
- for (const { value } of walkValues(tokens)) {
2574
- if (value.typ == EnumToken.IdenTokenType ||
2575
- value.typ == EnumToken.DashedIdenTokenType) {
2576
- if (value.val in mapping) {
2577
- value.val = mapping[value.val];
2331
+ // inlined: arena.setStr(value, mapping[idenToken])
2332
+ arena.stringValue[value] = arena.internString(mapping[idenToken]);
2333
+ }
2334
+ }
2335
+ else if (arena.strings[arena.name[node]] == "grid-template-areas" ||
2336
+ arena.strings[arena.name[node]] == "grid-template") {
2337
+ for (let token = arena.firstToken[node]; token != 0; token = arena.nextSibling[token]) {
2338
+ if (arena.dataTypes[token] == EnumToken.StringTokenType) {
2339
+ const str = arena.strings[arena.stringValue[token]];
2340
+ const tokens = parseString(arena, str.slice(1, -1));
2341
+ // inlined: arena.walkValues(tokens)
2342
+ for (const value of new ValueWalker(arena, tokens)) {
2343
+ if (arena.dataTypes[value] == EnumToken.IdenTokenType ||
2344
+ arena.dataTypes[value] == EnumToken.DashedIdenTokenType) {
2345
+ if (arena.strings[arena.stringValue[value]] in mapping) {
2346
+ // inlined: arena.setStr(value, mapping[arena.strings[arena.stringValue[value]]])
2347
+ arena.stringValue[value] = arena.internString(mapping[arena.strings[arena.stringValue[value]]]);
2578
2348
  }
2579
2349
  else {
2580
2350
  let result = moduleSettings.scoped & ModuleScopeEnumOptions.Global
2581
- ? value.val
2582
- : moduleSettings.generateScopedName(value.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
2351
+ ? arena.strings[arena.stringValue[value]]
2352
+ : moduleSettings.generateScopedName(arena.strings[arena.stringValue[value]], moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
2583
2353
  // @ts-expect-error
2584
2354
  if (result instanceof Promise) {
2585
2355
  result = await result;
2586
2356
  }
2587
- mapping[value.val] = result;
2588
- revMapping[result] = value.val;
2589
- value.val = result;
2357
+ mapping[arena.strings[arena.stringValue[value]]] = result;
2358
+ revMapping[result] = arena.strings[arena.stringValue[value]];
2359
+ // inlined: arena.setStr(value, result)
2360
+ arena.stringValue[value] = arena.internString(result);
2590
2361
  }
2591
2362
  }
2592
2363
  }
2593
- node.val[i].val =
2594
- node.val[i].val.charAt(0) +
2595
- renderTokens(tokens) +
2596
- node.val[i].val.charAt(node.val[i].val.length - 1);
2364
+ // inlined: arena.setStr(token, str.charAt(0) + renderTokens(arena, tokens) + str.charAt(str.length - 1))
2365
+ arena.stringValue[token] = arena.internString((str.charAt(0) + renderTokens(arena, tokens) + str.charAt(str.length - 1)));
2597
2366
  }
2598
2367
  }
2599
2368
  }
2600
- else if (node.nam == "animation" ||
2601
- node.nam == "animation-name") {
2602
- for (const { value } of walkValues(node.val, node)) {
2603
- if (value.typ == EnumToken.IdenTokenType &&
2369
+ else if (arena.strings[arena.name[node]] == "animation" ||
2370
+ arena.strings[arena.name[node]] == "animation-name") {
2371
+ // inlined: arena.walkValues(node as AstNode)
2372
+ for (const value of new ValueWalker(arena, node)) {
2373
+ if (arena.dataTypes[value] == EnumToken.IdenTokenType &&
2604
2374
  ![
2605
2375
  "none",
2606
2376
  "infinite",
@@ -2629,86 +2399,90 @@ async function doParse(iter, options = {}) {
2629
2399
  "inherit",
2630
2400
  "initial",
2631
2401
  "unset",
2632
- ].includes(value.val)) {
2633
- if (!(value.val in mapping)) {
2402
+ ].includes(arena.strings[arena.stringValue[value]])) {
2403
+ if (!(arena.strings[arena.stringValue[value]] in mapping)) {
2634
2404
  const result = moduleSettings.scoped & ModuleScopeEnumOptions.Global
2635
- ? value.val
2636
- : moduleSettings.generateScopedName(value.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
2405
+ ? arena.strings[arena.stringValue[value]]
2406
+ : moduleSettings.generateScopedName(arena.strings[arena.stringValue[value]], moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
2637
2407
  // @ts-expect-error
2638
- mapping[value.val] = result instanceof Promise ? await result : result;
2639
- revMapping[mapping[value.val]] = value.val;
2408
+ mapping[arena.strings[arena.stringValue[value]]] = result instanceof Promise ? await result : result;
2409
+ revMapping[mapping[arena.strings[arena.stringValue[value]]]] = arena.strings[arena.stringValue[value]];
2640
2410
  }
2641
- value.val = mapping[value.val];
2411
+ // inlined: arena.setStr(value, mapping[arena.strings[arena.stringValue[value]]])
2412
+ arena.stringValue[value] = arena.internString(mapping[arena.strings[arena.stringValue[value]]]);
2642
2413
  }
2643
2414
  }
2644
2415
  }
2645
- for (const { value, parent } of walkValues(node.val, node)) {
2646
- if (value.typ == EnumToken.DashedIdenTokenType) {
2647
- value.val = mapping[value.val];
2416
+ // inlined: arena.walkValues(node as AstNode)
2417
+ for (const value of new ValueWalker(arena, node)) {
2418
+ if (arena.dataTypes[value] == EnumToken.DashedIdenTokenType) {
2419
+ // inlined: arena.setStr(value, mapping[arena.strings[arena.stringValue[value]]])
2420
+ arena.stringValue[value] = arena.internString(mapping[arena.strings[arena.stringValue[value]]]);
2648
2421
  }
2649
- else if ((value.typ == EnumToken.IdenTokenType || isIdentColor(value)) &&
2650
- value.val in importedCssVariables) {
2651
- replaceNodeOrValue(
2652
- // @ts-ignore
2653
- parent, value, importedCssVariables[value.val].val);
2422
+ else if ((arena.dataTypes[value] == EnumToken.IdenTokenType || isIdentColor(arena, value)) &&
2423
+ arena.strings[arena.stringValue[value]] in importedCssVariables) {
2424
+ arena.replaceToken(value, ...arena.tokens(importedCssVariables[arena.strings[arena.stringValue[value]]][ARENAID]).map((token) => arena.clone(token, true)));
2654
2425
  }
2655
2426
  }
2656
2427
  }
2657
- else if (node.typ == EnumToken.RuleNodeType) {
2428
+ else if (nodeType == EnumToken.RuleNodeType) {
2658
2429
  // if (node[TOKENS] == null) {
2659
- const tokens = parseString(node.sel);
2660
- matchSelectorSyntax(tokens, [], options);
2661
- node[TOKENS] = trimArray(tokens);
2662
- // }
2430
+ const tokens = parseString(arena, arena.strings[arena.name[node]]);
2431
+ matchSelectorSyntax(arena, tokens, [], options);
2432
+ arena.setTokenList(node, trimArray(arena, tokens));
2663
2433
  let hasIdOrClass = false;
2664
- for (const { value } of walkValues(node[TOKENS], node,
2665
- // @ts-ignore
2666
- (value, parent) => {
2667
- if (value.typ == EnumToken.PseudoClassTokenType ||
2668
- value.typ == EnumToken.PseudoElementTokenType) {
2669
- const val = value.val.toLowerCase();
2434
+ // inlined: arena.walkValues(node as AstNode, filter)
2435
+ for (const value of new ValueWalker(arena, node, (arena, value) => {
2436
+ if (arena.dataTypes[value] == EnumToken.PseudoClassTokenType ||
2437
+ arena.dataTypes[value] == EnumToken.PseudoElementTokenType) {
2438
+ const val = arena.strings[arena.stringValue[value]].toLowerCase();
2670
2439
  switch (val) {
2671
2440
  case ":local":
2672
2441
  case ":global":
2673
2442
  {
2674
- let index = parent[TOKENS].indexOf(value);
2675
- parent[TOKENS].splice(index, 1);
2676
- if (parent[TOKENS][index]?.typ == EnumToken.WhitespaceTokenType ||
2677
- parent[TOKENS][index]?.typ ==
2678
- EnumToken.DescendantCombinatorTokenType) {
2679
- parent[TOKENS].splice(index, 1);
2443
+ // remove the pseudo class and the following whitespace
2444
+ const next = arena.nextSibling[value];
2445
+ arena.detach(value);
2446
+ if (next != 0 &&
2447
+ (arena.dataTypes[next] == EnumToken.WhitespaceTokenType ||
2448
+ arena.dataTypes[next] == EnumToken.DescendantCombinatorTokenType)) {
2449
+ arena.detach(next);
2680
2450
  }
2681
2451
  }
2682
2452
  break;
2683
2453
  }
2684
2454
  }
2685
- else if (value.typ == EnumToken.PseudoClassFuncTokenType) {
2686
- switch (value.val.toLowerCase()) {
2455
+ else if (arena.dataTypes[value] == EnumToken.PseudoClassFuncTokenType) {
2456
+ switch (arena.strings[arena.stringValue[value]].toLowerCase()) {
2687
2457
  case ":global":
2688
- for (const token of value.chi) {
2458
+ for (const token of arena.tokens(value)) {
2689
2459
  global.add(token);
2690
2460
  }
2691
- case ":local":
2692
- parent[TOKENS].splice(parent[TOKENS].indexOf(value), 1, ...value.chi);
2693
- break;
2461
+ case ":local": {
2462
+ // replace the function by its arguments. the arguments are visited
2463
+ const children = arena.tokens(value);
2464
+ arena.replaceToken(value, ...children);
2465
+ return children;
2466
+ }
2694
2467
  }
2695
2468
  }
2469
+ return null;
2696
2470
  })) {
2697
- if (value.typ == EnumToken.HashTokenType || value.typ == EnumToken.ClassSelectorTokenType) {
2471
+ if (arena.dataTypes[value] == EnumToken.HashTokenType || arena.dataTypes[value] == EnumToken.ClassSelectorTokenType) {
2698
2472
  hasIdOrClass = true;
2699
2473
  }
2700
2474
  if (processed.has(value)) {
2701
2475
  continue;
2702
2476
  }
2703
2477
  processed.add(value);
2704
- if (value.typ == EnumToken.PseudoClassTokenType || value.typ == EnumToken.PseudoElementTokenType) ;
2705
- else if (value.typ == EnumToken.PseudoClassFuncTokenType) ;
2478
+ if (arena.dataTypes[value] == EnumToken.PseudoClassTokenType || arena.dataTypes[value] == EnumToken.PseudoElementTokenType) ;
2479
+ else if (arena.dataTypes[value] == EnumToken.PseudoClassFuncTokenType) ;
2706
2480
  else {
2707
2481
  if (global.has(value)) {
2708
2482
  continue;
2709
2483
  }
2710
- if (value.typ == EnumToken.ClassSelectorTokenType) {
2711
- const val = value.val.slice(1);
2484
+ if (arena.dataTypes[value] == EnumToken.ClassSelectorTokenType) {
2485
+ const val = arena.strings[arena.stringValue[value]].slice(1);
2712
2486
  if (!(val in mapping)) {
2713
2487
  const result = moduleSettings.scoped & ModuleScopeEnumOptions.Global
2714
2488
  ? val
@@ -2722,48 +2496,52 @@ async function doParse(iter, options = {}) {
2722
2496
  : value;
2723
2497
  revMapping[mapping[val]] = val;
2724
2498
  }
2725
- value.val = "." + mapping[val];
2499
+ // inlined: arena.setStr(value, "." + mapping[val])
2500
+ arena.stringValue[value] = arena.internString(("." + mapping[val]));
2726
2501
  }
2727
2502
  }
2728
2503
  }
2729
2504
  if (moduleSettings.scoped & ModuleScopeEnumOptions.Pure) {
2730
2505
  if (!hasIdOrClass) {
2731
- throw new Error(`pure module: No id or class found in selector '${node.sel}' at '${(options.source?.getOffsets?.(node[LOCSTA]) ?? []).join(":")}'`);
2506
+ throw new Error(
2507
+ // inlined: options.source?.getOffsets?.(arena.getSpanStart(node as AstNode))
2508
+ `pure module: No id or class found in selector '${arena.strings[arena.name[node]]}' at '${(options.source?.lineStarts.getOffsets(arena.getSpanStart(node)) ?? []).join(":")}'`);
2732
2509
  }
2733
2510
  }
2734
- node.sel = "";
2735
- for (const token of node[TOKENS]) {
2736
- node.sel += renderValue(token);
2737
- }
2511
+ // inlined: arena.setSelector(node as AstNode, arena.tokens(node as AstNode).reduce((acc: string, token: Token) => acc + renderValue(arena, token), ""))
2512
+ arena.name[node] = arena.internString(arena.tokens(node).reduce((acc, token) => acc + renderValue(arena, token), ""));
2738
2513
  }
2739
- else if (node.typ == EnumToken.AtRuleNodeType || node.typ == EnumToken.KeyframesAtRuleNodeType) {
2740
- const val = node.nam.toLowerCase();
2741
- if (node[TOKENS] == null) {
2742
- node[TOKENS] = parseString(node.val);
2514
+ else if (nodeType == EnumToken.AtRuleNodeType || nodeType == EnumToken.KeyframesAtRuleNodeType) {
2515
+ const val = arena.strings[arena.name[node]].toLowerCase();
2516
+ if (arena.firstToken[node] == 0) {
2517
+ // inlined: arena.setTokens(node as AstNode, ...parseString(arena, arena.strings[arena.stringValue[node as AstNode]]))
2518
+ arena.setTokenList(node, parseString(arena, arena.strings[arena.stringValue[node]]));
2743
2519
  }
2744
2520
  if (val == "property" || val == "keyframes") {
2745
2521
  const prefix = val == "property" ? "--" : "";
2746
- for (const value of node[TOKENS]) {
2747
- if ((prefix == "--" && value.typ == EnumToken.DashedIdenTokenType) ||
2748
- (prefix == "" && value.typ == EnumToken.IdenTokenType)) {
2749
- if (!(value.val in mapping)) {
2522
+ for (const value of arena.tokens(node)) {
2523
+ if ((prefix == "--" && arena.dataTypes[value] == EnumToken.DashedIdenTokenType) ||
2524
+ (prefix == "" && arena.dataTypes[value] == EnumToken.IdenTokenType)) {
2525
+ if (!(arena.strings[arena.stringValue[value]] in mapping)) {
2750
2526
  const result = moduleSettings.scoped & ModuleScopeEnumOptions.Global
2751
- ? value.val
2752
- : moduleSettings.generateScopedName(value.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
2527
+ ? arena.strings[arena.stringValue[value]]
2528
+ : moduleSettings.generateScopedName(arena.strings[arena.stringValue[value]], moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
2753
2529
  // @ts-expect-error
2754
2530
  let val = result instanceof Promise ? await result : result;
2755
- mapping[value.val] =
2531
+ mapping[arena.strings[arena.stringValue[value]]] =
2756
2532
  prefix +
2757
2533
  (moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
2758
2534
  moduleSettings.naming & ModuleCaseTransformEnum.CamelCaseOnly
2759
2535
  ? getKeyName(val, moduleSettings.naming)
2760
2536
  : val);
2761
- revMapping[mapping[value.val]] = value.val;
2537
+ revMapping[mapping[arena.strings[arena.stringValue[value]]]] = arena.strings[arena.stringValue[value]];
2762
2538
  }
2763
- value.val = mapping[value.val];
2539
+ // inlined: arena.setStr(value, mapping[arena.strings[arena.stringValue[value]]])
2540
+ arena.stringValue[value] = arena.internString(mapping[arena.strings[arena.stringValue[value]]]);
2764
2541
  }
2765
2542
  }
2766
- node.val = renderTokens(node[TOKENS]);
2543
+ // inlined: arena.setValueString(node as AstNode, renderTokens(arena, arena.tokens(node as AstNode)))
2544
+ arena.stringValue[node] = arena.internString(renderTokens(arena, arena.tokens(node)));
2767
2545
  }
2768
2546
  }
2769
2547
  }
@@ -2790,110 +2568,115 @@ async function doParse(iter, options = {}) {
2790
2568
  }
2791
2569
  return result;
2792
2570
  }
2793
- function parseNode(tokens, context, options, errors, stats, invalidNodes) {
2571
+ function parseNode(arena, tokens, context, options, errors, stats, invalidNodes, parensMatch) {
2794
2572
  let i = 0;
2795
- if (tokens.at(-1)?.typ === EnumToken.EOFTokenType) {
2573
+ const contextType = arena.dataTypes[context];
2574
+ const dataTypes = arena.dataTypes;
2575
+ const lastToken = tokens[tokens.length - 1];
2576
+ if (lastToken != null && dataTypes[lastToken] === EnumToken.EOFTokenType) {
2796
2577
  tokens.pop();
2797
2578
  // check parenthesis are balanced
2798
- let matchCount = 0;
2799
- for (let i = 0; i < tokens.length; i++) {
2800
- const token = tokens[i];
2801
- if (token.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(token.typ)) {
2802
- matchCount++;
2803
- }
2804
- else if (token.typ === EnumToken.EndParensTokenType) {
2805
- matchCount--;
2806
- }
2807
- }
2579
+ let matchCount = parensMatch;
2808
2580
  if (matchCount > 0) {
2809
2581
  let k = tokens.length;
2810
2582
  while (k-- > 0 &&
2811
- (tokens[k].typ === EnumToken.WhitespaceTokenType || tokens[k].typ === EnumToken.CommentTokenType))
2583
+ (dataTypes[tokens[k]] === EnumToken.WhitespaceTokenType || dataTypes[tokens[k]] === EnumToken.CommentTokenType))
2812
2584
  ;
2813
- if (tokens[k]?.typ == EnumToken.SemiColonTokenType) {
2585
+ if (dataTypes[tokens[k]] == EnumToken.SemiColonTokenType) {
2814
2586
  matchCount = 0;
2815
2587
  }
2816
2588
  while (matchCount > 0) {
2817
- tokens.push({
2818
- typ: EnumToken.EndParensTokenType,
2819
- [LOCSRCID]: tokens[k]?.[LOCSRCID],
2820
- [LOCSTA]: tokens[k]?.[LOCSTA],
2821
- [LOCEND]: tokens[k]?.[LOCEND],
2822
- });
2589
+ const closing = arena.createToken(EnumToken.EndParensTokenType);
2590
+ if (k >= 0) {
2591
+ arena.copyLocation(closing, tokens[k]);
2592
+ }
2593
+ tokens.push(closing);
2823
2594
  matchCount--;
2824
2595
  }
2825
2596
  }
2826
2597
  }
2827
2598
  for (; i < tokens.length; i++) {
2828
- if (tokens[i].typ === EnumToken.CDOCOMMTokenType && context.typ !== EnumToken.StyleSheetNodeType) {
2599
+ if (arena.dataTypes[tokens[i]] === EnumToken.CDOCOMMTokenType && contextType !== EnumToken.StyleSheetNodeType) {
2829
2600
  errors.push({
2830
2601
  action: "drop",
2831
2602
  message: `CDOCOMM not allowed here ${JSON.stringify(tokens[i], null, 1)}`,
2832
2603
  node: tokens[i],
2833
- location: options.source.getSourceLocation(tokens[i][LOCSTA]),
2604
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(tokens[i]))
2605
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[i]))],
2834
2606
  });
2835
- tokens[i].typ = EnumToken.InvalidCommentTokenType;
2607
+ // the misplaced CDO / CDC comment is rendered as an empty string
2608
+ arena.dataTypes[tokens[i]] = EnumToken.BadCdoTokenType;
2836
2609
  continue;
2837
2610
  }
2838
- if (tokens[i].typ === EnumToken.CommentTokenType ||
2839
- tokens[i].typ === EnumToken.CDOCOMMTokenType ||
2840
- tokens[i].typ === EnumToken.WhitespaceTokenType) {
2611
+ if (arena.dataTypes[tokens[i]] === EnumToken.CommentTokenType ||
2612
+ arena.dataTypes[tokens[i]] === EnumToken.CDOCOMMTokenType ||
2613
+ arena.dataTypes[tokens[i]] === EnumToken.WhitespaceTokenType) {
2841
2614
  continue;
2842
2615
  }
2843
2616
  break;
2844
2617
  }
2845
2618
  if (i > 0) {
2846
- context.chi.push(...tokens.splice(0, i).filter((n) => n.typ !== EnumToken.WhitespaceTokenType));
2619
+ // the leading comments are appended to the context, then removed with the whitespace (no array is created)
2620
+ for (let k = 0; k < i; k++) {
2621
+ if (arena.dataTypes[tokens[k]] !== EnumToken.WhitespaceTokenType) {
2622
+ // inlined: arena.appendChildren(context, createCommentNode(arena, tokens[k]))
2623
+ arena.appendChildList(context, [createCommentNode(arena, tokens[k])]);
2624
+ }
2625
+ }
2626
+ tokens.copyWithin(0, i);
2627
+ tokens.length -= i;
2847
2628
  i = 0;
2848
2629
  }
2849
2630
  for (; i < tokens.length; i++) {
2850
- if (tokens[i].typ == EnumToken.CommentTokenType || tokens[i].typ == EnumToken.CDOCOMMTokenType) {
2851
- if (tokens[i].typ == EnumToken.CDOCOMMTokenType && context.typ != EnumToken.StyleSheetNodeType) {
2631
+ if (arena.dataTypes[tokens[i]] == EnumToken.CommentTokenType || arena.dataTypes[tokens[i]] == EnumToken.CDOCOMMTokenType) {
2632
+ if (arena.dataTypes[tokens[i]] == EnumToken.CDOCOMMTokenType && contextType != EnumToken.StyleSheetNodeType) {
2852
2633
  errors.push({
2853
2634
  action: "drop",
2854
2635
  message: `CDOCOMM not allowed here ${JSON.stringify(tokens[i], null, 1)}`,
2855
2636
  node: tokens[i],
2856
- location: options.source.getSourceLocation(tokens[i][LOCSTA]),
2637
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(tokens[i]))
2638
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[i]))],
2857
2639
  });
2858
- tokens[i].typ = EnumToken.InvalidCommentTokenType;
2640
+ // the misplaced CDO / CDC comment is rendered as an empty string
2641
+ arena.dataTypes[tokens[i]] = EnumToken.BadCdoTokenType;
2859
2642
  continue;
2860
2643
  }
2861
- tokens[i][ROOT] = context[ROOT];
2862
- context.chi.push(tokens[i]);
2644
+ // inlined: arena.appendChildren(context, createCommentNode(arena, tokens[i]))
2645
+ arena.appendChildList(context, [createCommentNode(arena, tokens[i])]);
2863
2646
  stats.nodesCount++;
2864
2647
  }
2865
- else if (tokens[i].typ != EnumToken.WhitespaceTokenType) {
2648
+ else if (arena.dataTypes[tokens[i]] != EnumToken.WhitespaceTokenType) {
2866
2649
  break;
2867
2650
  }
2868
2651
  }
2869
2652
  if (tokens.length == 0) {
2870
2653
  return null;
2871
2654
  }
2872
- let delim = tokens.at(-1);
2873
- if (delim.typ == EnumToken.SemiColonTokenType ||
2874
- delim.typ == EnumToken.BlockStartTokenType ||
2875
- delim.typ == EnumToken.BlockEndTokenType) {
2655
+ let delim = tokens[tokens.length - 1];
2656
+ if (arena.dataTypes[delim] == EnumToken.SemiColonTokenType ||
2657
+ arena.dataTypes[delim] == EnumToken.BlockStartTokenType ||
2658
+ arena.dataTypes[delim] == EnumToken.BlockEndTokenType) {
2876
2659
  tokens.pop();
2877
2660
  }
2878
2661
  if (tokens.length == 0) {
2879
2662
  return null;
2880
2663
  }
2881
- if (tokens[0]?.typ == EnumToken.AtRuleTokenType) {
2664
+ if (arena.dataTypes[tokens[0]] == EnumToken.AtRuleTokenType) {
2882
2665
  let nestedRule = false;
2883
2666
  let parent = context;
2884
2667
  let node;
2885
- while (parent != null) {
2886
- if (parent.typ == EnumToken.RuleNodeType) {
2668
+ while (parent != 0) {
2669
+ if (arena.dataTypes[parent] == EnumToken.RuleNodeType) {
2887
2670
  nestedRule = true;
2888
2671
  break;
2889
2672
  }
2890
- parent = parent[PARENT];
2673
+ parent = arena.parents[parent];
2891
2674
  }
2892
- node = parseAtRule(tokens, context, { ...options, nestedRule }, errors, delim.typ == EnumToken.BlockStartTokenType);
2675
+ node = parseAtRule(arena, tokens, context, { ...options, nestedRule }, errors, arena.dataTypes[delim] == EnumToken.BlockStartTokenType);
2893
2676
  if (node == null) {
2894
2677
  return null;
2895
2678
  }
2896
- if (node[STATE] &
2679
+ if (arena.flags[node] &
2897
2680
  (EnumAstNodeStatus.Invalid |
2898
2681
  EnumAstNodeStatus.Disallowed |
2899
2682
  EnumAstNodeStatus.Unknown |
@@ -2902,48 +2685,47 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
2902
2685
  invalidNodes.push(node);
2903
2686
  }
2904
2687
  stats.nodesCount++;
2905
- context.chi.push(node);
2906
- node[ROOT] = context[ROOT];
2907
- node[PARENT] = context;
2908
- // @ts-ignore
2688
+ // inlined: arena.appendChildren(context, node)
2689
+ arena.appendChildList(context, [node]);
2909
2690
  return node;
2910
2691
  }
2911
2692
  else {
2912
2693
  stats.nodesCount++;
2913
2694
  // rule
2914
- if (delim.typ == EnumToken.BlockStartTokenType) {
2915
- const node = parseSelector(tokens, context, options, errors);
2916
- context.chi.push(node);
2917
- node[PARENT] = context;
2918
- node[ROOT] = context[ROOT];
2919
- if (node[STATE] & EnumAstNodeStatus.Invalid ||
2920
- node[STATE] & EnumAstNodeStatus.Disallowed ||
2921
- node[STATE] & EnumAstNodeStatus.Unknown ||
2922
- node[STATE] & EnumAstNodeStatus.Unparsed ||
2923
- node[STATE] & EnumAstNodeStatus.Malformed) {
2695
+ if (arena.dataTypes[delim] == EnumToken.BlockStartTokenType) {
2696
+ const node = parseSelector(arena, tokens, context, options, errors);
2697
+ // inlined: arena.appendChildren(context, node)
2698
+ arena.appendChildList(context, [node]);
2699
+ if (arena.flags[node] &
2700
+ (EnumAstNodeStatus.Invalid |
2701
+ EnumAstNodeStatus.Disallowed |
2702
+ EnumAstNodeStatus.Unknown |
2703
+ EnumAstNodeStatus.Unparsed |
2704
+ EnumAstNodeStatus.Malformed)) {
2924
2705
  invalidNodes.push(node);
2925
2706
  }
2926
2707
  return node;
2927
2708
  }
2928
2709
  else {
2929
- const node = parseDeclaration(tokens, context, options, errors);
2930
- node[PARENT] = context;
2931
- node[ROOT] = context[ROOT];
2932
- if (context.typ === EnumToken.StyleSheetNodeType && node.typ === EnumToken.DeclarationNodeType) {
2933
- node[STATE] |=
2710
+ const node = parseDeclaration(arena, tokens, context, options, errors);
2711
+ if (contextType === EnumToken.StyleSheetNodeType &&
2712
+ arena.dataTypes[node] === EnumToken.DeclarationNodeType) {
2713
+ arena.flags[node] |=
2934
2714
  EnumAstNodeStatus.Invalid &
2935
2715
  ~(EnumAstNodeStatus.Validated | EnumAstNodeStatus.Unvalidated | EnumAstNodeStatus.Unparsed);
2936
2716
  errors.push({
2937
2717
  message: "<declaration> not allowed in <stylesheet>",
2938
2718
  action: "drop",
2939
2719
  node,
2940
- location: options.source.getSourceLocation(node[LOCSTA]),
2720
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(node))
2721
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(node))],
2941
2722
  });
2942
2723
  }
2943
- else if (options.lenient || node.typ === EnumToken.DeclarationNodeType) {
2944
- context.chi.push(node);
2724
+ else if (options.lenient || arena.dataTypes[node] === EnumToken.DeclarationNodeType) {
2725
+ // inlined: arena.appendChildren(context, node)
2726
+ arena.appendChildList(context, [node]);
2945
2727
  }
2946
- if (node[STATE] &
2728
+ if (arena.flags[node] &
2947
2729
  (EnumAstNodeStatus.Invalid |
2948
2730
  EnumAstNodeStatus.Disallowed |
2949
2731
  EnumAstNodeStatus.Unknown |
@@ -2962,166 +2744,151 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
2962
2744
  * @param errors
2963
2745
  * @param parseAsBlock
2964
2746
  */
2965
- function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2747
+ function parseAtRule(arena, stream, context, options, errors, parseAsBlock = null) {
2966
2748
  let success = true;
2967
- let atRuleName = stream[0].nam;
2749
+ const contextType = arena.dataTypes[context];
2750
+ const contextName = contextType === EnumToken.AtRuleNodeType ? arena.strings[arena.name[context]] : null;
2751
+ const atRule = stream.shift();
2752
+ // at-rule name
2753
+ const nam = arena.strings[arena.name[atRule]];
2754
+ let atRuleName = nam;
2755
+ // prelude tokens, state, errors and last token of the at-rule node
2756
+ let atRuleTokens = null;
2757
+ let atRuleState = 0;
2758
+ let atRuleErrors = [];
2759
+ let atRuleEnd = atRule;
2968
2760
  if (atRuleName.startsWith("-")) {
2969
2761
  atRuleName = atRuleName.replace(/^-[a-z]+-/, "").toLowerCase();
2970
2762
  }
2971
- const atRule = stream.shift();
2972
- const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + atRule.nam);
2763
+ const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + nam);
2973
2764
  const syntax = syntaxRules?.getPreludeRules()?.slice?.(1);
2974
2765
  const blockAllowed = syntaxRules?.getBlockRules() != null;
2975
2766
  if (syntaxRules == null) {
2976
2767
  errors.push({
2977
2768
  action: "drop",
2978
2769
  node: atRule,
2979
- location: options.source.getSourceLocation(atRule[LOCSTA]),
2770
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
2771
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
2980
2772
  message: "unknown at-rule",
2981
2773
  });
2982
- const result = matchGenericSyntax(stream, options);
2983
- atRule[TOKENS] = parseTokens(stream);
2984
- atRule[STATE] = result.success ? EnumAstNodeStatus.Unknown : EnumAstNodeStatus.Invalid;
2985
- atRule[ERRORS] = result.success ? [errors[errors.length - 1]] : [errors[errors.length - 1], ...result.errors];
2986
- // @ts-expect-error
2987
- return Object.assign(atRule, {
2988
- typ: EnumToken.AtRuleNodeType,
2989
- val: renderTokens(trimArray(stream), options),
2990
- ...(parseAsBlock ? { chi: [] } : {}),
2991
- });
2774
+ const result = matchGenericSyntax(arena, stream, options);
2775
+ atRuleTokens = parseTokens(arena, stream);
2776
+ atRuleState = result.success ? EnumAstNodeStatus.Unknown : EnumAstNodeStatus.Invalid;
2777
+ atRuleErrors = result.success ? [errors[errors.length - 1]] : [errors[errors.length - 1], ...result.errors];
2778
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimArray(arena, stream), options), !!parseAsBlock);
2992
2779
  }
2993
- else if (context.typ === EnumToken.AtRuleNodeType &&
2994
- "page" === context.nam &&
2995
- pageMarginBoxType.has(atRuleName.toLowerCase())) {
2780
+ else if (contextName === "page" && pageMarginBoxType.has(atRuleName.toLowerCase())) {
2996
2781
  if (parseAsBlock === false && syntaxRules.optionalBody === false) {
2997
2782
  errors.push({
2998
2783
  action: "drop",
2999
2784
  node: atRule,
3000
- location: options.source.getSourceLocation(atRule[LOCSTA]),
2785
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
2786
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
3001
2787
  message: parseAsBlock ? "at-rule block not supported" : "at-rule block is required",
3002
2788
  });
3003
- atRule[TOKENS] = parseTokens(stream);
3004
- atRule[STATE] = EnumAstNodeStatus.Invalid;
3005
- atRule[ERRORS] = [errors[errors.length - 1]];
3006
- // @ts-expect-error
3007
- return Object.assign(atRule, {
3008
- typ: EnumToken.AtRuleNodeType,
3009
- val: renderTokens(trimArray(stream), options),
3010
- ...(parseAsBlock ? { chi: [] } : {}),
3011
- });
2789
+ atRuleTokens = parseTokens(arena, stream);
2790
+ atRuleState = EnumAstNodeStatus.Invalid;
2791
+ atRuleErrors = [errors[errors.length - 1]];
2792
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimArray(arena, stream), options), !!parseAsBlock);
3012
2793
  }
3013
- const token = stream.find((t) => t.typ != EnumToken.WhitespaceTokenType && t.typ === EnumToken.CommentTokenType) ?? null;
2794
+ const token = stream.find((t) => arena.dataTypes[t] != EnumToken.WhitespaceTokenType && arena.dataTypes[t] === EnumToken.CommentTokenType) ?? null;
3014
2795
  if (token != null) {
3015
2796
  errors.push({
3016
2797
  action: "drop",
3017
2798
  node: token,
3018
- location: options.source.getSourceLocation(token[LOCSTA]),
2799
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(token))
2800
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(token))],
3019
2801
  message: `unexpected token`,
3020
2802
  });
3021
- atRule[TOKENS] = parseTokens(stream);
3022
- atRule[STATE] = EnumAstNodeStatus.Invalid;
3023
- atRule[ERRORS] = [errors[errors.length - 1]];
3024
- // @ts-expect-error
3025
- return Object.assign(atRule, {
3026
- typ: EnumToken.AtRuleNodeType,
3027
- val: renderTokens(trimArray(stream), options),
3028
- ...(parseAsBlock ? { chi: [] } : {}),
3029
- });
2803
+ atRuleTokens = parseTokens(arena, stream);
2804
+ atRuleState = EnumAstNodeStatus.Invalid;
2805
+ atRuleErrors = [errors[errors.length - 1]];
2806
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimArray(arena, stream), options), !!parseAsBlock);
3030
2807
  }
3031
2808
  }
3032
2809
  if (parseAsBlock === null) {
3033
2810
  parseAsBlock = blockAllowed;
3034
2811
  }
3035
- if (syntax != null && atRule.nam !== "layer" && parseAsBlock !== blockAllowed && !syntaxRules.optionalBody) {
2812
+ if (syntax != null && nam !== "layer" && parseAsBlock !== blockAllowed && !syntaxRules.optionalBody) {
3036
2813
  errors.push({
3037
2814
  action: "drop",
3038
2815
  node: atRule,
3039
- location: options.source.getSourceLocation(atRule[LOCSTA]),
2816
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
2817
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
3040
2818
  message: parseAsBlock ? "at-rule block not supported" : "at-rule block is required",
3041
2819
  });
3042
- atRule[TOKENS] = parseTokens(stream);
3043
- atRule[STATE] = EnumAstNodeStatus.Invalid;
3044
- atRule[ERRORS] = [errors[errors.length - 1]];
3045
- // @ts-expect-error
3046
- return Object.assign(atRule, {
3047
- typ: EnumToken.AtRuleNodeType,
3048
- val: renderTokens(trimArray(stream), options),
3049
- ...(parseAsBlock ? { chi: [] } : {}),
3050
- });
2820
+ atRuleTokens = parseTokens(arena, stream);
2821
+ atRuleState = EnumAstNodeStatus.Invalid;
2822
+ atRuleErrors = [errors[errors.length - 1]];
2823
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimArray(arena, stream), options), !!parseAsBlock);
3051
2824
  }
3052
2825
  switch (atRuleName) {
3053
2826
  case "charset": {
3054
2827
  let success = true;
2828
+ // exactly one space is expected. the whitespace text is not stored: check the source
3055
2829
  if (stream.length === 0 ||
3056
- stream[0].typ !== EnumToken.WhitespaceTokenType ||
3057
- stream[0].val !== " ") {
2830
+ arena.dataTypes[stream[0]] !== EnumToken.WhitespaceTokenType ||
2831
+ arena.getSpanEnd(stream[0]) - arena.getSpanStart(stream[0]) !== 1 ||
2832
+ // inlined: arena.getSource(stream[0]), options.sourcesMap?.get(...)?.getContent()
2833
+ options.sourcesMap?.get(arena.sources[arena.getSourceId(stream[0])] ?? 0)?.content.charCodeAt(arena.getSpanStart(stream[0])) !==
2834
+ 0x20) {
3058
2835
  success = false;
3059
2836
  errors.push({
3060
2837
  action: "drop",
3061
2838
  node: stream[0] ?? atRule,
3062
- location: options.source.getSourceLocation((stream[0] ?? atRule)[LOCSTA]),
2839
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[0] ?? atRule))
2840
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[0] ?? atRule))],
3063
2841
  message: "expecting <space>",
3064
2842
  });
3065
2843
  }
3066
- else if (stream[1].typ !== EnumToken.StringTokenType) {
2844
+ else if (arena.dataTypes[stream[1]] !== EnumToken.StringTokenType) {
3067
2845
  success = false;
3068
2846
  errors.push({
3069
2847
  action: "drop",
3070
2848
  node: stream[1] ?? atRule,
3071
- location: options.source.getSourceLocation((stream[1] ?? atRule)[LOCSTA]),
2849
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[1] ?? atRule))
2850
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[1] ?? atRule))],
3072
2851
  message: "expecting <string>",
3073
2852
  });
3074
2853
  }
3075
- if (success && stream[1].val.charCodeAt(0) !== 0x22) {
2854
+ if (success && arena.strings[arena.stringValue[stream[1]]].charCodeAt(0) !== 0x22) {
3076
2855
  success = false;
3077
2856
  errors.push({
3078
2857
  action: "drop",
3079
2858
  node: stream[1] ?? atRule,
3080
- location: options.source.getSourceLocation((stream[1] ?? atRule)[LOCSTA]),
2859
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[1] ?? atRule))
2860
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[1] ?? atRule))],
3081
2861
  message: "expecting double-quoted string",
3082
2862
  });
3083
2863
  }
3084
2864
  if (!success) {
3085
- atRule[TOKENS] = stream;
3086
- atRule[STATE] = EnumAstNodeStatus.Invalid;
3087
- atRule[ERRORS] = [errors[errors.length - 1]];
3088
- atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
3089
- // @ts-expect-error
3090
- return Object.assign(atRule, {
3091
- typ: EnumToken.AtRuleNodeType,
3092
- val: renderTokens(trimArray(stream), options),
3093
- });
2865
+ atRuleTokens = stream;
2866
+ atRuleState = EnumAstNodeStatus.Invalid;
2867
+ atRuleErrors = [errors[errors.length - 1]];
2868
+ atRuleEnd = stream.at(-1) ?? atRule;
2869
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimArray(arena, stream), options), false);
3094
2870
  }
3095
2871
  if (options.removeCharset) {
3096
2872
  return null;
3097
2873
  }
3098
- atRule[TOKENS] = stream;
3099
- atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3100
- atRule[ERRORS] = [];
3101
- atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
3102
- // @ts-expect-error
3103
- return Object.assign(atRule, {
3104
- typ: EnumToken.AtRuleNodeType,
3105
- val: renderTokens(trimArray(stream), options),
3106
- });
2874
+ atRuleTokens = stream;
2875
+ atRuleState = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
2876
+ atRuleErrors = [];
2877
+ atRuleEnd = stream.at(-1) ?? atRule;
2878
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimArray(arena, stream), options), false);
3107
2879
  }
3108
2880
  case "font-feature-values": {
3109
- const result = parseAtRuleFontFeatureValues(stream, atRule, options);
2881
+ const result = parseAtRuleFontFeatureValues(arena, stream, atRule, options);
3110
2882
  if (result.errors.length > 0) {
3111
2883
  for (const error of result.errors) {
3112
2884
  errors.push(error);
3113
2885
  }
3114
2886
  }
3115
- atRule[TOKENS] = stream;
3116
- atRule[STATE] = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3117
- atRule[ERRORS] = result.success ? [] : result.errors;
3118
- atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
3119
- // @ts-expect-error
3120
- return Object.assign(atRule, {
3121
- typ: EnumToken.AtRuleNodeType,
3122
- val: renderTokens(trimWhiteSpaceTokens(stream), options),
3123
- chi: [],
3124
- });
2887
+ atRuleTokens = stream;
2888
+ atRuleState = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
2889
+ atRuleErrors = result.success ? [] : result.errors;
2890
+ atRuleEnd = stream.at(-1) ?? atRule;
2891
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimWhiteSpaceTokens(arena, stream), options), true);
3125
2892
  }
3126
2893
  case "stylistic":
3127
2894
  case "historical-forms":
@@ -3129,60 +2896,52 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3129
2896
  case "swash":
3130
2897
  case "ornaments":
3131
2898
  case "annotation": {
3132
- let success = context.typ === EnumToken.AtRuleNodeType && "font-feature-values" === context.nam;
2899
+ let success = contextName === "font-feature-values";
3133
2900
  if (!success) {
3134
2901
  errors.push({
3135
2902
  action: "drop",
3136
2903
  node: atRule,
3137
- location: options.source.getSourceLocation(atRule[LOCSTA]),
3138
- message: `unexpected at-rule ${atRule.nam}`,
2904
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
2905
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
2906
+ message: `unexpected at-rule ${nam}`,
3139
2907
  });
3140
2908
  }
3141
2909
  if (success) {
3142
2910
  for (const token of stream) {
3143
- if (token.typ !== EnumToken.CommentTokenType && token.typ !== EnumToken.WhitespaceTokenType) {
2911
+ if (arena.dataTypes[token] !== EnumToken.CommentTokenType && arena.dataTypes[token] !== EnumToken.WhitespaceTokenType) {
3144
2912
  success = false;
3145
2913
  errors.push({
3146
2914
  action: "drop",
3147
2915
  node: token,
3148
- location: options.source.getSourceLocation(token[LOCSTA]),
2916
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(token))
2917
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(token))],
3149
2918
  message: `unexpected token`,
3150
2919
  });
3151
2920
  }
3152
2921
  }
3153
2922
  }
3154
- atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
3155
- atRule[TOKENS] = stream;
3156
- atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3157
- atRule[ERRORS] = [errors[errors.length - 1]];
3158
- // @ts-expect-error
3159
- return Object.assign(atRule, {
3160
- typ: EnumToken.AtRuleNodeType,
3161
- val: renderTokens(trimWhiteSpaceTokens(stream), options),
3162
- chi: [],
3163
- });
2923
+ atRuleEnd = stream.at(-1) ?? atRule;
2924
+ atRuleTokens = stream;
2925
+ atRuleState = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
2926
+ atRuleErrors = [errors[errors.length - 1]];
2927
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimWhiteSpaceTokens(arena, stream), options), true);
3164
2928
  }
3165
2929
  case "container": {
3166
- const result = parseAtRuleContainerQueryList(stream, atRule, options);
2930
+ const result = parseAtRuleContainerQueryList(arena, stream, atRule, options);
3167
2931
  if (result.errors.length > 0) {
3168
2932
  for (const error of result.errors) {
3169
2933
  errors.push(error);
3170
2934
  }
3171
2935
  }
3172
- atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
3173
- atRule[TOKENS] = stream;
3174
- atRule[STATE] = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3175
- atRule[ERRORS] = result.success ? [] : result.errors;
3176
- // @ts-expect-error
3177
- return Object.assign(atRule, {
3178
- typ: EnumToken.AtRuleNodeType,
3179
- val: renderTokens(trimWhiteSpaceTokens(stream), options),
3180
- chi: [],
3181
- });
2936
+ atRuleEnd = stream.at(-1) ?? atRule;
2937
+ atRuleTokens = stream;
2938
+ atRuleState = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
2939
+ atRuleErrors = result.success ? [] : result.errors;
2940
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimWhiteSpaceTokens(arena, stream), options), true);
3182
2941
  }
3183
2942
  case "custom-media": {
3184
- const tokens = trimArray(stream.slice(1));
3185
- const result = matchAllSyntaxes(syntax, createValidationContext(tokens), options);
2943
+ const tokens = trimArray(arena, stream.slice(1));
2944
+ const result = matchAllSyntaxes(arena, syntax, createValidationContext(arena, tokens), options);
3186
2945
  if (result.errors.length > 0) {
3187
2946
  for (const error of result.errors) {
3188
2947
  errors.push(error);
@@ -3190,46 +2949,38 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3190
2949
  }
3191
2950
  // @ts-expect-error
3192
2951
  options = { ...options, convertColor: false };
3193
- atRule[LOCEND] = (tokens.at(-1) ?? atRule)[LOCEND];
3194
- atRule[TOKENS] = tokens;
3195
- atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.ValidationFailed;
3196
- atRule[ERRORS] = result.success ? [] : result.errors;
3197
- // @ts-expect-error
3198
- return Object.assign(atRule, {
3199
- typ: EnumToken.AtRuleNodeType,
3200
- val: renderTokens(trimWhiteSpaceTokens(tokens), options),
3201
- });
2952
+ atRuleEnd = tokens.at(-1) ?? atRule;
2953
+ atRuleTokens = tokens;
2954
+ atRuleState = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.ValidationFailed;
2955
+ atRuleErrors = result.success ? [] : result.errors;
2956
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimWhiteSpaceTokens(arena, tokens), options), false);
3202
2957
  }
3203
2958
  case "keyframes": {
3204
- const tokens = trimArray(stream.slice(1));
3205
- const filtered = stream.filter((t) => t.typ !== EnumToken.WhitespaceTokenType && t.typ !== EnumToken.CommentTokenType);
2959
+ const tokens = trimArray(arena, stream.slice(1));
2960
+ const filtered = stream.filter((t) => arena.dataTypes[t] !== EnumToken.WhitespaceTokenType && arena.dataTypes[t] !== EnumToken.CommentTokenType);
3206
2961
  if (filtered.length != 1 ||
3207
- (filtered[0].typ !== EnumToken.IdenTokenType &&
3208
- filtered[0].typ !== EnumToken.StringTokenType &&
3209
- filtered[0].typ !== EnumToken.DashedIdenTokenType)) {
2962
+ (arena.dataTypes[filtered[0]] !== EnumToken.IdenTokenType &&
2963
+ arena.dataTypes[filtered[0]] !== EnumToken.StringTokenType &&
2964
+ arena.dataTypes[filtered[0]] !== EnumToken.DashedIdenTokenType)) {
3210
2965
  errors.push({
3211
2966
  action: "drop",
3212
2967
  node: atRule,
3213
- location: options.source.getSourceLocation(atRule[LOCSTA]),
2968
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
2969
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
3214
2970
  message: `expected <keyframe-name>`,
3215
2971
  });
3216
2972
  success = false;
3217
2973
  }
3218
2974
  // @ts-expect-error
3219
2975
  options = { ...options, convertColor: false };
3220
- atRule[LOCEND] = (tokens.at(-1) ?? atRule)[LOCEND];
3221
- atRule[TOKENS] = tokens;
3222
- atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3223
- atRule[ERRORS] = success ? [] : [errors[errors.length - 1]];
3224
- // @ts-expect-error
3225
- return Object.assign(atRule, {
3226
- typ: EnumToken.KeyframesAtRuleNodeType,
3227
- val: renderTokens(tokens, options),
3228
- chi: [],
3229
- });
2976
+ atRuleEnd = tokens.at(-1) ?? atRule;
2977
+ atRuleTokens = tokens;
2978
+ atRuleState = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
2979
+ atRuleErrors = success ? [] : [errors[errors.length - 1]];
2980
+ return createAtRuleNode(arena, atRule, EnumToken.KeyframesAtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, tokens, options), true);
3230
2981
  }
3231
2982
  case "namespace": {
3232
- const result = matchAllSyntaxes(syntax, createValidationContext(stream), options);
2983
+ const result = matchAllSyntaxes(arena, syntax, createValidationContext(arena, stream), options);
3233
2984
  if (!result.success) {
3234
2985
  for (const error of result.errors) {
3235
2986
  errors.push(error);
@@ -3244,13 +2995,13 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3244
2995
  let end = -1;
3245
2996
  let hasString = false;
3246
2997
  for (start = 0; start < stream.length; start++) {
3247
- if (stream[start].typ == EnumToken.UrlFunctionTokenDefType) {
2998
+ if (arena.dataTypes[stream[start]] == EnumToken.UrlFunctionTokenDefType) {
3248
2999
  start++;
3249
3000
  for (end = start; end < stream.length; end++) {
3250
- if (stream[end].typ == EnumToken.EndParensTokenType) {
3001
+ if (arena.dataTypes[stream[end]] == EnumToken.EndParensTokenType) {
3251
3002
  break;
3252
3003
  }
3253
- if (stream[end].typ === EnumToken.StringTokenType) {
3004
+ if (arena.dataTypes[stream[end]] === EnumToken.StringTokenType) {
3254
3005
  hasString = true;
3255
3006
  }
3256
3007
  }
@@ -3262,91 +3013,77 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3262
3013
  stream.splice(start - 1, end - start + 2, ...stream.slice(start, end));
3263
3014
  }
3264
3015
  }
3265
- atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
3266
- atRule[TOKENS] = stream.slice();
3267
- atRule[STATE] = valid ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3268
- atRule[ERRORS] = valid ? [] : result.errors;
3269
- // @ts-expect-error
3270
- return Object.assign(atRule, {
3271
- typ: EnumToken.AtRuleNodeType,
3272
- val: trimArray(stream).reduce((acc, t, index) => acc +
3273
- (t.typ === EnumToken.CommentTokenType ||
3274
- (t.typ === EnumToken.WhitespaceTokenType &&
3275
- stream[index + 1]?.typ === EnumToken.CommentTokenType &&
3276
- (stream.length < index + 3 || stream[index + 2]?.typ === EnumToken.WhitespaceTokenType))
3277
- ? ""
3278
- : renderValue(t, options)), ""),
3279
- ...(parseAsBlock ? { chi: [] } : {}),
3280
- });
3016
+ atRuleEnd = stream.at(-1) ?? atRule;
3017
+ atRuleTokens = stream.slice();
3018
+ atRuleState = valid ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3019
+ atRuleErrors = valid ? [] : result.errors;
3020
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, trimArray(arena, stream).reduce((acc, t, index) => acc +
3021
+ (arena.dataTypes[t] === EnumToken.CommentTokenType ||
3022
+ (arena.dataTypes[t] === EnumToken.WhitespaceTokenType &&
3023
+ arena.dataTypes[stream[index + 1]] === EnumToken.CommentTokenType &&
3024
+ (stream.length < index + 3 || arena.dataTypes[stream[index + 2]] === EnumToken.WhitespaceTokenType))
3025
+ ? ""
3026
+ : renderValue(arena, t, options)), ""), !!parseAsBlock);
3281
3027
  }
3282
3028
  case "import": {
3283
- const result = matchAtRuleImportSyntax(atRule, stream, context, options);
3029
+ const result = matchAtRuleImportSyntax(arena, atRule, stream, options);
3284
3030
  if (result.errors.length > 0) {
3285
3031
  for (const error of result.errors) {
3286
3032
  errors.push(error);
3287
3033
  }
3288
3034
  }
3289
3035
  else {
3290
- if (stream[0]?.typ == EnumToken.UrlFunctionTokenType &&
3291
- stream[0].chi.some((t) => t.typ == EnumToken.StringTokenType || t.typ == EnumToken.UrlTokenTokenType)) {
3292
- stream.splice(0, 1, ...stream[0].chi);
3293
- }
3294
- }
3295
- atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
3296
- atRule[TOKENS] = stream.slice();
3297
- atRule[STATE] = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3298
- atRule[ERRORS] = result.success ? [] : result.errors;
3299
- // @ts-expect-error
3300
- return Object.assign(atRule, {
3301
- typ: EnumToken.AtRuleNodeType,
3302
- val: stream.reduce((acc, t, index) => acc +
3303
- (t.typ === EnumToken.CommentTokenType ||
3304
- (t.typ === EnumToken.WhitespaceTokenType &&
3305
- stream[index + 1]?.typ === EnumToken.CommentTokenType &&
3306
- (stream.length < index + 3 || stream[index + 2]?.typ === EnumToken.WhitespaceTokenType))
3307
- ? ""
3308
- : renderValue(t, options)), ""),
3309
- });
3036
+ if (arena.dataTypes[stream[0]] == EnumToken.UrlFunctionTokenType &&
3037
+ arena.tokens(stream[0]).some((t) => arena.dataTypes[t] == EnumToken.StringTokenType || arena.dataTypes[t] == EnumToken.UrlTokenTokenType)) {
3038
+ stream.splice(0, 1, ...arena.tokens(stream[0]));
3039
+ }
3040
+ }
3041
+ atRuleEnd = stream.at(-1) ?? atRule;
3042
+ atRuleTokens = stream.slice();
3043
+ atRuleState = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3044
+ atRuleErrors = result.success ? [] : result.errors;
3045
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, stream.reduce((acc, t, index) => acc +
3046
+ (arena.dataTypes[t] === EnumToken.CommentTokenType ||
3047
+ (arena.dataTypes[t] === EnumToken.WhitespaceTokenType &&
3048
+ arena.dataTypes[stream[index + 1]] === EnumToken.CommentTokenType &&
3049
+ (stream.length < index + 3 || arena.dataTypes[stream[index + 2]] === EnumToken.WhitespaceTokenType))
3050
+ ? ""
3051
+ : renderValue(arena, t, options)), ""), false);
3310
3052
  }
3311
3053
  case "supports":
3312
3054
  case "when":
3313
3055
  case "else": {
3314
- trimWhiteSpaceTokens(stream);
3056
+ trimWhiteSpaceTokens(arena, stream);
3315
3057
  const result = atRuleName === "supports"
3316
- ? parseAtRuleSupportSyntax(stream, atRule, options)
3317
- : matchAtRuleWhenElseSyntax(stream, atRule, options);
3058
+ ? parseAtRuleSupportSyntax(arena, stream, atRule, options)
3059
+ : matchAtRuleWhenElseSyntax(arena, stream, atRule, options);
3318
3060
  if (result.errors.length > 0) {
3319
3061
  for (const error of result.errors) {
3320
3062
  errors.push(error);
3321
3063
  }
3322
3064
  }
3323
3065
  let success = result.success;
3324
- if (atRule.nam === "else") {
3325
- const siblings = context.chi;
3326
- let sibling = null;
3327
- let l = siblings.length;
3328
- while (l--) {
3329
- if (siblings[l].typ === EnumToken.WhitespaceTokenType ||
3330
- siblings[l].typ === EnumToken.CommentTokenType ||
3331
- siblings[l].typ === EnumToken.CDOCOMMTokenType) {
3066
+ if (nam === "else") {
3067
+ let sibling = arena.lastChild[context];
3068
+ while (sibling != 0) {
3069
+ if (arena.dataTypes[sibling] === EnumToken.WhitespaceTokenType ||
3070
+ arena.dataTypes[sibling] === EnumToken.CommentTokenType ||
3071
+ arena.dataTypes[sibling] === EnumToken.CDOCOMMTokenType) {
3072
+ sibling = arena.previousSibling[sibling];
3332
3073
  continue;
3333
3074
  }
3334
- sibling = siblings[l];
3335
3075
  break;
3336
3076
  }
3337
3077
  let missingWhen = false;
3338
3078
  let definedAfterLastElse = false;
3339
- if (sibling == null || sibling.typ !== EnumToken.AtRuleNodeType) {
3079
+ if (sibling == 0 || arena.dataTypes[sibling] !== EnumToken.AtRuleNodeType) {
3340
3080
  missingWhen = true;
3341
- // @ts-expect-error
3342
3081
  }
3343
- else if (sibling.nam !== "when") {
3344
- // @ts-expect-error
3345
- if (sibling.nam !== "else") {
3082
+ else if (arena.strings[arena.name[sibling]] !== "when") {
3083
+ if (arena.strings[arena.name[sibling]] !== "else") {
3346
3084
  missingWhen = true;
3347
- // @ts-expect-error
3348
3085
  }
3349
- else if (sibling.val === "") {
3086
+ else if (arena.stringValue[sibling] === 0) {
3350
3087
  definedAfterLastElse = true;
3351
3088
  }
3352
3089
  }
@@ -3355,7 +3092,8 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3355
3092
  errors.push({
3356
3093
  action: "drop",
3357
3094
  node: atRule,
3358
- location: options.source.getSourceLocation(atRule[LOCSTA]),
3095
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
3096
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
3359
3097
  message: "at-rule @when is required before @else block",
3360
3098
  });
3361
3099
  }
@@ -3364,155 +3102,143 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3364
3102
  errors.push({
3365
3103
  action: "drop",
3366
3104
  node: atRule,
3367
- location: options.source.getSourceLocation(atRule[LOCSTA]),
3105
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
3106
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
3368
3107
  message: "at-rule @else block is defined after last @else block",
3369
3108
  });
3370
3109
  }
3371
3110
  }
3372
3111
  // @ts-expect-error
3373
3112
  options = { ...options, minify: false, convertColor: false };
3374
- atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
3375
- atRule[TOKENS] = stream.slice();
3376
- atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3377
- atRule[ERRORS] = result.success ? [] : [errors[errors.length - 1]].concat(result.errors);
3378
- // @ts-expect-error
3379
- return Object.assign(atRule, {
3380
- typ: EnumToken.AtRuleNodeType,
3381
- val: renderTokens(stream, options),
3382
- chi: [],
3383
- });
3113
+ atRuleEnd = stream.at(-1) ?? atRule;
3114
+ atRuleTokens = stream.slice();
3115
+ atRuleState = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3116
+ atRuleErrors = result.success ? [] : [errors[errors.length - 1]].concat(result.errors);
3117
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, stream, options), true);
3384
3118
  }
3385
3119
  case "media": {
3386
3120
  options = { ...options, parseColor: false };
3387
- const result = parseMediaqueryList(stream, options);
3121
+ const result = parseMediaqueryList(arena, stream, options);
3388
3122
  if (result.errors.length > 0) {
3389
3123
  for (const error of result.errors) {
3390
3124
  errors.push(error);
3391
3125
  }
3392
3126
  }
3393
- atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
3394
- atRule[TOKENS] = stream.slice();
3395
- atRule[STATE] = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3396
- atRule[ERRORS] = result.success ? [] : result.errors;
3397
- // @ts-expect-error
3398
- return Object.assign(atRule, {
3399
- typ: EnumToken.AtRuleNodeType,
3400
- val: renderTokens(stream, options),
3401
- chi: [],
3402
- });
3127
+ atRuleEnd = stream.at(-1) ?? atRule;
3128
+ atRuleTokens = stream.slice();
3129
+ atRuleState = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3130
+ atRuleErrors = result.success ? [] : result.errors;
3131
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, stream, options), true);
3403
3132
  }
3404
3133
  case "scope": {
3405
- let context = createValidationContext(trimArray(stream));
3134
+ let context = createValidationContext(arena, trimArray(arena, stream));
3406
3135
  let success = true;
3407
3136
  // @ts-ignore
3408
- let range = context.peekRange((t) => t.typ === EnumToken.EndParensTokenType);
3409
- if (range[0]?.typ !== EnumToken.StartParensTokenType) {
3137
+ let range = context.peekRange((t) => arena.dataTypes[t] === EnumToken.EndParensTokenType);
3138
+ if (arena.dataTypes[range[0]] !== EnumToken.StartParensTokenType) {
3410
3139
  errors.push({
3411
3140
  action: "drop",
3412
3141
  node: range[0] ?? atRule,
3413
- location: options.source.getSourceLocation((range[0] ?? atRule)[LOCSTA]),
3142
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(range[0] ?? atRule))
3143
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(range[0] ?? atRule))],
3414
3144
  message: "expected '(' at start of @scope block",
3415
3145
  });
3416
3146
  success = false;
3147
+ // inlined: arena.typeOf(range.at(-1))
3417
3148
  }
3418
- else if (range.at(-1)?.typ !== EnumToken.EndParensTokenType) {
3149
+ else if (arena.dataTypes[range.at(-1)] !== EnumToken.EndParensTokenType) {
3419
3150
  errors.push({
3420
3151
  action: "drop",
3421
3152
  node: range.at(-1) ?? atRule,
3422
- location: options.source.getSourceLocation((range.at(-1) ?? atRule)[LOCSTA]),
3153
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(range.at(-1) ?? atRule))
3154
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(range.at(-1) ?? atRule))],
3423
3155
  message: "expected ')' at end of @scope block",
3424
3156
  });
3425
3157
  success = false;
3426
3158
  }
3427
3159
  else {
3428
3160
  const srange = range.slice(1, -1);
3429
- const result = matchSelectorSyntax(srange, errors, options, true);
3161
+ const result = matchSelectorSyntax(arena, srange, errors, options, true);
3430
3162
  if (!result.success) {
3431
3163
  success = false;
3432
3164
  }
3433
3165
  else {
3434
- stream.splice(stream.indexOf(range[0]) + 1, range.length - 2, ...trimWhiteSpaceTokens(srange));
3166
+ stream.splice(stream.indexOf(range[0]) + 1, range.length - 2, ...trimWhiteSpaceTokens(arena, srange));
3435
3167
  }
3436
3168
  }
3437
3169
  let index = stream.indexOf(range.at(-1));
3438
3170
  if (stream.length > index + 1) {
3439
- while (stream[++index]?.typ === EnumToken.WhitespaceTokenType ||
3440
- stream[index]?.typ === EnumToken.CommentTokenType)
3171
+ while (arena.dataTypes[stream[++index]] === EnumToken.WhitespaceTokenType ||
3172
+ arena.dataTypes[stream[index]] === EnumToken.CommentTokenType)
3441
3173
  ;
3442
3174
  if (index < stream.length) {
3443
- if (stream[index].typ !== EnumToken.IdenTokenType ||
3444
- "to" !== stream[index].val.toLowerCase()) {
3175
+ if (arena.dataTypes[stream[index]] !== EnumToken.IdenTokenType ||
3176
+ "to" !== arena.strings[arena.stringValue[stream[index]]].toLowerCase()) {
3445
3177
  errors.push({
3446
3178
  action: "drop",
3447
3179
  node: stream[index],
3448
- location: options.source.getSourceLocation(stream[index]?.[LOCSTA]),
3180
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[index]))
3181
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[index]))],
3449
3182
  message: "expected 'to' at end of @scope block",
3450
3183
  });
3451
3184
  success = false;
3452
3185
  }
3453
3186
  else {
3454
- while (stream[++index]?.typ === EnumToken.WhitespaceTokenType ||
3455
- stream[index]?.typ === EnumToken.CommentTokenType)
3187
+ while (arena.dataTypes[stream[++index]] === EnumToken.WhitespaceTokenType ||
3188
+ arena.dataTypes[stream[index]] === EnumToken.CommentTokenType)
3456
3189
  ;
3457
- if (stream[index].typ !== EnumToken.StartParensTokenType) {
3190
+ if (arena.dataTypes[stream[index]] !== EnumToken.StartParensTokenType) {
3458
3191
  errors.push({
3459
3192
  action: "drop",
3460
3193
  node: stream[index],
3461
- location: options.source.getSourceLocation(stream[index]?.[LOCSTA]),
3194
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[index]))
3195
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[index]))],
3462
3196
  message: "expected 'to' at end of @scope block",
3463
3197
  });
3464
3198
  success = false;
3465
3199
  }
3466
3200
  else {
3467
- context = createValidationContext(stream.slice(index));
3201
+ context = createValidationContext(arena, stream.slice(index));
3468
3202
  // @ts-ignore
3469
- range = context.peekRange((t) => t.typ === EnumToken.EndParensTokenType);
3470
- if (range.at(-1)?.typ !== EnumToken.EndParensTokenType) {
3203
+ range = context.peekRange((t) => arena.dataTypes[t] === EnumToken.EndParensTokenType);
3204
+ // inlined: arena.typeOf(range.at(-1))
3205
+ if (arena.dataTypes[range.at(-1)] !== EnumToken.EndParensTokenType) {
3471
3206
  errors.push({
3472
3207
  action: "drop",
3473
3208
  node: range.at(-1) ?? atRule,
3474
- location: options.source.getSourceLocation((range.at(-1) ?? atRule)[LOCSTA]),
3209
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(range.at(-1) ?? atRule))
3210
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(range.at(-1) ?? atRule))],
3475
3211
  message: "expected ')' at end of @scope block",
3476
3212
  });
3477
3213
  success = false;
3478
3214
  }
3479
3215
  else {
3480
3216
  const srange = range.slice(1, -1);
3481
- const result = matchSelectorSyntax(srange, errors, options, true);
3217
+ const result = matchSelectorSyntax(arena, srange, errors, options, true);
3482
3218
  if (!result.success) {
3483
3219
  success = false;
3484
3220
  }
3485
3221
  else {
3486
- stream.splice(stream.indexOf(range[0]) + 1, range.length - 2, ...trimWhiteSpaceTokens(srange));
3222
+ stream.splice(stream.indexOf(range[0]) + 1, range.length - 2, ...trimWhiteSpaceTokens(arena, srange));
3487
3223
  }
3488
3224
  }
3489
3225
  }
3490
3226
  }
3491
3227
  }
3492
3228
  }
3493
- atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
3494
- atRule[TOKENS] = stream.slice();
3495
- atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3496
- atRule[ERRORS] = success ? [] : [errors[errors.length - 1]];
3497
- // @ts-expect-error
3498
- return Object.assign(atRule, {
3499
- typ: EnumToken.AtRuleNodeType,
3500
- val: renderTokens(stream, options),
3501
- chi: [],
3502
- });
3229
+ atRuleEnd = stream.at(-1) ?? atRule;
3230
+ atRuleTokens = stream.slice();
3231
+ atRuleState = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3232
+ atRuleErrors = success ? [] : [errors[errors.length - 1]];
3233
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, stream, options), true);
3503
3234
  }
3504
3235
  case "page": {
3505
- trimArray(stream);
3506
- atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
3507
- atRule[TOKENS] = stream.slice();
3508
- atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3509
- atRule[ERRORS] = success ? [] : [errors[errors.length - 1]];
3510
- // @ts-expect-error
3511
- return Object.assign(atRule, {
3512
- typ: EnumToken.AtRuleNodeType,
3513
- val: renderTokens(stream, options),
3514
- chi: [],
3515
- });
3236
+ trimArray(arena, stream);
3237
+ atRuleEnd = stream.at(-1) ?? atRule;
3238
+ atRuleTokens = stream.slice();
3239
+ atRuleState = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3240
+ atRuleErrors = success ? [] : [errors[errors.length - 1]];
3241
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, stream, options), true);
3516
3242
  }
3517
3243
  case "top-left-corner":
3518
3244
  case "top-left":
@@ -3529,66 +3255,59 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3529
3255
  case "right-top":
3530
3256
  case "right-middle":
3531
3257
  case "right-bottom": {
3532
- if (context.typ !== EnumToken.AtRuleNodeType || context.nam !== "page") {
3258
+ if (contextName !== "page") {
3533
3259
  success = false;
3534
3260
  errors.push({
3535
3261
  action: "drop",
3536
3262
  node: atRule,
3537
- location: options.source.getSourceLocation(atRule[LOCSTA]),
3263
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
3264
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
3538
3265
  message: "node is allowed only in @page rule",
3539
3266
  });
3540
3267
  }
3541
3268
  else {
3542
- trimArray(stream);
3269
+ trimArray(arena, stream);
3543
3270
  for (let i = 0; i < stream.length; i++) {
3544
- if (stream[i].typ !== EnumToken.WhitespaceTokenType &&
3545
- stream[i].typ !== EnumToken.CommentTokenType) {
3271
+ if (arena.dataTypes[stream[i]] !== EnumToken.WhitespaceTokenType &&
3272
+ arena.dataTypes[stream[i]] !== EnumToken.CommentTokenType) {
3546
3273
  success = false;
3547
3274
  errors.push({
3548
3275
  action: "drop",
3549
3276
  node: stream[i],
3550
- location: options.source.getSourceLocation(stream[i][LOCSTA]),
3277
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
3278
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
3551
3279
  message: "expected whitespace or comment",
3552
3280
  });
3553
3281
  break;
3554
3282
  }
3555
3283
  }
3556
3284
  }
3557
- atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
3558
- atRule[TOKENS] = stream.slice();
3559
- atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3560
- atRule[ERRORS] = success ? [] : [errors[errors.length - 1]];
3561
- // @ts-expect-error
3562
- return Object.assign(atRule, {
3563
- typ: EnumToken.AtRuleNodeType,
3564
- val: renderTokens(stream, options),
3565
- chi: [],
3566
- });
3285
+ atRuleEnd = stream.at(-1) ?? atRule;
3286
+ atRuleTokens = stream.slice();
3287
+ atRuleState = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3288
+ atRuleErrors = success ? [] : [errors[errors.length - 1]];
3289
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, stream, options), true);
3567
3290
  }
3568
3291
  case "value": {
3569
3292
  let index = 0;
3570
3293
  let isVarDeclaration = false;
3571
3294
  for (; index < stream.length; index++) {
3572
- if (stream[index].typ == EnumToken.PseudoClassTokenType) {
3573
- Object.assign(stream[index], {
3574
- typ: EnumToken.IdenTokenType,
3575
- val: stream[index].val.slice(1),
3576
- });
3577
- stream.splice(index, 0, {
3578
- typ: EnumToken.ColonTokenType,
3579
- [LOCSRCID]: stream[index][LOCSRCID],
3580
- [LOCSTA]: stream[index][LOCSTA],
3581
- [LOCEND]: stream[index][LOCEND],
3582
- });
3295
+ if (arena.dataTypes[stream[index]] == EnumToken.PseudoClassTokenType) {
3296
+ const colon = arena.createToken(EnumToken.ColonTokenType);
3297
+ arena.copyLocation(colon, stream[index]);
3298
+ // inlined: arena.setStr(stream[index], arena.strings[arena.stringValue[stream[index]]].slice(1))
3299
+ arena.stringValue[stream[index]] = arena.internString(arena.strings[arena.stringValue[stream[index]]].slice(1));
3300
+ arena.dataTypes[stream[index]] = EnumToken.IdenTokenType;
3301
+ stream.splice(index, 0, colon);
3583
3302
  isVarDeclaration = true;
3584
3303
  break;
3585
3304
  }
3586
- else if (stream[index].typ == EnumToken.ColonTokenType) {
3305
+ else if (arena.dataTypes[stream[index]] == EnumToken.ColonTokenType) {
3587
3306
  isVarDeclaration = true;
3588
3307
  break;
3589
3308
  }
3590
- else if (stream[index].typ == EnumToken.IdenTokenType &&
3591
- equalsIgnoreCase("from", stream[index].val)) {
3309
+ else if (arena.dataTypes[stream[index]] == EnumToken.IdenTokenType &&
3310
+ equalsIgnoreCase("from", arena.strings[arena.stringValue[stream[index]]])) {
3592
3311
  break;
3593
3312
  }
3594
3313
  }
@@ -3596,63 +3315,40 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3596
3315
  // @value <ident>: <string>; // import from file as alias
3597
3316
  // @value id : <declaration-value>; // variable declaration
3598
3317
  // @value <ident># from <ident>; // import variables from alias
3599
- let result = matchAllSyntaxes(syntaxRules?.getPreludeRules()?.slice?.(1), createValidationContext(stream), options);
3600
- atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3601
- atRule[ERRORS] = success ? [] : [errors[errors.length - 1]];
3318
+ let result = matchAllSyntaxes(arena, syntaxRules?.getPreludeRules()?.slice?.(1), createValidationContext(arena, stream), options);
3319
+ atRuleState = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3320
+ atRuleErrors = success ? [] : [errors[errors.length - 1]];
3321
+ atRuleEnd = stream.at(-1) ?? atRule;
3602
3322
  if (!result.success) {
3603
3323
  for (const error of result.errors) {
3604
3324
  errors.push(error);
3605
3325
  }
3606
- return {
3607
- typ: EnumToken.AtRuleNodeType,
3608
- val: renderTokens(stream, options),
3609
- [LOCSRCID]: atRule[LOCSRCID],
3610
- [LOCSTA]: atRule[LOCSTA],
3611
- [LOCEND]: stream.at(-1)?.[LOCEND] ?? atRule[LOCEND],
3612
- [TOKENS]: stream,
3613
- [STATE]: EnumAstNodeStatus.Invalid,
3614
- [ERRORS]: result.errors,
3615
- };
3326
+ atRuleTokens = stream;
3327
+ atRuleState = EnumAstNodeStatus.Invalid;
3328
+ atRuleErrors = result.errors;
3329
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, stream, options), false);
3616
3330
  }
3617
3331
  if (isVarDeclaration) {
3618
- const nam = stream.find((t) => t.typ == EnumToken.IdenTokenType);
3619
- const value = trimArray(stream.slice(index + 1).filter((t) => t.typ != EnumToken.CommentTokenType));
3620
- if (value.length == 1 && value[0].typ == EnumToken.StringTokenType) {
3621
- // import from file as alias
3622
- return {
3623
- typ: EnumToken.CssVariableImportTokenType,
3624
- nam: nam.val,
3625
- val: value,
3626
- [LOCSRCID]: atRule[LOCSRCID],
3627
- [LOCSTA]: atRule[LOCSTA],
3628
- [LOCEND]: stream.at(-1)?.[LOCEND] ?? atRule[LOCEND],
3629
- [TOKENS]: stream,
3630
- [STATE]: EnumAstNodeStatus.Validated,
3631
- [ERRORS]: [],
3632
- };
3633
- }
3634
- // import variables from alias
3635
- return {
3636
- typ: EnumToken.CssVariableTokenType,
3637
- nam: nam.val,
3638
- val: value,
3639
- [LOCSRCID]: atRule[LOCSRCID],
3640
- [LOCSTA]: atRule[LOCSTA],
3641
- [LOCEND]: stream.at(-1)?.[LOCEND] ?? atRule[LOCEND],
3642
- [TOKENS]: stream,
3643
- [STATE]: EnumAstNodeStatus.Validated,
3644
- [ERRORS]: [],
3645
- };
3646
- }
3647
- atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
3648
- atRule[STATE] = EnumAstNodeStatus.Validated;
3649
- atRule[ERRORS] = [];
3650
- // @ts-expect-error
3651
- return Object.assign(atRule, {
3652
- typ: EnumToken.CssVariableDeclarationMapTokenType,
3653
- vars: trimArray(stream.slice(0, index)),
3654
- from: stream.slice(index + 1),
3655
- });
3332
+ const nam = stream.find((t) => arena.dataTypes[t] == EnumToken.IdenTokenType);
3333
+ const value = trimArray(arena, stream.slice(index + 1).filter((t) => arena.dataTypes[t] != EnumToken.CommentTokenType));
3334
+ // import from file as alias: @value <ident>: <string>;
3335
+ // variable declaration: @value <ident>: <declaration-value>;
3336
+ // the variable value is stored as the node values
3337
+ const node = arena.createDeclaration(value.length == 1 && arena.dataTypes[value[0]] == EnumToken.StringTokenType
3338
+ ? EnumToken.CssVariableImportTokenType
3339
+ : EnumToken.CssVariableTokenType, arena.strings[arena.stringValue[nam]], value, EnumAstNodeStatus.Validated, []);
3340
+ arena.spanLocation(node, atRule, atRuleEnd);
3341
+ return node;
3342
+ }
3343
+ atRuleState = EnumAstNodeStatus.Validated;
3344
+ atRuleErrors = [];
3345
+ // import variables from alias: @value <ident># from <ident>;
3346
+ // the imported variable names are stored as the node values, the alias tokens as the `l` field
3347
+ const node = arena.createDeclaration(EnumToken.CssVariableDeclarationMapTokenType, nam, trimArray(arena, stream.slice(0, index)), EnumAstNodeStatus.Validated, []);
3348
+ // inlined: arena.setField(node, TokenField.L, ...stream.slice(index + 1))
3349
+ arena.setTokenList(arena.fieldList(node, TokenField.L), stream.slice(index + 1));
3350
+ arena.spanLocation(node, atRule, atRuleEnd);
3351
+ return node;
3656
3352
  }
3657
3353
  default: {
3658
3354
  options = { ...options, parseColor: false };
@@ -3662,7 +3358,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3662
3358
  // check commas ',' or ',,'
3663
3359
  // check colon ':'
3664
3360
  // check 'or' and 'and'
3665
- result = matchGenericSyntax(stream, options);
3361
+ result = matchGenericSyntax(arena, stream, options);
3666
3362
  if (result.errors.length > 0) {
3667
3363
  for (const error of result.errors) {
3668
3364
  errors.push(error);
@@ -3670,7 +3366,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3670
3366
  }
3671
3367
  }
3672
3368
  else {
3673
- result = matchAtRuleSyntax(atRule, stream, options);
3369
+ result = matchAtRuleSyntax(arena, atRule, stream, options);
3674
3370
  if (result.errors.length > 0) {
3675
3371
  for (const error of result.errors) {
3676
3372
  errors.push(error);
@@ -3680,17 +3376,17 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3680
3376
  let i = 0;
3681
3377
  const stack = [];
3682
3378
  for (; i < stream.length; i++) {
3683
- if (stream[i].typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stream[i].typ)) {
3379
+ if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType || tokensfuncDefMap.has(arena.dataTypes[stream[i]])) {
3684
3380
  stack.push(stream[i]);
3685
3381
  continue;
3686
3382
  }
3687
- if (stream[i].typ === EnumToken.EndParensTokenType && stack.length > 0) {
3383
+ if (arena.dataTypes[stream[i]] === EnumToken.EndParensTokenType && stack.length > 0) {
3688
3384
  const index = stream.indexOf(stack[stack.length - 1]);
3689
- stream[index][LOCEND] = stream[i][LOCEND];
3690
- Object.assign(stream[index], {
3691
- typ: tokensfuncDefMap.get(stream[index].typ),
3692
- chi: trimWhiteSpaceTokens(stream.splice(index + 1, i - index - 1)),
3693
- });
3385
+ // inlined: arena.setSpanEnd(stream[index], arena.getSpanEnd(stream[i]))
3386
+ arena.setSpan(stream[index], arena.getSourceId(stream[index]), arena.getSpanStart(stream[index]), arena.getSpanEnd(stream[i]));
3387
+ arena.dataTypes[stream[index]] = tokensfuncDefMap.get(arena.dataTypes[stream[index]]);
3388
+ // inlined: arena.setTokens(stream[index], ...trimWhiteSpaceTokens(arena, stream.splice(index + 1, i - index - 1)))
3389
+ arena.setTokenList(stream[index], trimWhiteSpaceTokens(arena, stream.splice(index + 1, i - index - 1)));
3694
3390
  i = index;
3695
3391
  stream.splice(index + 1, 1);
3696
3392
  stack.pop();
@@ -3698,86 +3394,108 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3698
3394
  }
3699
3395
  }
3700
3396
  }
3701
- atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
3702
- atRule[TOKENS] = stream.slice();
3703
- atRule[STATE] = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3704
- atRule[ERRORS] = result.errors;
3705
- switch (atRule.nam) {
3397
+ atRuleEnd = stream.at(-1) ?? atRule;
3398
+ atRuleTokens = stream.slice();
3399
+ atRuleState = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
3400
+ atRuleErrors = result.errors;
3401
+ switch (nam) {
3706
3402
  case "mixin":
3707
- if (context != null && context.typ != EnumToken.StyleSheetNodeType) {
3708
- atRule[STATE] |= EnumAstNodeStatus.Disallowed;
3709
- atRule[ERRORS].push({
3403
+ if (contextType != EnumToken.StyleSheetNodeType) {
3404
+ atRuleState |= EnumAstNodeStatus.Disallowed;
3405
+ atRuleErrors.push({
3406
+ action: "drop",
3710
3407
  message: "mixin rule cannot be a nested group rule or rule style body",
3711
- location: atRule[LOCSTA],
3408
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
3409
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
3712
3410
  });
3713
3411
  }
3714
3412
  break;
3715
3413
  case "contents":
3716
3414
  let parent = context;
3717
- let valid = parent == null;
3718
- while (parent != null) {
3719
- if (parent.typ == EnumToken.AtRuleNodeType && "mixin" == parent.nam) {
3415
+ let valid = false;
3416
+ while (parent != 0) {
3417
+ if (arena.dataTypes[parent] == EnumToken.AtRuleNodeType &&
3418
+ "mixin" == arena.strings[arena.name[parent]]) {
3720
3419
  valid = true;
3721
3420
  break;
3722
3421
  }
3723
- parent = parent[PARENT];
3422
+ parent = arena.parents[parent];
3724
3423
  }
3725
3424
  if (!valid) {
3726
- atRule[STATE] |= EnumAstNodeStatus.Disallowed;
3727
- atRule[ERRORS].push({
3425
+ atRuleState |= EnumAstNodeStatus.Disallowed;
3426
+ atRuleErrors.push({
3427
+ action: "drop",
3728
3428
  message: "contents rule cannot be used outside mixin rule",
3729
- location: atRule[LOCSTA],
3429
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
3430
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
3730
3431
  });
3731
3432
  }
3732
3433
  break;
3733
3434
  case "layer":
3734
- if (context != null && context.typ != EnumToken.StyleSheetNodeType) {
3435
+ if (contextType != EnumToken.StyleSheetNodeType) {
3735
3436
  let parent = context;
3736
- while (parent != null) {
3737
- if (parent.typ == EnumToken.AtRuleNodeType && parent.nam == "mixin") {
3738
- atRule[STATE] |= EnumAstNodeStatus.Disallowed;
3739
- atRule[ERRORS].push({
3437
+ while (parent != 0) {
3438
+ if (arena.dataTypes[parent] == EnumToken.AtRuleNodeType &&
3439
+ arena.strings[arena.name[parent]] == "mixin") {
3440
+ atRuleState |= EnumAstNodeStatus.Disallowed;
3441
+ atRuleErrors.push({
3442
+ action: "drop",
3740
3443
  message: "at-rule layeris not allowed inside mixin rule",
3741
- location: atRule[LOCSTA],
3444
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(atRule))
3445
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(atRule))],
3742
3446
  });
3743
3447
  break;
3744
3448
  }
3745
- parent = parent[PARENT];
3449
+ parent = arena.parents[parent];
3746
3450
  }
3747
3451
  }
3748
3452
  }
3749
- // @ts-expect-error
3750
- return Object.assign(atRule, {
3751
- typ: EnumToken.AtRuleNodeType,
3752
- val: renderTokens(trimWhiteSpaceTokens(stream), options),
3753
- ...(parseAsBlock ? { chi: [] } : {}),
3754
- });
3453
+ return createAtRuleNode(arena, atRule, EnumToken.AtRuleNodeType, atRuleTokens, atRuleState, atRuleErrors, atRuleEnd, renderTokens(arena, trimWhiteSpaceTokens(arena, stream), options), !!parseAsBlock);
3755
3454
  }
3756
3455
  }
3757
3456
  }
3758
3457
  /**
3759
- * Parse a string as an array of declaration nodes
3458
+ * Parse a string as an array of declaration nodes.
3459
+ * The nodes are allocated in the given arena, or in a new arena when none is provided
3760
3460
  * @param declaration
3461
+ * @param arena
3761
3462
  *
3762
3463
  * Example:
3763
3464
  * ````ts
3764
3465
  *
3765
- * const declarations = await parseDeclarations('color: red; background: blue');
3766
- * console.log(declarations);
3466
+ * const {arena, nodes} = parseDeclarations('color: red; background: blue');
3467
+ * console.log(nodes.map((node) => arena.toObject(node)));
3767
3468
  * ```
3768
3469
  */
3769
- function parseDeclarations(declaration) {
3470
+ function parseDeclarations(declaration, arena) {
3770
3471
  const stream = `.x{${declaration}}`;
3472
+ const source = new SourceFile(stream, [], "");
3771
3473
  const result = doParseSync(new Tokenizer({
3772
3474
  stream,
3773
3475
  offset: 0,
3774
3476
  position: 0,
3775
- source: new SourceFile(stream, [], ""),
3477
+ source,
3776
3478
  currentPosition: 0,
3777
- }), { setParent: false, minify: false, validation: false });
3778
- // .then((result) => {
3779
- return result.ast.chi[0].chi.filter((t) => t.typ == EnumToken.DeclarationNodeType || t.typ == EnumToken.CommentNodeType);
3780
- // });
3479
+ }), {
3480
+ setParent: false,
3481
+ minify: false,
3482
+ validation: false,
3483
+ arena,
3484
+ source,
3485
+ sourcesMap: new Map([[source.id, source]]),
3486
+ });
3487
+ const rule = result.arena.firstChild[result.ast];
3488
+ const nodes = [];
3489
+ for (let node = rule == 0 ? 0 : result.arena.firstChild[rule]; node != 0;) {
3490
+ const next = result.arena.nextSibling[node];
3491
+ if (result.arena.dataTypes[node] == EnumToken.DeclarationNodeType ||
3492
+ result.arena.dataTypes[node] == EnumToken.CommentNodeType) {
3493
+ result.arena.detach(node);
3494
+ nodes.push(node);
3495
+ }
3496
+ node = next;
3497
+ }
3498
+ return { arena: result.arena, nodes };
3781
3499
  }
3782
3500
  /**
3783
3501
  * Parse css string and return an array of tokens
@@ -3794,14 +3512,15 @@ function parseDeclarations(declaration) {
3794
3512
  *
3795
3513
  * import {parseString} from '@tbela99/css-parser';
3796
3514
  *
3797
- * let tokens = parseString('body { color: red; }');
3798
- * console.log(tokens);
3515
+ * const arena = new AstArena();
3516
+ * let tokens = parseString(arena, 'body { color: red; }');
3517
+ * console.log(tokens.map((token) => arena.toObject(token)));
3799
3518
  *
3800
- * tokens = parseString('#c322c980');
3801
- * console.log(tokens);
3519
+ * tokens = parseString(arena, '#c322c980');
3520
+ * console.log(tokens.map((token) => arena.toObject(token)));
3802
3521
  * ```
3803
3522
  */
3804
- function parseString(src, options = { parseColor: true }, errors) {
3523
+ function parseString(arena, src, options = { parseColor: true }, errors) {
3805
3524
  const tokenizer = new Tokenizer({
3806
3525
  stream: src,
3807
3526
  buffer: "",
@@ -3813,42 +3532,20 @@ function parseString(src, options = { parseColor: true }, errors) {
3813
3532
  currentPosition: 0,
3814
3533
  });
3815
3534
  const mapped = [];
3816
- let token;
3817
- while (!tokenizer.done()) {
3535
+ // inlined: tokenizer.done()
3536
+ while (!(tokenizer.typ === EnumToken.EOF)) {
3818
3537
  tokenizer.next();
3819
- if (tokenizer.unit != null) {
3820
- token = {
3821
- typ: tokenizer.typ,
3822
- val: tokenizer.val,
3823
- unit: tokenizer.unit,
3824
- };
3825
- }
3826
- else if (tokenizer.val === null) {
3827
- token = {
3828
- typ: tokenizer.typ,
3829
- };
3830
- }
3831
- else if (tokenizer.kin != null) {
3832
- token = {
3833
- typ: tokenizer.typ,
3834
- val: tokenizer.val,
3835
- kin: tokenizer.kin,
3836
- };
3538
+ // tokens parsed from a string have no location
3539
+ mapped.push(allocateToken(arena, tokenizer, -1));
3540
+ }
3541
+ const result = parseTokens(arena, mapped, options, errors);
3542
+ // remove the EOF token and the whitespace before it: their ids are recycled
3543
+ const removed = result.splice(result.length - (arena.dataTypes[result[result.length - 2]] === EnumToken.WhitespaceTokenType ? 2 : 1), 2);
3544
+ for (let i = 0; i < removed.length; i++) {
3545
+ if (arena.parents[removed[i]] == 0 && arena.firstToken[removed[i]] == 0 && arena.firstChild[removed[i]] == 0) {
3546
+ arena.recycle(removed[i]);
3837
3547
  }
3838
- else {
3839
- token = {
3840
- typ: tokenizer.typ,
3841
- val: tokenizer.val,
3842
- };
3843
- }
3844
- token[LOCSRCID] = tokenizer.source.id;
3845
- token[LOCEND] = tokenizer.end;
3846
- token[LOCSTA] = tokenizer.sta;
3847
- mapped.push(token);
3848
- }
3849
- const result = parseTokens(mapped, options, errors);
3850
- // remove EOF token
3851
- result.splice(result.length - (result[result.length - 2]?.typ === EnumToken.WhitespaceTokenType ? 2 : 1), 2);
3548
+ }
3852
3549
  return result;
3853
3550
  }
3854
3551
  /**
@@ -3865,97 +3562,107 @@ function parseString(src, options = { parseColor: true }, errors) {
3865
3562
  *
3866
3563
  * import {parseString, parseTokens} from '@tbela99/css-parser';
3867
3564
  *
3868
- * let tokens = parseString('body { color: red; }');
3869
- * console.log(parseTokens(tokens));
3565
+ * let tokens = parseString(arena, 'body { color: red; }');
3566
+ * console.log(parseTokens(arena, tokens));
3870
3567
  *
3871
- * tokens = parseString('#c322c980');
3872
- * console.log(parseTokens(tokens));
3568
+ * tokens = parseString(arena, '#c322c980');
3569
+ * console.log(parseTokens(arena, tokens));
3873
3570
  * ```
3874
3571
  *
3875
3572
  * @private
3876
3573
  */
3877
- function parseTokens(tokens, options, errors) {
3574
+ function parseTokens(arena, tokens, options, errors) {
3878
3575
  const stack = [];
3879
3576
  let i = 0;
3880
3577
  let index;
3881
3578
  let t;
3579
+ let typ;
3882
3580
  options ??= { parseColor: true };
3883
3581
  for (; i < tokens.length; i++) {
3884
3582
  t = tokens[i];
3885
- if (t.typ === EnumToken.FunctionTokenDefType) {
3886
- if (tokens[i - 1]?.typ === EnumToken.ColonTokenType ||
3887
- tokens[i - 1]?.typ === EnumToken.DoubleColonTokenType) {
3888
- t = Object.assign(tokens[i - 1], {
3889
- typ: EnumToken.PseudoClassFunctionTokenDefType,
3890
- val: (tokens[i - 1].typ === EnumToken.ColonTokenType ? ":" : "::") +
3891
- tokens[i].val,
3892
- });
3893
- t[LOCEND] = tokens[i][LOCEND];
3583
+ typ = arena.dataTypes[t];
3584
+ if (typ === EnumToken.FunctionTokenDefType) {
3585
+ if (i > 0 &&
3586
+ (arena.dataTypes[tokens[i - 1]] === EnumToken.ColonTokenType ||
3587
+ arena.dataTypes[tokens[i - 1]] === EnumToken.DoubleColonTokenType)) {
3588
+ // merge the colon and the function name into a pseudo class function
3589
+ const previous = tokens[i - 1];
3590
+ // inlined: arena.setStr(previous, (arena.dataTypes[previous] === EnumToken.ColonTokenType ? ":" : "::") + arena.strings[arena.stringValue[t]])
3591
+ arena.stringValue[previous] = arena.internString(((arena.dataTypes[previous] === EnumToken.ColonTokenType ? ":" : "::") +
3592
+ arena.strings[arena.stringValue[t]]));
3593
+ arena.dataTypes[previous] = EnumToken.PseudoClassFunctionTokenDefType;
3594
+ // inlined: arena.setSpanEnd(previous, arena.getSpanEnd(t))
3595
+ arena.setSpan(previous, arena.getSourceId(previous), arena.getSpanStart(previous), arena.getSpanEnd(t));
3894
3596
  tokens.splice(i--, 1);
3597
+ t = previous;
3598
+ typ = EnumToken.PseudoClassFunctionTokenDefType;
3895
3599
  }
3896
3600
  }
3897
- if (t.typ === EnumToken.AttrStartTokenType ||
3898
- t.typ === EnumToken.StartParensTokenType ||
3899
- tokensfuncDefMap.has(t.typ)) {
3601
+ if (typ === EnumToken.AttrStartTokenType ||
3602
+ typ === EnumToken.StartParensTokenType ||
3603
+ tokensfuncDefMap.has(typ)) {
3900
3604
  stack.push(t);
3901
3605
  continue;
3902
3606
  }
3903
- if (t.typ === EnumToken.EndParensTokenType) {
3607
+ if (typ === EnumToken.EndParensTokenType) {
3904
3608
  if (stack.length === 0 ||
3905
- (stack.at(-1)?.typ !== EnumToken.StartParensTokenType && !tokensfuncDefMap.has(stack.at(-1)?.typ))) {
3609
+ (arena.dataTypes[stack[stack.length - 1]] !== EnumToken.StartParensTokenType &&
3610
+ !tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]]))) {
3906
3611
  // unbalanced parens
3907
- const node = stack.at(-1);
3612
+ const node = stack[stack.length - 1];
3908
3613
  errors?.push?.({
3909
3614
  action: "drop",
3910
3615
  message: `Unbalanced token ')'`,
3911
3616
  node,
3912
- location: options.source.getSourceLocation(node[LOCSTA]),
3617
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(node))
3618
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(node))],
3913
3619
  });
3914
3620
  // return [];
3915
3621
  continue;
3916
3622
  }
3917
3623
  tokens.splice(i, 1);
3918
- index = tokens.indexOf(stack.at(-1));
3919
- Object.assign(tokens[index], {
3920
- typ: tokens[index].typ === EnumToken.StartParensTokenType
3624
+ index = tokens.indexOf(stack[stack.length - 1]);
3625
+ const func = tokens[index];
3626
+ arena.dataTypes[func] =
3627
+ arena.dataTypes[func] === EnumToken.StartParensTokenType
3921
3628
  ? EnumToken.ParensTokenType
3922
- : tokensfuncDefMap.get(tokens[index].typ),
3923
- chi: trimArray(tokens.splice(index + 1, i - index - 1)),
3924
- });
3629
+ : tokensfuncDefMap.get(arena.dataTypes[func]);
3630
+ arena.setTokenList(func, trimArray(arena, tokens.splice(index + 1, i - index - 1)));
3631
+ // inlined: arena.setSpanEnd(func, arena.getSpanEnd(t))
3632
+ arena.setSpan(func, arena.getSourceId(func), arena.getSpanStart(func), arena.getSpanEnd(t));
3925
3633
  i = index;
3926
- if (tokens[index].typ === EnumToken.ColorTokenType && options?.parseColor) {
3927
- parseColor(tokens[index]);
3634
+ if (arena.dataTypes[func] === EnumToken.ColorTokenType && options?.parseColor) {
3635
+ parseColor(arena, func);
3928
3636
  }
3929
3637
  stack.pop();
3930
3638
  continue;
3931
3639
  }
3932
- if (t.typ === EnumToken.AttrEndTokenType) {
3933
- if (stack.at(-1)?.typ !== EnumToken.AttrStartTokenType) {
3640
+ if (typ === EnumToken.AttrEndTokenType) {
3641
+ if (stack.length === 0 || arena.dataTypes[stack[stack.length - 1]] !== EnumToken.AttrStartTokenType) {
3934
3642
  // unbalanced
3935
- const node = stack.at(-1);
3643
+ const node = stack[stack.length - 1];
3936
3644
  errors?.push?.({
3937
3645
  action: "drop",
3938
3646
  message: `Unbalanced token ']'`,
3939
3647
  node,
3940
- location: options.source.getSourceLocation(node[LOCSTA]),
3648
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(node))
3649
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(node))],
3941
3650
  });
3942
3651
  continue;
3943
3652
  }
3944
- index = tokens.indexOf(stack.at(-1));
3945
- const attr = stack.at(-1);
3946
- attr[LOCEND] = t[LOCEND];
3653
+ const attr = stack[stack.length - 1];
3654
+ index = tokens.indexOf(attr);
3655
+ // inlined: arena.setSpanEnd(attr, arena.getSpanEnd(t))
3656
+ arena.setSpan(attr, arena.getSourceId(attr), arena.getSpanStart(attr), arena.getSpanEnd(t));
3947
3657
  tokens.splice(i, 1);
3948
- Object.assign(attr, {
3949
- typ: EnumToken.AttrTokenType,
3950
- chi: tokens.splice(index + 1, i - index - 1),
3951
- });
3952
- tokens[index] = attr;
3658
+ arena.dataTypes[attr] = EnumToken.AttrTokenType;
3659
+ arena.setTokenList(attr, tokens.splice(index + 1, i - index - 1));
3953
3660
  i = index;
3954
3661
  stack.pop();
3955
3662
  continue;
3956
3663
  }
3957
- if (t.typ == EnumToken.IdenTokenType) {
3958
- if (t.val == "from" && i > 0) {
3664
+ if (typ == EnumToken.IdenTokenType) {
3665
+ if (arena.strings[arena.stringValue[t]] == "from" && i > 0) {
3959
3666
  const left = [];
3960
3667
  const right = [];
3961
3668
  let foundLeft = 0;
@@ -3963,13 +3670,13 @@ function parseTokens(tokens, options, errors) {
3963
3670
  let k = i;
3964
3671
  let l = i;
3965
3672
  while (k > 0) {
3966
- if (tokens[k - 1].typ == EnumToken.CommentTokenType ||
3967
- tokens[k - 1].typ == EnumToken.WhitespaceTokenType) {
3673
+ if (arena.dataTypes[tokens[k - 1]] == EnumToken.CommentTokenType ||
3674
+ arena.dataTypes[tokens[k - 1]] == EnumToken.WhitespaceTokenType) {
3968
3675
  left.push(tokens[--k]);
3969
3676
  continue;
3970
3677
  }
3971
- if (tokens[k - 1].typ == EnumToken.IdenTokenType ||
3972
- tokens[k - 1].typ == EnumToken.DashedIdenTokenType) {
3678
+ if (arena.dataTypes[tokens[k - 1]] == EnumToken.IdenTokenType ||
3679
+ arena.dataTypes[tokens[k - 1]] == EnumToken.DashedIdenTokenType) {
3973
3680
  foundLeft++;
3974
3681
  left.push(tokens[--k]);
3975
3682
  continue;
@@ -3977,11 +3684,13 @@ function parseTokens(tokens, options, errors) {
3977
3684
  break;
3978
3685
  }
3979
3686
  while (++l < tokens.length) {
3980
- if (tokens[l].typ == EnumToken.CommentTokenType || tokens[l].typ == EnumToken.WhitespaceTokenType) {
3687
+ if (arena.dataTypes[tokens[l]] == EnumToken.CommentTokenType ||
3688
+ arena.dataTypes[tokens[l]] == EnumToken.WhitespaceTokenType) {
3981
3689
  right.push(tokens[l]);
3982
3690
  continue;
3983
3691
  }
3984
- if (tokens[l].typ == EnumToken.IdenTokenType || tokens[l].typ == EnumToken.StringTokenType) {
3692
+ if (arena.dataTypes[tokens[l]] == EnumToken.IdenTokenType ||
3693
+ arena.dataTypes[tokens[l]] == EnumToken.StringTokenType) {
3985
3694
  foundRight++;
3986
3695
  right.push(tokens[l]);
3987
3696
  continue;
@@ -3989,32 +3698,38 @@ function parseTokens(tokens, options, errors) {
3989
3698
  break;
3990
3699
  }
3991
3700
  if (foundLeft > 0 && foundRight == 1) {
3992
- while (left?.[0].typ == EnumToken.WhitespaceTokenType) {
3701
+ while (left.length > 0 && arena.dataTypes[left[0]] == EnumToken.WhitespaceTokenType) {
3993
3702
  left.shift();
3994
3703
  }
3995
- while (left.at(-1)?.typ == EnumToken.WhitespaceTokenType) {
3704
+ while (left.length > 0 && arena.dataTypes[left[left.length - 1]] == EnumToken.WhitespaceTokenType) {
3996
3705
  left.pop();
3997
3706
  }
3998
- tokens.splice(k, l - k + 1, {
3999
- typ: EnumToken.ComposesSelectorNodeType,
4000
- l: left,
4001
- r: right.reduce((a, b) => {
4002
- return a == null
4003
- ? b
4004
- : b.typ == EnumToken.IdenTokenType || b.typ == EnumToken.StringTokenType
4005
- ? b
4006
- : a;
4007
- }, null),
4008
- });
3707
+ // composes selector: l = <ident>+, r = <ident> | <string> | null
3708
+ const composes = arena.createToken(EnumToken.ComposesSelectorNodeType);
3709
+ let from = 0;
3710
+ for (const b of right) {
3711
+ if (from == 0 ||
3712
+ arena.dataTypes[b] == EnumToken.IdenTokenType ||
3713
+ arena.dataTypes[b] == EnumToken.StringTokenType) {
3714
+ from = b;
3715
+ }
3716
+ }
3717
+ // inlined: arena.setField(composes, TokenField.L, ...left)
3718
+ arena.setTokenList(arena.fieldList(composes, TokenField.L), left);
3719
+ if (from != 0) {
3720
+ // inlined: arena.setField(composes, TokenField.R, from)
3721
+ arena.setTokenList(arena.fieldList(composes, TokenField.R), [from]);
3722
+ }
3723
+ tokens.splice(k, l - k + 1, composes);
4009
3724
  i = k;
4010
3725
  continue;
4011
3726
  }
4012
3727
  }
4013
- else if (options?.parseColor && isColor(t)) {
4014
- parseColor(t);
3728
+ else if (options?.parseColor && isColor(arena, t)) {
3729
+ parseColor(arena, t);
4015
3730
  }
4016
3731
  }
4017
- if (t.typ == EnumToken.WhitespaceTokenType &&
3732
+ if (typ == EnumToken.WhitespaceTokenType &&
4018
3733
  (i == 0 ||
4019
3734
  i + 1 == tokens.length ||
4020
3735
  [
@@ -4022,44 +3737,46 @@ function parseTokens(tokens, options, errors) {
4022
3737
  EnumToken.GteTokenType,
4023
3738
  EnumToken.LteTokenType,
4024
3739
  EnumToken.ColumnCombinatorTokenType,
4025
- ].includes(tokens[i + 1].typ) ||
4026
- (i > 0 && trimWhiteSpace.includes(tokens[i - 1].typ)))) {
3740
+ ].includes(arena.dataTypes[tokens[i + 1]]) ||
3741
+ (i > 0 && trimWhiteSpace.includes(arena.dataTypes[tokens[i - 1]])))) {
4027
3742
  tokens.splice(i--, 1);
4028
3743
  continue;
4029
3744
  }
4030
- if (t.typ == EnumToken.ColonTokenType) {
4031
- const typ = tokens[i + 1]?.typ;
4032
- if (typ != null) {
4033
- if (typ == EnumToken.FunctionTokenType) {
4034
- tokens[i + 1].typ = EnumToken.PseudoClassFuncTokenType;
3745
+ if (typ == EnumToken.ColonTokenType) {
3746
+ const next = i + 1 < tokens.length ? arena.dataTypes[tokens[i + 1]] : undefined;
3747
+ if (next != null) {
3748
+ if (next == EnumToken.FunctionTokenType) {
3749
+ arena.dataTypes[tokens[i + 1]] = EnumToken.PseudoClassFuncTokenType;
4035
3750
  }
4036
- else if (typ == EnumToken.IdenTokenType) {
4037
- tokens[i + 1].val = ":" + tokens[i + 1].val;
4038
- tokens[i + 1].typ = EnumToken.PseudoClassTokenType;
3751
+ else if (next == EnumToken.IdenTokenType) {
3752
+ // inlined: arena.setStr(tokens[i + 1], ":" + arena.strings[arena.stringValue[tokens[i + 1]]])
3753
+ arena.stringValue[tokens[i + 1]] = arena.internString((":" + arena.strings[arena.stringValue[tokens[i + 1]]]));
3754
+ arena.dataTypes[tokens[i + 1]] = EnumToken.PseudoClassTokenType;
4039
3755
  }
4040
- if (typ == EnumToken.FunctionTokenType || typ == EnumToken.IdenTokenType) {
3756
+ if (next == EnumToken.FunctionTokenType || next == EnumToken.IdenTokenType) {
4041
3757
  tokens.splice(i, 1);
4042
3758
  i--;
4043
3759
  }
4044
3760
  }
4045
3761
  }
4046
- if (t.typ === EnumToken.CommaTokenType) {
4047
- if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
3762
+ if (typ === EnumToken.CommaTokenType) {
3763
+ if (i + 1 < tokens.length && arena.dataTypes[tokens[i + 1]] === EnumToken.WhitespaceTokenType) {
4048
3764
  tokens.splice(i + 1, 1);
4049
3765
  }
4050
- if (tokens[i - 1]?.typ === EnumToken.WhitespaceTokenType) {
3766
+ if (i > 0 && arena.dataTypes[tokens[i - 1]] === EnumToken.WhitespaceTokenType) {
4051
3767
  tokens.splice(i - 1, 1);
4052
3768
  i--;
4053
3769
  }
4054
3770
  }
4055
3771
  }
4056
3772
  if (stack.length > 0) {
4057
- const node = stack.at(-1);
3773
+ const node = stack[stack.length - 1];
4058
3774
  errors?.push?.({
4059
3775
  action: "drop",
4060
- message: `Unbalanced token. Expecting ${node.typ === EnumToken.AttrStartTokenType ? "']'" : ")"}'`,
3776
+ message: `Unbalanced token. Expecting ${arena.dataTypes[node] === EnumToken.AttrStartTokenType ? "']'" : ")"}'`,
4061
3777
  node,
4062
- location: options.source.getSourceLocation(node[LOCSTA]),
3778
+ // inlined: options.source!.getSourceLocation(arena.getSpanStart(node))
3779
+ location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(node))],
4063
3780
  });
4064
3781
  }
4065
3782
  return tokens;