@tbela99/css-parser 1.8.2 → 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.
- package/CHANGELOG.md +62 -0
- package/README.md +129 -31
- package/dist/index-umd-web.js +27383 -24083
- package/dist/index.d.ts +2737 -2200
- package/dist/package.json.js +2 -2
- package/dist/src/data/properties.json.js +1 -1
- package/dist/src/data/validation.json.js +1 -1
- package/dist/src/lib/ast/arena-node.js +548 -0
- package/dist/src/lib/ast/arena.js +3198 -0
- package/dist/src/lib/ast/features/calc.js +85 -76
- package/dist/src/lib/ast/features/if.js +155 -115
- package/dist/src/lib/ast/features/index.js +1 -1
- package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
- package/dist/src/lib/ast/features/prefix.js +184 -183
- package/dist/src/lib/ast/features/shorthand.js +40 -46
- package/dist/src/lib/ast/features/transform.js +24 -24
- package/dist/src/lib/ast/features/type.js +1 -1
- package/dist/src/lib/ast/fingerprint.js +333 -0
- package/dist/src/lib/ast/math/expression.js +458 -543
- package/dist/src/lib/ast/math/math.js +19 -41
- package/dist/src/lib/ast/minify.js +590 -647
- package/dist/src/lib/ast/transform/compute.js +82 -85
- package/dist/src/lib/ast/transform/matrix.js +21 -38
- package/dist/src/lib/ast/transform/minify.js +82 -193
- package/dist/src/lib/ast/transform/perspective.js +2 -3
- package/dist/src/lib/ast/transform/rotate.js +1 -1
- package/dist/src/lib/ast/transform/scale.js +1 -1
- package/dist/src/lib/ast/transform/skew.js +1 -1
- package/dist/src/lib/ast/transform/translate.js +1 -1
- package/dist/src/lib/ast/transform/utils.js +2 -2
- package/dist/src/lib/ast/types.js +263 -258
- package/dist/src/lib/ast/walk.js +246 -610
- package/dist/src/lib/fs/resolve.js +1 -1
- package/dist/src/lib/parser/declaration/list.js +223 -139
- package/dist/src/lib/parser/declaration/map.js +251 -238
- package/dist/src/lib/parser/declaration/set.js +74 -66
- package/dist/src/lib/parser/linesmap.js +1 -1
- package/dist/src/lib/parser/parse.js +1379 -1658
- package/dist/src/lib/parser/source.js +7 -4
- package/dist/src/lib/parser/tokenize.js +406 -199
- package/dist/src/lib/parser/utils/angle.js +11 -13
- package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
- package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
- package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
- package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
- package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
- package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
- package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
- package/dist/src/lib/parser/utils/at-rule.js +5 -5
- package/dist/src/lib/parser/utils/base64.js +2 -2
- package/dist/src/lib/parser/utils/cache.js +1 -1
- package/dist/src/lib/parser/utils/config.js +1 -1
- package/dist/src/lib/parser/utils/declaration.js +233 -234
- package/dist/src/lib/parser/utils/eq.js +50 -3
- package/dist/src/lib/parser/utils/hash.js +2 -35
- package/dist/src/lib/parser/utils/selector.js +309 -387
- package/dist/src/lib/parser/utils/text.js +18 -4
- package/dist/src/lib/parser/utils/token.js +16 -69
- package/dist/src/lib/parser/utils/type.js +37 -24
- package/dist/src/lib/printer/render.js +965 -511
- package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
- package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
- package/dist/src/lib/syntax/color/a98rgb.js +1 -1
- package/dist/src/lib/syntax/color/alpha.js +36 -37
- package/dist/src/lib/syntax/color/cmyk.js +35 -52
- package/dist/src/lib/syntax/color/color-mix.js +96 -132
- package/dist/src/lib/syntax/color/color.js +222 -231
- package/dist/src/lib/syntax/color/hex.js +45 -55
- package/dist/src/lib/syntax/color/hsl.js +54 -62
- package/dist/src/lib/syntax/color/hwb.js +60 -68
- package/dist/src/lib/syntax/color/lab.js +59 -67
- package/dist/src/lib/syntax/color/lch.js +59 -67
- package/dist/src/lib/syntax/color/oklab.js +60 -68
- package/dist/src/lib/syntax/color/oklch.js +60 -67
- package/dist/src/lib/syntax/color/p3.js +1 -1
- package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
- package/dist/src/lib/syntax/color/rec2020.js +1 -1
- package/dist/src/lib/syntax/color/relative-color.js +94 -144
- package/dist/src/lib/syntax/color/rgb.js +54 -57
- package/dist/src/lib/syntax/color/srgb.js +55 -53
- package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
- package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
- package/dist/src/lib/syntax/color/utils/components.js +19 -20
- package/dist/src/lib/syntax/color/utils/distance.js +15 -13
- package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
- package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
- package/dist/src/lib/syntax/color/xyz.js +1 -1
- package/dist/src/lib/syntax/color/xyzd50.js +1 -1
- package/dist/src/lib/syntax/constants.js +2 -16
- package/dist/src/lib/syntax/syntax.js +301 -407
- package/dist/src/lib/validation/config.js +1 -1
- package/dist/src/lib/validation/match.js +959 -723
- package/dist/src/lib/validation/parser/parse.js +230 -112
- package/dist/src/lib/validation/parser/typedef.js +1 -1
- package/dist/src/lib/validation/utils/list.js +27 -12
- package/dist/src/node.js +12 -72
- package/dist/src/types.js +1 -1
- package/dist/src/utils/sync.js +9 -6
- package/dist/src/web.js +11 -73
- package/package.json +9 -10
- package/dist/index.cjs +0 -35870
- package/dist/src/lib/ast/clone.js +0 -51
- package/dist/src/lib/ast/expand.js +0 -311
- package/dist/src/lib/ast/find.js +0 -192
- 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:
|
|
5
|
+
Version: 2.0.0
|
|
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
|
|
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
|
-
*
|
|
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
|
|
67
|
+
function evaluateTokens(arena, tokens) {
|
|
19
68
|
let nodes;
|
|
20
69
|
if (tokens.length == 1 &&
|
|
21
|
-
(tokens[0]
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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 (
|
|
28
|
-
|
|
79
|
+
if (typ == EnumToken.WhitespaceTokenType || typ == EnumToken.CommaTokenType) {
|
|
80
|
+
continue;
|
|
29
81
|
}
|
|
30
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
|
41
|
-
|
|
89
|
+
for (const token of evaluateTokens(arena, chi[i])) {
|
|
90
|
+
children.push(token);
|
|
42
91
|
}
|
|
43
|
-
|
|
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]
|
|
50
|
-
result[0]
|
|
51
|
-
result[0]
|
|
52
|
-
return result[0]
|
|
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]
|
|
60
|
-
return inlineExpression(nodes[0]);
|
|
108
|
+
if (arena.dataTypes[nodes[0]] == EnumToken.BinaryExpressionTokenType) {
|
|
109
|
+
return inlineExpression(arena, nodes[0]);
|
|
61
110
|
}
|
|
62
|
-
|
|
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].
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
128
|
+
typ = arena.dataTypes[token];
|
|
129
|
+
if (typ == EnumToken.Add || typ == EnumToken.Plus) {
|
|
88
130
|
continue;
|
|
89
131
|
}
|
|
90
|
-
if (
|
|
91
|
-
if (!isScalarToken(nodes[i + 1])) {
|
|
92
|
-
token =
|
|
93
|
-
|
|
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(
|
|
114
|
-
map.set(
|
|
143
|
+
if (!map.has(typ)) {
|
|
144
|
+
map.set(typ, [token]);
|
|
115
145
|
}
|
|
116
146
|
else {
|
|
117
|
-
map.get(
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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 (
|
|
142
|
-
|
|
143
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
|
|
175
|
-
|
|
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 (
|
|
178
|
-
|
|
179
|
-
|
|
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(
|
|
182
|
-
|
|
197
|
+
else if (equalsIgnoreCase(val, "-Infinity")) {
|
|
198
|
+
arena.dataTypes[l] = lType = EnumToken.NegativeInfinityTokenType;
|
|
183
199
|
}
|
|
184
|
-
else if (equalsIgnoreCase(
|
|
185
|
-
|
|
200
|
+
else if (equalsIgnoreCase(val, "NaN")) {
|
|
201
|
+
arena.dataTypes[l] = lType = EnumToken.NaNTokenType;
|
|
186
202
|
}
|
|
187
203
|
}
|
|
188
|
-
if (
|
|
189
|
-
|
|
190
|
-
|
|
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(
|
|
193
|
-
|
|
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 (
|
|
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 (
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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 (
|
|
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]
|
|
211
|
-
|
|
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 (
|
|
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]
|
|
222
|
-
|
|
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
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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 (
|
|
249
|
-
if (typeof v1 == "number" &&
|
|
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" &&
|
|
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
|
-
|
|
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
|
-
|
|
300
|
-
|
|
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
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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 (
|
|
338
|
-
|
|
339
|
-
|
|
312
|
+
if (rType == EnumToken.IdenTokenType ||
|
|
313
|
+
rType == EnumToken.InfinityTokenType ||
|
|
314
|
+
rType == EnumToken.NegativeInfinityTokenType) {
|
|
340
315
|
return l;
|
|
341
316
|
}
|
|
342
|
-
|
|
343
|
-
|
|
317
|
+
// inlined: arena.isNumeric(r)
|
|
318
|
+
if (numericTokenTypes.has(arena.dataTypes[r])) {
|
|
319
|
+
setNumericValue(arena, r, 1);
|
|
344
320
|
}
|
|
345
|
-
return
|
|
321
|
+
return rType == EnumToken.NaNTokenType ? l : defaultReturn();
|
|
346
322
|
}
|
|
347
|
-
if (
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
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
|
-
|
|
355
|
-
|
|
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
|
-
|
|
372
|
-
|
|
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
|
-
|
|
376
|
-
|
|
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 (
|
|
380
|
-
l
|
|
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 =
|
|
363
|
+
const isNumber = rType == EnumToken.NumberTokenType || rType == EnumToken.IdenTokenType;
|
|
387
364
|
// Infinity * 0
|
|
388
365
|
if (value == 0 && op == EnumToken.Mul) {
|
|
389
|
-
l
|
|
390
|
-
return isNumber ? l : defaultReturn;
|
|
366
|
+
arena.dataTypes[l] = EnumToken.NaNTokenType;
|
|
367
|
+
return isNumber ? l : defaultReturn();
|
|
391
368
|
}
|
|
392
369
|
if (isNumber) {
|
|
393
|
-
|
|
394
|
-
r.val /= r.val * sign;
|
|
370
|
+
arena.numericValue[r] /= arena.numericValue[r] * sign;
|
|
395
371
|
}
|
|
396
372
|
if (op == EnumToken.Div) {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
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
|
|
403
|
-
|
|
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 (
|
|
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 =
|
|
388
|
+
const isNumber = lType == EnumToken.NumberTokenType || lType == EnumToken.IdenTokenType;
|
|
413
389
|
if (isNumber) {
|
|
414
|
-
|
|
390
|
+
arena.resetToken(l, EnumToken.NumberTokenType, sign);
|
|
415
391
|
}
|
|
416
392
|
if (value == 0 && op == EnumToken.Mul) {
|
|
417
|
-
|
|
393
|
+
arena.dataTypes[l] = EnumToken.NaNTokenType;
|
|
394
|
+
return l;
|
|
418
395
|
}
|
|
419
396
|
if (op == EnumToken.Div) {
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
397
|
+
if (isNumber) {
|
|
398
|
+
arena.resetToken(l, EnumToken.NumberTokenType, sign < 0 ? -0 : 0);
|
|
399
|
+
return l;
|
|
400
|
+
}
|
|
401
|
+
return defaultReturn();
|
|
423
402
|
}
|
|
424
|
-
return
|
|
403
|
+
return isNumber || value == 0 ? r : defaultReturn();
|
|
425
404
|
}
|
|
426
|
-
return defaultReturn;
|
|
405
|
+
return defaultReturn();
|
|
427
406
|
}
|
|
428
|
-
|
|
429
|
-
|
|
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.
|
|
416
|
+
return Math[arena.strings[arena.stringValue[t]].toUpperCase()];
|
|
432
417
|
}
|
|
433
|
-
|
|
434
|
-
|
|
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
|
-
|
|
437
|
-
|
|
438
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
486
|
-
?
|
|
487
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
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
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
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 (
|
|
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
|
|
545
|
-
if (
|
|
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
|
-
|
|
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 =
|
|
576
|
-
const v2 =
|
|
577
|
-
|
|
578
|
-
const
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
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
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
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 (
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
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
|
-
|
|
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
|
|
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 =
|
|
635
|
-
? values.shift()
|
|
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
|
|
640
|
-
curr
|
|
641
|
-
curr
|
|
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
|
-
|
|
645
|
-
const
|
|
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 (
|
|
654
|
-
const val1 = getValue(values[0]);
|
|
655
|
-
const val2 = values.length == 2 ? getValue(values[1]) : null;
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
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 (
|
|
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 &&
|
|
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 &&
|
|
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 (
|
|
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
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
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
|
|
722
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
675
|
+
function evaluateExpression(arena, token) {
|
|
676
|
+
if (arena.dataTypes[token] != EnumToken.BinaryExpressionTokenType) {
|
|
752
677
|
return token;
|
|
753
678
|
}
|
|
754
|
-
|
|
755
|
-
|
|
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 (
|
|
758
|
-
|
|
684
|
+
if (arena.dataTypes[l] == EnumToken.BinaryExpressionTokenType) {
|
|
685
|
+
l = evaluateExpression(arena, l);
|
|
759
686
|
}
|
|
760
|
-
return doEvaluate(
|
|
687
|
+
return doEvaluate(arena, l, r, arena.numericValue[token]);
|
|
761
688
|
}
|
|
762
|
-
function isScalarToken(token) {
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
(
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
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
|
|
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
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
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]
|
|
836
|
-
|
|
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
|
-
|
|
760
|
+
const typ = arena.dataTypes[tokens[i]];
|
|
761
|
+
isOp = opList.includes(typ === EnumToken.Plus ? EnumToken.Add : typ);
|
|
840
762
|
if (isOp ||
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
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
|
}
|