-
Notifications
You must be signed in to change notification settings - Fork 1
/
MathParser.py
434 lines (337 loc) · 15.4 KB
/
MathParser.py
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
# Generated from Math.g4 by ANTLR 4.7.2
# encoding: utf-8
from antlr4 import *
from io import StringIO
from typing.io import TextIO
import sys
def serializedATN():
with StringIO() as buf:
buf.write("\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\13")
buf.write("$\4\2\t\2\4\3\t\3\3\2\3\2\3\2\3\3\3\3\3\3\3\3\3\3\3\3")
buf.write("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\5\3\27\n\3\3\3\3\3\3\3\3")
buf.write("\3\3\3\3\3\7\3\37\n\3\f\3\16\3\"\13\3\3\3\2\3\4\4\2\4")
buf.write("\2\4\3\2\5\6\3\2\7\b\2&\2\6\3\2\2\2\4\26\3\2\2\2\6\7\5")
buf.write("\4\3\2\7\b\7\2\2\3\b\3\3\2\2\2\t\n\b\3\1\2\n\13\7\3\2")
buf.write("\2\13\f\5\4\3\2\f\r\7\4\2\2\r\27\3\2\2\2\16\17\t\2\2\2")
buf.write("\17\27\5\4\3\7\20\21\7\n\2\2\21\22\7\3\2\2\22\23\5\4\3")
buf.write("\2\23\24\7\4\2\2\24\27\3\2\2\2\25\27\7\t\2\2\26\t\3\2")
buf.write("\2\2\26\16\3\2\2\2\26\20\3\2\2\2\26\25\3\2\2\2\27 \3\2")
buf.write("\2\2\30\31\f\6\2\2\31\32\t\3\2\2\32\37\5\4\3\7\33\34\f")
buf.write("\5\2\2\34\35\t\2\2\2\35\37\5\4\3\6\36\30\3\2\2\2\36\33")
buf.write("\3\2\2\2\37\"\3\2\2\2 \36\3\2\2\2 !\3\2\2\2!\5\3\2\2\2")
buf.write("\" \3\2\2\2\5\26\36 ")
return buf.getvalue()
class MathParser ( Parser ):
grammarFileName = "Math.g4"
atn = ATNDeserializer().deserialize(serializedATN())
decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ]
sharedContextCache = PredictionContextCache()
literalNames = [ "<INVALID>", "'('", "')'", "'+'", "'-'", "'*'", "'/'" ]
symbolicNames = [ "<INVALID>", "<INVALID>", "<INVALID>", "OP_ADD", "OP_SUB",
"OP_MUL", "OP_DIV", "NUM", "ID", "WS" ]
RULE_compileUnit = 0
RULE_expr = 1
ruleNames = [ "compileUnit", "expr" ]
EOF = Token.EOF
T__0=1
T__1=2
OP_ADD=3
OP_SUB=4
OP_MUL=5
OP_DIV=6
NUM=7
ID=8
WS=9
def __init__(self, input:TokenStream, output:TextIO = sys.stdout):
super().__init__(input, output)
self.checkVersion("4.7.2")
self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache)
self._predicates = None
class CompileUnitContext(ParserRuleContext):
def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1):
super().__init__(parent, invokingState)
self.parser = parser
def expr(self):
return self.getTypedRuleContext(MathParser.ExprContext,0)
def EOF(self):
return self.getToken(MathParser.EOF, 0)
def getRuleIndex(self):
return MathParser.RULE_compileUnit
def enterRule(self, listener:ParseTreeListener):
if hasattr( listener, "enterCompileUnit" ):
listener.enterCompileUnit(self)
def exitRule(self, listener:ParseTreeListener):
if hasattr( listener, "exitCompileUnit" ):
listener.exitCompileUnit(self)
def accept(self, visitor:ParseTreeVisitor):
if hasattr( visitor, "visitCompileUnit" ):
return visitor.visitCompileUnit(self)
else:
return visitor.visitChildren(self)
def compileUnit(self):
localctx = MathParser.CompileUnitContext(self, self._ctx, self.state)
self.enterRule(localctx, 0, self.RULE_compileUnit)
try:
self.enterOuterAlt(localctx, 1)
self.state = 4
self.expr(0)
self.state = 5
self.match(MathParser.EOF)
except RecognitionException as re:
localctx.exception = re
self._errHandler.reportError(self, re)
self._errHandler.recover(self, re)
finally:
self.exitRule()
return localctx
class ExprContext(ParserRuleContext):
def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1):
super().__init__(parent, invokingState)
self.parser = parser
def getRuleIndex(self):
return MathParser.RULE_expr
def copyFrom(self, ctx:ParserRuleContext):
super().copyFrom(ctx)
class InfixExprContext(ExprContext):
def __init__(self, parser, ctx:ParserRuleContext): # actually a MathParser.ExprContext
super().__init__(parser)
self.left = None # ExprContext
self.op = None # Token
self.right = None # ExprContext
self.copyFrom(ctx)
def expr(self, i:int=None):
if i is None:
return self.getTypedRuleContexts(MathParser.ExprContext)
else:
return self.getTypedRuleContext(MathParser.ExprContext,i)
def OP_MUL(self):
return self.getToken(MathParser.OP_MUL, 0)
def OP_DIV(self):
return self.getToken(MathParser.OP_DIV, 0)
def OP_ADD(self):
return self.getToken(MathParser.OP_ADD, 0)
def OP_SUB(self):
return self.getToken(MathParser.OP_SUB, 0)
def enterRule(self, listener:ParseTreeListener):
if hasattr( listener, "enterInfixExpr" ):
listener.enterInfixExpr(self)
def exitRule(self, listener:ParseTreeListener):
if hasattr( listener, "exitInfixExpr" ):
listener.exitInfixExpr(self)
def accept(self, visitor:ParseTreeVisitor):
if hasattr( visitor, "visitInfixExpr" ):
return visitor.visitInfixExpr(self)
else:
return visitor.visitChildren(self)
class UnaryExprContext(ExprContext):
def __init__(self, parser, ctx:ParserRuleContext): # actually a MathParser.ExprContext
super().__init__(parser)
self.op = None # Token
self.copyFrom(ctx)
def expr(self):
return self.getTypedRuleContext(MathParser.ExprContext,0)
def OP_ADD(self):
return self.getToken(MathParser.OP_ADD, 0)
def OP_SUB(self):
return self.getToken(MathParser.OP_SUB, 0)
def enterRule(self, listener:ParseTreeListener):
if hasattr( listener, "enterUnaryExpr" ):
listener.enterUnaryExpr(self)
def exitRule(self, listener:ParseTreeListener):
if hasattr( listener, "exitUnaryExpr" ):
listener.exitUnaryExpr(self)
def accept(self, visitor:ParseTreeVisitor):
if hasattr( visitor, "visitUnaryExpr" ):
return visitor.visitUnaryExpr(self)
else:
return visitor.visitChildren(self)
class FuncExprContext(ExprContext):
def __init__(self, parser, ctx:ParserRuleContext): # actually a MathParser.ExprContext
super().__init__(parser)
self.func = None # Token
self.copyFrom(ctx)
def expr(self):
return self.getTypedRuleContext(MathParser.ExprContext,0)
def ID(self):
return self.getToken(MathParser.ID, 0)
def enterRule(self, listener:ParseTreeListener):
if hasattr( listener, "enterFuncExpr" ):
listener.enterFuncExpr(self)
def exitRule(self, listener:ParseTreeListener):
if hasattr( listener, "exitFuncExpr" ):
listener.exitFuncExpr(self)
def accept(self, visitor:ParseTreeVisitor):
if hasattr( visitor, "visitFuncExpr" ):
return visitor.visitFuncExpr(self)
else:
return visitor.visitChildren(self)
class NumberExprContext(ExprContext):
def __init__(self, parser, ctx:ParserRuleContext): # actually a MathParser.ExprContext
super().__init__(parser)
self.value = None # Token
self.copyFrom(ctx)
def NUM(self):
return self.getToken(MathParser.NUM, 0)
def enterRule(self, listener:ParseTreeListener):
if hasattr( listener, "enterNumberExpr" ):
listener.enterNumberExpr(self)
def exitRule(self, listener:ParseTreeListener):
if hasattr( listener, "exitNumberExpr" ):
listener.exitNumberExpr(self)
def accept(self, visitor:ParseTreeVisitor):
if hasattr( visitor, "visitNumberExpr" ):
return visitor.visitNumberExpr(self)
else:
return visitor.visitChildren(self)
class ParensExprContext(ExprContext):
def __init__(self, parser, ctx:ParserRuleContext): # actually a MathParser.ExprContext
super().__init__(parser)
self.copyFrom(ctx)
def expr(self):
return self.getTypedRuleContext(MathParser.ExprContext,0)
def enterRule(self, listener:ParseTreeListener):
if hasattr( listener, "enterParensExpr" ):
listener.enterParensExpr(self)
def exitRule(self, listener:ParseTreeListener):
if hasattr( listener, "exitParensExpr" ):
listener.exitParensExpr(self)
def accept(self, visitor:ParseTreeVisitor):
if hasattr( visitor, "visitParensExpr" ):
return visitor.visitParensExpr(self)
else:
return visitor.visitChildren(self)
def expr(self, _p:int=0):
_parentctx = self._ctx
_parentState = self.state
localctx = MathParser.ExprContext(self, self._ctx, _parentState)
_prevctx = localctx
_startState = 2
self.enterRecursionRule(localctx, 2, self.RULE_expr, _p)
self._la = 0 # Token type
try:
self.enterOuterAlt(localctx, 1)
self.state = 20
self._errHandler.sync(self)
token = self._input.LA(1)
if token in [MathParser.T__0]:
localctx = MathParser.ParensExprContext(self, localctx)
self._ctx = localctx
_prevctx = localctx
self.state = 8
self.match(MathParser.T__0)
self.state = 9
self.expr(0)
self.state = 10
self.match(MathParser.T__1)
pass
elif token in [MathParser.OP_ADD, MathParser.OP_SUB]:
localctx = MathParser.UnaryExprContext(self, localctx)
self._ctx = localctx
_prevctx = localctx
self.state = 12
localctx.op = self._input.LT(1)
_la = self._input.LA(1)
if not(_la==MathParser.OP_ADD or _la==MathParser.OP_SUB):
localctx.op = self._errHandler.recoverInline(self)
else:
self._errHandler.reportMatch(self)
self.consume()
self.state = 13
self.expr(5)
pass
elif token in [MathParser.ID]:
localctx = MathParser.FuncExprContext(self, localctx)
self._ctx = localctx
_prevctx = localctx
self.state = 14
localctx.func = self.match(MathParser.ID)
self.state = 15
self.match(MathParser.T__0)
self.state = 16
self.expr(0)
self.state = 17
self.match(MathParser.T__1)
pass
elif token in [MathParser.NUM]:
localctx = MathParser.NumberExprContext(self, localctx)
self._ctx = localctx
_prevctx = localctx
self.state = 19
localctx.value = self.match(MathParser.NUM)
pass
else:
raise NoViableAltException(self)
self._ctx.stop = self._input.LT(-1)
self.state = 30
self._errHandler.sync(self)
_alt = self._interp.adaptivePredict(self._input,2,self._ctx)
while _alt!=2 and _alt!=ATN.INVALID_ALT_NUMBER:
if _alt==1:
if self._parseListeners is not None:
self.triggerExitRuleEvent()
_prevctx = localctx
self.state = 28
self._errHandler.sync(self)
la_ = self._interp.adaptivePredict(self._input,1,self._ctx)
if la_ == 1:
localctx = MathParser.InfixExprContext(self, MathParser.ExprContext(self, _parentctx, _parentState))
localctx.left = _prevctx
self.pushNewRecursionContext(localctx, _startState, self.RULE_expr)
self.state = 22
if not self.precpred(self._ctx, 4):
from antlr4.error.Errors import FailedPredicateException
raise FailedPredicateException(self, "self.precpred(self._ctx, 4)")
self.state = 23
localctx.op = self._input.LT(1)
_la = self._input.LA(1)
if not(_la==MathParser.OP_MUL or _la==MathParser.OP_DIV):
localctx.op = self._errHandler.recoverInline(self)
else:
self._errHandler.reportMatch(self)
self.consume()
self.state = 24
localctx.right = self.expr(5)
pass
elif la_ == 2:
localctx = MathParser.InfixExprContext(self, MathParser.ExprContext(self, _parentctx, _parentState))
localctx.left = _prevctx
self.pushNewRecursionContext(localctx, _startState, self.RULE_expr)
self.state = 25
if not self.precpred(self._ctx, 3):
from antlr4.error.Errors import FailedPredicateException
raise FailedPredicateException(self, "self.precpred(self._ctx, 3)")
self.state = 26
localctx.op = self._input.LT(1)
_la = self._input.LA(1)
if not(_la==MathParser.OP_ADD or _la==MathParser.OP_SUB):
localctx.op = self._errHandler.recoverInline(self)
else:
self._errHandler.reportMatch(self)
self.consume()
self.state = 27
localctx.right = self.expr(4)
pass
self.state = 32
self._errHandler.sync(self)
_alt = self._interp.adaptivePredict(self._input,2,self._ctx)
except RecognitionException as re:
localctx.exception = re
self._errHandler.reportError(self, re)
self._errHandler.recover(self, re)
finally:
self.unrollRecursionContexts(_parentctx)
return localctx
def sempred(self, localctx:RuleContext, ruleIndex:int, predIndex:int):
if self._predicates == None:
self._predicates = dict()
self._predicates[1] = self.expr_sempred
pred = self._predicates.get(ruleIndex, None)
if pred is None:
raise Exception("No predicate with index:" + str(ruleIndex))
else:
return pred(localctx, predIndex)
def expr_sempred(self, localctx:ExprContext, predIndex:int):
if predIndex == 0:
return self.precpred(self._ctx, 4)
if predIndex == 1:
return self.precpred(self._ctx, 3)