-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
parse.js
1789 lines (1534 loc) · 47.3 KB
/
parse.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { factory } from '../utils/factory.js'
import { isAccessorNode, isConstantNode, isFunctionNode, isOperatorNode, isSymbolNode, rule2Node } from '../utils/is.js'
import { deepMap } from '../utils/collection.js'
import { safeNumberType } from '../utils/number.js'
import { hasOwnProperty } from '../utils/object.js'
const name = 'parse'
const dependencies = [
'typed',
'numeric',
'config',
'AccessorNode',
'ArrayNode',
'AssignmentNode',
'BlockNode',
'ConditionalNode',
'ConstantNode',
'FunctionAssignmentNode',
'FunctionNode',
'IndexNode',
'ObjectNode',
'OperatorNode',
'ParenthesisNode',
'RangeNode',
'RelationalNode',
'SymbolNode'
]
export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
typed,
numeric,
config,
AccessorNode,
ArrayNode,
AssignmentNode,
BlockNode,
ConditionalNode,
ConstantNode,
FunctionAssignmentNode,
FunctionNode,
IndexNode,
ObjectNode,
OperatorNode,
ParenthesisNode,
RangeNode,
RelationalNode,
SymbolNode
}) => {
/**
* Parse an expression. Returns a node tree, which can be evaluated by
* invoking node.evaluate().
*
* Note the evaluating arbitrary expressions may involve security risks,
* see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information.
*
* Syntax:
*
* math.parse(expr)
* math.parse(expr, options)
* math.parse([expr1, expr2, expr3, ...])
* math.parse([expr1, expr2, expr3, ...], options)
*
* Example:
*
* const node1 = math.parse('sqrt(3^2 + 4^2)')
* node1.compile().evaluate() // 5
*
* let scope = {a:3, b:4}
* const node2 = math.parse('a * b') // 12
* const code2 = node2.compile()
* code2.evaluate(scope) // 12
* scope.a = 5
* code2.evaluate(scope) // 20
*
* const nodes = math.parse(['a = 3', 'b = 4', 'a * b'])
* nodes[2].compile().evaluate() // 12
*
* See also:
*
* evaluate, compile
*
* @param {string | string[] | Matrix} expr Expression to be parsed
* @param {{nodes: Object<string, Node>}} [options] Available options:
* - `nodes` a set of custom nodes
* @return {Node | Node[]} node
* @throws {Error}
*/
const parse = typed(name, {
string: function (expression) {
return parseStart(expression, {})
},
'Array | Matrix': function (expressions) {
return parseMultiple(expressions, {})
},
'string, Object': function (expression, options) {
const extraNodes = options.nodes !== undefined ? options.nodes : {}
return parseStart(expression, extraNodes)
},
'Array | Matrix, Object': parseMultiple
})
function parseMultiple (expressions, options = {}) {
const extraNodes = options.nodes !== undefined ? options.nodes : {}
// parse an array or matrix with expressions
return deepMap(expressions, function (elem) {
if (typeof elem !== 'string') throw new TypeError('String expected')
return parseStart(elem, extraNodes)
})
}
// token types enumeration
const TOKENTYPE = {
NULL: 0,
DELIMITER: 1,
NUMBER: 2,
SYMBOL: 3,
UNKNOWN: 4
}
// map with all delimiters
const DELIMITERS = {
',': true,
'(': true,
')': true,
'[': true,
']': true,
'{': true,
'}': true,
'"': true,
'\'': true,
';': true,
'+': true,
'-': true,
'*': true,
'.*': true,
'/': true,
'./': true,
'%': true,
'^': true,
'.^': true,
'~': true,
'!': true,
'&': true,
'|': true,
'^|': true,
'=': true,
':': true,
'?': true,
'==': true,
'!=': true,
'<': true,
'>': true,
'<=': true,
'>=': true,
'<<': true,
'>>': true,
'>>>': true
}
// map with all named delimiters
const NAMED_DELIMITERS = {
mod: true,
to: true,
in: true,
and: true,
xor: true,
or: true,
not: true
}
const CONSTANTS = {
true: true,
false: false,
null: null,
undefined
}
const NUMERIC_CONSTANTS = [
'NaN',
'Infinity'
]
const ESCAPE_CHARACTERS = {
'"': '"',
"'": "'",
'\\': '\\',
'/': '/',
b: '\b',
f: '\f',
n: '\n',
r: '\r',
t: '\t'
// note that \u is handled separately in parseStringToken()
}
function initialState () {
return {
extraNodes: {}, // current extra nodes, must be careful not to mutate
expression: '', // current expression
comment: '', // last parsed comment
index: 0, // current index in expr
token: '', // current token
tokenType: TOKENTYPE.NULL, // type of the token
nestingLevel: 0, // level of nesting inside parameters, used to ignore newline characters
conditionalLevel: null // when a conditional is being parsed, the level of the conditional is stored here
}
}
/**
* View upto `length` characters of the expression starting at the current character.
*
* @param {Object} state
* @param {number} [length=1] Number of characters to view
* @returns {string}
* @private
*/
function currentString (state, length) {
return state.expression.substr(state.index, length)
}
/**
* View the current character. Returns '' if end of expression is reached.
*
* @param {Object} state
* @returns {string}
* @private
*/
function currentCharacter (state) {
return currentString(state, 1)
}
/**
* Get the next character from the expression.
* The character is stored into the char c. If the end of the expression is
* reached, the function puts an empty string in c.
* @private
*/
function next (state) {
state.index++
}
/**
* Preview the previous character from the expression.
* @return {string} cNext
* @private
*/
function prevCharacter (state) {
return state.expression.charAt(state.index - 1)
}
/**
* Preview the next character from the expression.
* @return {string} cNext
* @private
*/
function nextCharacter (state) {
return state.expression.charAt(state.index + 1)
}
/**
* Get next token in the current string expr.
* The token and token type are available as token and tokenType
* @private
*/
function getToken (state) {
state.tokenType = TOKENTYPE.NULL
state.token = ''
state.comment = ''
// skip over ignored characters:
while (true) {
// comments:
if (currentCharacter(state) === '#') {
while (currentCharacter(state) !== '\n' &&
currentCharacter(state) !== '') {
state.comment += currentCharacter(state)
next(state)
}
}
// whitespace: space, tab, and newline when inside parameters
if (parse.isWhitespace(currentCharacter(state), state.nestingLevel)) {
next(state)
} else {
break
}
}
// check for end of expression
if (currentCharacter(state) === '') {
// token is still empty
state.tokenType = TOKENTYPE.DELIMITER
return
}
// check for new line character
if (currentCharacter(state) === '\n' && !state.nestingLevel) {
state.tokenType = TOKENTYPE.DELIMITER
state.token = currentCharacter(state)
next(state)
return
}
const c1 = currentCharacter(state)
const c2 = currentString(state, 2)
const c3 = currentString(state, 3)
if (c3.length === 3 && DELIMITERS[c3]) {
state.tokenType = TOKENTYPE.DELIMITER
state.token = c3
next(state)
next(state)
next(state)
return
}
// check for delimiters consisting of 2 characters
if (c2.length === 2 && DELIMITERS[c2]) {
state.tokenType = TOKENTYPE.DELIMITER
state.token = c2
next(state)
next(state)
return
}
// check for delimiters consisting of 1 character
if (DELIMITERS[c1]) {
state.tokenType = TOKENTYPE.DELIMITER
state.token = c1
next(state)
return
}
// check for a number
if (parse.isDigitDot(c1)) {
state.tokenType = TOKENTYPE.NUMBER
// check for binary, octal, or hex
const c2 = currentString(state, 2)
if (c2 === '0b' || c2 === '0o' || c2 === '0x') {
state.token += currentCharacter(state)
next(state)
state.token += currentCharacter(state)
next(state)
while (parse.isHexDigit(currentCharacter(state))) {
state.token += currentCharacter(state)
next(state)
}
if (currentCharacter(state) === '.') {
// this number has a radix point
state.token += '.'
next(state)
// get the digits after the radix
while (parse.isHexDigit(currentCharacter(state))) {
state.token += currentCharacter(state)
next(state)
}
} else if (currentCharacter(state) === 'i') {
// this number has a word size suffix
state.token += 'i'
next(state)
// get the word size
while (parse.isDigit(currentCharacter(state))) {
state.token += currentCharacter(state)
next(state)
}
}
return
}
// get number, can have a single dot
if (currentCharacter(state) === '.') {
state.token += currentCharacter(state)
next(state)
if (!parse.isDigit(currentCharacter(state))) {
// this is no number, it is just a dot (can be dot notation)
state.tokenType = TOKENTYPE.DELIMITER
return
}
} else {
while (parse.isDigit(currentCharacter(state))) {
state.token += currentCharacter(state)
next(state)
}
if (parse.isDecimalMark(currentCharacter(state), nextCharacter(state))) {
state.token += currentCharacter(state)
next(state)
}
}
while (parse.isDigit(currentCharacter(state))) {
state.token += currentCharacter(state)
next(state)
}
// check for exponential notation like "2.3e-4", "1.23e50" or "2e+4"
if (currentCharacter(state) === 'E' || currentCharacter(state) === 'e') {
if (parse.isDigit(nextCharacter(state)) || nextCharacter(state) === '-' || nextCharacter(state) === '+') {
state.token += currentCharacter(state)
next(state)
if (currentCharacter(state) === '+' || currentCharacter(state) === '-') {
state.token += currentCharacter(state)
next(state)
}
// Scientific notation MUST be followed by an exponent
if (!parse.isDigit(currentCharacter(state))) {
throw createSyntaxError(state, 'Digit expected, got "' + currentCharacter(state) + '"')
}
while (parse.isDigit(currentCharacter(state))) {
state.token += currentCharacter(state)
next(state)
}
if (parse.isDecimalMark(currentCharacter(state), nextCharacter(state))) {
throw createSyntaxError(state, 'Digit expected, got "' + currentCharacter(state) + '"')
}
} else if (nextCharacter(state) === '.') {
next(state)
throw createSyntaxError(state, 'Digit expected, got "' + currentCharacter(state) + '"')
}
}
return
}
// check for variables, functions, named operators
if (parse.isAlpha(currentCharacter(state), prevCharacter(state), nextCharacter(state))) {
while (parse.isAlpha(currentCharacter(state), prevCharacter(state), nextCharacter(state)) || parse.isDigit(currentCharacter(state))) {
state.token += currentCharacter(state)
next(state)
}
if (hasOwnProperty(NAMED_DELIMITERS, state.token)) {
state.tokenType = TOKENTYPE.DELIMITER
} else {
state.tokenType = TOKENTYPE.SYMBOL
}
return
}
// something unknown is found, wrong characters -> a syntax error
state.tokenType = TOKENTYPE.UNKNOWN
while (currentCharacter(state) !== '') {
state.token += currentCharacter(state)
next(state)
}
throw createSyntaxError(state, 'Syntax error in part "' + state.token + '"')
}
/**
* Get next token and skip newline tokens
*/
function getTokenSkipNewline (state) {
do {
getToken(state)
}
while (state.token === '\n') // eslint-disable-line no-unmodified-loop-condition
}
/**
* Open parameters.
* New line characters will be ignored until closeParams(state) is called
*/
function openParams (state) {
state.nestingLevel++
}
/**
* Close parameters.
* New line characters will no longer be ignored
*/
function closeParams (state) {
state.nestingLevel--
}
/**
* Checks whether the current character `c` is a valid alpha character:
*
* - A latin letter (upper or lower case) Ascii: a-z, A-Z
* - An underscore Ascii: _
* - A dollar sign Ascii: $
* - A latin letter with accents Unicode: \u00C0 - \u02AF
* - A greek letter Unicode: \u0370 - \u03FF
* - A mathematical alphanumeric symbol Unicode: \u{1D400} - \u{1D7FF} excluding invalid code points
*
* The previous and next characters are needed to determine whether
* this character is part of a unicode surrogate pair.
*
* @param {string} c Current character in the expression
* @param {string} cPrev Previous character
* @param {string} cNext Next character
* @return {boolean}
*/
parse.isAlpha = function isAlpha (c, cPrev, cNext) {
return parse.isValidLatinOrGreek(c) ||
parse.isValidMathSymbol(c, cNext) ||
parse.isValidMathSymbol(cPrev, c)
}
/**
* Test whether a character is a valid latin, greek, or letter-like character
* @param {string} c
* @return {boolean}
*/
parse.isValidLatinOrGreek = function isValidLatinOrGreek (c) {
return /^[a-zA-Z_$\u00C0-\u02AF\u0370-\u03FF\u2100-\u214F]$/.test(c)
}
/**
* Test whether two given 16 bit characters form a surrogate pair of a
* unicode math symbol.
*
* https://unicode-table.com/en/
* https://www.wikiwand.com/en/Mathematical_operators_and_symbols_in_Unicode
*
* Note: In ES6 will be unicode aware:
* https://stackoverflow.com/questions/280712/javascript-unicode-regexes
* https://mathiasbynens.be/notes/es6-unicode-regex
*
* @param {string} high
* @param {string} low
* @return {boolean}
*/
parse.isValidMathSymbol = function isValidMathSymbol (high, low) {
return /^[\uD835]$/.test(high) &&
/^[\uDC00-\uDFFF]$/.test(low) &&
/^[^\uDC55\uDC9D\uDCA0\uDCA1\uDCA3\uDCA4\uDCA7\uDCA8\uDCAD\uDCBA\uDCBC\uDCC4\uDD06\uDD0B\uDD0C\uDD15\uDD1D\uDD3A\uDD3F\uDD45\uDD47-\uDD49\uDD51\uDEA6\uDEA7\uDFCC\uDFCD]$/.test(low)
}
/**
* Check whether given character c is a white space character: space, tab, or enter
* @param {string} c
* @param {number} nestingLevel
* @return {boolean}
*/
parse.isWhitespace = function isWhitespace (c, nestingLevel) {
// TODO: also take '\r' carriage return as newline? Or does that give problems on mac?
return c === ' ' || c === '\t' || (c === '\n' && nestingLevel > 0)
}
/**
* Test whether the character c is a decimal mark (dot).
* This is the case when it's not the start of a delimiter '.*', './', or '.^'
* @param {string} c
* @param {string} cNext
* @return {boolean}
*/
parse.isDecimalMark = function isDecimalMark (c, cNext) {
return c === '.' && cNext !== '/' && cNext !== '*' && cNext !== '^'
}
/**
* checks if the given char c is a digit or dot
* @param {string} c a string with one character
* @return {boolean}
*/
parse.isDigitDot = function isDigitDot (c) {
return ((c >= '0' && c <= '9') || c === '.')
}
/**
* checks if the given char c is a digit
* @param {string} c a string with one character
* @return {boolean}
*/
parse.isDigit = function isDigit (c) {
return (c >= '0' && c <= '9')
}
/**
* checks if the given char c is a hex digit
* @param {string} c a string with one character
* @return {boolean}
*/
parse.isHexDigit = function isHexDigit (c) {
return ((c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F'))
}
/**
* Start of the parse levels below, in order of precedence
* @return {Node} node
* @private
*/
function parseStart (expression, extraNodes) {
const state = initialState()
Object.assign(state, { expression, extraNodes })
getToken(state)
const node = parseBlock(state)
// check for garbage at the end of the expression
// an expression ends with a empty character '' and tokenType DELIMITER
if (state.token !== '') {
if (state.tokenType === TOKENTYPE.DELIMITER) {
// user entered a not existing operator like "//"
// TODO: give hints for aliases, for example with "<>" give as hint " did you mean !== ?"
throw createError(state, 'Unexpected operator ' + state.token)
} else {
throw createSyntaxError(state, 'Unexpected part "' + state.token + '"')
}
}
return node
}
/**
* Parse a block with expressions. Expressions can be separated by a newline
* character '\n', or by a semicolon ';'. In case of a semicolon, no output
* of the preceding line is returned.
* @return {Node} node
* @private
*/
function parseBlock (state) {
let node
const blocks = []
let visible
if (state.token !== '' && state.token !== '\n' && state.token !== ';') {
node = parseAssignment(state)
if (state.comment) {
node.comment = state.comment
}
}
// TODO: simplify this loop
while (state.token === '\n' || state.token === ';') { // eslint-disable-line no-unmodified-loop-condition
if (blocks.length === 0 && node) {
visible = (state.token !== ';')
blocks.push({ node, visible })
}
getToken(state)
if (state.token !== '\n' && state.token !== ';' && state.token !== '') {
node = parseAssignment(state)
if (state.comment) {
node.comment = state.comment
}
visible = (state.token !== ';')
blocks.push({ node, visible })
}
}
if (blocks.length > 0) {
return new BlockNode(blocks)
} else {
if (!node) {
node = new ConstantNode(undefined)
if (state.comment) {
node.comment = state.comment
}
}
return node
}
}
/**
* Assignment of a function or variable,
* - can be a variable like 'a=2.3'
* - or a updating an existing variable like 'matrix(2,3:5)=[6,7,8]'
* - defining a function like 'f(x) = x^2'
* @return {Node} node
* @private
*/
function parseAssignment (state) {
let name, args, value, valid
const node = parseConditional(state)
if (state.token === '=') {
if (isSymbolNode(node)) {
// parse a variable assignment like 'a = 2/3'
name = node.name
getTokenSkipNewline(state)
value = parseAssignment(state)
return new AssignmentNode(new SymbolNode(name), value)
} else if (isAccessorNode(node)) {
// parse a matrix subset assignment like 'A[1,2] = 4'
getTokenSkipNewline(state)
value = parseAssignment(state)
return new AssignmentNode(node.object, node.index, value)
} else if (isFunctionNode(node) && isSymbolNode(node.fn)) {
// parse function assignment like 'f(x) = x^2'
valid = true
args = []
name = node.name
node.args.forEach(function (arg, index) {
if (isSymbolNode(arg)) {
args[index] = arg.name
} else {
valid = false
}
})
if (valid) {
getTokenSkipNewline(state)
value = parseAssignment(state)
return new FunctionAssignmentNode(name, args, value)
}
}
throw createSyntaxError(state, 'Invalid left hand side of assignment operator =')
}
return node
}
/**
* conditional operation
*
* condition ? truePart : falsePart
*
* Note: conditional operator is right-associative
*
* @return {Node} node
* @private
*/
function parseConditional (state) {
let node = parseLogicalOr(state)
while (state.token === '?') { // eslint-disable-line no-unmodified-loop-condition
// set a conditional level, the range operator will be ignored as long
// as conditionalLevel === state.nestingLevel.
const prev = state.conditionalLevel
state.conditionalLevel = state.nestingLevel
getTokenSkipNewline(state)
const condition = node
const trueExpr = parseAssignment(state)
if (state.token !== ':') throw createSyntaxError(state, 'False part of conditional expression expected')
state.conditionalLevel = null
getTokenSkipNewline(state)
const falseExpr = parseAssignment(state) // Note: check for conditional operator again, right associativity
node = new ConditionalNode(condition, trueExpr, falseExpr)
// restore the previous conditional level
state.conditionalLevel = prev
}
return node
}
/**
* logical or, 'x or y'
* @return {Node} node
* @private
*/
function parseLogicalOr (state) {
let node = parseLogicalXor(state)
while (state.token === 'or') { // eslint-disable-line no-unmodified-loop-condition
getTokenSkipNewline(state)
node = new OperatorNode('or', 'or', [node, parseLogicalXor(state)])
}
return node
}
/**
* logical exclusive or, 'x xor y'
* @return {Node} node
* @private
*/
function parseLogicalXor (state) {
let node = parseLogicalAnd(state)
while (state.token === 'xor') { // eslint-disable-line no-unmodified-loop-condition
getTokenSkipNewline(state)
node = new OperatorNode('xor', 'xor', [node, parseLogicalAnd(state)])
}
return node
}
/**
* logical and, 'x and y'
* @return {Node} node
* @private
*/
function parseLogicalAnd (state) {
let node = parseBitwiseOr(state)
while (state.token === 'and') { // eslint-disable-line no-unmodified-loop-condition
getTokenSkipNewline(state)
node = new OperatorNode('and', 'and', [node, parseBitwiseOr(state)])
}
return node
}
/**
* bitwise or, 'x | y'
* @return {Node} node
* @private
*/
function parseBitwiseOr (state) {
let node = parseBitwiseXor(state)
while (state.token === '|') { // eslint-disable-line no-unmodified-loop-condition
getTokenSkipNewline(state)
node = new OperatorNode('|', 'bitOr', [node, parseBitwiseXor(state)])
}
return node
}
/**
* bitwise exclusive or (xor), 'x ^| y'
* @return {Node} node
* @private
*/
function parseBitwiseXor (state) {
let node = parseBitwiseAnd(state)
while (state.token === '^|') { // eslint-disable-line no-unmodified-loop-condition
getTokenSkipNewline(state)
node = new OperatorNode('^|', 'bitXor', [node, parseBitwiseAnd(state)])
}
return node
}
/**
* bitwise and, 'x & y'
* @return {Node} node
* @private
*/
function parseBitwiseAnd (state) {
let node = parseRelational(state)
while (state.token === '&') { // eslint-disable-line no-unmodified-loop-condition
getTokenSkipNewline(state)
node = new OperatorNode('&', 'bitAnd', [node, parseRelational(state)])
}
return node
}
/**
* Parse a chained conditional, like 'a > b >= c'
* @return {Node} node
*/
function parseRelational (state) {
const params = [parseShift(state)]
const conditionals = []
const operators = {
'==': 'equal',
'!=': 'unequal',
'<': 'smaller',
'>': 'larger',
'<=': 'smallerEq',
'>=': 'largerEq'
}
while (hasOwnProperty(operators, state.token)) { // eslint-disable-line no-unmodified-loop-condition
const cond = { name: state.token, fn: operators[state.token] }
conditionals.push(cond)
getTokenSkipNewline(state)
params.push(parseShift(state))
}
if (params.length === 1) {
return params[0]
} else if (params.length === 2) {
return new OperatorNode(conditionals[0].name, conditionals[0].fn, params)
} else {
return new RelationalNode(conditionals.map(c => c.fn), params)
}
}
/**
* Bitwise left shift, bitwise right arithmetic shift, bitwise right logical shift
* @return {Node} node
* @private
*/
function parseShift (state) {
let node, name, fn, params
node = parseConversion(state)
const operators = {
'<<': 'leftShift',
'>>': 'rightArithShift',
'>>>': 'rightLogShift'
}
while (hasOwnProperty(operators, state.token)) {
name = state.token
fn = operators[name]
getTokenSkipNewline(state)
params = [node, parseConversion(state)]
node = new OperatorNode(name, fn, params)
}
return node
}
/**
* conversion operators 'to' and 'in'
* @return {Node} node
* @private
*/
function parseConversion (state) {
let node, name, fn, params
node = parseRange(state)
const operators = {
to: 'to',
in: 'to' // alias of 'to'
}
while (hasOwnProperty(operators, state.token)) {
name = state.token
fn = operators[name]
getTokenSkipNewline(state)
if (name === 'in' && state.token === '') {
// end of expression -> this is the unit 'in' ('inch')
node = new OperatorNode('*', 'multiply', [node, new SymbolNode('in')], true)
} else {
// operator 'a to b' or 'a in b'
params = [node, parseRange(state)]
node = new OperatorNode(name, fn, params)
}
}
return node
}
/**
* parse range, "start:end", "start:step:end", ":", "start:", ":end", etc
* @return {Node} node
* @private
*/
function parseRange (state) {
let node
const params = []
if (state.token === ':') {
// implicit start=1 (one-based)
node = new ConstantNode(1)
} else {
// explicit start
node = parseAddSubtract(state)
}
if (state.token === ':' && (state.conditionalLevel !== state.nestingLevel)) {
// we ignore the range operator when a conditional operator is being processed on the same level
params.push(node)
// parse step and end
while (state.token === ':' && params.length < 3) { // eslint-disable-line no-unmodified-loop-condition
getTokenSkipNewline(state)
if (state.token === ')' || state.token === ']' || state.token === ',' || state.token === '') {
// implicit end
params.push(new SymbolNode('end'))
} else {
// explicit end
params.push(parseAddSubtract(state))
}
}
if (params.length === 3) {
// params = [start, step, end]
node = new RangeNode(params[0], params[2], params[1]) // start, end, step
} else { // length === 2
// params = [start, end]
node = new RangeNode(params[0], params[1]) // start, end
}
}
return node
}
/**
* add or subtract
* @return {Node} node
* @private
*/