forked from handsontable/formula-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
248 lines (212 loc) · 5.54 KB
/
Copy pathparser.js
File metadata and controls
248 lines (212 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import Emitter from 'tiny-emitter';
import evaluateByOperator from './evaluate-by-operator/evaluate-by-operator';
import {Parser as GrammarParser} from './grammar-parser/grammar-parser';
import {trimEdges} from './helper/string';
import {toNumber, invertNumber} from './helper/number';
import errorParser, {isValidStrict as isErrorValid, ERROR, ERROR_NAME} from './error';
import {extractLabel, toLabel} from './helper/cell';
/**
* @class Parser
*/
class Parser extends Emitter {
constructor() {
super();
this.parser = new GrammarParser();
this.parser.yy = {
toNumber,
trimEdges,
invertNumber,
throwError: (errorName) => this._throwError(errorName),
callVariable: (variable) => this._callVariable(variable),
evaluateByOperator,
callFunction: (name, params) => this._callFunction(name, params),
cellValue: (value) => this._callCellValue(value),
rangeValue: (start, end) => this._callRangeValue(start, end),
};
this.variables = Object.create(null);
this.functions = Object.create(null);
this
.setVariable('TRUE', true)
.setVariable('FALSE', false)
.setVariable('NULL', null);
}
/**
* Parse formula expression.
*
* @param {String} expression to parse.
* @return {*} Returns an object with tow properties `error` and `result`.
*/
parse(expression) {
let result = null;
let error = null;
try {
if (expression === '') {
result = '';
} else {
result = this.parser.parse(expression);
}
} catch (ex) {
const message = errorParser(ex.message);
if (message) {
error = message;
} else {
error = errorParser(ERROR);
}
}
if (result instanceof Error) {
error = errorParser(result.message) || errorParser(ERROR);
result = null;
}
return {
error,
result,
};
}
/**
* Set predefined variable name which can be visible while parsing formula expression.
*
* @param {String} name Variable name.
* @param {*} value Variable value.
* @returns {Parser}
*/
setVariable(name, value) {
this.variables[name] = value;
return this;
}
/**
* Get variable name.
*
* @param {String} name Variable name.
* @returns {*}
*/
getVariable(name) {
return this.variables[name];
}
/**
* Retrieve variable value by its name.
*
* @param name Variable name.
* @returns {*}
* @private
*/
_callVariable(name) {
let value = this.getVariable(name);
this.emit('callVariable', name, (newValue) => {
if (newValue !== void 0) {
value = newValue;
}
});
if (value === void 0) {
throw Error(ERROR_NAME);
}
return value;
}
/**
* Set custom function which can be visible while parsing formula expression.
*
* @param {String} name Custom function name.
* @param {Function} fn Custom function.
* @returns {Parser}
*/
setFunction(name, fn) {
this.functions[name] = fn;
return this;
}
/**
* Get custom function.
*
* @param {String} name Custom function name.
* @returns {*}
*/
getFunction(name) {
return this.functions[name];
}
/**
* Call function with provided params.
*
* @param name Function name.
* @param params Function params.
* @returns {*}
* @private
*/
_callFunction(name, params = []) {
const fn = this.getFunction(name);
let value;
if (fn) {
value = fn(params);
}
this.emit('callFunction', name, params, (newValue) => {
if (newValue !== void 0) {
value = newValue;
}
});
return value === void 0 ? evaluateByOperator(name, params) : value;
}
/**
* Retrieve value by its label (`B3`, `B$3`, `B$3`, `$B$3`).
*
* @param {String} label Coordinates.
* @returns {*}
* @private
*/
_callCellValue(label) {
label = label.toUpperCase();
const [row, column] = extractLabel(label);
let value = void 0;
this.emit('callCellValue', {label, row, column}, (_value) => {
value = _value;
});
return value;
}
/**
* Retrieve value by its label (`B3:A1`, `B$3:A1`, `B$3:$A1`, `$B$3:A$1`).
*
* @param {String} startLabel Coordinates of the first cell.
* @param {String} endLabel Coordinates of the last cell.
* @returns {Array} Returns an array of mixed values.
* @private
*/
_callRangeValue(startLabel, endLabel) {
startLabel = startLabel.toUpperCase();
endLabel = endLabel.toUpperCase();
const [startRow, startColumn] = extractLabel(startLabel);
const [endRow, endColumn] = extractLabel(endLabel);
let startCell = {};
let endCell = {};
if (startRow.index <= endRow.index) {
startCell.row = startRow;
endCell.row = endRow;
} else {
startCell.row = endRow;
endCell.row = startRow;
}
if (startColumn.index <= endColumn.index) {
startCell.column = startColumn;
endCell.column = endColumn;
} else {
startCell.column = endColumn;
endCell.column = startColumn;
}
startCell.label = toLabel(startCell.row, startCell.column);
endCell.label = toLabel(endCell.row, endCell.column);
let value = [];
this.emit('callRangeValue', startCell, endCell, (_value = []) => {
value = _value;
});
return value;
}
/**
* Try to throw error by its name.
*
* @param {String} errorName Error name.
* @returns {String}
* @private
*/
_throwError(errorName) {
if (isErrorValid(errorName)) {
throw Error(errorName);
}
throw Error(ERROR);
}
}
export default Parser;