@openeo/js-client 2.5.1 → 2.7.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/LICENSE +201 -201
- package/README.md +94 -84
- package/openeo.d.ts +310 -72
- package/openeo.js +5412 -5191
- package/openeo.min.js +1 -1
- package/package.json +72 -70
- package/src/authprovider.js +147 -138
- package/src/baseentity.js +162 -162
- package/src/basicprovider.js +69 -48
- package/src/browser.js +168 -168
- package/src/builder/builder.js +400 -400
- package/src/builder/formula.js +211 -211
- package/src/builder/node.js +268 -268
- package/src/builder/parameter.js +144 -144
- package/src/builder/tapdigit.js +489 -489
- package/src/capabilities.js +274 -220
- package/src/connection.js +1290 -1211
- package/src/env.js +16 -6
- package/src/filetypes.js +111 -111
- package/src/job.js +323 -322
- package/src/logs.js +109 -68
- package/src/node.js +168 -168
- package/src/oidcprovider.js +387 -375
- package/src/openeo.js +137 -138
- package/src/pages.js +349 -0
- package/src/service.js +222 -221
- package/src/typedefs.js +271 -243
- package/src/userfile.js +128 -128
- package/src/userprocess.js +166 -166
package/src/builder/tapdigit.js
CHANGED
|
@@ -1,489 +1,489 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
|
|
3
|
-
Copyright (C) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
|
|
4
|
-
|
|
5
|
-
Redistribution and use in source and binary forms, with or without
|
|
6
|
-
modification, are permitted provided that the following conditions are met:
|
|
7
|
-
|
|
8
|
-
* Redistributions of source code must retain the above copyright
|
|
9
|
-
notice, this list of conditions and the following disclaimer.
|
|
10
|
-
* Redistributions in binary form must reproduce the above copyright
|
|
11
|
-
notice, this list of conditions and the following disclaimer in the
|
|
12
|
-
documentation and/or other materials provided with the distribution.
|
|
13
|
-
* Neither the name of the <organization> nor the
|
|
14
|
-
names of its contributors may be used to endorse or promote products
|
|
15
|
-
derived from this software without specific prior written permission.
|
|
16
|
-
|
|
17
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
18
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
19
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
20
|
-
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
|
21
|
-
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
22
|
-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
23
|
-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
24
|
-
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
25
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
26
|
-
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
/* eslint-disable jsdoc/require-jsdoc */
|
|
30
|
-
/**
|
|
31
|
-
* @ignore
|
|
32
|
-
*/
|
|
33
|
-
let TapDigit = {
|
|
34
|
-
Token: {
|
|
35
|
-
Operator: 'Operator',
|
|
36
|
-
Identifier: 'Identifier',
|
|
37
|
-
Number: 'Number'
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const SUP_MAPPING = {
|
|
42
|
-
'⁰': 0,
|
|
43
|
-
'¹': 1,
|
|
44
|
-
'²': 2,
|
|
45
|
-
'³': 3,
|
|
46
|
-
'⁴': 4,
|
|
47
|
-
'⁵': 5,
|
|
48
|
-
'⁶': 6,
|
|
49
|
-
'⁷': 7,
|
|
50
|
-
'⁸': 8,
|
|
51
|
-
'⁹': 9
|
|
52
|
-
};
|
|
53
|
-
const SUP_STRING = Object.keys(SUP_MAPPING).join('');
|
|
54
|
-
|
|
55
|
-
TapDigit.Lexer = function () {
|
|
56
|
-
let expression = '',
|
|
57
|
-
length = 0,
|
|
58
|
-
index = 0,
|
|
59
|
-
marker = 0,
|
|
60
|
-
T = TapDigit.Token;
|
|
61
|
-
|
|
62
|
-
function peekNextChar() {
|
|
63
|
-
let idx = index;
|
|
64
|
-
return ((idx < length) ? expression.charAt(idx) : '\x00');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function getNextChar() {
|
|
68
|
-
let ch = '\x00',
|
|
69
|
-
idx = index;
|
|
70
|
-
if (idx < length) {
|
|
71
|
-
ch = expression.charAt(idx);
|
|
72
|
-
index += 1;
|
|
73
|
-
}
|
|
74
|
-
return ch;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function isWhiteSpace(ch) {
|
|
78
|
-
return (ch === '\u0009') || (ch === ' ') || (ch === '\u00A0');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function isLetter(ch) {
|
|
82
|
-
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function isDecimalDigit(ch) {
|
|
86
|
-
return (ch >= '0') && (ch <= '9');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function createToken(type, value) {
|
|
90
|
-
return {
|
|
91
|
-
type: type,
|
|
92
|
-
value: value,
|
|
93
|
-
start: marker,
|
|
94
|
-
end: index - 1
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function skipSpaces() {
|
|
99
|
-
let ch;
|
|
100
|
-
|
|
101
|
-
while (index < length) {
|
|
102
|
-
ch = peekNextChar();
|
|
103
|
-
if (!isWhiteSpace(ch)) {
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
getNextChar();
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function scanOperator() {
|
|
111
|
-
let ch = peekNextChar();
|
|
112
|
-
if (('+-*/()^,' + SUP_STRING).indexOf(ch) >= 0) {
|
|
113
|
-
return createToken(T.Operator, getNextChar());
|
|
114
|
-
}
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function isIdentifierStart(ch) {
|
|
119
|
-
return (ch === '_') || (ch === '#') || (ch === '$') || isLetter(ch);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function isAdditionalNamespaceChar(ch) {
|
|
123
|
-
return (ch === '-') || (ch === '.') || (ch === '~') || (ch === '@');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function isIdentifierPart(ch, ns = false) {
|
|
127
|
-
return (ch === '_') || isLetter(ch) || isDecimalDigit(ch) || (ns && isAdditionalNamespaceChar(ch));
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function scanIdentifier() {
|
|
131
|
-
let startCh = peekNextChar();
|
|
132
|
-
if (!isIdentifierStart(startCh)) {
|
|
133
|
-
return undefined;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
let id = getNextChar();
|
|
137
|
-
let ns = false;
|
|
138
|
-
while (true) {
|
|
139
|
-
let ch = peekNextChar();
|
|
140
|
-
// If the first character is a $, it is allowed that more $ follow directly after
|
|
141
|
-
if (startCh === '$') {
|
|
142
|
-
if (ch !== '$') {
|
|
143
|
-
startCh = ''; // Stop allowing $ once the first non-$ has been found
|
|
144
|
-
} // else: allowed
|
|
145
|
-
}
|
|
146
|
-
else if (ch === '@') {
|
|
147
|
-
ns = true;
|
|
148
|
-
}
|
|
149
|
-
else if (!isIdentifierPart(ch, ns)) {
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
id += getNextChar();
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return createToken(T.Identifier, id);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function scanNumber() {
|
|
159
|
-
let ch;
|
|
160
|
-
let number;
|
|
161
|
-
|
|
162
|
-
ch = peekNextChar();
|
|
163
|
-
if (!isDecimalDigit(ch) && (ch !== '.')) {
|
|
164
|
-
return undefined;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
number = '';
|
|
168
|
-
if (ch !== '.') {
|
|
169
|
-
number = getNextChar();
|
|
170
|
-
while (true) {
|
|
171
|
-
ch = peekNextChar();
|
|
172
|
-
if (!isDecimalDigit(ch)) {
|
|
173
|
-
break;
|
|
174
|
-
}
|
|
175
|
-
number += getNextChar();
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (ch === '.') {
|
|
180
|
-
number += getNextChar();
|
|
181
|
-
while (true) {
|
|
182
|
-
ch = peekNextChar();
|
|
183
|
-
if (!isDecimalDigit(ch)) {
|
|
184
|
-
break;
|
|
185
|
-
}
|
|
186
|
-
number += getNextChar();
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (ch === 'e' || ch === 'E') {
|
|
191
|
-
number += getNextChar();
|
|
192
|
-
ch = peekNextChar();
|
|
193
|
-
if (ch === '+' || ch === '-' || isDecimalDigit(ch)) {
|
|
194
|
-
number += getNextChar();
|
|
195
|
-
while (true) {
|
|
196
|
-
ch = peekNextChar();
|
|
197
|
-
if (!isDecimalDigit(ch)) {
|
|
198
|
-
break;
|
|
199
|
-
}
|
|
200
|
-
number += getNextChar();
|
|
201
|
-
}
|
|
202
|
-
} else {
|
|
203
|
-
ch = 'character ' + ch;
|
|
204
|
-
if (index >= length) {
|
|
205
|
-
ch = '<end>';
|
|
206
|
-
}
|
|
207
|
-
throw new SyntaxError('Unexpected ' + ch + ' after the exponent sign');
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (number === '.') {
|
|
212
|
-
throw new SyntaxError('Expecting decimal digits after the dot sign');
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return createToken(T.Number, number);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
function reset(str) {
|
|
219
|
-
expression = str;
|
|
220
|
-
length = str.length;
|
|
221
|
-
index = 0;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
function next() {
|
|
225
|
-
let token;
|
|
226
|
-
|
|
227
|
-
skipSpaces();
|
|
228
|
-
if (index >= length) {
|
|
229
|
-
return undefined;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
marker = index;
|
|
233
|
-
|
|
234
|
-
token = scanNumber();
|
|
235
|
-
if (typeof token !== 'undefined') {
|
|
236
|
-
return token;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
token = scanOperator();
|
|
240
|
-
if (typeof token !== 'undefined') {
|
|
241
|
-
return token;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
token = scanIdentifier();
|
|
245
|
-
if (typeof token !== 'undefined') {
|
|
246
|
-
return token;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
throw new SyntaxError('Unknown token from character ' + peekNextChar());
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function peek() {
|
|
254
|
-
let token;
|
|
255
|
-
let idx = index;
|
|
256
|
-
try {
|
|
257
|
-
token = next();
|
|
258
|
-
delete token.start;
|
|
259
|
-
delete token.end;
|
|
260
|
-
} catch (e) {
|
|
261
|
-
token = undefined;
|
|
262
|
-
}
|
|
263
|
-
index = idx;
|
|
264
|
-
|
|
265
|
-
return token;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return {
|
|
269
|
-
reset: reset,
|
|
270
|
-
next: next,
|
|
271
|
-
peek: peek
|
|
272
|
-
};
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
TapDigit.Parser = function () {
|
|
276
|
-
let lexer = new TapDigit.Lexer(),
|
|
277
|
-
T = TapDigit.Token;
|
|
278
|
-
|
|
279
|
-
function matchOp(token, op) {
|
|
280
|
-
return (typeof token !== 'undefined') &&
|
|
281
|
-
token.type === T.Operator &&
|
|
282
|
-
op.includes(token.value);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// ArgumentList := Expression |
|
|
286
|
-
// Expression ',' ArgumentList
|
|
287
|
-
function parseArgumentList() {
|
|
288
|
-
let token;
|
|
289
|
-
let expr;
|
|
290
|
-
let args = [];
|
|
291
|
-
|
|
292
|
-
while (true) {
|
|
293
|
-
expr = parseExpression();
|
|
294
|
-
if (typeof expr === 'undefined') {
|
|
295
|
-
// @todo maybe throw exception?
|
|
296
|
-
break;
|
|
297
|
-
}
|
|
298
|
-
args.push(expr);
|
|
299
|
-
token = lexer.peek();
|
|
300
|
-
if (!matchOp(token, ',')) {
|
|
301
|
-
break;
|
|
302
|
-
}
|
|
303
|
-
lexer.next();
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
return args;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// FunctionCall ::= Identifier '(' ')' ||
|
|
310
|
-
// Identifier '(' ArgumentList ')'
|
|
311
|
-
function parseFunctionCall(name) {
|
|
312
|
-
let args = [];
|
|
313
|
-
let token = lexer.next();
|
|
314
|
-
if (!matchOp(token, '(')) {
|
|
315
|
-
throw new SyntaxError('Expecting ( in a function call "' + name + '"');
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
token = lexer.peek();
|
|
319
|
-
if (!matchOp(token, ')')) {
|
|
320
|
-
args = parseArgumentList();
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
token = lexer.next();
|
|
324
|
-
if (!matchOp(token, ')')) {
|
|
325
|
-
throw new SyntaxError('Expecting ) in a function call "' + name + '"');
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
return {
|
|
329
|
-
'FunctionCall' : {
|
|
330
|
-
'name': name,
|
|
331
|
-
'args': args
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// Primary ::= Identifier |
|
|
337
|
-
// Number |
|
|
338
|
-
// '(' Expression ')' |
|
|
339
|
-
// FunctionCall
|
|
340
|
-
function parsePrimary() {
|
|
341
|
-
let expr;
|
|
342
|
-
let token = lexer.peek();
|
|
343
|
-
if (typeof token === 'undefined') {
|
|
344
|
-
throw new SyntaxError('Unexpected termination of expression');
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
if (token.type === T.Identifier) {
|
|
348
|
-
token = lexer.next();
|
|
349
|
-
if (matchOp(lexer.peek(), '(')) {
|
|
350
|
-
return parseFunctionCall(token.value);
|
|
351
|
-
} else {
|
|
352
|
-
return {
|
|
353
|
-
'Identifier': token.value
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
if (token.type === T.Number) {
|
|
359
|
-
token = lexer.next();
|
|
360
|
-
return {
|
|
361
|
-
'Number': token.value
|
|
362
|
-
};
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
if (matchOp(token, '(')) {
|
|
366
|
-
lexer.next();
|
|
367
|
-
expr = parseExpression();
|
|
368
|
-
token = lexer.next();
|
|
369
|
-
if (!matchOp(token, ')')) {
|
|
370
|
-
throw new SyntaxError('Expecting )');
|
|
371
|
-
}
|
|
372
|
-
return {
|
|
373
|
-
'Expression': expr
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
throw new SyntaxError('Parse error, can not process token ' + token.value);
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// Unary ::= Primary |
|
|
381
|
-
// '-' Unary
|
|
382
|
-
function parseUnary() {
|
|
383
|
-
let expr;
|
|
384
|
-
let token = lexer.peek();
|
|
385
|
-
if (matchOp(token, '-+')) {
|
|
386
|
-
token = lexer.next();
|
|
387
|
-
expr = parseUnary();
|
|
388
|
-
return {
|
|
389
|
-
'Unary': {
|
|
390
|
-
operator: token.value,
|
|
391
|
-
expression: expr
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
return parsePrimary();
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
function parseSuperscript(ch) {
|
|
400
|
-
if (typeof SUP_MAPPING[ch] === 'number') {
|
|
401
|
-
return {'Number': SUP_MAPPING[ch]};
|
|
402
|
-
}
|
|
403
|
-
return null;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// Power ::= Unary |
|
|
407
|
-
// Power '^' Unary |
|
|
408
|
-
// Power⁰¹²³⁴⁵⁶⁷⁸⁹
|
|
409
|
-
function parsePower() {
|
|
410
|
-
let expr = parseUnary();
|
|
411
|
-
let token = lexer.peek();
|
|
412
|
-
while (matchOp(token, '^' + SUP_STRING)) {
|
|
413
|
-
token = lexer.next();
|
|
414
|
-
expr = {
|
|
415
|
-
'Binary': {
|
|
416
|
-
operator: '^',
|
|
417
|
-
left: expr,
|
|
418
|
-
right: token.value !== '^' ? parseSuperscript(token.value) : parseUnary()
|
|
419
|
-
}
|
|
420
|
-
};
|
|
421
|
-
token = lexer.peek();
|
|
422
|
-
}
|
|
423
|
-
return expr;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
// Multiplicative ::= Power |
|
|
427
|
-
// Multiplicative '*' Power |
|
|
428
|
-
// Multiplicative '/' Power |
|
|
429
|
-
function parseMultiplicative() {
|
|
430
|
-
let expr = parsePower();
|
|
431
|
-
let token = lexer.peek();
|
|
432
|
-
while (matchOp(token, '*/')) {
|
|
433
|
-
token = lexer.next();
|
|
434
|
-
expr = {
|
|
435
|
-
'Binary': {
|
|
436
|
-
operator: token.value,
|
|
437
|
-
left: expr,
|
|
438
|
-
right: parsePower()
|
|
439
|
-
}
|
|
440
|
-
};
|
|
441
|
-
token = lexer.peek();
|
|
442
|
-
}
|
|
443
|
-
return expr;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// Additive ::= Multiplicative |
|
|
447
|
-
// Additive '+' Multiplicative |
|
|
448
|
-
// Additive '-' Multiplicative
|
|
449
|
-
function parseAdditive() {
|
|
450
|
-
let expr = parseMultiplicative();
|
|
451
|
-
let token = lexer.peek();
|
|
452
|
-
while (matchOp(token, '+-')) {
|
|
453
|
-
token = lexer.next();
|
|
454
|
-
expr = {
|
|
455
|
-
'Binary': {
|
|
456
|
-
operator: token.value,
|
|
457
|
-
left: expr,
|
|
458
|
-
right: parseMultiplicative()
|
|
459
|
-
}
|
|
460
|
-
};
|
|
461
|
-
token = lexer.peek();
|
|
462
|
-
}
|
|
463
|
-
return expr;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
// Expression ::= Additive
|
|
467
|
-
function parseExpression() {
|
|
468
|
-
return parseAdditive();
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
function parse(expression) {
|
|
472
|
-
lexer.reset(expression);
|
|
473
|
-
let expr = parseExpression();
|
|
474
|
-
let token = lexer.next();
|
|
475
|
-
if (typeof token !== 'undefined') {
|
|
476
|
-
throw new SyntaxError('Unexpected token ' + token.value);
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
return {
|
|
480
|
-
'Expression': expr
|
|
481
|
-
};
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
return {
|
|
485
|
-
parse: parse
|
|
486
|
-
};
|
|
487
|
-
};
|
|
488
|
-
|
|
489
|
-
module.exports = TapDigit;
|
|
1
|
+
/*
|
|
2
|
+
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
|
|
3
|
+
Copyright (C) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
* Redistributions of source code must retain the above copyright
|
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
|
10
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
11
|
+
notice, this list of conditions and the following disclaimer in the
|
|
12
|
+
documentation and/or other materials provided with the distribution.
|
|
13
|
+
* Neither the name of the <organization> nor the
|
|
14
|
+
names of its contributors may be used to endorse or promote products
|
|
15
|
+
derived from this software without specific prior written permission.
|
|
16
|
+
|
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
18
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
19
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
20
|
+
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
|
21
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
22
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
23
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
24
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
25
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
26
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/* eslint-disable jsdoc/require-jsdoc */
|
|
30
|
+
/**
|
|
31
|
+
* @ignore
|
|
32
|
+
*/
|
|
33
|
+
let TapDigit = {
|
|
34
|
+
Token: {
|
|
35
|
+
Operator: 'Operator',
|
|
36
|
+
Identifier: 'Identifier',
|
|
37
|
+
Number: 'Number'
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const SUP_MAPPING = {
|
|
42
|
+
'⁰': 0,
|
|
43
|
+
'¹': 1,
|
|
44
|
+
'²': 2,
|
|
45
|
+
'³': 3,
|
|
46
|
+
'⁴': 4,
|
|
47
|
+
'⁵': 5,
|
|
48
|
+
'⁶': 6,
|
|
49
|
+
'⁷': 7,
|
|
50
|
+
'⁸': 8,
|
|
51
|
+
'⁹': 9
|
|
52
|
+
};
|
|
53
|
+
const SUP_STRING = Object.keys(SUP_MAPPING).join('');
|
|
54
|
+
|
|
55
|
+
TapDigit.Lexer = function () {
|
|
56
|
+
let expression = '',
|
|
57
|
+
length = 0,
|
|
58
|
+
index = 0,
|
|
59
|
+
marker = 0,
|
|
60
|
+
T = TapDigit.Token;
|
|
61
|
+
|
|
62
|
+
function peekNextChar() {
|
|
63
|
+
let idx = index;
|
|
64
|
+
return ((idx < length) ? expression.charAt(idx) : '\x00');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function getNextChar() {
|
|
68
|
+
let ch = '\x00',
|
|
69
|
+
idx = index;
|
|
70
|
+
if (idx < length) {
|
|
71
|
+
ch = expression.charAt(idx);
|
|
72
|
+
index += 1;
|
|
73
|
+
}
|
|
74
|
+
return ch;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function isWhiteSpace(ch) {
|
|
78
|
+
return (ch === '\u0009') || (ch === ' ') || (ch === '\u00A0');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function isLetter(ch) {
|
|
82
|
+
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isDecimalDigit(ch) {
|
|
86
|
+
return (ch >= '0') && (ch <= '9');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function createToken(type, value) {
|
|
90
|
+
return {
|
|
91
|
+
type: type,
|
|
92
|
+
value: value,
|
|
93
|
+
start: marker,
|
|
94
|
+
end: index - 1
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function skipSpaces() {
|
|
99
|
+
let ch;
|
|
100
|
+
|
|
101
|
+
while (index < length) {
|
|
102
|
+
ch = peekNextChar();
|
|
103
|
+
if (!isWhiteSpace(ch)) {
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
getNextChar();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function scanOperator() {
|
|
111
|
+
let ch = peekNextChar();
|
|
112
|
+
if (('+-*/()^,' + SUP_STRING).indexOf(ch) >= 0) {
|
|
113
|
+
return createToken(T.Operator, getNextChar());
|
|
114
|
+
}
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function isIdentifierStart(ch) {
|
|
119
|
+
return (ch === '_') || (ch === '#') || (ch === '$') || isLetter(ch);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function isAdditionalNamespaceChar(ch) {
|
|
123
|
+
return (ch === '-') || (ch === '.') || (ch === '~') || (ch === '@');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isIdentifierPart(ch, ns = false) {
|
|
127
|
+
return (ch === '_') || isLetter(ch) || isDecimalDigit(ch) || (ns && isAdditionalNamespaceChar(ch));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function scanIdentifier() {
|
|
131
|
+
let startCh = peekNextChar();
|
|
132
|
+
if (!isIdentifierStart(startCh)) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let id = getNextChar();
|
|
137
|
+
let ns = false;
|
|
138
|
+
while (true) {
|
|
139
|
+
let ch = peekNextChar();
|
|
140
|
+
// If the first character is a $, it is allowed that more $ follow directly after
|
|
141
|
+
if (startCh === '$') {
|
|
142
|
+
if (ch !== '$') {
|
|
143
|
+
startCh = ''; // Stop allowing $ once the first non-$ has been found
|
|
144
|
+
} // else: allowed
|
|
145
|
+
}
|
|
146
|
+
else if (ch === '@') {
|
|
147
|
+
ns = true;
|
|
148
|
+
}
|
|
149
|
+
else if (!isIdentifierPart(ch, ns)) {
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
id += getNextChar();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return createToken(T.Identifier, id);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function scanNumber() {
|
|
159
|
+
let ch;
|
|
160
|
+
let number;
|
|
161
|
+
|
|
162
|
+
ch = peekNextChar();
|
|
163
|
+
if (!isDecimalDigit(ch) && (ch !== '.')) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
number = '';
|
|
168
|
+
if (ch !== '.') {
|
|
169
|
+
number = getNextChar();
|
|
170
|
+
while (true) {
|
|
171
|
+
ch = peekNextChar();
|
|
172
|
+
if (!isDecimalDigit(ch)) {
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
number += getNextChar();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (ch === '.') {
|
|
180
|
+
number += getNextChar();
|
|
181
|
+
while (true) {
|
|
182
|
+
ch = peekNextChar();
|
|
183
|
+
if (!isDecimalDigit(ch)) {
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
number += getNextChar();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (ch === 'e' || ch === 'E') {
|
|
191
|
+
number += getNextChar();
|
|
192
|
+
ch = peekNextChar();
|
|
193
|
+
if (ch === '+' || ch === '-' || isDecimalDigit(ch)) {
|
|
194
|
+
number += getNextChar();
|
|
195
|
+
while (true) {
|
|
196
|
+
ch = peekNextChar();
|
|
197
|
+
if (!isDecimalDigit(ch)) {
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
number += getNextChar();
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
ch = 'character ' + ch;
|
|
204
|
+
if (index >= length) {
|
|
205
|
+
ch = '<end>';
|
|
206
|
+
}
|
|
207
|
+
throw new SyntaxError('Unexpected ' + ch + ' after the exponent sign');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (number === '.') {
|
|
212
|
+
throw new SyntaxError('Expecting decimal digits after the dot sign');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return createToken(T.Number, number);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function reset(str) {
|
|
219
|
+
expression = str;
|
|
220
|
+
length = str.length;
|
|
221
|
+
index = 0;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function next() {
|
|
225
|
+
let token;
|
|
226
|
+
|
|
227
|
+
skipSpaces();
|
|
228
|
+
if (index >= length) {
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
marker = index;
|
|
233
|
+
|
|
234
|
+
token = scanNumber();
|
|
235
|
+
if (typeof token !== 'undefined') {
|
|
236
|
+
return token;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
token = scanOperator();
|
|
240
|
+
if (typeof token !== 'undefined') {
|
|
241
|
+
return token;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
token = scanIdentifier();
|
|
245
|
+
if (typeof token !== 'undefined') {
|
|
246
|
+
return token;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
throw new SyntaxError('Unknown token from character ' + peekNextChar());
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function peek() {
|
|
254
|
+
let token;
|
|
255
|
+
let idx = index;
|
|
256
|
+
try {
|
|
257
|
+
token = next();
|
|
258
|
+
delete token.start;
|
|
259
|
+
delete token.end;
|
|
260
|
+
} catch (e) {
|
|
261
|
+
token = undefined;
|
|
262
|
+
}
|
|
263
|
+
index = idx;
|
|
264
|
+
|
|
265
|
+
return token;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
reset: reset,
|
|
270
|
+
next: next,
|
|
271
|
+
peek: peek
|
|
272
|
+
};
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
TapDigit.Parser = function () {
|
|
276
|
+
let lexer = new TapDigit.Lexer(),
|
|
277
|
+
T = TapDigit.Token;
|
|
278
|
+
|
|
279
|
+
function matchOp(token, op) {
|
|
280
|
+
return (typeof token !== 'undefined') &&
|
|
281
|
+
token.type === T.Operator &&
|
|
282
|
+
op.includes(token.value);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// ArgumentList := Expression |
|
|
286
|
+
// Expression ',' ArgumentList
|
|
287
|
+
function parseArgumentList() {
|
|
288
|
+
let token;
|
|
289
|
+
let expr;
|
|
290
|
+
let args = [];
|
|
291
|
+
|
|
292
|
+
while (true) {
|
|
293
|
+
expr = parseExpression();
|
|
294
|
+
if (typeof expr === 'undefined') {
|
|
295
|
+
// @todo maybe throw exception?
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
args.push(expr);
|
|
299
|
+
token = lexer.peek();
|
|
300
|
+
if (!matchOp(token, ',')) {
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
lexer.next();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return args;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// FunctionCall ::= Identifier '(' ')' ||
|
|
310
|
+
// Identifier '(' ArgumentList ')'
|
|
311
|
+
function parseFunctionCall(name) {
|
|
312
|
+
let args = [];
|
|
313
|
+
let token = lexer.next();
|
|
314
|
+
if (!matchOp(token, '(')) {
|
|
315
|
+
throw new SyntaxError('Expecting ( in a function call "' + name + '"');
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
token = lexer.peek();
|
|
319
|
+
if (!matchOp(token, ')')) {
|
|
320
|
+
args = parseArgumentList();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
token = lexer.next();
|
|
324
|
+
if (!matchOp(token, ')')) {
|
|
325
|
+
throw new SyntaxError('Expecting ) in a function call "' + name + '"');
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return {
|
|
329
|
+
'FunctionCall' : {
|
|
330
|
+
'name': name,
|
|
331
|
+
'args': args
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Primary ::= Identifier |
|
|
337
|
+
// Number |
|
|
338
|
+
// '(' Expression ')' |
|
|
339
|
+
// FunctionCall
|
|
340
|
+
function parsePrimary() {
|
|
341
|
+
let expr;
|
|
342
|
+
let token = lexer.peek();
|
|
343
|
+
if (typeof token === 'undefined') {
|
|
344
|
+
throw new SyntaxError('Unexpected termination of expression');
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (token.type === T.Identifier) {
|
|
348
|
+
token = lexer.next();
|
|
349
|
+
if (matchOp(lexer.peek(), '(')) {
|
|
350
|
+
return parseFunctionCall(token.value);
|
|
351
|
+
} else {
|
|
352
|
+
return {
|
|
353
|
+
'Identifier': token.value
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (token.type === T.Number) {
|
|
359
|
+
token = lexer.next();
|
|
360
|
+
return {
|
|
361
|
+
'Number': token.value
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (matchOp(token, '(')) {
|
|
366
|
+
lexer.next();
|
|
367
|
+
expr = parseExpression();
|
|
368
|
+
token = lexer.next();
|
|
369
|
+
if (!matchOp(token, ')')) {
|
|
370
|
+
throw new SyntaxError('Expecting )');
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
'Expression': expr
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
throw new SyntaxError('Parse error, can not process token ' + token.value);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Unary ::= Primary |
|
|
381
|
+
// '-' Unary
|
|
382
|
+
function parseUnary() {
|
|
383
|
+
let expr;
|
|
384
|
+
let token = lexer.peek();
|
|
385
|
+
if (matchOp(token, '-+')) {
|
|
386
|
+
token = lexer.next();
|
|
387
|
+
expr = parseUnary();
|
|
388
|
+
return {
|
|
389
|
+
'Unary': {
|
|
390
|
+
operator: token.value,
|
|
391
|
+
expression: expr
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
return parsePrimary();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function parseSuperscript(ch) {
|
|
400
|
+
if (typeof SUP_MAPPING[ch] === 'number') {
|
|
401
|
+
return {'Number': SUP_MAPPING[ch]};
|
|
402
|
+
}
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Power ::= Unary |
|
|
407
|
+
// Power '^' Unary |
|
|
408
|
+
// Power⁰¹²³⁴⁵⁶⁷⁸⁹
|
|
409
|
+
function parsePower() {
|
|
410
|
+
let expr = parseUnary();
|
|
411
|
+
let token = lexer.peek();
|
|
412
|
+
while (matchOp(token, '^' + SUP_STRING)) {
|
|
413
|
+
token = lexer.next();
|
|
414
|
+
expr = {
|
|
415
|
+
'Binary': {
|
|
416
|
+
operator: '^',
|
|
417
|
+
left: expr,
|
|
418
|
+
right: token.value !== '^' ? parseSuperscript(token.value) : parseUnary()
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
token = lexer.peek();
|
|
422
|
+
}
|
|
423
|
+
return expr;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Multiplicative ::= Power |
|
|
427
|
+
// Multiplicative '*' Power |
|
|
428
|
+
// Multiplicative '/' Power |
|
|
429
|
+
function parseMultiplicative() {
|
|
430
|
+
let expr = parsePower();
|
|
431
|
+
let token = lexer.peek();
|
|
432
|
+
while (matchOp(token, '*/')) {
|
|
433
|
+
token = lexer.next();
|
|
434
|
+
expr = {
|
|
435
|
+
'Binary': {
|
|
436
|
+
operator: token.value,
|
|
437
|
+
left: expr,
|
|
438
|
+
right: parsePower()
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
token = lexer.peek();
|
|
442
|
+
}
|
|
443
|
+
return expr;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// Additive ::= Multiplicative |
|
|
447
|
+
// Additive '+' Multiplicative |
|
|
448
|
+
// Additive '-' Multiplicative
|
|
449
|
+
function parseAdditive() {
|
|
450
|
+
let expr = parseMultiplicative();
|
|
451
|
+
let token = lexer.peek();
|
|
452
|
+
while (matchOp(token, '+-')) {
|
|
453
|
+
token = lexer.next();
|
|
454
|
+
expr = {
|
|
455
|
+
'Binary': {
|
|
456
|
+
operator: token.value,
|
|
457
|
+
left: expr,
|
|
458
|
+
right: parseMultiplicative()
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
token = lexer.peek();
|
|
462
|
+
}
|
|
463
|
+
return expr;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// Expression ::= Additive
|
|
467
|
+
function parseExpression() {
|
|
468
|
+
return parseAdditive();
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function parse(expression) {
|
|
472
|
+
lexer.reset(expression);
|
|
473
|
+
let expr = parseExpression();
|
|
474
|
+
let token = lexer.next();
|
|
475
|
+
if (typeof token !== 'undefined') {
|
|
476
|
+
throw new SyntaxError('Unexpected token ' + token.value);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
return {
|
|
480
|
+
'Expression': expr
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return {
|
|
485
|
+
parse: parse
|
|
486
|
+
};
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
module.exports = TapDigit;
|