@gridsheet/react-core 0.12.4-rc.1 → 0.12.4
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/dist/components/Cell.js +185 -197
- package/dist/components/Cell.js.map +1 -1
- package/dist/components/ContextMenu.js +198 -208
- package/dist/components/ContextMenu.js.map +1 -1
- package/dist/components/Editor.d.ts.map +1 -1
- package/dist/components/Editor.js +262 -275
- package/dist/components/Editor.js.map +1 -1
- package/dist/components/Emitter.js +29 -39
- package/dist/components/Emitter.js.map +1 -1
- package/dist/components/GridSheet.js +109 -119
- package/dist/components/GridSheet.js.map +1 -1
- package/dist/components/HeaderLeftCell.js +61 -72
- package/dist/components/HeaderLeftCell.js.map +1 -1
- package/dist/components/HeaderTopCell.js +62 -73
- package/dist/components/HeaderTopCell.js.map +1 -1
- package/dist/components/Resizer.js +79 -90
- package/dist/components/Resizer.js.map +1 -1
- package/dist/components/SearchBox.d.ts.map +1 -1
- package/dist/components/SearchBox.js +54 -64
- package/dist/components/SearchBox.js.map +1 -1
- package/dist/components/StoreInitializer.js +71 -81
- package/dist/components/StoreInitializer.js.map +1 -1
- package/dist/components/Tabular.d.ts +2 -2
- package/dist/components/Tabular.d.ts.map +1 -1
- package/dist/components/Tabular.js +85 -95
- package/dist/components/Tabular.js.map +1 -1
- package/dist/constants.js +26 -36
- package/dist/constants.js.map +1 -1
- package/dist/formula/evaluator.d.ts +1 -1
- package/dist/formula/evaluator.js +490 -500
- package/dist/formula/evaluator.js.map +1 -1
- package/dist/formula/functions/__base.js +17 -27
- package/dist/formula/functions/__base.js.map +1 -1
- package/dist/formula/functions/__utils.js +103 -113
- package/dist/formula/functions/__utils.js.map +1 -1
- package/dist/formula/functions/abs.js +21 -31
- package/dist/formula/functions/abs.js.map +1 -1
- package/dist/formula/functions/abs.spec.js +25 -35
- package/dist/formula/functions/abs.spec.js.map +1 -1
- package/dist/formula/functions/acos.js +28 -38
- package/dist/formula/functions/acos.js.map +1 -1
- package/dist/formula/functions/add.js +47 -57
- package/dist/formula/functions/add.js.map +1 -1
- package/dist/formula/functions/and.js +28 -38
- package/dist/formula/functions/and.js.map +1 -1
- package/dist/formula/functions/asin.js +28 -38
- package/dist/formula/functions/asin.js.map +1 -1
- package/dist/formula/functions/atan.js +26 -36
- package/dist/formula/functions/atan.js.map +1 -1
- package/dist/formula/functions/atan2.js +32 -42
- package/dist/formula/functions/atan2.js.map +1 -1
- package/dist/formula/functions/average.js +40 -50
- package/dist/formula/functions/average.js.map +1 -1
- package/dist/formula/functions/col.js +28 -38
- package/dist/formula/functions/col.js.map +1 -1
- package/dist/formula/functions/concat.js +27 -37
- package/dist/formula/functions/concat.js.map +1 -1
- package/dist/formula/functions/concatenate.js +25 -35
- package/dist/formula/functions/concatenate.js.map +1 -1
- package/dist/formula/functions/cos.js +26 -36
- package/dist/formula/functions/cos.js.map +1 -1
- package/dist/formula/functions/count.js +35 -45
- package/dist/formula/functions/count.js.map +1 -1
- package/dist/formula/functions/counta.js +35 -45
- package/dist/formula/functions/counta.js.map +1 -1
- package/dist/formula/functions/countif.js +30 -40
- package/dist/formula/functions/countif.js.map +1 -1
- package/dist/formula/functions/divide.js +32 -42
- package/dist/formula/functions/divide.js.map +1 -1
- package/dist/formula/functions/eq.js +25 -35
- package/dist/formula/functions/eq.js.map +1 -1
- package/dist/formula/functions/exp.js +28 -38
- package/dist/formula/functions/exp.js.map +1 -1
- package/dist/formula/functions/gt.js +27 -37
- package/dist/formula/functions/gt.js.map +1 -1
- package/dist/formula/functions/gte.js +27 -37
- package/dist/formula/functions/gte.js.map +1 -1
- package/dist/formula/functions/hlookup.js +67 -77
- package/dist/formula/functions/hlookup.js.map +1 -1
- package/dist/formula/functions/if.js +36 -46
- package/dist/formula/functions/if.js.map +1 -1
- package/dist/formula/functions/iferror.js +38 -48
- package/dist/formula/functions/iferror.js.map +1 -1
- package/dist/formula/functions/iferror.spec.js +53 -63
- package/dist/formula/functions/iferror.spec.js.map +1 -1
- package/dist/formula/functions/len.js +26 -36
- package/dist/formula/functions/len.js.map +1 -1
- package/dist/formula/functions/lenb.js +26 -36
- package/dist/formula/functions/lenb.js.map +1 -1
- package/dist/formula/functions/ln.js +28 -38
- package/dist/formula/functions/ln.js.map +1 -1
- package/dist/formula/functions/log.js +33 -43
- package/dist/formula/functions/log.js.map +1 -1
- package/dist/formula/functions/log10.js +28 -38
- package/dist/formula/functions/log10.js.map +1 -1
- package/dist/formula/functions/lt.js +27 -37
- package/dist/formula/functions/lt.js.map +1 -1
- package/dist/formula/functions/lte.js +27 -37
- package/dist/formula/functions/lte.js.map +1 -1
- package/dist/formula/functions/max.js +42 -52
- package/dist/formula/functions/max.js.map +1 -1
- package/dist/formula/functions/min.js +42 -52
- package/dist/formula/functions/min.js.map +1 -1
- package/dist/formula/functions/minus.js +47 -57
- package/dist/formula/functions/minus.js.map +1 -1
- package/dist/formula/functions/mod.js +30 -40
- package/dist/formula/functions/mod.js.map +1 -1
- package/dist/formula/functions/mod.spec.js +47 -57
- package/dist/formula/functions/mod.spec.js.map +1 -1
- package/dist/formula/functions/multiply.js +27 -37
- package/dist/formula/functions/multiply.js.map +1 -1
- package/dist/formula/functions/ne.js +25 -35
- package/dist/formula/functions/ne.js.map +1 -1
- package/dist/formula/functions/not.js +30 -40
- package/dist/formula/functions/not.js.map +1 -1
- package/dist/formula/functions/now.js +21 -31
- package/dist/formula/functions/now.js.map +1 -1
- package/dist/formula/functions/or.js +28 -38
- package/dist/formula/functions/or.js.map +1 -1
- package/dist/formula/functions/pi.js +19 -29
- package/dist/formula/functions/pi.js.map +1 -1
- package/dist/formula/functions/power.js +24 -34
- package/dist/formula/functions/power.js.map +1 -1
- package/dist/formula/functions/product.js +37 -47
- package/dist/formula/functions/product.js.map +1 -1
- package/dist/formula/functions/radians.js +26 -36
- package/dist/formula/functions/radians.js.map +1 -1
- package/dist/formula/functions/rand.js +19 -29
- package/dist/formula/functions/rand.js.map +1 -1
- package/dist/formula/functions/round.js +34 -44
- package/dist/formula/functions/round.js.map +1 -1
- package/dist/formula/functions/rounddown.js +34 -44
- package/dist/formula/functions/rounddown.js.map +1 -1
- package/dist/formula/functions/roundup.js +34 -44
- package/dist/formula/functions/roundup.js.map +1 -1
- package/dist/formula/functions/row.js +28 -38
- package/dist/formula/functions/row.js.map +1 -1
- package/dist/formula/functions/sin.js +26 -36
- package/dist/formula/functions/sin.js.map +1 -1
- package/dist/formula/functions/sqrt.js +28 -38
- package/dist/formula/functions/sqrt.js.map +1 -1
- package/dist/formula/functions/sum.js +42 -52
- package/dist/formula/functions/sum.js.map +1 -1
- package/dist/formula/functions/sum.spec.js +35 -45
- package/dist/formula/functions/sum.spec.js.map +1 -1
- package/dist/formula/functions/sumif.js +56 -66
- package/dist/formula/functions/sumif.js.map +1 -1
- package/dist/formula/functions/tan.js +26 -36
- package/dist/formula/functions/tan.js.map +1 -1
- package/dist/formula/functions/uminus.js +23 -33
- package/dist/formula/functions/uminus.js.map +1 -1
- package/dist/formula/functions/vlookup.js +67 -77
- package/dist/formula/functions/vlookup.js.map +1 -1
- package/dist/formula/mapping.js +116 -126
- package/dist/formula/mapping.js.map +1 -1
- package/dist/formula/solver.js +61 -71
- package/dist/formula/solver.js.map +1 -1
- package/dist/index.js +31 -41
- package/dist/index.js.map +1 -1
- package/dist/lib/autofill.js +334 -344
- package/dist/lib/autofill.js.map +1 -1
- package/dist/lib/clipboard.js +45 -55
- package/dist/lib/clipboard.js.map +1 -1
- package/dist/lib/converters.js +130 -140
- package/dist/lib/converters.js.map +1 -1
- package/dist/lib/hooks.d.ts +1 -0
- package/dist/lib/hooks.d.ts.map +1 -0
- package/dist/lib/hooks.js +2 -0
- package/dist/lib/hooks.js.map +1 -0
- package/dist/lib/structs.js +254 -264
- package/dist/lib/structs.js.map +1 -1
- package/dist/lib/table.d.ts +1 -3
- package/dist/lib/table.d.ts.map +1 -1
- package/dist/lib/table.js +897 -911
- package/dist/lib/table.js.map +1 -1
- package/dist/lib/time.d.ts.map +1 -1
- package/dist/lib/time.js +59 -69
- package/dist/lib/time.js.map +1 -1
- package/dist/lib/virtualization.d.ts +3 -21
- package/dist/lib/virtualization.d.ts.map +1 -1
- package/dist/lib/virtualization.js +115 -125
- package/dist/lib/virtualization.js.map +1 -1
- package/dist/parsers/core.js +109 -119
- package/dist/parsers/core.js.map +1 -1
- package/dist/renderers/checkbox.js +11 -21
- package/dist/renderers/checkbox.js.map +1 -1
- package/dist/renderers/core.d.ts +1 -1
- package/dist/renderers/core.d.ts.map +1 -1
- package/dist/renderers/core.js +129 -139
- package/dist/renderers/core.js.map +1 -1
- package/dist/renderers/thousand_separator.js +15 -25
- package/dist/renderers/thousand_separator.js.map +1 -1
- package/dist/store/actions.d.ts.map +1 -1
- package/dist/store/actions.js +484 -498
- package/dist/store/actions.js.map +1 -1
- package/dist/store/helpers.js +78 -88
- package/dist/store/helpers.js.map +1 -1
- package/dist/store/index.js +5 -15
- package/dist/store/index.js.map +1 -1
- package/dist/styles/embedder.js +17 -27
- package/dist/styles/embedder.js.map +1 -1
- package/dist/styles/minified.js +6 -16
- package/dist/styles/minified.js.map +1 -1
- package/dist/types.d.ts +6 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -12
- package/dist/utils.js +9 -19
- package/dist/utils.js.map +1 -1
- package/package.json +9 -2
|
@@ -1,567 +1,557 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.convertFormulaAbsolute = exports.Parser = exports.Lexer = exports.Token = exports.Function = exports.IdRange = exports.Id = exports.Range = exports.Ref = exports.InvalidRef = exports.Unreferenced = exports.Value = exports.FormulaError = void 0;
|
|
13
|
-
const structs_1 = require("../lib/structs");
|
|
14
|
-
const converters_1 = require("../lib/converters");
|
|
15
|
-
// strip sharp and dollars
|
|
16
|
-
const getId = (idString, stripAbsolute = true) => {
|
|
17
|
-
let id = idString.slice(1);
|
|
18
|
-
if (stripAbsolute && id.startsWith("$")) {
|
|
19
|
-
id = id.slice(1);
|
|
20
|
-
}
|
|
21
|
-
if (stripAbsolute && id.endsWith("$")) {
|
|
22
|
-
id = id.slice(0, -1);
|
|
23
|
-
}
|
|
24
|
-
return id;
|
|
25
|
-
};
|
|
26
|
-
class FormulaError {
|
|
27
|
-
constructor(code, message, error) {
|
|
28
|
-
this.code = code;
|
|
29
|
-
this.message = message;
|
|
30
|
-
this.error = error;
|
|
31
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertFormulaAbsolute = exports.Parser = exports.Lexer = exports.Token = exports.Function = exports.IdRange = exports.Id = exports.Range = exports.Ref = exports.InvalidRef = exports.Unreferenced = exports.Value = exports.FormulaError = void 0;
|
|
4
|
+
const structs_1 = require("../lib/structs");
|
|
5
|
+
const converters_1 = require("../lib/converters");
|
|
6
|
+
// strip sharp and dollars
|
|
7
|
+
const getId = (idString, stripAbsolute = true) => {
|
|
8
|
+
let id = idString.slice(1);
|
|
9
|
+
if (stripAbsolute && id.startsWith("$")) {
|
|
10
|
+
id = id.slice(1);
|
|
32
11
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
constructor(value) {
|
|
36
|
-
this.value = value;
|
|
37
|
-
}
|
|
12
|
+
if (stripAbsolute && id.endsWith("$")) {
|
|
13
|
+
id = id.slice(0, -1);
|
|
38
14
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
15
|
+
return id;
|
|
16
|
+
};
|
|
17
|
+
class FormulaError {
|
|
18
|
+
constructor(code, message, error) {
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.message = message;
|
|
21
|
+
this.error = error;
|
|
43
22
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
23
|
+
}
|
|
24
|
+
exports.FormulaError = FormulaError;
|
|
25
|
+
class Entity {
|
|
26
|
+
constructor(value) {
|
|
27
|
+
this.value = value;
|
|
49
28
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
29
|
+
}
|
|
30
|
+
class Value extends Entity {
|
|
31
|
+
evaluate({}) {
|
|
32
|
+
return this.value;
|
|
55
33
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
34
|
+
}
|
|
35
|
+
exports.Value = Value;
|
|
36
|
+
class Unreferenced extends Entity {
|
|
37
|
+
evaluate({}) {
|
|
38
|
+
throw new FormulaError("#REF!", `Reference does not exist.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.Unreferenced = Unreferenced;
|
|
42
|
+
class InvalidRef extends Entity {
|
|
43
|
+
evaluate({}) {
|
|
44
|
+
throw new FormulaError("#NAME?", `Invalid ref: ${this.value}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.InvalidRef = InvalidRef;
|
|
48
|
+
class Ref extends Entity {
|
|
49
|
+
constructor(value) {
|
|
50
|
+
super(value.toUpperCase());
|
|
51
|
+
}
|
|
52
|
+
evaluate({ table }) {
|
|
53
|
+
const { y, x } = (0, converters_1.a2p)(this.value);
|
|
54
|
+
return table.trim({ top: y, left: x, bottom: y, right: x });
|
|
55
|
+
}
|
|
56
|
+
id(table) {
|
|
57
|
+
const id = table.getIdByAddress(this.value);
|
|
58
|
+
if (id) {
|
|
59
|
+
return id;
|
|
71
60
|
}
|
|
61
|
+
return this.value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.Ref = Ref;
|
|
65
|
+
class Range extends Entity {
|
|
66
|
+
constructor(value) {
|
|
67
|
+
super(value.toUpperCase());
|
|
72
68
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
69
|
+
evaluate({ table }) {
|
|
70
|
+
const area = (0, structs_1.rangeToArea)(this.complementRange(table));
|
|
71
|
+
return table.trim(area);
|
|
72
|
+
}
|
|
73
|
+
idRange(table) {
|
|
74
|
+
return this.value
|
|
75
|
+
.split(":")
|
|
76
|
+
.map((ref) => table.getIdByAddress(ref))
|
|
77
|
+
.join(":");
|
|
78
|
+
}
|
|
79
|
+
complementRange(table) {
|
|
80
|
+
const cells = this.value.split(":");
|
|
81
|
+
let [start = "", end = ""] = cells;
|
|
82
|
+
if (!start.match(/[1-9]\d*/)) {
|
|
83
|
+
start += "1";
|
|
77
84
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return table.trim(area);
|
|
85
|
+
if (!start.match(/[a-zA-Z]/)) {
|
|
86
|
+
start = "A" + start;
|
|
81
87
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
.split(":")
|
|
85
|
-
.map((ref) => table.getIdByAddress(ref))
|
|
86
|
-
.join(":");
|
|
88
|
+
if (!end.match(/[1-9]\d*/)) {
|
|
89
|
+
end += table.getNumRows();
|
|
87
90
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
let [start = "", end = ""] = cells;
|
|
91
|
-
if (!start.match(/[1-9]\d*/)) {
|
|
92
|
-
start += "1";
|
|
93
|
-
}
|
|
94
|
-
if (!start.match(/[a-zA-Z]/)) {
|
|
95
|
-
start = "A" + start;
|
|
96
|
-
}
|
|
97
|
-
if (!end.match(/[1-9]\d*/)) {
|
|
98
|
-
end += table.getNumRows();
|
|
99
|
-
}
|
|
100
|
-
if (!end.match(/[a-zA-Z]/)) {
|
|
101
|
-
end = (0, converters_1.x2c)(table.getNumCols() + 1) + end;
|
|
102
|
-
}
|
|
103
|
-
return `${start}:${end}`;
|
|
91
|
+
if (!end.match(/[a-zA-Z]/)) {
|
|
92
|
+
end = (0, converters_1.x2c)(table.getNumCols() + 1) + end;
|
|
104
93
|
}
|
|
94
|
+
return `${start}:${end}`;
|
|
105
95
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
ref(table, slideY = 0, slideX = 0) {
|
|
114
|
-
return table.getAddressById(getId(this.value, false), slideY, slideX);
|
|
115
|
-
}
|
|
116
|
-
slide(table, slideY = 0, slideX = 0) {
|
|
117
|
-
const address = this.ref(table, slideY, slideX);
|
|
118
|
-
if (address == null || address.length < 2) {
|
|
119
|
-
return "#REF!";
|
|
120
|
-
}
|
|
121
|
-
return table.getIdByAddress(address);
|
|
122
|
-
}
|
|
96
|
+
}
|
|
97
|
+
exports.Range = Range;
|
|
98
|
+
class Id extends Entity {
|
|
99
|
+
evaluate({ table }) {
|
|
100
|
+
const id = getId(this.value);
|
|
101
|
+
const { y, x } = table.getPointById(id);
|
|
102
|
+
return table.trim({ top: y, left: x, bottom: y, right: x });
|
|
123
103
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
evaluate({ table }) {
|
|
127
|
-
const ids = this.value.split(":");
|
|
128
|
-
const [p1, p2] = ids
|
|
129
|
-
.map((id) => getId(id))
|
|
130
|
-
.map((id) => table.getPointById(id));
|
|
131
|
-
return table.trim({ top: p1.y, left: p1.x, bottom: p2.y, right: p2.x });
|
|
132
|
-
}
|
|
133
|
-
range(table, slideY = 0, slideX = 0) {
|
|
134
|
-
return this.value
|
|
135
|
-
.split(":")
|
|
136
|
-
.map((id) => getId(id, false))
|
|
137
|
-
.map((id) => table.getAddressById(id, slideY, slideX))
|
|
138
|
-
.join(":");
|
|
139
|
-
}
|
|
140
|
-
slide(table, slideY = 0, slideX = 0) {
|
|
141
|
-
const range = this.range(table, slideY, slideX);
|
|
142
|
-
return new Range(range).idRange(table);
|
|
143
|
-
}
|
|
104
|
+
ref(table, slideY = 0, slideX = 0) {
|
|
105
|
+
return table.getAddressById(getId(this.value, false), slideY, slideX);
|
|
144
106
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
this.precedence = precedence;
|
|
150
|
-
this.args = args;
|
|
107
|
+
slide(table, slideY = 0, slideX = 0) {
|
|
108
|
+
const address = this.ref(table, slideY, slideX);
|
|
109
|
+
if (address == null || address.length < 2) {
|
|
110
|
+
return "#REF!";
|
|
151
111
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
112
|
+
return table.getIdByAddress(address);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.Id = Id;
|
|
116
|
+
class IdRange extends Entity {
|
|
117
|
+
evaluate({ table }) {
|
|
118
|
+
const ids = this.value.split(":");
|
|
119
|
+
const [p1, p2] = ids
|
|
120
|
+
.map((id) => getId(id))
|
|
121
|
+
.map((id) => table.getPointById(id));
|
|
122
|
+
return table.trim({ top: p1.y, left: p1.x, bottom: p2.y, right: p2.x });
|
|
123
|
+
}
|
|
124
|
+
range(table, slideY = 0, slideX = 0) {
|
|
125
|
+
return this.value
|
|
126
|
+
.split(":")
|
|
127
|
+
.map((id) => getId(id, false))
|
|
128
|
+
.map((id) => table.getAddressById(id, slideY, slideX))
|
|
129
|
+
.join(":");
|
|
130
|
+
}
|
|
131
|
+
slide(table, slideY = 0, slideX = 0) {
|
|
132
|
+
const range = this.range(table, slideY, slideX);
|
|
133
|
+
return new Range(range).idRange(table);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.IdRange = IdRange;
|
|
137
|
+
class Function {
|
|
138
|
+
constructor(name, precedence = 0, args = []) {
|
|
139
|
+
this.name = name;
|
|
140
|
+
this.precedence = precedence;
|
|
141
|
+
this.args = args;
|
|
142
|
+
}
|
|
143
|
+
evaluate({ table }) {
|
|
144
|
+
const name = this.name.toLowerCase();
|
|
145
|
+
const Func = table.getFunction(name);
|
|
146
|
+
if (Func == null) {
|
|
147
|
+
throw new FormulaError("#NAME?", `Unknown function: ${name}`);
|
|
148
|
+
}
|
|
149
|
+
const func = new Func({ args: this.args, table });
|
|
150
|
+
return func.call();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
exports.Function = Function;
|
|
154
|
+
const ZERO = new Value(0);
|
|
155
|
+
const INFIX_FUNCTION_NAME_MAP = {
|
|
156
|
+
"+": "add",
|
|
157
|
+
"-": "minus",
|
|
158
|
+
"/": "divide",
|
|
159
|
+
"*": "multiply",
|
|
160
|
+
"^": "power",
|
|
161
|
+
"&": "concat",
|
|
162
|
+
"=": "eq",
|
|
163
|
+
"<>": "ne",
|
|
164
|
+
">": "gt",
|
|
165
|
+
">=": "gte",
|
|
166
|
+
"<": "lt",
|
|
167
|
+
"<=": "lte",
|
|
168
|
+
};
|
|
169
|
+
const PREFIX_FUNCTION_NAME_MAP = {
|
|
170
|
+
"-": "uminus",
|
|
171
|
+
};
|
|
172
|
+
class Token {
|
|
173
|
+
constructor(type, entity, precedence = 0) {
|
|
174
|
+
this.type = type;
|
|
175
|
+
this.entity = entity;
|
|
176
|
+
this.precedence = precedence;
|
|
177
|
+
}
|
|
178
|
+
convert() {
|
|
179
|
+
switch (this.type) {
|
|
180
|
+
case "VALUE":
|
|
181
|
+
return new Value(this.entity);
|
|
182
|
+
case "ID":
|
|
183
|
+
return new Id(this.entity);
|
|
184
|
+
case "ID_RANGE":
|
|
185
|
+
return new IdRange(this.entity);
|
|
186
|
+
case "REF":
|
|
187
|
+
return new Ref(this.entity);
|
|
188
|
+
case "RANGE":
|
|
189
|
+
return new Range(this.entity);
|
|
190
|
+
case "INFIX_OPERATOR": {
|
|
191
|
+
const name = INFIX_FUNCTION_NAME_MAP[this.entity];
|
|
192
|
+
return new Function(name, this.precedence);
|
|
157
193
|
}
|
|
158
|
-
|
|
159
|
-
|
|
194
|
+
case "PREFIX_OPERATOR": {
|
|
195
|
+
const name = PREFIX_FUNCTION_NAME_MAP[this.entity];
|
|
196
|
+
return new Function(name, this.precedence);
|
|
197
|
+
}
|
|
198
|
+
case "FUNCTION":
|
|
199
|
+
return new Function(this.entity);
|
|
200
|
+
case "UNREFERENCED":
|
|
201
|
+
return new Unreferenced(this.entity);
|
|
202
|
+
case "INVALID_REF":
|
|
203
|
+
return new InvalidRef(this.entity);
|
|
160
204
|
}
|
|
161
205
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
206
|
+
}
|
|
207
|
+
exports.Token = Token;
|
|
208
|
+
const isWhiteSpace = (char) => {
|
|
209
|
+
return char === " " || char === "\n" || char === "\t";
|
|
210
|
+
};
|
|
211
|
+
const TOKEN_OPEN = new Token("OPEN", "("), TOKEN_CLOSE = new Token("CLOSE", ")"), TOKEN_COMMA = new Token("COMMA", ","), TOKEN_ADD = new Token("INFIX_OPERATOR", "+", 3), TOKEN_MINUS = new Token("INFIX_OPERATOR", "-", 3), TOKEN_UMINUS = new Token("PREFIX_OPERATOR", "-", 6), TOKEN_DIVIDE = new Token("INFIX_OPERATOR", "/", 4), TOKEN_MULTIPLY = new Token("INFIX_OPERATOR", "*", 4), TOKEN_POWER = new Token("INFIX_OPERATOR", "^", 5), TOKEN_CONCAT = new Token("INFIX_OPERATOR", "&", 4), TOKEN_GTE = new Token("INFIX_OPERATOR", ">=", 2), TOKEN_GT = new Token("INFIX_OPERATOR", ">", 2), TOKEN_LTE = new Token("INFIX_OPERATOR", "<=", 2), TOKEN_LT = new Token("INFIX_OPERATOR", "<", 2), TOKEN_NE = new Token("INFIX_OPERATOR", "<>", 1), TOKEN_EQ = new Token("INFIX_OPERATOR", "=", 1);
|
|
212
|
+
const BOOLS = { ["true"]: true, ["false"]: false };
|
|
213
|
+
class Lexer {
|
|
214
|
+
constructor(formula) {
|
|
215
|
+
this.tokens = [];
|
|
216
|
+
this.formula = formula;
|
|
217
|
+
this.index = 0;
|
|
218
|
+
this.tokens = [];
|
|
219
|
+
}
|
|
220
|
+
isWhiteSpace() {
|
|
221
|
+
return isWhiteSpace(this.formula[this.index]);
|
|
222
|
+
}
|
|
223
|
+
next(base = 1) {
|
|
224
|
+
this.index += base;
|
|
225
|
+
}
|
|
226
|
+
get(base = 0) {
|
|
227
|
+
const c = this.formula[this.index + base];
|
|
228
|
+
return c;
|
|
229
|
+
}
|
|
230
|
+
getToken(base = 0) {
|
|
231
|
+
return this.tokens[this.tokens.length + base];
|
|
232
|
+
}
|
|
233
|
+
stringifyToId(table, slideY = 0, slideX = 0) {
|
|
234
|
+
return this.tokens
|
|
235
|
+
.map((t) => {
|
|
236
|
+
switch (t.type) {
|
|
189
237
|
case "VALUE":
|
|
190
|
-
|
|
238
|
+
if (typeof t.entity === "number" || typeof t.entity === "boolean") {
|
|
239
|
+
return t.entity;
|
|
240
|
+
}
|
|
241
|
+
return `"${t.entity}"`;
|
|
191
242
|
case "ID":
|
|
192
|
-
return new Id(
|
|
243
|
+
return new Id(t.entity).slide(table, slideY, slideX);
|
|
193
244
|
case "ID_RANGE":
|
|
194
|
-
return new IdRange(
|
|
245
|
+
return new IdRange(t.entity).slide(table, slideY, slideX);
|
|
195
246
|
case "REF":
|
|
196
|
-
return new Ref(
|
|
247
|
+
return new Ref(t.entity).id(table);
|
|
197
248
|
case "RANGE":
|
|
198
|
-
return new Range(
|
|
199
|
-
case "INFIX_OPERATOR": {
|
|
200
|
-
const name = INFIX_FUNCTION_NAME_MAP[this.entity];
|
|
201
|
-
return new Function(name, this.precedence);
|
|
202
|
-
}
|
|
203
|
-
case "PREFIX_OPERATOR": {
|
|
204
|
-
const name = PREFIX_FUNCTION_NAME_MAP[this.entity];
|
|
205
|
-
return new Function(name, this.precedence);
|
|
206
|
-
}
|
|
207
|
-
case "FUNCTION":
|
|
208
|
-
return new Function(this.entity);
|
|
209
|
-
case "UNREFERENCED":
|
|
210
|
-
return new Unreferenced(this.entity);
|
|
211
|
-
case "INVALID_REF":
|
|
212
|
-
return new InvalidRef(this.entity);
|
|
249
|
+
return new Range(t.entity).idRange(table);
|
|
213
250
|
}
|
|
214
|
-
|
|
251
|
+
return t.entity;
|
|
252
|
+
})
|
|
253
|
+
.join("");
|
|
215
254
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
return
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
.map((t) => {
|
|
267
|
-
switch (t.type) {
|
|
268
|
-
case "VALUE":
|
|
269
|
-
if (typeof t.entity === "number" || typeof t.entity === "boolean") {
|
|
270
|
-
return t.entity;
|
|
271
|
-
}
|
|
272
|
-
return `"${t.entity}"`;
|
|
273
|
-
case "ID":
|
|
274
|
-
return new Id(t.entity).ref(table);
|
|
275
|
-
case "ID_RANGE":
|
|
276
|
-
return new IdRange(t.entity).range(table);
|
|
255
|
+
stringifyToRef(table) {
|
|
256
|
+
return this.tokens
|
|
257
|
+
.map((t) => {
|
|
258
|
+
switch (t.type) {
|
|
259
|
+
case "VALUE":
|
|
260
|
+
if (typeof t.entity === "number" || typeof t.entity === "boolean") {
|
|
261
|
+
return t.entity;
|
|
262
|
+
}
|
|
263
|
+
return `"${t.entity}"`;
|
|
264
|
+
case "ID":
|
|
265
|
+
return new Id(t.entity).ref(table);
|
|
266
|
+
case "ID_RANGE":
|
|
267
|
+
return new IdRange(t.entity).range(table);
|
|
268
|
+
}
|
|
269
|
+
return t.entity;
|
|
270
|
+
})
|
|
271
|
+
.join("");
|
|
272
|
+
}
|
|
273
|
+
tokenize() {
|
|
274
|
+
var _a, _b;
|
|
275
|
+
while (this.index <= this.formula.length) {
|
|
276
|
+
this.skipSpaces();
|
|
277
|
+
const char = this.get();
|
|
278
|
+
this.next();
|
|
279
|
+
switch (char) {
|
|
280
|
+
case undefined:
|
|
281
|
+
return;
|
|
282
|
+
case "(":
|
|
283
|
+
this.tokens.push(TOKEN_OPEN);
|
|
284
|
+
continue;
|
|
285
|
+
case ")":
|
|
286
|
+
this.tokens.push(TOKEN_CLOSE);
|
|
287
|
+
continue;
|
|
288
|
+
case ",":
|
|
289
|
+
this.tokens.push(TOKEN_COMMA);
|
|
290
|
+
continue;
|
|
291
|
+
case "+":
|
|
292
|
+
this.tokens.push(TOKEN_ADD);
|
|
293
|
+
continue;
|
|
294
|
+
case "-": {
|
|
295
|
+
const prev1 = (_a = this.getToken(-1)) === null || _a === void 0 ? void 0 : _a.type;
|
|
296
|
+
const prev2 = (_b = this.getToken(-2)) === null || _b === void 0 ? void 0 : _b.type;
|
|
297
|
+
if (prev1 === "INFIX_OPERATOR" ||
|
|
298
|
+
(prev1 === "SPACE" && prev2 === "INFIX_OPERATOR")) {
|
|
299
|
+
this.tokens.push(TOKEN_UMINUS);
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
this.tokens.push(TOKEN_MINUS);
|
|
303
|
+
}
|
|
304
|
+
continue;
|
|
277
305
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
this.
|
|
296
|
-
|
|
297
|
-
case ",":
|
|
298
|
-
this.tokens.push(TOKEN_COMMA);
|
|
299
|
-
continue;
|
|
300
|
-
case "+":
|
|
301
|
-
this.tokens.push(TOKEN_ADD);
|
|
302
|
-
continue;
|
|
303
|
-
case "-": {
|
|
304
|
-
const prev1 = (_a = this.getToken(-1)) === null || _a === void 0 ? void 0 : _a.type;
|
|
305
|
-
const prev2 = (_b = this.getToken(-2)) === null || _b === void 0 ? void 0 : _b.type;
|
|
306
|
-
if (prev1 === "INFIX_OPERATOR" ||
|
|
307
|
-
(prev1 === "SPACE" && prev2 === "INFIX_OPERATOR")) {
|
|
308
|
-
this.tokens.push(TOKEN_UMINUS);
|
|
309
|
-
}
|
|
310
|
-
else {
|
|
311
|
-
this.tokens.push(TOKEN_MINUS);
|
|
312
|
-
}
|
|
306
|
+
case "/":
|
|
307
|
+
this.tokens.push(TOKEN_DIVIDE);
|
|
308
|
+
continue;
|
|
309
|
+
case "*":
|
|
310
|
+
this.tokens.push(TOKEN_MULTIPLY);
|
|
311
|
+
continue;
|
|
312
|
+
case "^":
|
|
313
|
+
this.tokens.push(TOKEN_POWER);
|
|
314
|
+
continue;
|
|
315
|
+
case "&":
|
|
316
|
+
this.tokens.push(TOKEN_CONCAT);
|
|
317
|
+
continue;
|
|
318
|
+
case "=":
|
|
319
|
+
this.tokens.push(TOKEN_EQ);
|
|
320
|
+
continue;
|
|
321
|
+
case ">":
|
|
322
|
+
if (this.get() === "=") {
|
|
323
|
+
this.next();
|
|
324
|
+
this.tokens.push(TOKEN_GTE);
|
|
313
325
|
continue;
|
|
314
326
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
this.
|
|
320
|
-
|
|
321
|
-
case "^":
|
|
322
|
-
this.tokens.push(TOKEN_POWER);
|
|
323
|
-
continue;
|
|
324
|
-
case "&":
|
|
325
|
-
this.tokens.push(TOKEN_CONCAT);
|
|
326
|
-
continue;
|
|
327
|
-
case "=":
|
|
328
|
-
this.tokens.push(TOKEN_EQ);
|
|
329
|
-
continue;
|
|
330
|
-
case ">":
|
|
331
|
-
if (this.get() === "=") {
|
|
332
|
-
this.next();
|
|
333
|
-
this.tokens.push(TOKEN_GTE);
|
|
334
|
-
continue;
|
|
335
|
-
}
|
|
336
|
-
this.tokens.push(TOKEN_GT);
|
|
337
|
-
continue;
|
|
338
|
-
case "<":
|
|
339
|
-
if (this.get() === "=") {
|
|
340
|
-
this.next();
|
|
341
|
-
this.tokens.push(TOKEN_LTE);
|
|
342
|
-
continue;
|
|
343
|
-
}
|
|
344
|
-
if (this.get() === ">") {
|
|
345
|
-
this.next();
|
|
346
|
-
this.tokens.push(TOKEN_NE);
|
|
347
|
-
continue;
|
|
348
|
-
}
|
|
349
|
-
this.tokens.push(TOKEN_LT);
|
|
350
|
-
continue;
|
|
351
|
-
case '"': {
|
|
352
|
-
const buf = this.getString();
|
|
353
|
-
this.tokens.push(new Token("VALUE", buf));
|
|
327
|
+
this.tokens.push(TOKEN_GT);
|
|
328
|
+
continue;
|
|
329
|
+
case "<":
|
|
330
|
+
if (this.get() === "=") {
|
|
331
|
+
this.next();
|
|
332
|
+
this.tokens.push(TOKEN_LTE);
|
|
354
333
|
continue;
|
|
355
334
|
}
|
|
356
|
-
|
|
357
|
-
let buf = char;
|
|
358
|
-
while (true) {
|
|
359
|
-
const c = this.get();
|
|
360
|
-
if (c === "(") {
|
|
361
|
-
this.tokens.push(new Token("FUNCTION", buf), TOKEN_OPEN);
|
|
335
|
+
if (this.get() === ">") {
|
|
362
336
|
this.next();
|
|
337
|
+
this.tokens.push(TOKEN_NE);
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
this.tokens.push(TOKEN_LT);
|
|
341
|
+
continue;
|
|
342
|
+
case '"': {
|
|
343
|
+
const buf = this.getString();
|
|
344
|
+
this.tokens.push(new Token("VALUE", buf));
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
} // switch end
|
|
348
|
+
let buf = char;
|
|
349
|
+
while (true) {
|
|
350
|
+
const c = this.get();
|
|
351
|
+
if (c === "(") {
|
|
352
|
+
this.tokens.push(new Token("FUNCTION", buf), TOKEN_OPEN);
|
|
353
|
+
this.next();
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
if (c == null || c.match(/[ +\-/*^&=<>),]/)) {
|
|
357
|
+
if (buf.length === 0) {
|
|
363
358
|
break;
|
|
364
359
|
}
|
|
365
|
-
if (
|
|
366
|
-
|
|
367
|
-
|
|
360
|
+
if (buf.match(/^[+-]?(\d*[.])?\d+$/)) {
|
|
361
|
+
this.tokens.push(new Token("VALUE", parseFloat(buf)));
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
// @ts-ignore
|
|
365
|
+
const bool = BOOLS[buf.toLowerCase()];
|
|
366
|
+
if (bool != null) {
|
|
367
|
+
this.tokens.push(new Token("VALUE", bool));
|
|
368
|
+
}
|
|
369
|
+
else if (buf.startsWith("#")) {
|
|
370
|
+
if (buf === "#REF!") {
|
|
371
|
+
this.tokens.push(new Token("UNREFERENCED", buf));
|
|
372
|
+
}
|
|
373
|
+
else if (buf.indexOf(":") !== -1) {
|
|
374
|
+
this.tokens.push(new Token("ID_RANGE", buf));
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
this.tokens.push(new Token("ID", buf));
|
|
378
|
+
}
|
|
368
379
|
}
|
|
369
|
-
if (buf.
|
|
370
|
-
this.tokens.push(new Token("
|
|
380
|
+
else if (buf.indexOf(":") !== -1) {
|
|
381
|
+
this.tokens.push(new Token("RANGE", buf));
|
|
371
382
|
}
|
|
372
383
|
else {
|
|
373
384
|
// @ts-ignore
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
this.tokens.push(new Token("VALUE", bool));
|
|
377
|
-
}
|
|
378
|
-
else if (buf.startsWith("#")) {
|
|
379
|
-
if (buf === "#REF!") {
|
|
380
|
-
this.tokens.push(new Token("UNREFERENCED", buf));
|
|
381
|
-
}
|
|
382
|
-
else if (buf.indexOf(":") !== -1) {
|
|
383
|
-
this.tokens.push(new Token("ID_RANGE", buf));
|
|
384
|
-
}
|
|
385
|
-
else {
|
|
386
|
-
this.tokens.push(new Token("ID", buf));
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
else if (buf.indexOf(":") !== -1) {
|
|
390
|
-
this.tokens.push(new Token("RANGE", buf));
|
|
385
|
+
if (isNaN(buf[buf.length - 1])) {
|
|
386
|
+
this.tokens.push(new Token("INVALID_REF", buf));
|
|
391
387
|
}
|
|
392
388
|
else {
|
|
393
|
-
|
|
394
|
-
if (isNaN(buf[buf.length - 1])) {
|
|
395
|
-
this.tokens.push(new Token("INVALID_REF", buf));
|
|
396
|
-
}
|
|
397
|
-
else {
|
|
398
|
-
this.tokens.push(new Token("REF", buf));
|
|
399
|
-
}
|
|
389
|
+
this.tokens.push(new Token("REF", buf));
|
|
400
390
|
}
|
|
401
391
|
}
|
|
402
|
-
break;
|
|
403
392
|
}
|
|
404
|
-
|
|
405
|
-
this.next();
|
|
393
|
+
break;
|
|
406
394
|
}
|
|
395
|
+
buf += c;
|
|
396
|
+
this.next();
|
|
407
397
|
}
|
|
408
398
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
if (space !== "") {
|
|
415
|
-
this.tokens.push(new Token("SPACE", space));
|
|
416
|
-
}
|
|
399
|
+
}
|
|
400
|
+
skipSpaces() {
|
|
401
|
+
let space = "";
|
|
402
|
+
while (this.isWhiteSpace()) {
|
|
403
|
+
space += this.formula[this.index++];
|
|
417
404
|
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
405
|
+
if (space !== "") {
|
|
406
|
+
this.tokens.push(new Token("SPACE", space));
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
getString() {
|
|
410
|
+
let buf = "";
|
|
411
|
+
while (true) {
|
|
412
|
+
const c = this.get();
|
|
413
|
+
this.next();
|
|
414
|
+
if (c == null) {
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
if (c === '"') {
|
|
418
|
+
if (this.get() === '"') {
|
|
419
|
+
// escape
|
|
420
|
+
buf += '"';
|
|
421
|
+
this.next();
|
|
422
|
+
continue;
|
|
436
423
|
}
|
|
437
424
|
else {
|
|
438
|
-
|
|
425
|
+
break;
|
|
439
426
|
}
|
|
440
427
|
}
|
|
441
|
-
|
|
428
|
+
else {
|
|
429
|
+
buf += c;
|
|
430
|
+
}
|
|
442
431
|
}
|
|
432
|
+
return buf;
|
|
443
433
|
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
};
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
434
|
+
}
|
|
435
|
+
exports.Lexer = Lexer;
|
|
436
|
+
class Parser {
|
|
437
|
+
constructor(tokens) {
|
|
438
|
+
this.index = 0;
|
|
439
|
+
this.depth = 0;
|
|
440
|
+
this.tokens = tokens;
|
|
441
|
+
}
|
|
442
|
+
build() {
|
|
443
|
+
const { expr } = this.parse(false);
|
|
444
|
+
return expr;
|
|
445
|
+
}
|
|
446
|
+
parse(underFunction) {
|
|
447
|
+
const stack = [];
|
|
448
|
+
let lastOperator;
|
|
449
|
+
const complement = (hasNext = false) => {
|
|
450
|
+
if (lastOperator) {
|
|
451
|
+
const outer = stack.pop();
|
|
452
|
+
lastOperator.args.push(outer);
|
|
453
|
+
}
|
|
454
|
+
return { hasNext, expr: stack.shift() };
|
|
455
|
+
};
|
|
456
|
+
while (this.tokens.length > this.index) {
|
|
457
|
+
const token = this.tokens[this.index++];
|
|
458
|
+
if (token.type === "SPACE") {
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
if (token.type === "COMMA") {
|
|
462
|
+
if (!underFunction) {
|
|
463
|
+
throw new FormulaError("#ERROR!", "Invalid comma");
|
|
469
464
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
465
|
+
return complement(true);
|
|
466
|
+
}
|
|
467
|
+
else if (token.type === "VALUE" ||
|
|
468
|
+
token.type === "ID" ||
|
|
469
|
+
token.type === "ID_RANGE" ||
|
|
470
|
+
token.type === "REF" ||
|
|
471
|
+
token.type === "RANGE" ||
|
|
472
|
+
token.type === "UNREFERENCED" ||
|
|
473
|
+
token.type === "INVALID_REF") {
|
|
474
|
+
const expr = token.convert();
|
|
475
|
+
stack.push(expr);
|
|
476
|
+
}
|
|
477
|
+
else if (token.type === "FUNCTION") {
|
|
478
|
+
this.index++;
|
|
479
|
+
this.depth++;
|
|
480
|
+
const func = token.convert();
|
|
481
|
+
stack.push(func);
|
|
482
|
+
while (true) {
|
|
483
|
+
const { expr, hasNext } = this.parse(true);
|
|
484
|
+
if (expr) {
|
|
485
|
+
func.args.push(expr);
|
|
473
486
|
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
else if (token.type === "VALUE" ||
|
|
477
|
-
token.type === "ID" ||
|
|
478
|
-
token.type === "ID_RANGE" ||
|
|
479
|
-
token.type === "REF" ||
|
|
480
|
-
token.type === "RANGE" ||
|
|
481
|
-
token.type === "UNREFERENCED" ||
|
|
482
|
-
token.type === "INVALID_REF") {
|
|
483
|
-
const expr = token.convert();
|
|
484
|
-
stack.push(expr);
|
|
485
|
-
}
|
|
486
|
-
else if (token.type === "FUNCTION") {
|
|
487
|
-
this.index++;
|
|
488
|
-
this.depth++;
|
|
489
|
-
const func = token.convert();
|
|
490
|
-
stack.push(func);
|
|
491
|
-
while (true) {
|
|
492
|
-
const { expr, hasNext } = this.parse(true);
|
|
493
|
-
if (expr) {
|
|
494
|
-
func.args.push(expr);
|
|
495
|
-
}
|
|
496
|
-
if (!hasNext) {
|
|
497
|
-
break;
|
|
498
|
-
}
|
|
487
|
+
if (!hasNext) {
|
|
488
|
+
break;
|
|
499
489
|
}
|
|
500
490
|
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
return complement();
|
|
491
|
+
}
|
|
492
|
+
else if (token.type === "OPEN") {
|
|
493
|
+
this.depth++;
|
|
494
|
+
const { expr } = this.parse(false);
|
|
495
|
+
stack.push(expr);
|
|
496
|
+
}
|
|
497
|
+
else if (token.type === "CLOSE") {
|
|
498
|
+
if (this.depth-- === 0) {
|
|
499
|
+
throw new FormulaError("#ERROR!", "Unexpected end paren");
|
|
511
500
|
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
throw new FormulaError("#ERROR!", "Missing left expression");
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
if (lastOperator == null) {
|
|
524
|
-
operator.args.push(left);
|
|
525
|
-
stack.unshift(operator);
|
|
526
|
-
}
|
|
527
|
-
else if (operator.precedence > lastOperator.precedence) {
|
|
528
|
-
operator.args.push(left);
|
|
529
|
-
lastOperator.args.push(operator);
|
|
530
|
-
stack.unshift(lastOperator);
|
|
501
|
+
return complement();
|
|
502
|
+
}
|
|
503
|
+
else if (token.type === "INFIX_OPERATOR") {
|
|
504
|
+
const operator = token.convert();
|
|
505
|
+
let left = stack.pop();
|
|
506
|
+
if (left == null) {
|
|
507
|
+
if (operator.name === "minus" || operator.name === "add") {
|
|
508
|
+
left = ZERO;
|
|
531
509
|
}
|
|
532
510
|
else {
|
|
533
|
-
|
|
534
|
-
operator.args.push(outer);
|
|
535
|
-
lastOperator.args.push(left);
|
|
536
|
-
stack.unshift(operator);
|
|
511
|
+
throw new FormulaError("#ERROR!", "Missing left expression");
|
|
537
512
|
}
|
|
538
|
-
lastOperator = operator;
|
|
539
513
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
514
|
+
if (lastOperator == null) {
|
|
515
|
+
operator.args.push(left);
|
|
516
|
+
stack.unshift(operator);
|
|
517
|
+
}
|
|
518
|
+
else if (operator.precedence > lastOperator.precedence) {
|
|
519
|
+
operator.args.push(left);
|
|
520
|
+
lastOperator.args.push(operator);
|
|
521
|
+
stack.unshift(lastOperator);
|
|
522
|
+
}
|
|
523
|
+
else {
|
|
524
|
+
const outer = stack.shift();
|
|
525
|
+
operator.args.push(outer);
|
|
526
|
+
lastOperator.args.push(left);
|
|
527
|
+
stack.unshift(operator);
|
|
549
528
|
}
|
|
529
|
+
lastOperator = operator;
|
|
530
|
+
}
|
|
531
|
+
else if (token.type === "PREFIX_OPERATOR") {
|
|
532
|
+
const operator = token.convert();
|
|
533
|
+
if (lastOperator) {
|
|
534
|
+
lastOperator.args.push(operator);
|
|
535
|
+
}
|
|
536
|
+
else {
|
|
537
|
+
stack.unshift(operator);
|
|
538
|
+
}
|
|
539
|
+
lastOperator = operator;
|
|
550
540
|
}
|
|
551
|
-
return complement();
|
|
552
541
|
}
|
|
542
|
+
return complement();
|
|
553
543
|
}
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
544
|
+
}
|
|
545
|
+
exports.Parser = Parser;
|
|
546
|
+
const convertFormulaAbsolute = ({ value, table, slideY = 0, slideX = 0, }) => {
|
|
547
|
+
if (typeof value === "string" || value instanceof String) {
|
|
548
|
+
if (value.charAt(0) === "=") {
|
|
549
|
+
const lexer = new Lexer(value.substring(1));
|
|
550
|
+
lexer.tokenize();
|
|
551
|
+
return "=" + lexer.stringifyToId(table, slideY, slideX);
|
|
562
552
|
}
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
553
|
+
}
|
|
554
|
+
return value;
|
|
555
|
+
};
|
|
556
|
+
exports.convertFormulaAbsolute = convertFormulaAbsolute;
|
|
567
557
|
//# sourceMappingURL=evaluator.js.map
|