Repository navigation
Expand file tree
/
Copy path_parser.py
More file actions
233 lines (195 loc) · 6.92 KB
/
Copy path_parser.py
File metadata and controls
233 lines (195 loc) · 6.92 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
"""_parser.py -- expression and condition parsing for the .mag interpreter.
Recursive descent with precedence, calls, chained indexing and boolean structure.
Measured over this repository's formula files, all of which is needed:
IsZero(...) and friends 1347 uses
ccs[1][2][3] indexing 531 uses in one split file
bare booleans, `if ADD_DEBUG then` 1791 uses
lt / le / ge / ne as well as eq 101 uses between them
negative literals, `eq -1` 53 uses
Negative literals are kept as written, not normalised: in characteristic 2,
-1 == 1, and that coincidence is a real defect in the testers.
Grammar, loosest binding first:
cond := or
or := and ( 'or' and )*
and := not ( 'and' not )*
not := 'not' not | rel
rel := add ( ('eq'|'ne'|'lt'|'le'|'gt'|'ge') add )?
add := mul ( ('+'|'-') mul )*
mul := unary ( ('*'|'/') unary )*
unary := '-' unary | power
power := postfix ( '^' ['-'] INT )*
postfix := atom ( '(' [args] ')' | '[' cond ']' )*
atom := INT | IDENT | '(' cond ')'
"""
from __future__ import annotations
import re
__all__ = ["tokens", "parse_expr", "parse_cond", "ParseError"]
class ParseError(Exception):
pass
KEYWORDS = {"eq", "ne", "lt", "le", "gt", "ge", "and", "or", "not"}
RELATIONS = {"eq", "ne", "lt", "le", "gt", "ge"}
_TOKEN = re.compile(r"""
\s*(?:
(?P<int>\d+)
| (?P<ident>[A-Za-z_][A-Za-z_0-9]*)
| (?P<op><=|>=|[-+*/^(),;\[\]<>])
| (?P<bad>\S)
)
""", re.X)
def tokens(s):
"""Token list. Raises rather than silently truncating on a bad character."""
out, i = [], 0
while i < len(s):
m = _TOKEN.match(s, i)
if not m:
break
if m.group("bad"):
raise ParseError("unexpected character %r in %r" % (m.group("bad"), s))
out.append(m.group("int") or m.group("ident") or m.group("op"))
i = m.end()
return out
class _P:
def __init__(self, toks, src):
self.t, self.i, self.src = toks, 0, src
def peek(self, k=0):
j = self.i + k
return self.t[j] if j < len(self.t) else None
def take(self):
if self.i >= len(self.t):
raise ParseError("unexpected end of %r" % self.src)
v = self.t[self.i]
self.i += 1
return v
def expect(self, v):
got = self.take()
if got != v:
raise ParseError("expected %r, got %r, in %r" % (v, got, self.src))
# -- grammar ----------------------------------------------------------
def cond(self):
return self.or_()
def or_(self):
n = self.and_()
while self.peek() == "or":
self.take()
n = ("or", n, self.and_())
return n
def and_(self):
n = self.not_()
while self.peek() == "and":
self.take()
n = ("and", n, self.not_())
return n
def not_(self):
if self.peek() == "not":
self.take()
return ("not", self.not_())
return self.rel()
def rel(self):
n = self.add()
if self.peek() in RELATIONS:
op = self.take()
return ("cmp", op, n, self.add())
return n
def add(self):
n = self.mul()
while self.peek() in ("+", "-"):
op = self.take()
n = (op, n, self.mul())
return n
def mul(self):
n = self.unary()
while self.peek() in ("*", "/"):
op = self.take()
n = (op, n, self.unary())
return n
def unary(self):
if self.peek() == "-":
self.take()
return ("neg", self.unary())
if self.peek() == "+":
# Magma accepts a leading '+'; same tree as its absence.
self.take()
return self.unary()
return self.power()
def power(self):
n = self.postfix()
while self.peek() == "^":
self.take()
neg = False
if self.peek() == "-":
self.take()
neg = True
e = self.take()
if not e.isdigit():
raise ParseError("exponent must be an integer literal, got %r, "
"in %r" % (e, self.src))
n = ("pow", n, -int(e) if neg else int(e))
return n
def postfix(self):
n = self.atom()
while True:
if self.peek() == "(":
if n[0] != "var":
raise ParseError("cannot call a non-name in %r" % self.src)
self.take()
args = []
if self.peek() != ")":
args.append(self.cond())
while self.peek() == ",":
self.take()
args.append(self.cond())
self.expect(")")
n = ("call", n[1], args)
elif self.peek() == "[":
self.take()
idx = self.cond()
self.expect("]")
n = ("index", n, idx)
else:
return n
def atom(self):
tk = self.take()
if tk == "(":
n = self.cond()
self.expect(")")
return n
if tk in ("[", "<"):
# Sequence `[..]` and tuple `<..>` literals, both nestable, both
# mapped to one list type: both are 1-based indexable and nothing in
# these files depends on Magma's homogeneous/heterogeneous
# distinction. The tuple form is needed for the genus-2 negReduced
# nch2 Precompute, which returns `<<<f0,...>>>`.
#
# `<` is unambiguous here: the formulas spell comparison
# `lt`/`le`/`gt`, never `<`, and the one other use of angle brackets,
# `R<x> := PolynomialRing(...)`, is matched as a whole statement
# before any expression parsing happens.
close = "]" if tk == "[" else ">"
items = []
if self.peek() != close:
items.append(self.cond())
while self.peek() == ",":
self.take()
items.append(self.cond())
self.expect(close)
return ("list", items)
if tk.isdigit():
return ("int", int(tk))
if tk in KEYWORDS:
raise ParseError("unexpected keyword %r in %r" % (tk, self.src))
return ("var", tk)
def _parse(s, want_cond):
p = _P(tokens(s), s)
n = p.cond() if want_cond else p.cond()
if p.i != len(p.t):
raise ParseError("trailing tokens %r in %r" % (p.t[p.i:], s))
return n
def parse_expr(s):
"""Parse an expression. Conditions are a superset, so this shares the entry."""
return _parse(s, False)
def parse_cond(s):
"""Parse a condition, same AST shape as parse_expr.
A bare expression is a truth test to the evaluator, which is what
`if ADD_DEBUG then` needs.
"""
return _parse(s, True)