@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,11 +2,11 @@
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
- import { PropertyList } from '../../parser/declaration/list.js';
9
- import { EnumToken } from '../types.js';
8
+ import { DeclarationMemo, PropertyList } from '../../parser/declaration/list.js';
9
+ import { EnumToken, EnumAstNodeStatus } from '../types.js';
10
10
  import { FeatureWalkMode } from './type.js';
11
11
 
12
12
  class ComputeShorthandFeature {
@@ -27,65 +27,59 @@ class ComputeShorthandFeature {
27
27
  options.features.push(new ComputeShorthandFeature(options));
28
28
  }
29
29
  }
30
- run(ast, options) {
31
- if (!("chi" in ast || ast.chi?.length == 0)) {
30
+ run(arena, ast, options, _parent, context) {
31
+ if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren) || arena.firstChild[ast] == 0) {
32
32
  return null;
33
33
  }
34
- // @ts-ignore
35
- const j = ast.chi.length;
36
- let k = 0;
37
- let l;
38
- const rules = [];
39
- const declarations = [];
34
+ // the keys of the declarations are shared by the rules of the minification
35
+ const memo = context == null ? null : (context.declarations ??= new DeclarationMemo());
36
+ const list = new PropertyList(arena, options, memo);
40
37
  let doNotSort = false;
41
- if (ast.typ == EnumToken.AtRuleNodeType) {
42
- switch (ast.nam) {
38
+ let hasDeclarations = false;
39
+ if (arena.dataTypes[ast] == EnumToken.AtRuleNodeType) {
40
+ switch (arena.strings[arena.name[ast]]) {
43
41
  case "function":
44
42
  case "mixin":
45
43
  doNotSort = true;
46
44
  break;
47
45
  }
48
46
  }
49
- // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example
50
- if (doNotSort) {
51
- for (const node of ast.chi) {
52
- declarations.push(node);
47
+ // the declarations (with their leading comments) are moved into the property list,
48
+ // the rules (with their leading comments) stay in place
49
+ let child = arena.firstChild[ast];
50
+ while (child != 0) {
51
+ let last = child;
52
+ if (!doNotSort) {
53
+ // capture comments with the next node
54
+ while (arena.nextSibling[last] != 0 && arena.dataTypes[last] == EnumToken.CommentNodeType) {
55
+ last = arena.nextSibling[last];
56
+ }
53
57
  }
54
- }
55
- else {
56
- // @ts-ignore
57
- for (; k < j; k++) {
58
- l = k;
59
- // capture comments with the next token
60
- while (l + 1 < j) {
61
- // @ts-ignore
62
- const node = ast.chi[l];
63
- if (node.typ == EnumToken.CommentNodeType) {
64
- l++;
65
- continue;
66
- }
67
- break;
58
+ const next = arena.nextSibling[last];
59
+ // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example
60
+ if (doNotSort || arena.dataTypes[last] == EnumToken.DeclarationNodeType) {
61
+ hasDeclarations = true;
62
+ for (let node = child; node != next;) {
63
+ const nextNode = arena.nextSibling[node];
64
+ arena.detach(node);
65
+ list.addDeclaration(node);
66
+ node = nextNode;
68
67
  }
69
- // @ts-ignore
70
- const node = ast.chi[l];
71
- if (node.typ == EnumToken.DeclarationNodeType) {
72
- for (let m = k; m <= l; m++) {
73
- declarations.push(ast.chi[m]);
74
- }
68
+ }
69
+ child = next;
70
+ }
71
+ if (hasDeclarations) {
72
+ // the declarations are inserted before the rules
73
+ const first = arena.firstChild[ast];
74
+ for (const node of list) {
75
+ if (first == 0) {
76
+ arena.appendChild(ast, node);
75
77
  }
76
78
  else {
77
- for (let m = k; m <= l; m++) {
78
- rules.push(ast.chi[m]);
79
- }
79
+ arena.insertBefore(first, node);
80
80
  }
81
- k = l;
82
81
  }
83
82
  }
84
- if (declarations.length > 0) {
85
- ast.chi.length = 0;
86
- // @ts-expect-error
87
- ast.chi.push(...new PropertyList(options).add(declarations), ...rules);
88
- }
89
83
  return ast;
90
84
  }
91
85
  }
@@ -2,7 +2,7 @@
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 { EnumToken, EnumAstNodeStatus } from '../types.js';
@@ -10,8 +10,11 @@ import { compute } from '../transform/compute.js';
10
10
  import { filterValues, renderValue } from '../../printer/render.js';
11
11
  import { minifyTransformFunctions, eqMatrix } from '../transform/minify.js';
12
12
  import { FeatureWalkMode } from './type.js';
13
- import { STATE } from '../../syntax/constants.js';
14
13
 
14
+ /**
15
+ * transform property and its prefixed versions
16
+ */
17
+ const transformProperty = /^(-[a-z]+-)?transform$/;
15
18
  class TransformCssFeature {
16
19
  accept = new Set([
17
20
  EnumToken.RuleNodeType,
@@ -31,52 +34,49 @@ class TransformCssFeature {
31
34
  options.features.push(new TransformCssFeature());
32
35
  }
33
36
  }
34
- run(ast) {
35
- if (ast.chi == null) {
37
+ run(arena, ast) {
38
+ if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren)) {
36
39
  return null;
37
40
  }
38
- let i = 0;
39
41
  let node;
40
- // @ts-ignore
41
- for (; i < ast.chi.length; i++) {
42
- // @ts-ignore
43
- node = ast.chi[i];
44
- if (node[STATE] & (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.ValidationFailed)) {
42
+ for (node = arena.firstChild[ast]; node != 0; node = arena.nextSibling[node]) {
43
+ if (arena.flags[node] & (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.ValidationFailed)) {
45
44
  continue;
46
45
  }
47
- if (node.typ != EnumToken.DeclarationNodeType ||
48
- !node.nam.match(/^(-[a-z]+-)?transform$/)) {
46
+ if (arena.dataTypes[node] != EnumToken.DeclarationNodeType ||
47
+ !transformProperty.test(arena.strings[arena.name[node]])) {
49
48
  continue;
50
49
  }
51
50
  const children = [];
52
- for (const child of node.val) {
53
- children.push(child.typ == EnumToken.TransformFunctionTokenType
54
- ? minifyTransformFunctions(child)
51
+ for (const child of arena.tokens(node)) {
52
+ children.push(arena.dataTypes[child] == EnumToken.TransformFunctionTokenType
53
+ ? minifyTransformFunctions(arena, child)
55
54
  : child);
56
55
  }
57
- let { matrix, cumulative, minified } = compute(children) ?? {
56
+ let { matrix, cumulative, minified } = compute(arena, children) ?? {
58
57
  matrix: null,
59
58
  cumulative: null,
60
59
  minified: null,
61
60
  };
62
61
  if (matrix == null || cumulative == null || minified == null) {
63
- node.val = children.map((t) => t.typ == EnumToken.TransformFunctionTokenType ? minifyTransformFunctions(t) : t);
62
+ arena.setTokenList(node, children);
64
63
  continue;
65
64
  }
66
- let r = [filterValues(children)];
67
- if (eqMatrix(matrix, cumulative)) {
65
+ let r = [filterValues(arena, children)];
66
+ if (eqMatrix(arena, matrix, cumulative)) {
68
67
  r.push(cumulative);
69
68
  }
70
- if (eqMatrix(matrix, minified)) {
69
+ if (eqMatrix(arena, matrix, minified)) {
71
70
  r.push(minified);
72
71
  }
73
- const l = renderValue(matrix).length;
74
- node.val = r.reduce((acc, curr) => {
75
- if (curr.reduce((acc, t) => acc + renderValue(t), "").length < l) {
72
+ const l = renderValue(arena, matrix).length;
73
+ // inlined: arena.setTokens(node, ...r.reduce((acc: Token[], curr: Token[]): Token[] => { if (curr.reduce((acc: string, t: Token) => acc + renderValue(arena, t), "").length < l) { return curr; } return acc; }, [matrix]))
74
+ arena.setTokenList(node, r.reduce((acc, curr) => {
75
+ if (curr.reduce((acc, t) => acc + renderValue(arena, t), "").length < l) {
76
76
  return curr;
77
77
  }
78
78
  return acc;
79
- }, [matrix]);
79
+ }, [matrix]));
80
80
  }
81
81
  return null;
82
82
  }
@@ -2,7 +2,7 @@
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
  /**
@@ -0,0 +1,333 @@
1
+ /*
2
+ CSS parser, minifier and validator for node and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.0.0
6
+ License: MIT
7
+ */
8
+ /**
9
+ * flags of the header of a serialized token: the fields that follow the header. the header is never negative
10
+ * (8 bits type, 8 bits color type, flags)
11
+ */
12
+ var Field;
13
+ (function (Field) {
14
+ Field[Field["Name"] = 65536] = "Name";
15
+ Field[Field["String"] = 131072] = "String";
16
+ Field[Field["Unit"] = 262144] = "Unit";
17
+ // 32 bits integer numeric value, other than 0
18
+ Field[Field["Integer"] = 524288] = "Integer";
19
+ // raw bits of the numeric value (2 values)
20
+ Field[Field["Number"] = 1048576] = "Number";
21
+ // token list, terminated by an end marker
22
+ Field[Field["Tokens"] = 2097152] = "Tokens";
23
+ // children list, terminated by an end marker
24
+ Field[Field["Children"] = 4194304] = "Children";
25
+ })(Field || (Field = {}));
26
+ /**
27
+ * end of a token list or of a children list
28
+ */
29
+ const End = -1;
30
+ /**
31
+ * numeric value conversion buffer
32
+ */
33
+ const f64 = new Float64Array(1);
34
+ /**
35
+ * raw bits of the numeric value conversion buffer
36
+ */
37
+ const bits = new Int32Array(f64.buffer);
38
+ /**
39
+ * canonical value: 0 for -0, NaN for all the NaN values
40
+ * @param value
41
+ */
42
+ function normalize(value) {
43
+ return value !== value ? NaN : value + 0;
44
+ }
45
+ /**
46
+ * Memoize values by token structure. Lookups return a slot, the value of a slot is `undefined` until it is set.
47
+ *
48
+ * The keys are stored in a typed array pool (one allocation, grown by doubling): a miss copies the key into the pool.
49
+ *
50
+ * ```ts
51
+ * const memo = new StructuralMemo<string>();
52
+ * const slot = memo.list(arena, arena.getFirstToken(declaration));
53
+ * let value = memo.get(slot);
54
+ *
55
+ * if (value === undefined) {
56
+ * value = expensiveRender(declaration);
57
+ * memo.set(slot, value);
58
+ * }
59
+ * ```
60
+ *
61
+ * @internal
62
+ */
63
+ class StructuralMemo {
64
+ numericEquality;
65
+ /**
66
+ * An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
67
+ * collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
68
+ */
69
+ static shape = new StructuralMemo();
70
+ /**
71
+ * values by slot. read and written by the library (see {@link get} and {@link set})
72
+ *
73
+ * @internal
74
+ */
75
+ values = [];
76
+ /**
77
+ * key hash, key offset in the pool and key length of each slot (3 values per slot)
78
+ */
79
+ slots = new Int32Array(64 * 3);
80
+ /**
81
+ * serialized keys. the key being looked up is serialized after the last key: it is kept when it is inserted
82
+ */
83
+ pool = new Int32Array(1024);
84
+ /**
85
+ * used length of the pool
86
+ */
87
+ poolSize = 0;
88
+ /**
89
+ * open addressing hash table: slot + 1, 0 when empty
90
+ */
91
+ table = new Int32Array(128);
92
+ /**
93
+ * end of the key being looked up in the pool
94
+ */
95
+ used = 0;
96
+ /**
97
+ * number of distinct structures (lookups that inserted a new entry)
98
+ */
99
+ size = 0;
100
+ /**
101
+ * number of lookups that found an existing entry
102
+ */
103
+ hits = 0;
104
+ /**
105
+ * @param numericEquality compare the numeric values with `==` semantics (`0` and `-0` are equal) and consider
106
+ * all the `NaN` values equal. the raw bits are compared otherwise
107
+ */
108
+ constructor(numericEquality = false) {
109
+ this.numericEquality = numericEquality;
110
+ }
111
+ /**
112
+ * number of int32 values allocated by the memo
113
+ */
114
+ get footprint() {
115
+ return this.slots.length + this.pool.length + this.table.length;
116
+ }
117
+ /**
118
+ * slot of a token list (the token and its next siblings)
119
+ * @param arena
120
+ * @param first first token of the list. 0 for an empty list
121
+ * @param prefix extra key component, not negative (an interned name id for instance)
122
+ */
123
+ list(arena, first, prefix = 0) {
124
+ let used = this.poolSize;
125
+ if (used + 1 > this.pool.length) {
126
+ this.growPool(used + 1);
127
+ }
128
+ this.pool[used++] = prefix;
129
+ for (let token = first; token != 0; token = arena.nextSibling[token]) {
130
+ used = this.serialize(arena, token, used);
131
+ }
132
+ this.used = used;
133
+ return this.lookup();
134
+ }
135
+ /**
136
+ * slot of a single token (its next siblings are ignored)
137
+ * @param arena
138
+ * @param token
139
+ */
140
+ token(arena, token) {
141
+ this.used = this.serialize(arena, token, this.poolSize);
142
+ return this.lookup();
143
+ }
144
+ /**
145
+ * value of a slot. `undefined` if the value has not been set
146
+ * @param slot
147
+ */
148
+ get(slot) {
149
+ return this.values[slot];
150
+ }
151
+ /**
152
+ * set the value of a slot
153
+ * @param slot
154
+ * @param value
155
+ */
156
+ set(slot, value) {
157
+ this.values[slot] = value;
158
+ }
159
+ /**
160
+ * drop the entries. the allocated buffers are kept
161
+ */
162
+ clear() {
163
+ this.values.length = 0;
164
+ this.table.fill(0);
165
+ this.poolSize = 0;
166
+ this.used = 0;
167
+ this.size = 0;
168
+ this.hits = 0;
169
+ }
170
+ /**
171
+ * serialize a token after the used part of the pool: a header, the fields that are not empty, then its token list
172
+ * and its children list
173
+ * @param arena
174
+ * @param token
175
+ * @param used end of the serialized key
176
+ * @returns the new end of the serialized key
177
+ */
178
+ serialize(arena, token, used) {
179
+ // header, 5 fields, end marker
180
+ let buffer = used + 7 > this.pool.length ? this.growPool(used + 7) : this.pool;
181
+ const name = arena.name[token];
182
+ const str = arena.stringValue[token];
183
+ const unit = arena.unit[token];
184
+ const firstToken = arena.firstToken[token];
185
+ const firstChild = arena.firstChild[token];
186
+ let value = arena.numericValue[token];
187
+ // type and color type are 8 bits values
188
+ let header = arena.dataTypes[token] | (arena.colorType[token] << 8);
189
+ const index = used++;
190
+ if (name != 0) {
191
+ header |= 65536 /* Field.Name */;
192
+ buffer[used++] = name;
193
+ }
194
+ if (str != 0) {
195
+ header |= 131072 /* Field.String */;
196
+ buffer[used++] = str;
197
+ }
198
+ if (unit != 0) {
199
+ header |= 262144 /* Field.Unit */;
200
+ buffer[used++] = unit;
201
+ }
202
+ if (this.numericEquality) {
203
+ value = normalize(value);
204
+ }
205
+ // 0 is not serialized. -0 is not an integer: its raw bits are serialized
206
+ if (value !== 0 || 1 / value < 0) {
207
+ if ((value | 0) === value && value !== 0) {
208
+ header |= 524288 /* Field.Integer */;
209
+ buffer[used++] = value;
210
+ }
211
+ else {
212
+ header |= 1048576 /* Field.Number */;
213
+ f64[0] = value;
214
+ buffer[used++] = bits[0];
215
+ buffer[used++] = bits[1];
216
+ }
217
+ }
218
+ if (firstToken != 0) {
219
+ header |= 2097152 /* Field.Tokens */;
220
+ }
221
+ if (firstChild != 0) {
222
+ header |= 4194304 /* Field.Children */;
223
+ }
224
+ buffer[index] = header;
225
+ if (firstToken != 0) {
226
+ for (let child = firstToken; child != 0; child = arena.nextSibling[child]) {
227
+ used = this.serialize(arena, child, used);
228
+ }
229
+ buffer = used + 1 > this.pool.length ? this.growPool(used + 1) : this.pool;
230
+ buffer[used++] = End;
231
+ }
232
+ if (firstChild != 0) {
233
+ for (let child = firstChild; child != 0; child = arena.nextSibling[child]) {
234
+ used = this.serialize(arena, child, used);
235
+ }
236
+ buffer = used + 1 > this.pool.length ? this.growPool(used + 1) : this.pool;
237
+ buffer[used++] = End;
238
+ }
239
+ return used;
240
+ }
241
+ /**
242
+ * grow the pool
243
+ * @param size minimum size
244
+ */
245
+ growPool(size) {
246
+ this.pool = grow(this.pool, size);
247
+ return this.pool;
248
+ }
249
+ /**
250
+ * find or insert the key serialized after the used part of the pool
251
+ */
252
+ lookup() {
253
+ const pool = this.pool;
254
+ const base = this.poolSize;
255
+ const length = this.used - base;
256
+ let hash = 0x811c9dc5 ^ length;
257
+ for (let i = base; i < this.used; i++) {
258
+ hash = Math.imul(hash ^ pool[i], 0x01000193);
259
+ }
260
+ // the multiplications only propagate the bits upwards: the low bits, used as the table index, are mixed with
261
+ // the high bits (murmur3 finalizer)
262
+ hash ^= hash >>> 16;
263
+ hash = Math.imul(hash, 0x85ebca6b);
264
+ hash ^= hash >>> 13;
265
+ hash = Math.imul(hash, 0xc2b2ae35);
266
+ hash ^= hash >>> 16;
267
+ const table = this.table;
268
+ const mask = table.length - 1;
269
+ let index = hash & mask;
270
+ let slot;
271
+ while ((slot = table[index]) != 0) {
272
+ slot--;
273
+ const meta = slot * 3;
274
+ if (this.slots[meta] == hash && this.slots[meta + 2] == length) {
275
+ const offset = this.slots[meta + 1];
276
+ let i = 0;
277
+ while (i < length && pool[offset + i] == pool[base + i]) {
278
+ i++;
279
+ }
280
+ if (i == length) {
281
+ this.hits++;
282
+ return slot;
283
+ }
284
+ }
285
+ index = (index + 1) & mask;
286
+ }
287
+ // insert: the serialized key is kept in the pool
288
+ slot = this.size++;
289
+ if (slot * 3 == this.slots.length) {
290
+ this.slots = grow(this.slots, slot * 3 + 3);
291
+ }
292
+ this.slots[slot * 3] = hash;
293
+ this.slots[slot * 3 + 1] = base;
294
+ this.slots[slot * 3 + 2] = length;
295
+ this.values.push(undefined);
296
+ this.poolSize = this.used;
297
+ table[index] = slot + 1;
298
+ // load factor <= 0.5
299
+ if (this.size * 2 > table.length) {
300
+ this.rehash();
301
+ }
302
+ return slot;
303
+ }
304
+ rehash() {
305
+ // the table grows by 4: fewer reallocations
306
+ const table = new Int32Array(this.table.length * 4);
307
+ const mask = table.length - 1;
308
+ for (let slot = 0; slot < this.size; slot++) {
309
+ let index = this.slots[slot * 3] & mask;
310
+ while (table[index] != 0) {
311
+ index = (index + 1) & mask;
312
+ }
313
+ table[index] = slot + 1;
314
+ }
315
+ this.table = table;
316
+ }
317
+ }
318
+ /**
319
+ * grow an int32 buffer: its size is multiplied by 4 (fewer reallocations)
320
+ * @param buffer
321
+ * @param size minimum size
322
+ */
323
+ function grow(buffer, size) {
324
+ let length = buffer.length * 4;
325
+ while (length < size) {
326
+ length *= 4;
327
+ }
328
+ const result = new Int32Array(length);
329
+ result.set(buffer);
330
+ return result;
331
+ }
332
+
333
+ export { StructuralMemo };