-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathparser.py
More file actions
1350 lines (1175 loc) · 46.6 KB
/
Copy pathparser.py
File metadata and controls
1350 lines (1175 loc) · 46.6 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
# -*- coding: utf-8 -*-
"""
Precedence-climbing Parsing routines for grammar symbols.
See README.md or
https://mathics-development-guide.readthedocs.io/en/latest/extending/code-overview/scanning-and-parsing.html#parser
"""
import string
from typing import Optional, Union
from mathics_scanner.errors import (
EscapeSyntaxError,
InvalidSyntaxError,
NamedCharacterSyntaxError,
SyntaxError,
)
from mathics_scanner.tokeniser import Token, Tokeniser, is_symbol_name
from mathics.core.convert.op import builtin_constants
from mathics.core.parser.ast import (
Filename,
Node,
NullString,
NullSymbol,
Number,
Number1,
NumberM1,
String,
Symbol,
)
from mathics.core.parser.operators import (
all_operators,
binary_operators,
box_operators,
flat_binary_operators,
inequality_operators,
left_binary_operators,
nonassoc_binary_operators,
operator_precedences,
postfix_operators,
prefix_operators,
right_binary_operators,
ternary_operators,
)
special_symbols = builtin_constants.copy()
# FIXME: should rework so we don't have to do this
# We have the character name ImaginaryI and ImaginaryJ, but we should
# have the *operator* name, "I".
special_symbols["\uf74f"] = special_symbols["\uf74e"] = "I"
# An operator precedence value that will ensure that whatever operator
# this is attached to does not have parenthesis surrounding it.
# Operator precedence values are integers; If if an operator
# "op" is greater than the surrounding precedence, then "op"
# will be surrounded by parenthesis, e.g. ... (...op...) ...
# In named-characters.yml of mathics-scanner we start at 0.
# However, negative values would also work.
NEVER_ADD_PARENTHESIS: int = 0
permitted_digits = {c: i for i, c in enumerate(string.digits + string.ascii_lowercase)}
permitted_digits["."] = 0
def unescape_string(s: str) -> str:
"""
Turn a string representation with quotes and backslashes into
the equivalent string with the quotes removed and the backslashes
evaluated.
For example, '"a\\n\\c"' becomes 'a\nb\nc'
"""
assert len(s) >= 2 and s[0] == s[-1]
# Special cases to avoid the Deprecation Warning
if s in ('"\\!"', '"\\$"', '"\\W+"', '"\\d"'):
return s[1:-1]
s = s.encode("raw_unicode_escape").decode("unicode_escape")
return s[1:-1]
class Parser:
def __init__(self):
# no implicit times on these tokens
self.halt_tags = set(
[
"END",
"RawRightAssociation",
"RawRightParenthesis",
"RawComma",
"RawRightBrace",
"RawRightBracket",
"RawColon",
"DifferentialD",
]
)
def backtrack(self, pos):
"""
Rewinds parse state (self.pos and self.current_token) so that
another parse sequence can be considered.
"""
assert self.tokeniser.pos >= pos
self.tokeniser.pos = pos
self.current_token = None # See note below on "consume"
def consume(self):
"""
We mark that the current token is "consumed" by setting it
to None. Then, when parsing is requested, the function
"next()" will see that the current token is None and
read a new token".
"""
self.current_token = None
def expect(self, expected_tag: str):
"""
This "expect()" function is the sort of thing one expects to see in
top-down predictive parsing. In this kind of parsing, a "expected_tag" is, well, expected,
and here we verify that what was expected is found. When this is not so, then we have
a syntax error.
"""
token = self.next_noend()
if token.tag == expected_tag:
self.consume()
else:
tag, pre_error, post_error = self.tokeniser.sntx_message(token.pos)
raise InvalidSyntaxError(tag, pre_error, post_error)
def get_more_input(self, pos: int):
self.tokeniser.get_more_input()
self.backtrack(pos)
@property
def is_inside_rowbox(self) -> bool:
r"""
Return True iff we parsing inside a RowBox, i.e. RowBox[...]
or \( ... \)
"""
return self.box_depth > 0
def next(self) -> Token:
if self.current_token is None:
self.current_token = self.tokeniser.next()
return self.current_token
def next_noend(self) -> Token:
"returns next token which is not END"
while True:
token = self.next()
if token.tag != "END":
return token
self.get_more_input(token.pos)
def parse(self, feeder) -> Optional[Node]:
"""
top-level parsing routine. This kicks off parsing
by doing some initialization and then calling
self.parse_e()
"""
self.feeder = feeder
self.tokeniser = Tokeniser(feeder)
self.current_token = None
self.bracket_depth = 0
self.box_depth = 0
return self.parse_e()
def parse_e(self) -> Optional[Node]:
"""
Parse the single top-level or "start" expression.
This is called right after doing parse setup.
"""
result = []
while self.next().tag != "END":
result.append(self.parse_expr(NEVER_ADD_PARENTHESIS))
if len(result) > 1:
return Node("Times", *result)
if len(result) == 1:
return result[0]
else:
return None
def parse_binary_operator(
self, expr1, token: Token, expr1_precedence: int
) -> Optional[Node]:
"""
Implements parsing and transformation of binary operators:
expr1 <binary-operator> expr2
when it is applicable.
When called, we have parsed "expr1" and seen <binary-operator> passed as "token". This routine
may cause expr2 to get scanned and parsed.
"expr1_precendence" is the precedence of "expr1" and is used
to determine whether parsing should be interpreted as:
(... expr1) <binary-operator> expr2
or:
... (expr1 <binary-operator> expr2)
In the first case, we will return None (no further tokens
added). A higher level will handle group (... expr1) and
pass that as expr1 in another call to this routine.
In this situation, this routine will get called again with a
new expr1 that contains (... expr1).
However, in the second case:
...(expr1 <binary-operator> expr2),
we return Node(<binary-operator>, expr1, expr2)
"""
tag = token.tag
operator_precedence = binary_operators[tag]
if expr1_precedence > operator_precedence:
return None
self.consume()
if tag not in right_binary_operators:
operator_precedence += 1
expr2 = self.parse_expr(operator_precedence)
# Handle nonassociative operators
if (
tag in nonassoc_binary_operators
and expr1.get_head_name() == tag
and not expr1.parenthesised
):
tag, pre_error, post_error = self.tokeniser.sntx_message(token.pos)
raise InvalidSyntaxError(tag, pre_error, post_error)
result = Node(tag, expr1, expr2)
# Flatten the tree if required
if tag in flat_binary_operators:
result.flatten()
return result
def parse_box_expr(self, precedence: int) -> Union[String, Node]:
r"""
Parse a box expression returning an AST Node tree for this.
If there is only an Atom we return a String of that.
Otherwise we return an AST Node tree for this.
This code recognizes grammar rules of the form:
<b_tag_fn(expr1)>
| \( box-expr \)
| \( box-expr <box-operator> box-expr \)
"""
result = None
new_result = None
while True:
if self.is_inside_rowbox:
token = self.next_noend()
else:
token = self.next()
tag = token.tag
method = getattr(self, "b_" + tag, None)
if method is not None:
new_result = method(result, token, precedence)
elif tag in box_operators:
new_result = self.parse_box_operator(result, token, precedence)
elif tag in ("OtherscriptBox", "RightRowBox"):
break
elif tag == "END":
self.get_more_input(token.pos)
elif result is None and tag != "END":
self.consume()
# TODO: handle non-box expressions inside RowBox
# new_result = self.parse_expr(precedence)
# if new_result is None:
# self.consume()
# new_result = String(token.text)
# if new_result.value == r"\(":
# new_result = self.p_LeftRowBox(token)
# elif isinstance(new_result, (Symbol, Number)):
# new_result = String(new_result.value)
new_result = String(token.text)
if new_result.value == r"\(":
new_result = self.p_LeftRowBox(token)
else:
new_result = None
if new_result is None:
break
else:
result = new_result
if result is None:
result = NullString
return result
def parse_box_operator(
self, box_expr1, token: Token, box_expr1_precedence: int
) -> Optional[Node]:
"""
Implements parsing and transformation of box operators:
box_expr1 <box-operator> box_expr2
when it is applicable.
When called, we have parsed "box_expr1" and seen <box-operator> passed as "token". This routine
may cause box_expr2 to get scanned and parsed.
"box_expr1_precendence" is the precedence of "box_expr1" and is used
to determine whether parsing should be interpreted as:
(... box_expr1) <box-operator> box_expr2
or:
... (box_expr1 <box-operator> box_expr2)
In the first case, we will return None (no further tokens
added). A higher level will handle group (... box_expr1) and
pass that as box_expr1 in another call to this routine.
In this situation, this routine will get called again with a
new box_expr1 that contains (... box_expr1).
However, in the second case:
...(box_expr1 <box-operator> box_expr2),
we return Node(<box-operator>, expr1, expr2)
"""
tag = token.tag
operator_precedence = binary_operators[tag]
if box_expr1_precedence > operator_precedence:
return None
self.consume()
# We don't handle any notion of right associativity yet,
# if there is such a thing....
# if tag not in right_box_operators:
# operator_precedence += 1
box_expr2 = self.parse_expr(operator_precedence)
# Is there such a thing as non-associative box operators?
# Handle nonassociative box operators
# if (
# tag in nonassoc_binary_operators
# and expr1.get_head_name() == tag
# and not expr1.parenthesised
# ):
# self.tokeniser.sntx_message(token.pos)
# raise InvalidSyntaxError()
result = Node(tag, box_expr1, box_expr2)
return result
def parse_comparison(
self, expr1, token: Token, expr1_precedence: int
) -> Optional[Node]:
"""
Implements parsing and transformation of comparison (equality and inequality) operators:
expr1 <comparison-operator> expr2
when it is applicable.
When called, we have parsed "expr1" and seen <comparison-operator> passed as "token". This routine
may cause expr2 to get scanned and parsed.
"expr1_precendence" is the precedence of "expr1" and is used
to determine whether parsing should be interpreted as:
(... expr1) <comparison-operator> expr2
or:
... (expr1 <comparison-operator> expr2)
In the first case, we will return None (no further tokens
added) and a higher level of parsing resolve and parse:
(... expr1) <comparison_operator> expr2
In this situation, this routine will get called again with a
new expr1 that contains (... expr1).
In the latter case, we flatten expressions if expr1 is not
parenthesized
"""
tag = token.tag
operator_precedence = flat_binary_operators[tag]
if expr1_precedence > operator_precedence:
return None
self.consume()
head = expr1.get_head_name()
expr2 = self.parse_expr(operator_precedence + 1)
if head == "Inequality" and not expr1.parenthesised:
expr1.children.append(Symbol(tag))
expr1.children.append(expr2)
elif head in inequality_operators and head != tag and not expr1.parenthesised:
children: list = []
first = True
for child in expr1.children:
if not first:
children.append(Symbol(head))
children.append(child)
first = False
children.append(Symbol(tag))
children.append(expr2)
expr1 = Node("Inequality", *children)
else:
expr1 = Node(tag, expr1, expr2).flatten()
return expr1
def parse_expr(self, precedence: int) -> Optional[Node]:
"""
Parse an expression returning an AST Node tree for this.
This code recognizes grammar rules of the form:
<e_tag_fn(expr1)>
| expr1 inequality_operator expr2 ...
| expr1 binary_operator expr2 ...
| expr1 ternary_operator expr2 ternary_operator2 expr3 ...
| expr1 postfix_operator ...
| box-expr box-operator box-expr2 (* only if inside rowbox *)
| expr1 expr2 ... (* implicit multiplication *)
and transforming this into its corresponding Node S-expression form.
"precedence" is an operator precedence of the parent node which is
often the operator token seen just before a grouping symbol which probably
caused "parse_expr" to get called. For example in:
(a + b) * (c + d)
or equivalently:
(a + b) (c + d)
or
(a + b)(c + d)
if we have been called to parse "(c + d)", "precedence" will be the
precedence for "Times", also sometimes known as "*".
"""
result = self.parse_p()
# Note: Number and String below are the mathics.core.parser's Number, String and Symbol,
# not mathics.core.atom's Number and String, and Symbol.
if self.is_inside_rowbox and isinstance(result, (Number, Symbol)):
result = String(result.value)
while True:
if self.bracket_depth > 0 or self.is_inside_rowbox:
token = self.next_noend()
if token.tag in ("OtherscriptBox", "RightRowBox"):
if self.is_inside_rowbox:
break
else:
tag, pre_error, post_error = self.tokeniser.sntx_message(
token.pos
)
raise InvalidSyntaxError(tag, pre_error, post_error)
else:
try:
token = self.next()
except (NamedCharacterSyntaxError, EscapeSyntaxError) as escape_error:
self.tokeniser.feeder.message(
escape_error.name, escape_error.tag, *escape_error.args
)
raise
tag = token.tag
method = getattr(self, "e_" + tag, None)
if method is not None:
new_result = method(result, token, precedence)
elif tag in inequality_operators:
new_result = self.parse_comparison(result, token, precedence)
elif tag in binary_operators:
new_result = self.parse_binary_operator(result, token, precedence)
elif tag in ternary_operators:
new_result = self.parse_ternary_operator(result, token, precedence)
elif tag in postfix_operators:
new_result = self.parse_postfix(result, token, precedence)
elif (
tag not in self.halt_tags
and flat_binary_operators["Times"] >= precedence
):
if self.is_inside_rowbox:
if tag in box_operators:
new_result = self.parse_box_operator(result, token, precedence)
else:
# Inside a RowBox, implicit multiplication is treated as
# concatenation.
child = self.parse_expr(precedence)
children = [result, child]
new_result = Node("RowBox", Node("List", *children))
else:
# There is an implicit multiplication.
operator_precedence = flat_binary_operators["Times"]
child = self.parse_expr(operator_precedence + 1)
new_result = Node("Times", result, child).flatten()
else:
new_result = None
if new_result is None:
break
else:
result = new_result
return result
def parse_p(self):
"""Parse a "p_"-tagged expression.
"p_" tags include prefix operators, left-bracketed expressions
and tokens that can be identified by some prefix, like a number
or a string.
"""
token = self.next_noend()
tag = token.tag
method = getattr(self, "p_" + tag, None)
if method is not None:
return method(token)
elif tag in prefix_operators:
self.consume()
operator_precedence = prefix_operators[tag]
child = self.parse_expr(operator_precedence)
return Node(tag, child)
elif self.is_inside_rowbox:
return None
else:
tag, pre_error, post_error = self.tokeniser.sntx_message(token.pos)
raise InvalidSyntaxError(tag, pre_error, post_error)
def parse_postfix(
self, expr1, token: Token, expr1_precedence: int
) -> Optional[Node]:
"""Implements grammar rule:
expr : expr1 <postfix-operator>
when it is applicable.
When called, we have parsed "expr1" and <prefix-operator> in "token".
"expr1_precedence" is the precedence of expr1 and is used to determine whether parsing
should be interpreted as:
(... expr1) <postfix-operator>
or:
... (expr1 <postfix-operator>)
In the first case, we return None and at a higher level we may get called
again with (... expr1) passed as a new expr1 parameter.
In the latter case, we return Node(<postfix-operator>, expr1)
"""
tag = token.tag
prefix_operator_precedence = postfix_operators[tag]
if prefix_operator_precedence < expr1_precedence:
return None
self.consume()
return Node(tag, expr1)
# Note: returning a list is different from how most other parse_ routines
# work and it makes the type system more complicated.
def parse_seq(self) -> list:
result: list = []
while True:
token = self.next_noend()
tag = token.tag
if tag == "RawComma":
self.tokeniser.feeder.message("Syntax", "com")
result.append(NullSymbol)
self.consume()
elif tag in ("RawRightAssociation", "RawRightBrace", "RawRightBracket"):
if result:
self.tokeniser.feeder.message("Syntax", "com")
result.append(NullSymbol)
break
else:
result.append(self.parse_expr(NEVER_ADD_PARENTHESIS))
token = self.next_noend()
tag = token.tag
if tag == "RawComma":
self.consume()
continue
elif tag in ("RawRightAssociation", "RawRightBrace", "RawRightBracket"):
break
return result
def parse_ternary_operator(
self, expr1, token: Token, expr1_precedence: int
) -> Optional[Node]:
raise NotImplementedError
# B methods
#
# b_xxx methods are called from parse_box_expr.
# They expect args (Node, Token precedence) and return Node or None.
# The first argument may be None if the LHS is absent.
# Used for boxes.
def b_FormBox(
self, box_expr1, token: Token, box_expr1_precedence: int
) -> Optional[Node]:
operator_precedence = all_operators["FormBox"]
if box_expr1_precedence > operator_precedence:
return None
if box_expr1 is None:
box_expr1 = Symbol("StandardForm") # RawForm
elif is_symbol_name(box_expr1.value):
box_expr1 = Symbol(box_expr1.value, context=None)
else:
box_expr1 = Node("Removed", String("$$Failure"))
self.consume()
box2 = self.parse_box_expr(operator_precedence)
return Node("FormBox", box2, box_expr1)
def b_FractionBox(
self, box_expr1, token: Token, box_expr1_precendence: int
) -> Optional[Node]:
operator_precedence = all_operators["FractionBox"]
if box_expr1_precendence > operator_precedence:
return None
if box_expr1 is None:
box_expr1 = NullString
self.consume()
box_expr2 = self.parse_box_expr(operator_precedence + 1)
return Node("FractionBox", box_expr1, box_expr2)
def b_OverscriptBox(
self, box_expr1, token: Token, box_expr1_precedence: int
) -> Optional[Node]:
operator_precedence = all_operators["OverscriptBox"]
if box_expr1_precedence > operator_precedence:
return None
if box_expr1 is None:
box_expr1 = NullString
self.consume()
box_expr2 = self.parse_box_expr(operator_precedence)
if self.next().tag == "OtherscriptBox":
self.consume()
box_expr3 = self.parse_box_expr(all_operators["UnderoverscriptBox"])
return Node("UnderoverscriptBox", box_expr1, box_expr3, box_expr2)
else:
return Node("OverscriptBox", box_expr1, box_expr2)
def b_SqrtBox(self, box0, token: Token, p: int) -> Optional[Node]:
if box0 is not None:
return None
self.consume()
operator_precedence = all_operators["SqrtBox"]
box_expr1 = self.parse_box_expr(operator_precedence)
if self.next().tag == "OtherscriptBox":
self.consume()
box2 = self.parse_box_expr(operator_precedence)
return Node("RadicalBox", box_expr1, box2)
else:
return Node("SqrtBox", box_expr1)
def b_SubscriptBox(
self, box_expr1, token: Token, box_expr1_precedence: int
) -> Optional[Node]:
operator_precedence = all_operators["SubscriptBox"]
if box_expr1_precedence > operator_precedence:
return None
if box_expr1 is None:
box_expr1 = NullString
self.consume()
box_expr2 = self.parse_box_expr(operator_precedence)
if self.next().tag == "OtherscriptBox":
self.consume()
box_expr3 = self.parse_box_expr(all_operators["SubsuperscriptBox"])
return Node("SubsuperscriptBox", box_expr1, box_expr2, box_expr3)
else:
return Node("SubscriptBox", box_expr1, box_expr2)
def b_SuperscriptBox(
self, box_expr1, token: Token, box_expr1_precedence: int
) -> Optional[Node]:
operator_precedence = all_operators["SuperscriptBox"]
if box_expr1_precedence > operator_precedence:
return None
if box_expr1 is None:
box_expr1 = NullString
self.consume()
box2 = self.parse_box_expr(operator_precedence)
if self.next().tag == "OtherscriptBox":
self.consume()
box3 = self.parse_box_expr(all_operators["SubsuperscriptBox"])
return Node("SubsuperscriptBox", box_expr1, box3, box2)
else:
return Node("SuperscriptBox", box_expr1, box2)
def b_UnderscriptBox(
self, box_expr1, token: Token, box_expr1_precedence: int
) -> Optional[Node]:
operator_precedence = all_operators["UnderscriptBox"]
if box_expr1_precedence > operator_precedence:
return None
if box_expr1 is None:
box_expr1 = NullString
self.consume()
box_expr2 = self.parse_box_expr(operator_precedence)
if self.next().tag == "OtherscriptBox":
self.consume()
box_expr3 = self.parse_box_expr(all_operators["UnderoverscriptBox"])
return Node("UnderoverscriptBox", box_expr1, box_expr2, box_expr3)
else:
return Node("UnderscriptBox", box_expr1, box_expr2)
# E methods
#
# e_xxx methods are called from parse_e.
# They expect args (Node, Token precedence) and return Node or None.
# Used for binary and ternary operators.
# return None if precedence is too low.
def e_Alternatives(self, expr1, token: Token, p: int) -> Optional[Node]:
q = flat_binary_operators["Alternatives"]
if q < p:
return None
self.consume()
expr2 = self.parse_expr(q + 1)
return Node("Alternatives", expr1, expr2).flatten()
def e_ApplyList(self, expr1, token: Token, p: int) -> Optional[Node]:
operator_precedence = right_binary_operators["Apply"]
if operator_precedence < p:
return None
self.consume()
expr2 = self.parse_expr(operator_precedence)
expr3 = Node("List", Number1)
return Node("Apply", expr1, expr2, expr3)
def e_Derivative(self, expr1, token: Token, p: int) -> Optional[Node]:
q = postfix_operators["Derivative"]
if q < p:
return None
n = 0
while self.next().tag == "Derivative":
self.consume()
n += 1
head = Node("Derivative", Number(str(n)))
return Node(head, expr1)
def e_Divide(self, expr1, token: Token, expr1_precedence: int):
"""
Implements parsing and transformation of Divide
expr1 / expr2
when it is applicable.
When called, we have parsed "expr1" and seen "/" passed as "token". This routine
may cause expr2 to get scanned and parsed.
"expr1_precendence" is the precedence of "expr1" and is used
to determine whether parsing should be interpreted as:
(... expr1) / expr2
or:
... (expr1 / expr2)
In the first case, we will return None (no further tokens
added). A higher level will handle group (... expr1) and
pass that as expr1 in another call to this routine.
However, in the second case:
...(expr1 <binary-operator> expr2),
we return Node(Times, expr1, Node(Power, expr2, -1))
"""
operator_precedence = left_binary_operators["Divide"]
if expr1_precedence > operator_precedence:
return None
self.consume()
expr2 = self.parse_expr(operator_precedence + 1)
return Node("Times", expr1, Node("Power", expr2, NumberM1)).flatten()
def e_Infix(self, expr1, token: Token, expr1_precedence) -> Optional[Node]:
"""
Used to implement the rule:
expr : expr1 '~' expr2 '~' expr3
when applicable.
When called, we have parsed expr1 and seen token "~". This routine will
may cause expr2 ~ expr3 to get scanned and parsed if applicable based on expr1_precedence.
"expr1_precendence" is the precedence of expr1 and is used whether parsing
should be interpreted as:
(expr1) ~ expr2 ~ expr3
or:
(expr1 ~ expr2) ~ expr3
"""
operator_precedence = ternary_operators["Infix"]
if expr1_precedence > operator_precedence:
return None
self.consume()
expr2 = self.parse_expr(operator_precedence + 1)
self.expect("Infix")
expr3 = self.parse_expr(operator_precedence + 1)
return Node(expr2, expr1, expr3)
def e_Function(self, expr1, token: Token, p: int) -> Optional[Node]:
operator_precedence = postfix_operators["Function"]
if operator_precedence < p:
return None
# postfix or right-binary determined by symbol
self.consume()
if token.text == "&":
return Node("Function", expr1)
else:
expr2 = self.parse_expr(operator_precedence)
return Node("Function", expr1, expr2)
def e_MessageName(self, expr1, token: Token, p: int) -> Node:
elements = [expr1]
while self.next().tag == "MessageName":
self.consume()
token = self.next()
if token.tag == "Symbol":
# silently convert Symbol to String
self.consume()
element = String(token.text)
elif token.tag == "String":
element = self.p_String(token)
else:
tag, pre, post = self.tokeniser.sntx_message(token.pos)
raise InvalidSyntaxError(tag, pre, post)
elements.append(element)
return Node("MessageName", *elements)
def e_Minus(self, expr1, _: Token, p: int) -> Optional[Node]:
q = left_binary_operators["Subtract"]
if q < p:
return None
self.consume()
expr2 = self.parse_expr(q + 1)
if isinstance(expr2, Number) and not expr2.value.startswith("-"):
expr2.value = "-" + expr2.value
else:
expr2 = Node("Times", NumberM1, expr2).flatten()
return Node("Plus", expr1, expr2).flatten()
def e_Prefix(self, expr1, _: Token, expr1_precedence: int) -> Optional[Node]:
"""
Used to parse:
expr1 @ expr2
into the Node S-expression form of
expr1(expr2)
When called, we have parsed expr1 and seen token "@".
"expr1_precendence" is the precedence of expr1 and is used whether parsing
should be interpreted as:
(... expr1) @ expr2
or:
... (expr1 @ expr2)
In the first case, we return None and at a higher level we may get called
again with (... expr1) passed as a new expr1 parameter.
"""
operator_precedence = flat_binary_operators["Prefix"]
if expr1_precedence > operator_precedence:
return None
self.consume()
expr2 = self.parse_expr(operator_precedence)
return Node(expr1, expr2)
def e_Postfix(self, expr1, token: Token, expr1_precedence: int) -> Optional[Node]:
"""
Used to parse
expr1 // expr2
into the Node S-expression form of
expr2(expr1)
When called, we have parsed expr1 and seen token "//".
"expr1_precendence" is the precedence of expr1 and is used whether parsing
should be interpreted as:
(... expr1) // expr2
or:
... (expr1 // expr2)
In the first case, we return None and at a higher level we may get called
again with (... expr1) passed as a new expr1 parameter.
"""
operator_precedence = left_binary_operators["Postfix"]
if expr1_precedence > operator_precedence:
# Mark the completion of (... expr1)
return None
self.consume()
# Precedence[Postix] is lower than expr1; Postfix[] is left associative.
expr2 = self.parse_expr(operator_precedence + 1)
return Node(expr2, expr1)
def e_RawColon(self, expr1, token: Token, p: int) -> Optional[Node]:
head_name = expr1.get_head_name()
if head_name == "Symbol":
head = "Pattern"
elif head_name in (
"Blank",
"BlankSequence",
"BlankNullSequence",
"Pattern",
"Optional",
):
head = "Optional"
else:
return None
q = all_operators[head]
if p == 151:
return None
self.consume()
expr2 = self.parse_expr(q + 1)
return Node(head, expr1, expr2)
def e_RawLeftBracket(self, expr, token: Token, p: int) -> Optional[Node]:
q = all_operators["Part"]
if q < p:
return None
self.consume()
self.bracket_depth += 1
token = self.next_noend()
if token.tag == "RawLeftBracket":
self.consume()
seq = self.parse_seq()
self.expect("RawRightBracket")
self.expect("RawRightBracket")
self.bracket_depth -= 1
return Node("Part", expr, *seq)
else:
seq = self.parse_seq()
self.expect("RawRightBracket")
self.bracket_depth -= 1
if self.is_inside_rowbox:
# Handle function calls inside a RowBox.
result = Node("List", expr, String("["), *seq, String("]"))
else:
result = Node(expr, *seq)
result.parenthesised = True
return result
def e_Semicolon(self, expr1, token: Token, expr1_precedence: int) -> Optional[Node]:
"""
Used to parse
expr1 ; expr2
into S-expression:
CompondExpression(expr1, expr2)
When called, we have parsed expr1 and seen token ";".
"expr1_precendence" is the precedence of expr1 and is used whether parsing
should be interpreted as:
(... expr1) ; expr2
or:
... (expr1 ; exp2)
In the first case, we return None and at a higher level we may get called
again with (... expr1) passed as a new expr1 parameter.
In the latter case, we return Node(CompoundExpression, expr1, expr2)
"""
operator_precedence = flat_binary_operators["CompoundExpression"]
if expr1_precedence > operator_precedence:
return None
self.consume()
# XXX this has to come before call to self.next()
pos = self.tokeniser.pos
messages = list(self.feeder.messages)
# So that e.g. 'x = 1;' doesn't wait for newline in the frontend
tag = self.next().tag
expr2: Union[Symbol, Node, None]
if tag == "END" and self.bracket_depth == 0:
expr2 = NullSymbol
return Node("CompoundExpression", expr1, expr2).flatten()
# XXX look for next expr otherwise backtrack
try:
expr2 = self.parse_expr(operator_precedence + 1)
except SyntaxError:
self.backtrack(pos)
self.feeder.messages = messages
expr2 = NullSymbol
return Node("CompoundExpression", expr1, expr2).flatten()
def e_Span(self, expr1, token: Token, p) -> Optional[Node]:
q = ternary_operators["Span"]
if q < p:
return None
if expr1.get_head_name() == "Span" and not expr1.parenthesised:
return None
self.consume()
# Span[expr1, expr2]
expr2: Union[Symbol, Node, None]
token = self.next()
if token.tag == "Span":
expr2 = Symbol("All")