-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathearley-tests.js
More file actions
1948 lines (1898 loc) · 75.9 KB
/
earley-tests.js
File metadata and controls
1948 lines (1898 loc) · 75.9 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
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 { Grammar, Tokenizer } from './earley-parser.js'
describe( 'Grammar and Tokenizer classes', () => {
it( 'should be defined', () => {
expect( Grammar ).to.be.ok
expect( Tokenizer ).to.be.ok
} )
} )
describe( 'A simple grammar', () => {
let G
// Define the grammar here. D is for digit, I for (nonnegative) integer, M for
// multiplication expression, and S for summation expression.
beforeEach( () => {
G = new Grammar( 'S' )
G.addRule( 'D', /[0-9]/ )
G.addRule( 'I', 'D', 'I D' )
G.addRule( 'M', 'I', [ 'M', /\*/, 'I' ] )
G.addRule( 'S', 'M', [ 'S', /\+/, 'M' ] )
} )
it( 'Should correctly parse nonnegative integers', () => {
expect( G.parse( '5' ) ).to.eql(
[ [ 'S', [ 'M', [ 'I', [ 'D', '5' ] ] ] ] ] )
expect( G.parse( '19' ) ).to.eql(
[ [ 'S', [ 'M', [ 'I', [ 'I', [ 'D', '1' ] ], [ 'D', '9' ] ] ] ] ] )
G.setOption( 'addCategories', false )
G.setOption( 'collapseBranches', true )
expect( G.parse( '5' ) ).to.eql( [ '5' ] )
expect( G.parse( '19' ) ).to.eql( [ [ '1', '9' ] ] )
} )
it( 'should parse products of nonnegative integers', () => {
expect( G.parse( '7*5' ) ).to.eql(
[ [ 'S', [ 'M', [ 'M', [ 'I', [ 'D', '7' ] ] ], '*',
[ 'I', [ 'D', '5' ] ] ] ] ] )
G.setOption( 'addCategories', false )
G.setOption( 'collapseBranches', true )
expect( G.parse( '7*5*3*1' ) ).to.eql(
[ [ [ [ '7', '*', '5' ], '*', '3' ], '*', '1' ] ] )
} )
it( 'should parse sums of products of nonnegative integers', () => {
expect( G.parse( '1+2' ) ).to.eql(
[ [ 'S', [ 'S', [ 'M', [ 'I', [ 'D', '1' ] ] ] ], '+',
[ 'M', [ 'I', [ 'D', '2' ] ] ] ] ] )
G.setOption( 'addCategories', false )
G.setOption( 'collapseBranches', true )
expect( G.parse( '3*6+9' ) ).to.eql(
[ [ [ '3', '*', '6' ], '+', '9' ] ] )
expect( G.parse( '3+6*9' ) ).to.eql(
[ [ '3', '+', [ '6', '*', '9' ] ] ] )
G.setOption( 'expressionBuilder', x =>
x instanceof Array ? `(${x.join('')})` : `${x}` )
expect( G.parse( '3+6*9' ) ).to.eql( [ '(3+(6*9))' ] )
} )
} )
describe( 'A simple Tokenizer', () => {
it( 'should tokenize arithmetic expressions', () => {
const T = new Tokenizer
T.addType( /[a-zA-Z_][a-zA-Z_0-9]*/ )
T.addType( /\.[0-9]+|[0-9]+\.?[0-9]*/ )
T.addType( /"(?:[^\\"]|\\\\|\\")*"/ )
T.addType( /[()+/*-]/ )
expect( T.tokenize( '5' ) ).to.eql( [ '5' ] )
expect( T.tokenize( '19' ) ).to.eql( [ '19' ] )
expect( T.tokenize( '6-9' ) ).to.eql( [ '6', '-', '9' ] )
expect( T.tokenize( 'x*-5.0/(_tmp+k)' ) ).to.eql(
[ 'x', '*', '-', '5.0', '/', '(', '_tmp', '+', 'k', ')' ] )
expect( T.tokenize( 'alert("message")' ) ).to.eql(
[ 'alert', '(', '"message"', ')' ] )
} )
it( 'should support format functions', () => {
const T = new Tokenizer
T.addType( /\s/, () => null )
T.addType( /[a-zA-Z_][a-zA-Z_0-9]*/ )
T.addType( /\.[0-9]+|[0-9]+\.?[0-9]*/ )
T.addType( /"(?:[^\\"]|\\\\|\\")*"/ )
T.addType( /\/((?:[^\\\/]|\\\\|\\\/)*)\//,
( text, match ) => `RegExp(${match[1]})` )
T.addType( /[()+/*-]/ )
expect( T.tokenize( '5' ) ).to.eql( [ '5' ] )
expect( T.tokenize( '19' ) ).to.eql( [ '19' ] )
expect( T.tokenize( '6-9' ) ).to.eql( [ '6', '-', '9' ] )
expect( T.tokenize( 'x*-5.0/(_tmp+k)' ) ).to.eql(
[ 'x', '*', '-', '5.0', '/', '(', '_tmp', '+', 'k', ')' ] )
expect( T.tokenize( 'alert("message")' ) ).to.eql(
[ 'alert', '(', '"message"', ')' ] )
expect( T.tokenize( 'my(/regexp/)+6' ) ).to.eql(
[ 'my', '(', 'RegExp(regexp)', ')', '+', '6' ] )
expect( T.tokenize( '64 - 8320 + K' ) ).to.eql(
[ '64', '-', '8320', '+', 'K' ] )
} )
it( 'should support format strings', () => {
const T = new Tokenizer
T.addType( /[a-zA-Z_][a-zA-Z_0-9]*/ )
T.addType( /\.[0-9]+|[0-9]+\.?[0-9]*/ )
T.addType( /"(?:[^\\"]|\\\\|\\")*"/ )
T.addType( /\/((?:[^\\\/]|\\\\|\\\/)*)\//, 'RegExp(%1)' )
T.addType( /[()+/*-]/ )
expect( T.tokenize( '5' ) ).to.eql( [ '5' ] )
expect( T.tokenize( '19' ) ).to.eql( [ '19' ] )
expect( T.tokenize( '6-9' ) ).to.eql( [ '6', '-', '9' ] )
expect( T.tokenize( 'x*-5.0/(_tmp+k)' ) ).to.eql(
[ 'x', '*', '-', '5.0', '/', '(', '_tmp', '+', 'k', ')' ] )
expect( T.tokenize( 'alert("message")' ) ).to.eql(
[ 'alert', '(', '"message"', ')' ] )
expect( T.tokenize( 'my(/regexp/)+6' ) ).to.eql(
[ 'my', '(', 'RegExp(regexp)', ')', '+', '6' ] )
} )
} )
describe( 'Tokenizing and parsing', () => {
it( 'should support parsing arrays', () => {
const G = new Grammar( 'S' )
G.addRule( 'I', /[0-9]+/ )
G.addRule( 'M', 'I', [ 'M', /\*/, 'I' ] )
G.addRule( 'S', 'M', [ 'S', /\+/, 'M' ] )
expect( G.parse( [ '5' ] ) ).to.eql(
[ [ 'S', [ 'M', [ 'I', '5' ] ] ] ] )
expect( G.parse( [ '19' ] ) ).to.eql(
[ [ 'S', [ 'M', [ 'I', '19' ] ] ] ] )
G.setOption( 'addCategories', false )
G.setOption( 'collapseBranches', true )
expect( G.parse( [ '5' ] ) ).to.eql( [ '5' ] )
expect( G.parse( [ '19' ] ) ).to.eql( [ '19' ] )
expect( G.parse( [ '7', '*', '50', '*', '33', '*', '1' ] ) )
.to.eql( [ [ [ [ '7', '*', '50' ], '*', '33' ], '*', '1' ] ] )
G.setOption( 'expressionBuilder', x =>
x instanceof Array ? `(${x.join('')})` : `${x}` )
expect( G.parse( [ '333', '+', '726', '*', '2349' ] ) )
.to.eql( [ '(333+(726*2349))' ] )
} )
it( 'should be chainable', () => {
const T = new Tokenizer
T.addType( /\s/, () => null )
T.addType( /[a-zA-Z_][a-zA-Z_0-9]*/ )
T.addType( /\.[0-9]+|[0-9]+\.?[0-9]*/ )
T.addType( /"(?:[^\\"]|\\\\|\\")*"/ )
T.addType( /[()+/*-]/ )
const G = new Grammar( 'expr' )
G.addRule( 'expr', 'sumdiff' )
G.addRule( 'atomic', /[a-zA-Z_][a-zA-Z_0-9]*/ )
G.addRule( 'atomic', /\.[0-9]+|[0-9]+\.?[0-9]*/ )
G.addRule( 'atomic', /"(?:[^\\"]|\\\\|\\")*"/ )
G.addRule( 'atomic', [ /\(/, 'sumdiff', /\)/ ] )
G.addRule( 'prodquo', [ 'atomic' ] )
G.addRule( 'prodquo', [ 'prodquo', /[*/]/, 'atomic' ] )
G.addRule( 'sumdiff', [ 'prodquo' ] )
G.addRule( 'sumdiff', [ 'sumdiff', /[+-]/, 'prodquo' ] )
G.setOption( 'addCategories', false )
G.setOption( 'collapseBranches', true )
G.setOption( 'expressionBuilder', expr =>
( expr[0] == '(' && expr[2] == ')' && expr.length == 3 ) ?
expr[1] : expr )
expect( G.parse( T.tokenize( 'ident-7.8/other' ) ) ).to.eql(
[ [ 'ident', '-', [ '7.8', '/', 'other' ] ] ] )
expect( G.parse( T.tokenize( 'ident*7.8/other' ) ) ).to.eql(
[ [ [ 'ident', '*', '7.8' ], '/', 'other' ] ] )
expect( G.parse( T.tokenize( 'ident*(7.8/other)' ) ) ).to.eql(
[ [ 'ident', '*', [ '7.8', '/', 'other' ] ] ] )
} )
it( 'should be connectable using a parser option', () => {
const T = new Tokenizer
T.addType( /\s/, () => null )
T.addType( /[a-zA-Z_][a-zA-Z_0-9]*/ )
T.addType( /\.[0-9]+|[0-9]+\.?[0-9]*/ )
T.addType( /"(?:[^\\"]|\\\\|\\")*"/ )
T.addType( /[()+/*-]/ )
const G = new Grammar( 'expr' )
G.addRule( 'expr', 'sumdiff' )
G.addRule( 'atomic', /[a-zA-Z_][a-zA-Z_0-9]*/ )
G.addRule( 'atomic', /\.[0-9]+|[0-9]+\.?[0-9]*/ )
G.addRule( 'atomic', /"(?:[^\\"]|\\\\|\\")*"/ )
G.addRule( 'atomic', [ /\(/, 'sumdiff', /\)/ ] )
G.addRule( 'prodquo', [ 'atomic' ] )
G.addRule( 'prodquo', [ 'prodquo', /[*\/]/, 'atomic' ] )
G.addRule( 'sumdiff', [ 'prodquo' ] )
G.addRule( 'sumdiff', [ 'sumdiff', /[+-]/, 'prodquo' ] )
G.setOption( 'addCategories', false )
G.setOption( 'collapseBranches', true )
G.setOption( 'expressionBuilder', expr =>
( expr[0] == '(' && expr[2] == ')' && expr.length == 3 ) ?
expr[1] : expr )
G.setOption( 'tokenizer', T )
expect( G.parse( T.tokenize( 'ident-7.8/other' ) ) ).to.eql(
[ [ 'ident', '-', [ '7.8', '/', 'other' ] ] ] )
expect( G.parse( T.tokenize( 'ident*7.8/other' ) ) ).to.eql(
[ [ [ 'ident', '*', '7.8' ], '/', 'other' ] ] )
expect( G.parse( T.tokenize( 'ident*(7.8/other)' ) ) ).to.eql(
[ [ 'ident', '*', [ '7.8', '/', 'other' ] ] ] )
} )
} )
describe( 'A larger, useful grammar', () => {
let G
const comparator = ( a, b ) => JSON.stringify( a ) == JSON.stringify( b )
// The following function defines a large, complex grammar based on the
// types of content that can be produced by the MathQuill interactive
// equation-builder widget used in some web pages. The exact details are
// not important, but merely that it supports a large set of common
// mathematical content.
// We use the names of various OpenMath symbols here for historical reasons.
// For a time, this repository aimed to support the construction of OpenMath
// objects, though that is no longer a priority. It still makes for a
// useful set of tests.
beforeEach( () => {
G = new Grammar( 'expression' )
// Rules for numbers
G.addRule( 'digit', /[0-9]/ )
G.addRule( 'nonnegint', 'digit' )
G.addRule( 'nonnegint', [ 'digit', 'nonnegint' ] )
G.addRule( 'integer', 'nonnegint' )
G.addRule( 'integer', [ /\u2212|-/, 'nonnegint' ] )
G.addRule( 'float', [ 'integer', /\./, 'nonnegint' ] )
G.addRule( 'float', [ 'integer', /\./ ] )
G.addRule( 'infinity', [ /\u221e/ ] )
// Rules for variables
G.addRule( 'variable', /[a-zA-Z\u0374-\u03FF]/ )
// The above together are called "atomics"
G.addRule( 'atomic', 'integer' )
G.addRule( 'atomic', 'float' )
G.addRule( 'atomic', 'variable' )
G.addRule( 'atomic', 'infinity' )
// Rules for the operations of arithmetic
G.addRule( 'factor', 'atomic' )
G.addRule( 'factor', [ 'atomic', /sup/, 'atomic' ] )
G.addRule( 'factor', [ 'factor', /[%]/ ] )
G.addRule( 'factor', [ /\$/, 'factor' ] )
G.addRule( 'factor', [ 'factor', /sup/, /\u2218/ ] ) // # degree symbol
G.addRule( 'prodquo', 'factor' )
G.addRule( 'prodquo', [ 'prodquo', /[\u00f7\u00d7\u00b7]/, 'factor' ] )
// the above three are divide, times, and cdot
G.addRule( 'prodquo', [ /\u2212|-/, 'prodquo' ] )
G.addRule( 'sumdiff', 'prodquo' )
G.addRule( 'sumdiff', [ 'sumdiff', /[+\u00b1\u2212-]/, 'prodquo' ] )
// the escapes above are for the \pm symbol and the alternate - sign
// Rules for logarithms
G.addRule( 'ln', [ /ln/, 'atomic' ] )
G.addRule( 'log', [ /log/, 'atomic' ] )
G.addRule( 'log', [ /log/, /sub/, 'atomic', 'atomic' ] )
G.addRule( 'prodquo', 'ln' )
G.addRule( 'prodquo', 'log' )
// Rules for factorial
G.addRule( 'factorial', [ 'atomic', /!/ ] )
G.addRule( 'factor', 'factorial' )
// Rules for the operations of set theory (still incomplete)
G.addRule( 'setdiff', 'variable' )
G.addRule( 'setdiff', [ 'setdiff', /[\u223c]/, 'variable' ] )
// Rules for subscripts, which count as function application (so that "x sub i"
// still contains i as a free variable)
G.addRule( 'subscripted', [ 'atomic', /sub/, 'atomic' ] )
G.addRule( 'noun', 'subscripted' )
// Rules for various structures, like fractions, which are treated indivisibly,
// and thus as if they were atomics
G.addRule( 'fraction', [ /fraction/, /\(/, 'atomic', 'atomic', /\)/ ] )
G.addRule( 'atomic', 'fraction' )
G.addRule( 'root', [ /\u221a/, 'atomic' ] )
G.addRule( 'root', [ /nthroot/, 'atomic', /√/, 'atomic' ] )
G.addRule( 'atomic', 'root' )
G.addRule( 'decoration', [ /overline/, 'atomic' ] )
G.addRule( 'decoration', [ /overarc/, 'atomic' ] )
G.addRule( 'atomic', 'decoration' )
G.addRule( 'trigfunc', [ /sin|cos|tan|cot|sec|csc/ ] )
G.addRule( 'trigapp', [ 'trigfunc', 'prodquo' ] )
G.addRule( 'trigapp', [ 'trigfunc', /sup/, /\(/, /-|\u2212/, /1/, /\)/, 'prodquo' ] )
G.addRule( 'atomic', 'trigapp' )
// Rules for limits and summations
G.addRule( 'limit',
[ /lim/, /sub/, /\(/, 'variable', /[\u2192]/, 'expression', /\)/, 'prodquo' ] )
// 2192 is a right arrow
G.addRule( 'takesleftcoeff', 'limit' )
G.addRule( 'sum', [ /[\u03a3]/, // summation sign
/sub/, /\(/, 'variable', /[=]/, 'expression', /\)/,
/sup/, 'atomic', 'prodquo' ] )
G.addRule( 'sum', [ /[\u03a3]/, /sup/, 'atomic', // summation sign
/sub/, /\(/, 'variable', /[=]/, 'expression', /\)/,
'prodquo' ] )
G.addRule( 'takesleftcoeff', 'sum' )
// Rules for differential and integral calculus
G.addRule( 'differential', [ /d/, 'atomic' ] )
G.addRule( 'difffrac', [ /fraction/, /\(/, /d/, /\(/, /d/, 'variable', /\)/, /\)/ ] )
G.addRule( 'indefint', [ /[\u222b]/, 'prodquo' ] ) // integral sign
G.addRule( 'defint', [ /[\u222b]/, /sub/, 'atomic', /sup/, 'atomic', 'prodquo' ] ) // again
G.addRule( 'defint', [ /[\u222b]/, /sup/, 'atomic', /sub/, 'atomic', 'prodquo' ] ) // again
G.addRule( 'factor', 'differential' )
G.addRule( 'factor', 'difffrac' )
G.addRule( 'takesleftcoeff', 'indefint' )
G.addRule( 'takesleftcoeff', 'defint' )
// The category `takesleftcoeff` contains those things that can be multiplied
// on the left, unambiguously, by a coefficient. For instance, a limit, when
// multiplied on the left by a coefficient, is clearly the coefficient times
// the entire limit, as a consequence of the opening marker "lim" which removes
// the possibility for ambiguity. The same is true of summations and
// integrals.
G.addRule( 'sumdiff', 'takesleftcoeff' )
G.addRule( 'sumdiff', [ 'factor', /[\u00f7\u00d7\u00b7]/, 'takesleftcoeff' ] )
G.addRule( 'sumdiff', [ 'prodquo', /[+\u00b1\u2212-]/, 'takesleftcoeff' ] )
// So far we've only defined rules for forming mathematical nouns, so we wrap
// the highest-level non-terminal defined so far, sumdiff, in the label "noun."
G.addRule( 'noun', 'sumdiff' )
G.addRule( 'noun', 'setdiff' )
// Rules for forming sentences from nouns, by placing relations between them
G.addRule( 'atomicsentence',
[ 'noun', /[=\u2260\u2248\u2243\u2264\u2265<>]/, 'noun' ] )
// =, \ne, \approx, \cong, \le, \ge, <, >
G.addRule( 'atomicsentence', [ /[\u00ac]/, 'atomicsentence' ] )
G.addRule( 'sentence', 'atomicsentence' )
G.addRule( 'sentence', [ /[\u2234]/, 'sentence' ] ) // the therefore symbol
// Rules for groupers
G.addRule( 'atomic', [ /\(/, 'noun', /\)/ ] )
G.addRule( 'atomicsentence', [ /\(/, 'sentence', /\)/ ] )
G.addRule( 'interval', [ /[\(\[]/, 'noun', /,/, 'noun', /[\)\]]/ ] )
G.addRule( 'atomic', 'interval' )
G.addRule( 'absval', [ /\|/, 'noun', /\|/ ] )
G.addRule( 'atomic', 'absval' )
// And finally, place "expression" at the top of the grammar; one is permitted
// to use this grammar to express mathematical nouns or complete sentences
G.addRule( 'expression', 'noun' )
G.addRule( 'expression', 'sentence' )
// A function that recursively assembles a made-up proprietary data
// structure from the hierarchy of arrays created by the parser
const makeSym = ( name, category ) => ['Sym:',name+'.'+category]
G.setOption( 'comparator', comparator )
const symbols = {
'+' : makeSym( 'plus', 'arith1' ),
'-' : makeSym( 'minus', 'arith1' ),
'\u2212' : makeSym( 'minus', 'arith1' ),
'\u00b1' : makeSym( 'plusminus', 'multiops' ),
'\u00d7' : makeSym( 'times', 'arith1' ),
'\u00b7' : makeSym( 'times', 'arith1' ),
'\u00f7' : makeSym( 'divide', 'arith1' ),
'^' : makeSym( 'power', 'arith1' ),
'\u221e' : makeSym( 'infinity', 'nums1' ),
'\u221a' : makeSym( 'root', 'arith1' ),
'\u223c' : makeSym( 'set1', 'setdiff' ), // alternate form of ~
'=' : makeSym( 'eq', 'relation1' ),
'<' : makeSym( 'lt', 'relation1' ),
'>' : makeSym( 'gt', 'relation1' ),
'\u2260' : makeSym( 'neq', 'relation1' ),
'\u2248' : makeSym( 'approx', 'relation1' ),
'\u2264' : makeSym( 'le', 'relation1' ),
'\u2265' : makeSym( 'ge', 'relation1' ),
'\u2243' : makeSym( 'modulo_relation', 'integer2' ),
'\u00ac' : makeSym( 'not', 'logic1' ),
'\u2218' : makeSym( 'degrees', 'units' ),
'$' : makeSym( 'dollars', 'units' ),
'%' : makeSym( 'percent', 'units' ),
'\u222b' : makeSym( 'int', 'calculus1' ),
'def\u222b' : makeSym( 'defint', 'calculus1' ),
'ln' : makeSym( 'ln', 'transc1' ),
'log' : makeSym( 'log', 'transc1' ),
'unary-' : makeSym( 'unary_minus', 'arith1' ),
'overarc' : makeSym( 'overarc', 'decoration' ),
'overline' : makeSym( 'overline', 'decoration' ),
'd' : makeSym( 'd', 'diff' ),
'interval_oc' : makeSym( 'interval_oc', 'interval1' ),
'interval_co' : makeSym( 'interval_co', 'interval1' ),
'interval_oo' : makeSym( 'interval_oo', 'interval1' ),
'interval_cc' : makeSym( 'interval_cc', 'interval1' )
}
G.setOption( 'expressionBuilder', expr => {
// console.log( 'eBuilder', JSON.stringify( expr ) )
const build = ( ...args ) => {
// const orig = args.slice()
args = args.map( a => {
if ( typeof( a ) == 'number' ) a = expr[a]
if ( symbols.hasOwnProperty( a ) ) a = symbols[a]
if ( typeof( a ) == 'string' ) a = ['Atomic:',a]
return a
} )
// console.log( '\t', JSON.stringify([orig,args]) )
const tmp = ['App:',...args]
if ( G.expressionBuilderDebug )
console.log( 'build', JSON.stringify( args ), '-->', tmp )
return tmp
}
const atomicValue = atomic =>
[ 'Int:', 'Float:', 'Var:', 'Sym:' ].includes( atomic[0] ) ?
atomic[1] : null
let result
switch ( expr[0] ) {
case 'digit':
case 'nonnegint':
result = expr.slice( 1 ).join( '' )
break
case 'integer':
result = ['Int:',parseInt(expr.slice(1).join(''))]
break
case 'float':
result = [
'Float:',
parseFloat(atomicValue(expr[1])+expr.slice(2).join(''))
]
break
case 'variable':
result = ['Var:',expr[1]]
break
case 'infinity':
result = symbols[expr[1]]
break
case 'sumdiff':
case 'prodquo':
if ( expr.length == 3 )
result = build( 'unary-', 2 )
else if ( expr.length == 4 )
result = build( 2, 1, 3 )
break
case 'factor':
if ( expr.length == 3 )
result = expr[2] == '%' ?
build( '\u00d7', 1, symbols['%'] ) :
build( '\u00d7', 2, symbols['$'] )
else if ( expr.length == 4 )
result = expr[3] == '\u2218' ?
build( '\u00d7', 1, symbols['\u2218'] ) :
build( '^', 1, 3 )
break
case 'fraction':
result = build( '\u00f7', 3, 4 )
break
case 'root':
if ( expr.length == 3 )
result = build( '\u221a', 2, ['Int:',2] )
else if ( expr.length == 5 )
result = build( '\u221a', 4, 2 )
break
case 'ln':
result = build( 'ln', 2 )
break
case 'log':
if ( expr.length == 3 )
result = build( 'log', ['Int:',10], 2 )
else if ( expr.length == 5 )
result = build( 'log', 3, 4 )
break
case 'atomic':
result = expr.length == 4 && expr[1] == '(' && expr[3] == ')' ?
expr[2] : undefined
break
case 'atomicsentence':
if ( expr.length == 3 )
result = build( 1, 2 )
else if ( expr.length == 4 )
result = build( 2, 1, 3 )
break
case 'decoration':
result = build( 1, 2 )
break
case 'sentence':
result = expr[1] == '\u2234' ? expr[2] : undefined
break
case 'interval':
const left = expr[1] == '(' ? 'o' : 'c'
const right = expr[5] == ')' ? 'o' : 'c'
result = build( `interval_${left}${right}`, 2, 4 )
break
case 'absval':
result = build( makeSym( 'abs', 'arith1' ), 2 )
break
case 'trigapp':
if ( expr.length == 3 )
result = build( makeSym( expr[1], 'transc1' ), 2 )
else if ( expr.length == 8 )
result = build( makeSym( 'arc' + expr[1], 'transc1' ), 7 )
break
case 'subscripted':
result = build( 1, 3 )
break
case 'factorial':
result = build( makeSym( 'factorial', 'integer1' ), 1 )
break
case 'limit':
result = build(
makeSym( 'limit', 'limit1' ), 6,
makeSym( 'both_sides', 'limit1' ),
[ 'Bind:', makeSym( 'lambda', 'fns1' ), expr[4], expr[8] ] )
break
case 'sum':
let [ varname, from, to ] = expr[2] == 'sup' ?
[ 6, 8, 3 ] : [ 4, 6, 9 ]
result = build(
makeSym( 'sum', 'arith1' ),
[ 'App:', makeSym( 'interval', 'interval1' ),
expr[from], expr[to] ],
[ 'Bind:', makeSym( 'lambda', 'fns1' ),
expr[varname], expr[10] ] )
break
case 'differential':
result = build( 'd', 2 )
break
case 'difffrac':
result = build( '\u00f7', 'd', build( 'd', 6 ) )
break
case 'indefint':
result = build( '\u222b', 2 )
break
case 'defint':
let [ a, b ] = expr[2] == 'sup' ? [ 5, 3 ] : [ 3, 5 ]
result = build( 'def\u222b', a, b, 6 )
break
}
if ( !result ) {
result = expr[1]
// console.log( 'No result, so choosing expr[1] = ',
// JSON.stringify( result ) )
}
if ( G.expressionBuilderDebug )
console.log( JSON.stringify( expr ), '--->', JSON.stringify( result ) )
return result
} )
} )
let input, output
it( 'should parse numbers', () => {
// An integer first (which also counts as a float)
input = '1 0 0'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql( ['Int:',100] )
// A floating point value second
input = '3 . 1 4 1 5 9'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql( ['Float:',3.14159] )
// Let's pretend infinity is a number, and include it in this test.
input = [ '\u221e' ] // infinity symbol
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql( ['Sym:','infinity.nums1'] )
} )
it( 'should parse variables', () => {
// Roman letters, upper and lower case
input = [ "x" ]
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql( ['Var:','x'] )
input = [ "R" ]
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql( ['Var:','R'] )
// Greek letters
input = [ "α" ]
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql( ['Var:','α'] )
input = [ "π" ]
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql( ['Var:','π'] )
// Subscripted variables
input = 'x sub i'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql( ['App:',['Var:','x'],['Var:','i']] )
input = 'T sub ( j + k )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Var:','T'],
['App:',['Sym:','plus.arith1'],['Var:','j'],['Var:','k']]
]
)
} )
it( 'should parse simple arithmetic expressions', () => {
// Try one of each operation in isolation
input = '6 + k'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','plus.arith1'],['Int:',6],['Var:','k']]
)
input = '1 . 9 - T'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','minus.arith1'],['Float:',1.9],['Var:','T']]
)
input = '0 . 2 · 0 . 3'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','times.arith1'],['Float:',0.2],['Float:',0.3]]
)
input = 'v ÷ w'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','divide.arith1'],['Var:','v'],['Var:','w']]
)
input = 'v ± w'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','plusminus.multiops'],['Var:','v'],['Var:','w']]
)
input = '2 sup k'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','power.arith1'],['Int:',2],['Var:','k']]
)
// Now try same-precedence operators in sequence, and ensure that they
// left-associate.
input = '5 . 0 - K + e'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','plus.arith1'],
['App:',['Sym:','minus.arith1'],['Float:',5.0],['Var:','K']],
['Var:','e']
]
)
input = '5 . 0 × K ÷ e'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','divide.arith1'],
['App:',['Sym:','times.arith1'],['Float:',5.0],['Var:','K']],
['Var:','e']
]
)
input = '( a sup b ) sup c'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','power.arith1'],
['App:',['Sym:','power.arith1'],['Var:','a'],['Var:','b']],
['Var:','c']
]
)
// Now try different-precendence operators in combination, and ensure that
// precedence is respected.
input = '5 . 0 - K · e'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','minus.arith1'],
['Float:',5.0],
['App:',['Sym:','times.arith1'],['Var:','K'],['Var:','e']]
]
)
input = '5 . 0 × K + e'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','plus.arith1'],
['App:',['Sym:','times.arith1'],['Float:',5.0],['Var:','K']],
['Var:','e']
]
)
input = 'u sup v × w sup x'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','times.arith1'],
['App:',['Sym:','power.arith1'],['Var:','u'],['Var:','v']],
['App:',['Sym:','power.arith1'],['Var:','w'],['Var:','x']]
]
)
// Verify that unary negation works.
// Note that this is the first time where there are two possible
// parsing results--just this first test in this section.
// The answer can be unary negation of 7 OR a single atomic number -7.
input = '- 7'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 2 )
expect( output[0] ).to.eql( ['Int:',-7] )
expect( output[1] ).to.eql( ['App:',['Sym:','unary_minus.arith1'],['Int:',7]] )
input = 'A + - B'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','plus.arith1'],
['Var:','A'],
['App:',['Sym:','unary_minus.arith1'],['Var:','B']]
]
)
input = '- A + B'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','plus.arith1'],
['App:',['Sym:','unary_minus.arith1'],['Var:','A']],
['Var:','B']
]
)
input = '- A sup B'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','unary_minus.arith1'],
['App:',['Sym:','power.arith1'],['Var:','A'],['Var:','B']]
]
)
} )
it( 'should respect parentheses', () => {
// First, verify that a chain of sums left-associates.
input = '6 + k + 5'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','plus.arith1'],
['App:',['Sym:','plus.arith1'],['Int:',6],['Var:','k']],
['Int:',5]
]
)
// Now verify that we can override that with parentheses.
input = '6 + ( k + 5 )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','plus.arith1'],
['Int:',6],
['App:',['Sym:','plus.arith1'],['Var:','k'],['Int:',5]]
]
)
// And verify that parentheses override precedence as well. Contrast the
// following tests to those at the end of the previous section, which tested
// the default precendence of these operators.
input = '( 5 . 0 - K ) · e'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','times.arith1'],
['App:',['Sym:','minus.arith1'],['Float:',5.0],['Var:','K']],
['Var:','e']
]
)
input = '5 . 0 × ( K + e )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','times.arith1'],
['Float:',5.0],
['App:',['Sym:','plus.arith1'],['Var:','K'],['Var:','e']]
]
)
input = '- ( K + e )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','unary_minus.arith1'],
['App:',['Sym:','plus.arith1'],['Var:','K'],['Var:','e']]
]
)
input = '- ( A sup B )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','unary_minus.arith1'],
['App:',['Sym:','power.arith1'],['Var:','A'],['Var:','B']]
]
)
} )
it( 'should support fractions', () => {
// Let's begin with fractions of atomics.
input = 'fraction ( 1 2 )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','divide.arith1'],['Int:',1],['Int:',2]]
)
input = 'fraction ( p q )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','divide.arith1'],['Var:','p'],['Var:','q']]
)
// Now we'll try fractions of larger things
input = 'fraction ( ( 1 + t ) 3 )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','divide.arith1'],
['App:',['Sym:','plus.arith1'],['Int:',1],['Var:','t']],
['Int:',3]
]
)
input = 'fraction ( ( a + b ) ( a - b ) )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','divide.arith1'],
['App:',['Sym:','plus.arith1'],['Var:','a'],['Var:','b']],
['App:',['Sym:','minus.arith1'],['Var:','a'],['Var:','b']]
]
)
// And lastly we verify that parsing takes place correctly inside the
// numerator and denominator of fractions.
input = 'fraction ( ( 1 + 2 × v ) ( - w ) )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','divide.arith1'],
['App:',
['Sym:','plus.arith1'],
['Int:',1],
['App:',['Sym:','times.arith1'],['Int:',2],['Var:','v']]
],
['App:',['Sym:','unary_minus.arith1'],['Var:','w']]
]
)
} )
it( 'should support square roots and nth roots', () => {
// First, square roots of simple expressions.
input = '√ 2'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','root.arith1'],['Int:',2],['Int:',2]]
)
input = '√ ( 1 0 - k + 9 . 6 )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','root.arith1'],
['App:',
['Sym:','plus.arith1'],
['App:',['Sym:','minus.arith1'],['Int:',10],['Var:','k']],
['Float:',9.6]
],
['Int:',2]
]
)
// Second, nth roots of simple expressions.
input = 'nthroot p √ 2'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','root.arith1'],['Int:',2],['Var:','p']]
)
input = 'nthroot 5 0 √ ( 1 0 - k + 9 . 6 )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','root.arith1'],
['App:',
['Sym:','plus.arith1'],
['App:',['Sym:','minus.arith1'],['Int:',10],['Var:','k']],
['Float:',9.6]
],
['Int:',50]
]
)
// Next, square roots of fractions and of other roots, and placed in context.
input = 'fraction ( 6 √ fraction ( 1 2 ) )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','divide.arith1'],
['Int:',6],
['App:',
['Sym:','root.arith1'],
['App:',['Sym:','divide.arith1'],['Int:',1],['Int:',2]],
['Int:',2]
]
]
)
input = '√ ( 1 + √ 5 ) + 1'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','plus.arith1'],
['App:',
['Sym:','root.arith1'],
['App:',
['Sym:','plus.arith1'],
['Int:',1],
['App:',['Sym:','root.arith1'],['Int:',5],['Int:',2]]
],
['Int:',2]
],
['Int:',1]
]
)
// Finally, nth roots containing more complex expressions.
input = 'nthroot ( 2 + t ) √ ( 1 ÷ ∞ )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','root.arith1'],
['App:',['Sym:','divide.arith1'],['Int:',1],['Sym:','infinity.nums1']],
['App:',['Sym:','plus.arith1'],['Int:',2],['Var:','t']]
]
)
} )
it( 'should support logarithms of all types', () => {
// Natural logarithms of a simple thing and a larger thing.
input = 'ln x'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql( ['App:',['Sym:','ln.transc1'],['Var:','x']] )
input = 'ln fraction ( 2 ( x + 1 ) )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','ln.transc1'],
['App:',
['Sym:','divide.arith1'],
['Int:',2],
['App:',['Sym:','plus.arith1'],['Var:','x'],['Int:',1]]
]
]
)
// Logarithms with an implied base 10, of a simple thing and a larger thing.
input = 'log 1 0 0 0'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','log.transc1'],['Int:',10],['Int:',1000]]
)
input = 'log ( e sup x × y )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','log.transc1'],
['Int:',10],
['App:',
['Sym:','times.arith1'],
['App:',['Sym:','power.arith1'],['Var:','e'],['Var:','x']],
['Var:','y']
]
]
)
// Logarithms with an explicit base, of a simple thing and a larger thing.
input = 'log sub ( 3 1 ) 6 5'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',['Sym:','log.transc1'],['Int:',31],['Int:',65]]
)
input = 'log sub ( - t ) ( k + 5 )'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(
['App:',
['Sym:','log.transc1'],
['App:',['Sym:','unary_minus.arith1'],['Var:','t']],
['App:',['Sym:','plus.arith1'],['Var:','k'],['Int:',5]]
]
)
} )
it( 'should support sentences', () => {
// First, relations among nouns.
input = '2 < 3'.split( ' ' )
output = G.parse( input )
expect( output.length ).to.equal( 1 )
expect( output[0] ).to.eql(