@tbela99/css-parser 1.8.2 → 2.0.1

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 +27383 -24083
  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/cache.js +333 -0
  11. package/dist/src/lib/ast/features/calc.js +85 -76
  12. package/dist/src/lib/ast/features/if.js +155 -115
  13. package/dist/src/lib/ast/features/index.js +1 -1
  14. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  15. package/dist/src/lib/ast/features/prefix.js +184 -183
  16. package/dist/src/lib/ast/features/shorthand.js +40 -46
  17. package/dist/src/lib/ast/features/transform.js +24 -24
  18. package/dist/src/lib/ast/features/type.js +1 -1
  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 -1658
  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 -35870
  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,469 +2,479 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.1
6
6
  License: MIT
7
7
  */
8
+ import { numericTokenTypes, TokenField } from '../arena.js';
8
9
  import { equalsIgnoreCase } from '../../parser/utils/text.js';
9
- import { mathFuncs, PARENT, LOCEND, LOCSTA, LOCSRCID } from '../../syntax/constants.js';
10
+ import { mathFuncs } from '../../syntax/constants.js';
10
11
  import { EnumToken } from '../types.js';
11
12
  import { rem, compute } from './math.js';
12
13
 
13
14
  const numbers = new Set(["sin", "cos", "asin", "acos", "sign", "tan"]);
14
15
  /**
15
- * evaluate an array of tokens
16
+ * numeric value of a token: a number or a fraction
17
+ * @param arena
18
+ * @param token
19
+ */
20
+ function numericValue(arena, token) {
21
+ const fraction = arena.getFraction(token);
22
+ if (fraction != 0) {
23
+ return [arena.numericValue[arena.firstChild[fraction]], arena.numericValue[arena.lastChild[fraction]]];
24
+ }
25
+ return getValue(arena, token);
26
+ }
27
+ /**
28
+ * set the numeric value of a token: a number or a fraction
29
+ * @param arena
30
+ * @param token
31
+ * @param val
32
+ */
33
+ function setNumericValue(arena, token, val) {
34
+ if (typeof val == "number") {
35
+ arena.setFraction(token, null);
36
+ arena.numericValue[token] = val;
37
+ return;
38
+ }
39
+ arena.setFraction(token, arena.createFraction(arena.createToken(EnumToken.NumberTokenType, val[0]), arena.createToken(EnumToken.NumberTokenType, val[1])));
40
+ }
41
+ /**
42
+ * allocate a token with the location of another token
43
+ * @param arena
44
+ * @param typ
45
+ * @param location
46
+ * @param val
47
+ * @param unit
48
+ */
49
+ function tokenAt(arena, typ, location, val, unit) {
50
+ const token = arena.createToken(typ, val, unit);
51
+ arena.copyLocation(token, location);
52
+ return token;
53
+ }
54
+ /**
55
+ * evaluate an array of tokens. the tokens are cloned: the result does not share any token with the input
56
+ * @param arena
57
+ * @param tokens
58
+ */
59
+ function evaluate(arena, tokens) {
60
+ return evaluateTokens(arena, tokens.map((token) => arena.clone(token, true)));
61
+ }
62
+ /**
63
+ * evaluate an array of tokens. the tokens are moved into the result
64
+ * @param arena
16
65
  * @param tokens
17
66
  */
18
- function evaluate(tokens) {
67
+ function evaluateTokens(arena, tokens) {
19
68
  let nodes;
20
69
  if (tokens.length == 1 &&
21
- (tokens[0].typ == EnumToken.MathFunctionTokenType || tokens[0].typ == EnumToken.FunctionTokenType) &&
22
- mathFuncs.includes(tokens[0].val)) {
23
- const chi = tokens[0].chi.reduce((acc, t) => {
24
- if (acc.length == 0 || t.typ == EnumToken.CommaTokenType) {
25
- acc.push([]);
70
+ (arena.dataTypes[tokens[0]] == EnumToken.MathFunctionTokenType ||
71
+ arena.dataTypes[tokens[0]] == EnumToken.FunctionTokenType) &&
72
+ mathFuncs.includes(arena.strings[arena.stringValue[tokens[0]]])) {
73
+ const chi = [];
74
+ for (let t = arena.firstToken[tokens[0]]; t != 0; t = arena.nextSibling[t]) {
75
+ const typ = arena.dataTypes[t];
76
+ if (chi.length == 0 || typ == EnumToken.CommaTokenType) {
77
+ chi.push([]);
26
78
  }
27
- if ([EnumToken.WhitespaceTokenType, EnumToken.CommaTokenType, EnumToken.CommaTokenType].includes(t.typ)) {
28
- return acc;
79
+ if (typ == EnumToken.WhitespaceTokenType || typ == EnumToken.CommaTokenType) {
80
+ continue;
29
81
  }
30
- acc.at(-1).push(t);
31
- return acc;
32
- }, []);
33
- for (let i = 0; i < chi.length; i++) {
34
- chi[i] = evaluate(chi[i]);
82
+ chi[chi.length - 1].push(t);
35
83
  }
36
- tokens[0].chi = chi.reduce((acc, t) => {
37
- if (acc.length > 0) {
38
- acc.push({ typ: EnumToken.CommaTokenType });
84
+ const children = [];
85
+ for (let i = 0; i < chi.length; i++) {
86
+ if (children.length > 0) {
87
+ children.push(arena.createToken(EnumToken.CommaTokenType));
39
88
  }
40
- for (const token of t) {
41
- acc.push(token);
89
+ for (const token of evaluateTokens(arena, chi[i])) {
90
+ children.push(token);
42
91
  }
43
- return acc;
44
- });
45
- const result = evaluateFunc(tokens[0]);
92
+ }
93
+ arena.setTokenList(tokens[0], children);
94
+ const result = evaluateFunc(arena, tokens[0]);
46
95
  if (result == null) {
47
96
  return tokens;
48
97
  }
49
- if (result[0].typ === EnumToken.MathFunctionTokenType &&
50
- result[0].val === "calc" &&
51
- result[0].chi.length === 1) {
52
- return result[0].chi.slice();
98
+ if (arena.dataTypes[result[0]] === EnumToken.MathFunctionTokenType &&
99
+ arena.strings[arena.stringValue[result[0]]] === "calc" &&
100
+ arena.tokenCount(result[0]) === 1) {
101
+ return arena.tokens(result[0]);
53
102
  }
54
103
  return result;
55
104
  }
56
- nodes = inlineExpression(evaluateExpression(buildExpression(tokens)));
105
+ nodes = inlineExpression(arena, evaluateExpression(arena, buildExpression(arena, tokens)));
57
106
  if (nodes.length <= 1) {
58
107
  if (nodes.length == 1) {
59
- if (nodes[0].typ == EnumToken.BinaryExpressionTokenType) {
60
- return inlineExpression(nodes[0]);
108
+ if (arena.dataTypes[nodes[0]] == EnumToken.BinaryExpressionTokenType) {
109
+ return inlineExpression(arena, nodes[0]);
61
110
  }
62
- // @ts-ignore
63
- if (nodes[0].typ == EnumToken.IdenTokenType &&
111
+ if (arena.dataTypes[nodes[0]] == EnumToken.IdenTokenType &&
64
112
  // @ts-expect-error
65
- typeof Math[nodes[0].val.toUpperCase()] == "number") {
66
- return [
67
- {
68
- ...nodes[0],
69
- // @ts-ignore
70
- val: Math[nodes[0].val.toUpperCase()],
71
- typ: EnumToken.NumberTokenType,
72
- [LOCSRCID]: nodes[0][LOCSRCID],
73
- [LOCSTA]: nodes[0][LOCSTA],
74
- [LOCEND]: nodes[0][LOCEND],
75
- [PARENT]: nodes[0][PARENT],
76
- },
77
- ];
113
+ typeof Math[arena.strings[arena.stringValue[nodes[0]]].toUpperCase()] == "number") {
114
+ // @ts-ignore
115
+ const val = Math[arena.strings[arena.stringValue[nodes[0]]].toUpperCase()];
116
+ arena.resetToken(nodes[0], EnumToken.NumberTokenType, val);
117
+ return nodes;
78
118
  }
79
119
  }
80
120
  return nodes;
81
121
  }
82
122
  const map = new Map();
83
123
  let token;
124
+ let typ;
84
125
  let i;
85
126
  for (i = 0; i < nodes.length; i++) {
86
127
  token = nodes[i];
87
- if (token.typ == EnumToken.Add || token.typ == EnumToken.Plus) {
128
+ typ = arena.dataTypes[token];
129
+ if (typ == EnumToken.Add || typ == EnumToken.Plus) {
88
130
  continue;
89
131
  }
90
- if (token.typ == EnumToken.Sub) {
91
- if (!isScalarToken(nodes[i + 1])) {
92
- token = {
93
- typ: EnumToken.ListToken,
94
- chi: [nodes[i], nodes[i + 1]],
95
- [LOCSRCID]: nodes[i][LOCSRCID],
96
- [LOCSTA]: nodes[i][LOCSTA],
97
- [LOCEND]: nodes[i + 1][LOCEND],
98
- [PARENT]: nodes[i + 1][PARENT],
99
- };
132
+ if (typ == EnumToken.Sub) {
133
+ if (!isScalarToken(arena, nodes[i + 1])) {
134
+ token = arena.createList(EnumToken.ListToken, [nodes[i], nodes[i + 1]]);
135
+ arena.spanLocation(token, nodes[i], nodes[i + 1]);
100
136
  }
101
137
  else {
102
- token = doEvaluate(nodes[i + 1], {
103
- typ: EnumToken.NumberTokenType,
104
- val: -1,
105
- [LOCSRCID]: nodes[i + 1][LOCSRCID],
106
- [LOCSTA]: nodes[i + 1][LOCSTA],
107
- [LOCEND]: nodes[i + 1][LOCEND],
108
- [PARENT]: nodes[i + 1][PARENT],
109
- }, EnumToken.Mul);
138
+ token = doEvaluate(arena, nodes[i + 1], tokenAt(arena, EnumToken.NumberTokenType, nodes[i + 1], -1), EnumToken.Mul);
110
139
  }
111
140
  i++;
141
+ typ = arena.dataTypes[token];
112
142
  }
113
- if (!map.has(token.typ)) {
114
- map.set(token.typ, [token]);
143
+ if (!map.has(typ)) {
144
+ map.set(typ, [token]);
115
145
  }
116
146
  else {
117
- map.get(token.typ).push(token);
118
- }
119
- }
120
- return [...map].reduce((acc, curr) => {
121
- const token = curr[1].reduce((acc, curr) => doEvaluate(acc, curr, EnumToken.Add));
122
- if (token.typ != EnumToken.BinaryExpressionTokenType) {
123
- if ("val" in token && +token.val < 0) {
124
- acc.push({
125
- typ: EnumToken.Sub,
126
- [LOCSRCID]: token[LOCSRCID],
127
- [LOCSTA]: token[LOCSTA],
128
- [LOCEND]: token[LOCEND],
129
- [PARENT]: token[PARENT],
130
- }, {
131
- ...token,
132
- val: -token.val,
133
- [LOCSRCID]: token[LOCSRCID],
134
- [LOCSTA]: token[LOCSTA],
135
- [LOCEND]: token[LOCEND],
136
- [PARENT]: token[PARENT],
137
- });
138
- return acc;
147
+ map.get(typ).push(token);
148
+ }
149
+ }
150
+ const result = [];
151
+ for (const [key, values] of map) {
152
+ const token = values.reduce((acc, curr) => doEvaluate(arena, acc, curr, EnumToken.Add));
153
+ if (arena.dataTypes[token] != EnumToken.BinaryExpressionTokenType) {
154
+ // inlined: arena.isNumeric(token)
155
+ if (numericTokenTypes.has(arena.dataTypes[token]) && arena.numericValue[token] < 0) {
156
+ arena.numericValue[token] = -arena.numericValue[token];
157
+ result.push(tokenAt(arena, EnumToken.Sub, token), token);
158
+ continue;
139
159
  }
140
160
  }
141
- if (acc.length > 0 && curr[0] != EnumToken.ListToken) {
142
- if ("val" in token && +token.val == 0) {
143
- return acc;
161
+ if (result.length > 0 && key != EnumToken.ListToken) {
162
+ // inlined: arena.isNumeric(token)
163
+ if (numericTokenTypes.has(arena.dataTypes[token]) && arena.numericValue[token] == 0) {
164
+ continue;
144
165
  }
145
- acc.push({
146
- typ: EnumToken.Add,
147
- [LOCSRCID]: token[LOCSRCID],
148
- [LOCSTA]: token[LOCSTA],
149
- [LOCEND]: token[LOCEND],
150
- [PARENT]: token[PARENT],
151
- });
152
- }
153
- acc.push(token);
154
- return acc;
155
- }, []);
166
+ result.push(tokenAt(arena, EnumToken.Add, token));
167
+ }
168
+ result.push(token);
169
+ }
170
+ return result;
156
171
  }
157
172
  /**
158
173
  * evaluate arithmetic operation
174
+ * @param arena
159
175
  * @param l
160
176
  * @param r
161
177
  * @param op
162
178
  */
163
- function doEvaluate(l, r, op) {
164
- const defaultReturn = {
165
- typ: EnumToken.BinaryExpressionTokenType,
166
- op,
167
- l,
168
- r,
169
- [LOCSRCID]: l[LOCSRCID],
170
- [LOCSTA]: l[LOCSTA],
171
- [LOCEND]: r?.[LOCEND] ?? l[LOCEND],
172
- [PARENT]: l[PARENT],
179
+ function doEvaluate(arena, l, r, op) {
180
+ const defaultReturn = () => {
181
+ const token = arena.createBinaryExpression(op, l, r);
182
+ arena.spanLocation(token, l, r);
183
+ return token;
173
184
  };
174
- if (!isScalarToken(l) || !isScalarToken(r) || (l.typ == r.typ && "unit" in l && "unit" in r && l.unit != r.unit)) {
175
- return defaultReturn;
185
+ let lType = arena.dataTypes[l];
186
+ let rType = arena.dataTypes[r];
187
+ if (!isScalarToken(arena, l) ||
188
+ !isScalarToken(arena, r) ||
189
+ (lType == rType && arena.unit[l] != 0 && arena.unit[r] != 0 && arena.unit[l] != arena.unit[r])) {
190
+ return defaultReturn();
176
191
  }
177
- if (l.typ == EnumToken.IdenTokenType) {
178
- if (equalsIgnoreCase(l.val, "Infinity")) {
179
- Object.assign(l, { typ: EnumToken.InfinityTokenType });
192
+ if (lType == EnumToken.IdenTokenType) {
193
+ const val = arena.strings[arena.stringValue[l]];
194
+ if (equalsIgnoreCase(val, "Infinity")) {
195
+ arena.dataTypes[l] = lType = EnumToken.InfinityTokenType;
180
196
  }
181
- else if (equalsIgnoreCase(l.val, "-Infinity")) {
182
- Object.assign(l, { typ: EnumToken.NegativeInfinityTokenType });
197
+ else if (equalsIgnoreCase(val, "-Infinity")) {
198
+ arena.dataTypes[l] = lType = EnumToken.NegativeInfinityTokenType;
183
199
  }
184
- else if (equalsIgnoreCase(l.val, "NaN")) {
185
- Object.assign(l, { typ: EnumToken.NaNTokenType });
200
+ else if (equalsIgnoreCase(val, "NaN")) {
201
+ arena.dataTypes[l] = lType = EnumToken.NaNTokenType;
186
202
  }
187
203
  }
188
- if (r.typ == EnumToken.IdenTokenType) {
189
- if (equalsIgnoreCase(r.val, "Infinity")) {
190
- Object.assign(r, { typ: EnumToken.InfinityTokenType });
204
+ if (rType == EnumToken.IdenTokenType) {
205
+ const val = arena.strings[arena.stringValue[r]];
206
+ if (equalsIgnoreCase(val, "Infinity")) {
207
+ arena.dataTypes[r] = rType = EnumToken.InfinityTokenType;
191
208
  }
192
- else if (equalsIgnoreCase(r.val, "-Infinity")) {
193
- Object.assign(r, { typ: EnumToken.NegativeInfinityTokenType });
209
+ else if (equalsIgnoreCase(val, "-Infinity")) {
210
+ arena.dataTypes[r] = rType = EnumToken.NegativeInfinityTokenType;
194
211
  }
195
212
  }
196
213
  // https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/calc-keyword#notes
197
- if (l.typ == EnumToken.NaNTokenType || r.typ == EnumToken.NaNTokenType) {
198
- return computeNaN(l, r, defaultReturn);
214
+ if (lType == EnumToken.NaNTokenType || rType == EnumToken.NaNTokenType) {
215
+ return computeNaN(arena, l, r, defaultReturn);
199
216
  }
200
- if (l.typ == EnumToken.InfinityTokenType ||
201
- l.typ == EnumToken.NegativeInfinityTokenType ||
202
- r.typ == EnumToken.InfinityTokenType ||
203
- r.typ == EnumToken.NegativeInfinityTokenType) {
204
- return computeInfinity(l, r, op, defaultReturn);
217
+ if (lType == EnumToken.InfinityTokenType ||
218
+ lType == EnumToken.NegativeInfinityTokenType ||
219
+ rType == EnumToken.InfinityTokenType ||
220
+ rType == EnumToken.NegativeInfinityTokenType) {
221
+ return computeInfinity(arena, l, r, op, defaultReturn);
205
222
  }
206
- if (l.typ == EnumToken.FunctionTokenType || l.typ == EnumToken.MathFunctionTokenType) {
207
- const val = evaluateFunc(l);
223
+ if (lType == EnumToken.FunctionTokenType || lType == EnumToken.MathFunctionTokenType) {
224
+ const val = evaluateFunc(arena, l);
208
225
  if (val == null ||
209
226
  (val.length == 1 &&
210
- (val[0].typ == EnumToken.MathFunctionTokenType || val[0].typ == EnumToken.FunctionTokenType))) {
211
- return defaultReturn;
227
+ (arena.dataTypes[val[0]] == EnumToken.MathFunctionTokenType ||
228
+ arena.dataTypes[val[0]] == EnumToken.FunctionTokenType))) {
229
+ return defaultReturn();
212
230
  }
213
231
  if (val.length == 1) {
214
232
  l = val[0];
233
+ lType = arena.dataTypes[l];
215
234
  }
216
235
  }
217
- if (r.typ == EnumToken.FunctionTokenType || r.typ == EnumToken.MathFunctionTokenType) {
218
- const val = evaluateFunc(r);
236
+ if (rType == EnumToken.FunctionTokenType || rType == EnumToken.MathFunctionTokenType) {
237
+ const val = evaluateFunc(arena, r);
219
238
  if (val == null ||
220
239
  (val.length == 1 &&
221
- (val[0].typ == EnumToken.MathFunctionTokenType || val[0].typ == EnumToken.FunctionTokenType))) {
222
- return defaultReturn;
240
+ (arena.dataTypes[val[0]] == EnumToken.MathFunctionTokenType ||
241
+ arena.dataTypes[val[0]] == EnumToken.FunctionTokenType))) {
242
+ return defaultReturn();
223
243
  }
224
244
  if (val.length == 1) {
225
245
  r = val[0];
246
+ rType = arena.dataTypes[r];
226
247
  }
227
248
  }
228
249
  if (op == EnumToken.Add || op == EnumToken.Plus || op == EnumToken.Sub) {
229
- // @ts-ignore
230
- if (l.typ != r.typ) {
231
- return defaultReturn;
232
- }
233
- }
234
- let typ = l.typ == EnumToken.NumberTokenType || l.typ === EnumToken.IdenTokenType
235
- ? r.typ
236
- : r.typ == EnumToken.NumberTokenType
237
- ? l.typ
238
- : l.typ == EnumToken.PercentageTokenType
239
- ? r.typ
240
- : l.typ;
241
- // @ts-expect-error
242
- let v1 = l.val?.typ == EnumToken.FractionTokenType ? l.val : getValue(l);
243
- let v2 = r.val?.typ == EnumToken.FractionTokenType
244
- ? // @ts-expect-error
245
- r.val
246
- : getValue(r);
250
+ if (lType != rType) {
251
+ return defaultReturn();
252
+ }
253
+ }
254
+ let typ = lType == EnumToken.NumberTokenType || lType === EnumToken.IdenTokenType
255
+ ? rType
256
+ : rType == EnumToken.NumberTokenType
257
+ ? lType
258
+ : lType == EnumToken.PercentageTokenType
259
+ ? rType
260
+ : lType;
261
+ let v1 = numericValue(arena, l);
262
+ let v2 = numericValue(arena, r);
247
263
  if (op == EnumToken.Mul) {
248
- if (l.typ != EnumToken.NumberTokenType && r.typ != EnumToken.NumberTokenType) {
249
- if (typeof v1 == "number" && l.typ == EnumToken.PercentageTokenType) {
250
- v1 = {
251
- typ: EnumToken.FractionTokenType,
252
- l: {
253
- typ: EnumToken.NumberTokenType,
254
- val: v1,
255
- [LOCSRCID]: l[LOCSRCID],
256
- [LOCSTA]: l[LOCSTA],
257
- [LOCEND]: l[LOCEND],
258
- [PARENT]: l[PARENT],
259
- },
260
- r: {
261
- typ: EnumToken.NumberTokenType,
262
- val: 100,
263
- [LOCSRCID]: r[LOCSRCID],
264
- [LOCSTA]: r[LOCSTA],
265
- [LOCEND]: r[LOCEND],
266
- [PARENT]: r[PARENT],
267
- },
268
- };
264
+ if (lType != EnumToken.NumberTokenType && rType != EnumToken.NumberTokenType) {
265
+ if (typeof v1 == "number" && lType == EnumToken.PercentageTokenType) {
266
+ v1 = [v1, 100];
269
267
  }
270
- else if (typeof v2 == "number" && r.typ == EnumToken.PercentageTokenType) {
271
- v2 = {
272
- typ: EnumToken.FractionTokenType,
273
- l: {
274
- typ: EnumToken.NumberTokenType,
275
- val: v2,
276
- [LOCSRCID]: l[LOCSRCID],
277
- [LOCSTA]: l[LOCSTA],
278
- [LOCEND]: l[LOCEND],
279
- [PARENT]: l[PARENT],
280
- },
281
- r: {
282
- typ: EnumToken.NumberTokenType,
283
- val: 100,
284
- [LOCSRCID]: r[LOCSRCID],
285
- [LOCSTA]: r[LOCSTA],
286
- [LOCEND]: r[LOCEND],
287
- [PARENT]: r[PARENT],
288
- },
289
- };
268
+ else if (typeof v2 == "number" && rType == EnumToken.PercentageTokenType) {
269
+ v2 = [v2, 100];
290
270
  }
291
271
  }
292
272
  }
293
273
  if (op == EnumToken.Div && v2 == 0) {
294
274
  if (v1 == 0) {
295
- return Object.assign(l, { typ: EnumToken.IdenTokenType, val: "NaN" });
275
+ arena.resetToken(l, EnumToken.IdenTokenType, "NaN");
276
+ return l;
296
277
  }
297
278
  let sign = Math.sign(v1) * Math.sign(v2);
298
279
  sign = Object.is(sign, -0) ? -1 : 1;
299
- Object.assign(l, {
300
- typ: l.typ == EnumToken.IdenTokenType ? EnumToken.NumberTokenType : l.typ,
301
- val: Object.is(sign, -0) ? -1 : sign,
302
- });
303
- return defaultReturn;
280
+ arena.resetToken(l, lType == EnumToken.IdenTokenType ? EnumToken.NumberTokenType : lType, sign);
281
+ return defaultReturn();
304
282
  }
305
283
  // @ts-ignore
306
284
  const val = compute(v1, v2, op);
307
- const token = {
308
- ...(l.typ === EnumToken.NumberTokenType || l.typ === EnumToken.IdenTokenType ? r : l),
309
- typ,
310
- val /* : typeof val == 'number' ? minifyNumber(val) : val */,
311
- [LOCSRCID]: l[LOCSRCID],
312
- [LOCSTA]: l[LOCSTA],
313
- [LOCEND]: r?.[LOCEND] ?? l[LOCEND],
314
- [PARENT]: l[PARENT],
315
- };
316
- if (token.typ == EnumToken.IdenTokenType) {
317
- // @ts-ignore
318
- token.typ = EnumToken.NumberTokenType;
319
- }
285
+ // the result inherits the unit of the dimension operand
286
+ const base = lType === EnumToken.NumberTokenType || lType === EnumToken.IdenTokenType ? r : l;
287
+ const token = arena.createToken(typ == EnumToken.IdenTokenType ? EnumToken.NumberTokenType : typ);
288
+ arena.unit[token] = arena.unit[base];
289
+ setNumericValue(arena, token, val);
290
+ arena.spanLocation(token, l, r);
320
291
  return token;
321
292
  }
322
293
  /**
323
294
  *
295
+ * @param arena
324
296
  * @param l
325
297
  * @param r
326
298
  * @param defaultReturn
327
299
  * @returns
328
300
  */
329
- function computeNaN(l, r, defaultReturn) {
330
- if (l.typ == EnumToken.NaNTokenType) {
331
- if (r.typ == EnumToken.NumberTokenType ||
332
- // @ts-expect-error
333
- (r.typ == EnumToken.IdenTokenType && typeof Math[r.val.toUpperCase()] == "string")) {
301
+ function computeNaN(arena, l, r, defaultReturn) {
302
+ const lType = arena.dataTypes[l];
303
+ const rType = arena.dataTypes[r];
304
+ if (lType == EnumToken.NaNTokenType) {
305
+ if (rType == EnumToken.NumberTokenType ||
306
+ (rType == EnumToken.IdenTokenType &&
307
+ // @ts-expect-error
308
+ typeof Math[arena.strings[arena.stringValue[r]].toUpperCase()] == "string")) {
334
309
  return l;
335
310
  }
336
311
  // NaN Infinity e pi ...
337
- if (r.typ == EnumToken.IdenTokenType ||
338
- r.typ == EnumToken.InfinityTokenType ||
339
- r.typ == EnumToken.NegativeInfinityTokenType) {
312
+ if (rType == EnumToken.IdenTokenType ||
313
+ rType == EnumToken.InfinityTokenType ||
314
+ rType == EnumToken.NegativeInfinityTokenType) {
340
315
  return l;
341
316
  }
342
- if (typeof r.val == "number") {
343
- Object.assign(r, { val: 1 });
317
+ // inlined: arena.isNumeric(r)
318
+ if (numericTokenTypes.has(arena.dataTypes[r])) {
319
+ setNumericValue(arena, r, 1);
344
320
  }
345
- return r.typ == EnumToken.NaNTokenType ? l : defaultReturn;
321
+ return rType == EnumToken.NaNTokenType ? l : defaultReturn();
346
322
  }
347
- if (r.typ == EnumToken.NaNTokenType) {
348
- // if ((r as IdentToken).val == "NaN") {
349
- if (l.typ == EnumToken.NumberTokenType ||
350
- // @ts-expect-error
351
- (l.typ == EnumToken.IdenTokenType && typeof Math[l.val.toUpperCase()] == "string")) {
323
+ if (rType == EnumToken.NaNTokenType) {
324
+ if (lType == EnumToken.NumberTokenType ||
325
+ (lType == EnumToken.IdenTokenType &&
326
+ // @ts-expect-error
327
+ typeof Math[arena.strings[arena.stringValue[l]].toUpperCase()] == "string")) {
352
328
  return r;
353
329
  }
354
- if (typeof l.val == "number") {
355
- Object.assign(l, { val: 1 });
330
+ // inlined: arena.isNumeric(l)
331
+ if (numericTokenTypes.has(arena.dataTypes[l])) {
332
+ setNumericValue(arena, l, 1);
356
333
  }
357
- // return defaultReturn;
358
334
  }
359
- // }
360
- return defaultReturn;
335
+ return defaultReturn();
361
336
  }
362
337
  /**
363
338
  *
339
+ * @param arena
364
340
  * @param l
365
341
  * @param r
366
342
  * @param op
367
343
  * @param defaultReturn
368
344
  * @returns
369
345
  */
370
- function computeInfinity(l, r, op, defaultReturn) {
371
- if (l.typ == EnumToken.InfinityTokenType || l.typ == EnumToken.NegativeInfinityTokenType) {
372
- if (r.typ == EnumToken.InfinityTokenType || r.typ == EnumToken.NegativeInfinityTokenType) {
346
+ function computeInfinity(arena, l, r, op, defaultReturn) {
347
+ const lType = arena.dataTypes[l];
348
+ const rType = arena.dataTypes[r];
349
+ if (lType == EnumToken.InfinityTokenType || lType == EnumToken.NegativeInfinityTokenType) {
350
+ if (rType == EnumToken.InfinityTokenType || rType == EnumToken.NegativeInfinityTokenType) {
373
351
  // Infinity / Infinity Infinity - Infinity
374
- if (op == EnumToken.Div ||
375
- (op == EnumToken.Add && l.typ != r.typ) ||
376
- (op == EnumToken.Sub && l.typ == r.typ)) {
377
- return Object.assign(l, { typ: EnumToken.NaNTokenType });
352
+ if (op == EnumToken.Div || (op == EnumToken.Add && lType != rType) || (op == EnumToken.Sub && lType == rType)) {
353
+ arena.dataTypes[l] = EnumToken.NaNTokenType;
354
+ return l;
378
355
  }
379
- if (l.typ != r.typ) {
380
- l.typ = EnumToken.NegativeInfinityTokenType;
356
+ if (lType != rType) {
357
+ arena.dataTypes[l] = EnumToken.NegativeInfinityTokenType;
381
358
  }
382
359
  return l;
383
360
  }
384
- const value = getValue(r);
361
+ const value = getValue(arena, r);
385
362
  const sign = Math.sign(value);
386
- const isNumber = r.typ == EnumToken.NumberTokenType || r.typ == EnumToken.IdenTokenType;
363
+ const isNumber = rType == EnumToken.NumberTokenType || rType == EnumToken.IdenTokenType;
387
364
  // Infinity * 0
388
365
  if (value == 0 && op == EnumToken.Mul) {
389
- l.typ = EnumToken.NaNTokenType;
390
- return isNumber ? l : defaultReturn;
366
+ arena.dataTypes[l] = EnumToken.NaNTokenType;
367
+ return isNumber ? l : defaultReturn();
391
368
  }
392
369
  if (isNumber) {
393
- // @ts-ignore
394
- r.val /= r.val * sign;
370
+ arena.numericValue[r] /= arena.numericValue[r] * sign;
395
371
  }
396
372
  if (op == EnumToken.Div) {
397
- return isNumber
398
- ? Object.assign(r, { typ: EnumToken.NumberTokenType, val: sign < 0 ? -0 : 0 })
399
- : defaultReturn;
373
+ if (isNumber) {
374
+ arena.resetToken(r, EnumToken.NumberTokenType, sign < 0 ? -0 : 0);
375
+ return r;
376
+ }
377
+ return defaultReturn();
400
378
  }
401
379
  if (sign == -1) {
402
- l.typ =
403
- l.typ == EnumToken.InfinityTokenType
404
- ? EnumToken.NegativeInfinityTokenType
405
- : EnumToken.InfinityTokenType;
380
+ arena.dataTypes[l] =
381
+ lType == EnumToken.InfinityTokenType ? EnumToken.NegativeInfinityTokenType : EnumToken.InfinityTokenType;
406
382
  }
407
- return isNumber ? l : defaultReturn;
383
+ return isNumber ? l : defaultReturn();
408
384
  }
409
- if (r.typ == EnumToken.InfinityTokenType || r.typ == EnumToken.NegativeInfinityTokenType) {
410
- const value = getValue(l);
385
+ if (rType == EnumToken.InfinityTokenType || rType == EnumToken.NegativeInfinityTokenType) {
386
+ const value = getValue(arena, l);
411
387
  let sign = Math.sign(value);
412
- const isNumber = l.typ == EnumToken.NumberTokenType || l.typ == EnumToken.IdenTokenType;
388
+ const isNumber = lType == EnumToken.NumberTokenType || lType == EnumToken.IdenTokenType;
413
389
  if (isNumber) {
414
- Object.assign(l, { typ: EnumToken.NumberTokenType, val: sign });
390
+ arena.resetToken(l, EnumToken.NumberTokenType, sign);
415
391
  }
416
392
  if (value == 0 && op == EnumToken.Mul) {
417
- return Object.assign(l, { typ: EnumToken.NaNTokenType });
393
+ arena.dataTypes[l] = EnumToken.NaNTokenType;
394
+ return l;
418
395
  }
419
396
  if (op == EnumToken.Div) {
420
- return isNumber
421
- ? Object.assign(l, { typ: EnumToken.NumberTokenType, val: sign < 0 ? -0 : 0 })
422
- : defaultReturn;
397
+ if (isNumber) {
398
+ arena.resetToken(l, EnumToken.NumberTokenType, sign < 0 ? -0 : 0);
399
+ return l;
400
+ }
401
+ return defaultReturn();
423
402
  }
424
- return l.typ == isNumber || value == 0 ? r : defaultReturn;
403
+ return isNumber || value == 0 ? r : defaultReturn();
425
404
  }
426
- return defaultReturn;
405
+ return defaultReturn();
427
406
  }
428
- function getValue(t) {
429
- if (t.typ == EnumToken.IdenTokenType) {
407
+ /**
408
+ * numeric value of a scalar token
409
+ * @param arena
410
+ * @param t
411
+ */
412
+ function getValue(arena, t) {
413
+ const typ = arena.dataTypes[t];
414
+ if (typ == EnumToken.IdenTokenType) {
430
415
  // @ts-ignore
431
- return Math[t.val.toUpperCase()];
416
+ return Math[arena.strings[arena.stringValue[t]].toUpperCase()];
432
417
  }
433
- // @ts-ignore
434
- return t.typ == EnumToken.FractionTokenType ? t.l.val / t.r.val : +t.val;
418
+ if (typ == EnumToken.FractionTokenType) {
419
+ return arena.numericValue[arena.firstChild[t]] / arena.numericValue[arena.lastChild[t]];
420
+ }
421
+ // inlined: arena.isNumeric(t)
422
+ if (!numericTokenTypes.has(arena.dataTypes[t])) {
423
+ // not a number
424
+ return NaN;
425
+ }
426
+ const fraction = arena.getFraction(t);
427
+ if (fraction != 0) {
428
+ return arena.numericValue[arena.firstChild[fraction]] / arena.numericValue[arena.lastChild[fraction]];
429
+ }
430
+ return arena.numericValue[t];
435
431
  }
436
- function evaluateFunc(token) {
437
- const values = token.chi.slice();
438
- switch (token.val) {
432
+ /**
433
+ * convert an angle token to radians (in place)
434
+ * @param arena
435
+ * @param token
436
+ */
437
+ function toRadians(arena, token) {
438
+ if (arena.dataTypes[token] == EnumToken.NumberTokenType) {
439
+ arena.dataTypes[token] = EnumToken.AngleTokenType;
440
+ // inlined: arena.setUnitString(token, "rad")
441
+ arena.unit[token] = arena.internString("rad");
442
+ }
443
+ else if (arena.dataTypes[token] == EnumToken.AngleTokenType && arena.strings[arena.unit[token]] != "rad") {
444
+ switch (arena.strings[arena.unit[token]]) {
445
+ case "deg":
446
+ arena.numericValue[token] *= Math.PI / 180;
447
+ // inlined: arena.setUnitString(token, "rad")
448
+ arena.unit[token] = arena.internString("rad");
449
+ break;
450
+ case "grad":
451
+ arena.numericValue[token] *= Math.PI / 200;
452
+ // inlined: arena.setUnitString(token, "rad")
453
+ arena.unit[token] = arena.internString("rad");
454
+ break;
455
+ case "turn":
456
+ arena.numericValue[token] *= 2 * Math.PI;
457
+ // inlined: arena.setUnitString(token, "rad")
458
+ arena.unit[token] = arena.internString("rad");
459
+ break;
460
+ }
461
+ }
462
+ return token;
463
+ }
464
+ /**
465
+ * evaluate a math function
466
+ * @param arena
467
+ * @param token
468
+ */
469
+ function evaluateFunc(arena, token) {
470
+ const values = arena.tokens(token);
471
+ const name = arena.strings[arena.stringValue[token]];
472
+ switch (name) {
439
473
  case "sin":
440
474
  case "cos":
441
475
  case "tan":
442
476
  for (let i = 0; i < values.length; i++) {
443
- if (values[i].typ == EnumToken.NumberTokenType) {
444
- values[i] = Object.assign(values[i], { typ: EnumToken.AngleTokenType, unit: "rad" });
445
- }
446
- else if (values[i].typ == EnumToken.AngleTokenType && values[i].unit != "rad") {
447
- switch (values[i].unit) {
448
- case "deg":
449
- Object.assign(values[i], {
450
- unit: "rad",
451
- val: values[i].val * (Math.PI / 180),
452
- });
453
- break;
454
- case "grad":
455
- Object.assign(values[i], {
456
- unit: "rad",
457
- val: values[i].val * (Math.PI / 200),
458
- });
459
- break;
460
- case "turn":
461
- Object.assign(values[i], {
462
- unit: "rad",
463
- val: values[i].val * (2 * Math.PI),
464
- });
465
- break;
466
- }
467
- }
477
+ toRadians(arena, values[i]);
468
478
  }
469
479
  case "asin":
470
480
  case "acos":
@@ -472,177 +482,106 @@ function evaluateFunc(token) {
472
482
  case "abs":
473
483
  case "sign":
474
484
  case "sqrt":
475
- case "exp":
476
- const value = evaluate(values);
485
+ case "exp": {
486
+ const value = evaluateTokens(arena, values);
487
+ let val = arena.dataTypes[value[0]] == EnumToken.NumberTokenType ||
488
+ arena.dataTypes[value[0]] == EnumToken.AngleTokenType ||
489
+ arena.dataTypes[value[0]] == EnumToken.PercentageTokenType
490
+ ? getValue(arena, value[0])
491
+ : getValue(arena, value[0]);
477
492
  // @ts-ignore
478
- let val = value[0].typ == EnumToken.NumberTokenType ||
479
- value[0].typ == EnumToken.AngleTokenType ||
480
- value[0].typ == EnumToken.PercentageTokenType
481
- ? +value[0].val
482
- : // @ts-expect-error
483
- value[0].l.val / value[0].r.val;
493
+ const computed = Math[name](val);
484
494
  return [
485
- token.val == "asin" || token.val == "acos" || token.val == "atan"
486
- ? {
487
- typ: EnumToken.AngleTokenType,
488
- val: Math[token.val](val),
489
- unit: "rad",
490
- [LOCSRCID]: value[0][LOCSRCID],
491
- [LOCSTA]: value[0][LOCSTA],
492
- [LOCEND]: value[0][LOCEND],
493
- [PARENT]: value[0][PARENT],
494
- }
495
- : {
496
- typ: numbers.has(token.val) ? EnumToken.NumberTokenType : value[0].typ,
497
- val: Math[token.val](val),
498
- [LOCSRCID]: value[0][LOCSRCID],
499
- [LOCSTA]: value[0][LOCSTA],
500
- [LOCEND]: value[0][LOCEND],
501
- [PARENT]: value[0][PARENT],
502
- },
495
+ name == "asin" || name == "acos" || name == "atan"
496
+ ? tokenAt(arena, EnumToken.AngleTokenType, value[0], computed, "rad")
497
+ : tokenAt(arena, numbers.has(name) ? EnumToken.NumberTokenType : arena.dataTypes[value[0]], value[0], computed),
503
498
  ];
499
+ }
504
500
  case "hypot": {
505
- const chi = values.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(t.typ));
501
+ const chi = values.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(arena.dataTypes[t]));
506
502
  let all = [];
507
503
  let ref = chi[0];
508
504
  for (let i = 0; i < chi.length; i++) {
509
- // @ts-ignore
510
- const val = getValue(chi[i]);
505
+ const val = getValue(arena, chi[i]);
511
506
  if (Number.isNaN(val)) {
512
507
  return null;
513
508
  }
514
509
  all.push(val);
515
510
  }
516
- return [
517
- {
518
- ...ref,
519
- val: Math.hypot(...all),
520
- [LOCSRCID]: token[LOCSRCID],
521
- [LOCSTA]: token[LOCSTA],
522
- [LOCEND]: token[LOCEND],
523
- [PARENT]: token[PARENT],
524
- },
525
- ];
511
+ const result = arena.clone(ref, true);
512
+ setNumericValue(arena, result, Math.hypot(...all));
513
+ arena.copyLocation(result, token);
514
+ return [result];
526
515
  }
527
516
  case "mod":
528
517
  case "atan2":
529
518
  case "pow":
530
519
  case "rem": {
531
520
  {
532
- let typ = token.chi[0]?.typ;
533
- for (let i = 1; i < token.chi.length; i++) {
534
- if (token.chi[i].typ == EnumToken.WhitespaceTokenType ||
535
- token.chi[i].typ == EnumToken.CommaTokenType ||
536
- token.chi[i].typ == EnumToken.CommentTokenType) {
521
+ const first = arena.firstToken[token];
522
+ const typ = first == 0 ? 0 : arena.dataTypes[first];
523
+ for (let t = first == 0 ? 0 : arena.nextSibling[first]; t != 0; t = arena.nextSibling[t]) {
524
+ if (arena.dataTypes[t] == EnumToken.WhitespaceTokenType ||
525
+ arena.dataTypes[t] == EnumToken.CommaTokenType ||
526
+ arena.dataTypes[t] == EnumToken.CommentTokenType) {
537
527
  continue;
538
528
  }
539
- if (token.chi[i].typ != typ) {
529
+ if (arena.dataTypes[t] != typ) {
540
530
  return null;
541
531
  }
542
532
  }
543
533
  }
544
- const chi = values.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
545
- if (token.val == "atan2") {
534
+ const chi = values.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(arena.dataTypes[t]));
535
+ if (name == "atan2") {
546
536
  for (let i = 0; i < chi.length; i++) {
547
- if (chi[i].typ == EnumToken.NumberTokenType) {
548
- chi[i] = Object.assign(chi[i], { typ: EnumToken.AngleTokenType, unit: "rad" });
549
- }
550
- else if (chi[i].typ == EnumToken.AngleTokenType && chi[i].unit != "rad") {
551
- switch (chi[i].unit) {
552
- case "deg":
553
- Object.assign(chi[i], {
554
- unit: "rad",
555
- val: chi[i].val * (Math.PI / 180),
556
- });
557
- break;
558
- case "grad":
559
- Object.assign(chi[i], {
560
- unit: "rad",
561
- val: chi[i].val * (Math.PI / 200),
562
- });
563
- break;
564
- case "turn":
565
- Object.assign(chi[i], {
566
- unit: "rad",
567
- val: chi[i].val * (2 * Math.PI),
568
- });
569
- break;
570
- }
571
- }
537
+ toRadians(arena, chi[i]);
572
538
  }
573
539
  }
574
540
  // https://developer.mozilla.org/en-US/docs/Web/CSS/mod
575
- const v1 = evaluate([chi[0]]);
576
- const v2 = evaluate([chi[2]]);
577
- // @ts-ignore
578
- const val1 = getValue(v1[0]);
579
- // @ts-ignore
580
- const val2 = getValue(v2[0]);
581
- if (token.val == "rem") {
582
- return [
583
- {
584
- ...v1[0],
585
- val: +(val1 % val2).toFixed(rem(val1, val2)),
586
- },
587
- ];
541
+ const v1 = evaluateTokens(arena, [chi[0]]);
542
+ const v2 = evaluateTokens(arena, [chi[2]]);
543
+ const val1 = getValue(arena, v1[0]);
544
+ const val2 = getValue(arena, v2[0]);
545
+ const result = arena.clone(v1[0], true);
546
+ if (name == "rem") {
547
+ setNumericValue(arena, result, +(val1 % val2).toFixed(rem(val1, val2)));
548
+ return [result];
588
549
  }
589
- if (token.val == "pow") {
590
- return [
591
- {
592
- ...v1[0],
593
- val: Math.pow(val1, val2),
594
- [LOCSRCID]: token[LOCSRCID],
595
- [LOCSTA]: token[LOCSTA],
596
- [LOCEND]: token[LOCEND],
597
- [PARENT]: token[PARENT],
598
- },
599
- ];
550
+ arena.copyLocation(result, token);
551
+ if (name == "pow") {
552
+ setNumericValue(arena, result, Math.pow(val1, val2));
553
+ return [result];
600
554
  }
601
- if (token.val == "atan2") {
602
- return [
603
- {
604
- ...{},
605
- ...v1[0],
606
- typ: EnumToken.AngleTokenType,
607
- unit: "rad",
608
- val: Math.atan2(val1, val2),
609
- [LOCSRCID]: token[LOCSRCID],
610
- [LOCSTA]: token[LOCSTA],
611
- [LOCEND]: token[LOCEND],
612
- [PARENT]: token[PARENT],
613
- },
614
- ];
555
+ if (name == "atan2") {
556
+ arena.dataTypes[result] = EnumToken.AngleTokenType;
557
+ // inlined: arena.setUnitString(result, "rad")
558
+ arena.unit[result] = arena.internString("rad");
559
+ setNumericValue(arena, result, Math.atan2(val1, val2));
560
+ return [result];
615
561
  }
616
- return [
617
- {
618
- ...v1[0],
619
- val: val2 == 0 ? val1 : val1 - Math.floor(val1 / val2) * val2,
620
- [LOCSRCID]: token[LOCSRCID],
621
- [LOCSTA]: token[LOCSTA],
622
- [LOCEND]: token[LOCEND],
623
- [PARENT]: token[PARENT],
624
- },
625
- ];
562
+ setNumericValue(arena, result, val2 == 0 ? val1 : val1 - Math.floor(val1 / val2) * val2);
563
+ return [result];
626
564
  }
627
565
  case "clamp":
628
- token.chi = values;
566
+ arena.setTokenList(token, values);
629
567
  return [token];
630
568
  case "log":
631
569
  case "round":
632
570
  case "min":
633
571
  case "max": {
634
- const strategy = token.val == "round" && values[0]?.typ == EnumToken.IdenTokenType
635
- ? values.shift().val
572
+ const strategy = name == "round" && values.length > 0 && arena.dataTypes[values[0]] == EnumToken.IdenTokenType
573
+ ? arena.strings[arena.stringValue[values.shift()]]
636
574
  : null;
637
575
  const valuesMap = new Map();
638
576
  for (const curr of values) {
639
- if (curr.typ == EnumToken.CommaTokenType ||
640
- curr.typ == EnumToken.WhitespaceTokenType ||
641
- curr.typ == EnumToken.CommentTokenType) {
577
+ if (arena.dataTypes[curr] == EnumToken.CommaTokenType ||
578
+ arena.dataTypes[curr] == EnumToken.WhitespaceTokenType ||
579
+ arena.dataTypes[curr] == EnumToken.CommentTokenType) {
642
580
  continue;
643
581
  }
644
- const result = evaluate([curr]);
645
- const key = result[0].typ + ("unit" in result[0] ? result[0].unit : "");
582
+ // the argument is evaluated on a copy: the function is returned unchanged when it cannot be computed
583
+ const result = evaluateTokens(arena, [arena.clone(curr, true)]);
584
+ const key = arena.dataTypes[result[0]] + arena.strings[arena.unit[result[0]]];
646
585
  if (!valuesMap.has(key)) {
647
586
  valuesMap.set(key, []);
648
587
  }
@@ -650,40 +589,34 @@ function evaluateFunc(token) {
650
589
  }
651
590
  if (valuesMap.size == 1) {
652
591
  const values = valuesMap.values().next().value;
653
- if (token.val == "log") {
654
- const val1 = getValue(values[0]);
655
- const val2 = values.length == 2 ? getValue(values[1]) : null;
656
- return [
657
- {
658
- ...values[0],
659
- val: Math.log(val1) / Math.log(val2),
660
- [LOCSRCID]: token[LOCSRCID],
661
- [LOCSTA]: token[LOCSTA],
662
- [LOCEND]: token[LOCEND],
663
- [PARENT]: token[PARENT],
664
- },
665
- ];
592
+ if (name == "log") {
593
+ const val1 = getValue(arena, values[0]);
594
+ const val2 = values.length == 2 ? getValue(arena, values[1]) : null;
595
+ const result = arena.clone(values[0], true);
596
+ setNumericValue(arena, result, Math.log(val1) / Math.log(val2));
597
+ arena.copyLocation(result, token);
598
+ return [result];
666
599
  }
667
- if (token.val == "min" || token.val == "max") {
668
- let val = getValue(values[0]);
600
+ if (name == "min" || name == "max") {
601
+ let val = getValue(arena, values[0]);
669
602
  let val2 = val;
670
603
  let ret = values[0];
671
604
  for (const curr of values.slice(1)) {
672
- val2 = getValue(curr);
673
- if (val2 < val && token.val == "min") {
605
+ val2 = getValue(arena, curr);
606
+ if (val2 < val && name == "min") {
674
607
  val = val2;
675
608
  ret = curr;
676
609
  }
677
- else if (val2 > val && token.val == "max") {
610
+ else if (val2 > val && name == "max") {
678
611
  val = val2;
679
612
  ret = curr;
680
613
  }
681
614
  }
682
615
  return [ret];
683
616
  }
684
- if (token.val == "round") {
685
- let val = getValue(values[0]);
686
- let val2 = getValue(values[1]);
617
+ if (name == "round") {
618
+ let val = getValue(arena, values[0]);
619
+ let val2 = getValue(arena, values[1]);
687
620
  if (strategy == null || strategy == "down") {
688
621
  val = val - (val % val2);
689
622
  }
@@ -695,17 +628,10 @@ function evaluateFunc(token) {
695
628
  ? Math.round(val / val2) * val2
696
629
  : Math.ceil(val / val2) * val2;
697
630
  }
698
- // @ts-ignore
699
- return [
700
- {
701
- ...values[0],
702
- val,
703
- [LOCSRCID]: token[LOCSRCID],
704
- [LOCSTA]: token[LOCSTA],
705
- [LOCEND]: token[LOCEND],
706
- [PARENT]: token[PARENT],
707
- },
708
- ];
631
+ const result = arena.clone(values[0], true);
632
+ setNumericValue(arena, result, val);
633
+ arena.copyLocation(result, token);
634
+ return [result];
709
635
  }
710
636
  }
711
637
  }
@@ -714,26 +640,24 @@ function evaluateFunc(token) {
714
640
  }
715
641
  /**
716
642
  * convert BinaryExpression into an array
643
+ * @param arena
717
644
  * @param token
718
645
  */
719
- function inlineExpression(token) {
646
+ function inlineExpression(arena, token) {
720
647
  const result = [];
721
- if (token.typ == EnumToken.BinaryExpressionTokenType) {
722
- if ([EnumToken.Mul, EnumToken.Div].includes(token.op)) {
648
+ if (arena.dataTypes[token] == EnumToken.BinaryExpressionTokenType) {
649
+ const op = arena.numericValue[token];
650
+ if (op == EnumToken.Mul || op == EnumToken.Div) {
723
651
  result.push(token);
724
652
  }
725
653
  else {
726
- for (const child of inlineExpression(token.l)) {
654
+ const l = arena.field(token, TokenField.L);
655
+ const r = arena.field(token, TokenField.R);
656
+ for (const child of inlineExpression(arena, l)) {
727
657
  result.push(child);
728
658
  }
729
- result.push({
730
- typ: token.op,
731
- [LOCSRCID]: token[LOCSRCID],
732
- [LOCSTA]: token[LOCSTA],
733
- [LOCEND]: token[LOCEND],
734
- [PARENT]: token[PARENT],
735
- });
736
- for (const child of inlineExpression(token.r)) {
659
+ result.push(tokenAt(arena, op, token));
660
+ for (const child of inlineExpression(arena, r)) {
737
661
  result.push(child);
738
662
  }
739
663
  }
@@ -745,42 +669,50 @@ function inlineExpression(token) {
745
669
  }
746
670
  /**
747
671
  * evaluate expression
672
+ * @param arena
748
673
  * @param token
749
674
  */
750
- function evaluateExpression(token) {
751
- if (token.typ != EnumToken.BinaryExpressionTokenType) {
675
+ function evaluateExpression(arena, token) {
676
+ if (arena.dataTypes[token] != EnumToken.BinaryExpressionTokenType) {
752
677
  return token;
753
678
  }
754
- if (token.r.typ == EnumToken.BinaryExpressionTokenType) {
755
- token.r = (evaluateExpression(token.r));
679
+ let l = arena.field(token, TokenField.L);
680
+ let r = arena.field(token, TokenField.R);
681
+ if (arena.dataTypes[r] == EnumToken.BinaryExpressionTokenType) {
682
+ r = evaluateExpression(arena, r);
756
683
  }
757
- if (token.l.typ == EnumToken.BinaryExpressionTokenType) {
758
- token.l = (evaluateExpression(token.l));
684
+ if (arena.dataTypes[l] == EnumToken.BinaryExpressionTokenType) {
685
+ l = evaluateExpression(arena, l);
759
686
  }
760
- return doEvaluate(token.l, token.r, token.op);
687
+ return doEvaluate(arena, l, r, arena.numericValue[token]);
761
688
  }
762
- function isScalarToken(token) {
763
- return ("unit" in token ||
764
- token.typ == EnumToken.MathFunctionTokenType ||
765
- (token.typ == EnumToken.FunctionTokenType && mathFuncs.includes(token.val)) ||
766
- // @ts-ignore
767
- (token.typ == EnumToken.IdenTokenType && typeof Math[token.val.toUpperCase()] == "number") ||
768
- (token.typ == EnumToken.IdenTokenType && token.val == "NaN") ||
769
- (token.typ == EnumToken.IdenTokenType &&
770
- (equalsIgnoreCase(token.val, "Infinity") ||
771
- equalsIgnoreCase(token.val, "-Infinity"))) ||
772
- [EnumToken.NumberTokenType, EnumToken.FractionTokenType, EnumToken.PercentageTokenType].includes(token.typ) ||
773
- token.typ == EnumToken.NaNTokenType ||
774
- token.typ == EnumToken.InfinityTokenType ||
775
- token.typ == EnumToken.NegativeInfinityTokenType);
689
+ function isScalarToken(arena, token) {
690
+ const typ = arena.dataTypes[token];
691
+ return (arena.unit[token] != 0 ||
692
+ typ == EnumToken.MathFunctionTokenType ||
693
+ (typ == EnumToken.FunctionTokenType && mathFuncs.includes(arena.strings[arena.stringValue[token]])) ||
694
+ (typ == EnumToken.IdenTokenType &&
695
+ // @ts-ignore
696
+ typeof Math[arena.strings[arena.stringValue[token]].toUpperCase()] == "number") ||
697
+ (typ == EnumToken.IdenTokenType && arena.strings[arena.stringValue[token]] == "NaN") ||
698
+ (typ == EnumToken.IdenTokenType &&
699
+ (equalsIgnoreCase(arena.strings[arena.stringValue[token]], "Infinity") ||
700
+ equalsIgnoreCase(arena.strings[arena.stringValue[token]], "-Infinity"))) ||
701
+ typ == EnumToken.NumberTokenType ||
702
+ typ == EnumToken.FractionTokenType ||
703
+ typ == EnumToken.PercentageTokenType ||
704
+ typ == EnumToken.NaNTokenType ||
705
+ typ == EnumToken.InfinityTokenType ||
706
+ typ == EnumToken.NegativeInfinityTokenType);
776
707
  }
777
708
  /**
778
709
  *
779
710
  * generate a binary expression tree
711
+ * @param arena
780
712
  * @param tokens
781
713
  */
782
- function buildExpression(tokens) {
783
- return factor(factor(tokens.filter((t) => t.typ != EnumToken.WhitespaceTokenType), ["/", "*"]), ["+", "-"])[0];
714
+ function buildExpression(arena, tokens) {
715
+ return factor(arena, factor(arena, tokens.filter((t) => arena.dataTypes[t] != EnumToken.WhitespaceTokenType), ["/", "*"]), ["+", "-"])[0];
784
716
  }
785
717
  function getArithmeticOperation(op) {
786
718
  if (op == "+") {
@@ -797,64 +729,47 @@ function getArithmeticOperation(op) {
797
729
  /**
798
730
  *
799
731
  * generate a binary expression tree
732
+ * @param arena
800
733
  * @param token
801
734
  */
802
- function factorToken(token) {
803
- if (token.typ == EnumToken.ParensTokenType ||
804
- ((token.typ == EnumToken.MathFunctionTokenType || token.typ == EnumToken.FunctionTokenType) &&
805
- token.val == "calc")) {
806
- if ((token.typ == EnumToken.MathFunctionTokenType || token.typ == EnumToken.FunctionTokenType) &&
807
- token.val == "calc") {
808
- token = {
809
- ...token,
810
- typ: EnumToken.ParensTokenType,
811
- [LOCSRCID]: token[LOCSRCID],
812
- [LOCSTA]: token[LOCSTA],
813
- [LOCEND]: token[LOCEND],
814
- [PARENT]: token[PARENT],
815
- };
816
- // @ts-ignore
817
- delete token.val;
818
- }
819
- return buildExpression(token.chi);
735
+ function factorToken(arena, token) {
736
+ const typ = arena.dataTypes[token];
737
+ if (typ == EnumToken.ParensTokenType ||
738
+ ((typ == EnumToken.MathFunctionTokenType || typ == EnumToken.FunctionTokenType) &&
739
+ arena.strings[arena.stringValue[token]] == "calc")) {
740
+ return buildExpression(arena, arena.tokens(token));
820
741
  }
821
742
  return token;
822
743
  }
823
744
  /**
824
745
  * generate a binary expression tree
746
+ * @param arena
825
747
  * @param tokens
826
748
  * @param ops
827
749
  */
828
- function factor(tokens, ops) {
750
+ function factor(arena, tokens, ops) {
829
751
  let isOp;
830
752
  const opList = ops.map((x) => getArithmeticOperation(x));
831
753
  if (tokens.length == 1) {
832
- return [factorToken(tokens[0])];
754
+ return [factorToken(arena, tokens[0])];
833
755
  }
834
756
  for (let i = 0; i < tokens.length; i++) {
835
- if (tokens[i].typ == EnumToken.ListToken) {
836
- // @ts-ignore
837
- tokens.splice(i, 1, ...tokens[i].chi);
757
+ if (arena.dataTypes[tokens[i]] == EnumToken.ListToken) {
758
+ tokens.splice(i, 1, ...arena.tokens(tokens[i]));
838
759
  }
839
- isOp = opList.includes(tokens[i].typ === EnumToken.Plus ? EnumToken.Add : tokens[i].typ);
760
+ const typ = arena.dataTypes[tokens[i]];
761
+ isOp = opList.includes(typ === EnumToken.Plus ? EnumToken.Add : typ);
840
762
  if (isOp ||
841
- tokens[i].typ === EnumToken.Star ||
842
- // @ts-ignore
843
- (tokens[i].typ == EnumToken.LiteralTokenType && ops.includes(tokens[i].val))) {
844
- tokens.splice(i - 1, 3, {
845
- typ: EnumToken.BinaryExpressionTokenType,
846
- op: isOp
847
- ? tokens[i].typ === EnumToken.Plus
848
- ? EnumToken.Add
849
- : tokens[i].typ
850
- : getArithmeticOperation(tokens[i].val),
851
- l: factorToken(tokens[i - 1]),
852
- r: factorToken(tokens[i + 1]),
853
- [LOCSRCID]: tokens[i - 1][LOCSRCID],
854
- [LOCSTA]: tokens[i - 1][LOCSTA],
855
- [LOCEND]: tokens[i + 1][LOCEND],
856
- [PARENT]: tokens[i + 1][PARENT],
857
- });
763
+ typ === EnumToken.Star ||
764
+ (typ == EnumToken.LiteralTokenType && ops.includes(arena.strings[arena.stringValue[tokens[i]]]))) {
765
+ const op = isOp
766
+ ? typ === EnumToken.Plus
767
+ ? EnumToken.Add
768
+ : typ
769
+ : getArithmeticOperation(arena.strings[arena.stringValue[tokens[i]]]);
770
+ const expression = arena.createBinaryExpression(op, factorToken(arena, tokens[i - 1]), factorToken(arena, tokens[i + 1]));
771
+ arena.spanLocation(expression, tokens[i - 1], tokens[i + 1]);
772
+ tokens.splice(i - 1, 3, expression);
858
773
  i--;
859
774
  }
860
775
  }