1 // Generated from doc/examples/simple_expression/Expr.g4 by ANTLR 4.9.2
2 module ExprParser;
3 
4 import antlr.v4.runtime.atn.ATN : ATN;
5 alias ATNType = ATN;
6 import antlr.v4.runtime.atn.ATNDeserializer;
7 import antlr.v4.runtime.atn.PredictionContextCache;
8 import antlr.v4.runtime.atn.PredictionModeConst;
9 import antlr.v4.runtime.atn.ParserATNSimulator;
10 import antlr.v4.runtime.atn.InterfaceParserATNSimulator;
11 import antlr.v4.runtime.dfa.DFA;
12 import antlr.v4.runtime.BailErrorStrategy;
13 import antlr.v4.runtime.RuntimeMetaData;
14 import antlr.v4.runtime.Parser;
15 import antlr.v4.runtime.Token;
16 import antlr.v4.runtime.CommonToken;
17 import antlr.v4.runtime.TokenConstantDefinition;
18 import antlr.v4.runtime.ParserRuleContext;
19 import antlr.v4.runtime.InterfaceRuleContext;
20 import antlr.v4.runtime.LexerNoViableAltException;
21 import antlr.v4.runtime.FailedPredicateException;
22 import antlr.v4.runtime.NoViableAltException;
23 import antlr.v4.runtime.RecognitionException;
24 import antlr.v4.runtime.Vocabulary;
25 import antlr.v4.runtime.VocabularyImpl;
26 import antlr.v4.runtime.TokenStream;
27 import antlr.v4.runtime.tree.ParseTreeListener;
28 import antlr.v4.runtime.tree.ParseTreeVisitor;
29 import antlr.v4.runtime.tree.TerminalNode;
30 import antlr.v4.runtime.tree.ParseTreeWalker;
31 import ExprListener : ExprListener;
32 import ExprVisitor : ExprVisitor;
33 import std.conv : to;
34 import std.variant : Variant;
35 
36 public class ExprParser : Parser {
37     static this() { RuntimeMetaData.checkVersion("4.9.2", RuntimeMetaData.VERSION); }
38 
39     protected static DFA[] _decisionToDFA;
40     protected PredictionContextCache _sharedContextCache =
41         new PredictionContextCache();
42     public enum int
43         T__0=1,T__1=2,T__2=3,T__3=4,T__4=5,T__5=6,NEWLINE=7,INT=8;
44     public enum int
45         RULE_prog = 0,RULE_expr = 1;
46     public static const string[] ruleNames = [
47         "prog","expr"
48     ];
49 
50     private static const string[] _LITERAL_NAMES = [
51         null,"'/'","'*'","'-'","'+'","'('","')'"
52     ];
53     private static const string[] _SYMBOLIC_NAMES = [
54         null,null,null,null,null,null,null,"NEWLINE","INT"
55     ];
56     public static Vocabulary VOCABULARY;
57 
58     /**
59      * @deprecated Use {@link #VOCABULARY} instead.
60      */
61     public static string[_SYMBOLIC_NAMES.length] tokenNames;
62 
63     static this() {
64         VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
65         int index = 0;
66         foreach (ref tokenName; tokenNames)
67             {
68                 tokenName = VOCABULARY.getLiteralName(index);
69                     if (!tokenName) {
70                         tokenName = VOCABULARY.getSymbolicName(index);
71                 }
72                 if (!tokenName)
73                 {
74                     tokenName = "<INVALID>";
75                 }
76             }
77             ++index;
78     }
79 
80     override public string[] getTokenNames() {
81         return tokenNames;
82     }
83 
84     override public Vocabulary getVocabulary() {
85         return VOCABULARY;
86     }
87 
88     override
89     public string getGrammarFileName() { return "Expr.g4"; }
90 
91     override
92     public string[] getRuleNames() { return ruleNames.dup; }
93 
94     override
95     public wstring getSerializedATN() { return _serializedATN; }
96 
97     override
98     public ATNType getATN() { return _ATN; }
99 
100     public this(TokenStream input) {
101         super(input);
102         _interp = new ParserATNSimulator(this,_ATN, _decisionToDFA, _sharedContextCache);
103     }
104 
105     public static class ProgContext : ParserRuleContext {
106         public ExprContext[] expr() {
107             return getRuleContexts!ExprContext;
108         }
109         public ExprContext expr(int i) {
110             return getRuleContext!ExprContext(i);
111         }
112         public TerminalNode[] NEWLINE() { return getTokens(ExprParser.NEWLINE); }
113         public TerminalNode NEWLINE(int i) {
114             return getToken(ExprParser.NEWLINE, i);
115         }
116         public this(ParserRuleContext parent, int invokingState) {
117             super(parent, invokingState);
118         }
119         override public size_t getRuleIndex() { return RULE_prog; }
120         override
121         public void enterRule(ParseTreeListener listener) {
122             if (cast(ExprListener) listener)
123                 (cast(ExprListener) listener).enterProg(this);
124         }
125         override
126         public void exitRule(ParseTreeListener listener) {
127             if (cast(ExprListener) listener)
128                 (cast(ExprListener) listener).exitProg(this);
129         }
130         override
131         public Variant accept(ParseTreeVisitor visitor) {
132             if (cast(ExprVisitor) visitor)
133                 return (cast(ExprVisitor) visitor).visitProg(this);
134             else
135                 return visitor.visitChildren(this);
136         }
137     }
138 
139     public ProgContext prog() {
140         ProgContext _localctx = new ProgContext(ctx_, getState());
141         enterRule(_localctx, 0, RULE_prog);
142         int _la;
143         try {
144             enterOuterAlt(_localctx, 1);
145             {
146             setState(9);
147             _errHandler.sync(this);
148             _la = _input.LA(1);
149             while (_la==T__4 || _la==INT) {
150                 {
151                 {
152                 setState(4);
153                 expr(0);
154                 setState(5);
155                 match(NEWLINE);
156                 }
157                 }
158                 setState(11);
159                 _errHandler.sync(this);
160                 _la = _input.LA(1);
161             }
162             }
163         }
164         catch (RecognitionException re) {
165             _localctx.exception = re;
166             _errHandler.reportError(this, re);
167             _errHandler.recover(this, re);
168         }
169         finally {
170             exitRule();
171         }
172         return _localctx;
173     }
174 
175     public static class ExprContext : ParserRuleContext {
176         public this(ParserRuleContext parent, int invokingState) {
177             super(parent, invokingState);
178         }
179         override public size_t getRuleIndex() { return RULE_expr; }
180      
181         public this() { }
182             alias copyFrom = ParserRuleContext.copyFrom;
183         public void copyFrom(ExprContext ctx) {
184             super.copyFrom(ctx);
185         }
186     }
187     public static class DivContext : ExprContext {
188         public ExprContext[] expr() {
189             return getRuleContexts!ExprContext;
190         }
191         public ExprContext expr(int i) {
192             return getRuleContext!ExprContext(i);
193         }
194             alias copyFrom = ExprContext.copyFrom;
195         public this(ExprContext ctx) { copyFrom(ctx); }
196         override
197         public void enterRule(ParseTreeListener listener) {
198             if (cast(ExprListener) listener)
199                 (cast(ExprListener) listener).enterDiv(this);
200         }
201         override
202         public void exitRule(ParseTreeListener listener) {
203             if (cast(ExprListener) listener)
204                 (cast(ExprListener) listener).exitDiv(this);
205         }
206         override
207         public Variant accept(ParseTreeVisitor visitor) {
208             if (cast(ExprVisitor) visitor)
209                 return (cast(ExprVisitor) visitor).visitDiv(this);
210             else
211                 return visitor.visitChildren(this);
212         }
213     }
214     public static class AddContext : ExprContext {
215         public ExprContext[] expr() {
216             return getRuleContexts!ExprContext;
217         }
218         public ExprContext expr(int i) {
219             return getRuleContext!ExprContext(i);
220         }
221             alias copyFrom = ExprContext.copyFrom;
222         public this(ExprContext ctx) { copyFrom(ctx); }
223         override
224         public void enterRule(ParseTreeListener listener) {
225             if (cast(ExprListener) listener)
226                 (cast(ExprListener) listener).enterAdd(this);
227         }
228         override
229         public void exitRule(ParseTreeListener listener) {
230             if (cast(ExprListener) listener)
231                 (cast(ExprListener) listener).exitAdd(this);
232         }
233         override
234         public Variant accept(ParseTreeVisitor visitor) {
235             if (cast(ExprVisitor) visitor)
236                 return (cast(ExprVisitor) visitor).visitAdd(this);
237             else
238                 return visitor.visitChildren(this);
239         }
240     }
241     public static class SubContext : ExprContext {
242         public ExprContext[] expr() {
243             return getRuleContexts!ExprContext;
244         }
245         public ExprContext expr(int i) {
246             return getRuleContext!ExprContext(i);
247         }
248             alias copyFrom = ExprContext.copyFrom;
249         public this(ExprContext ctx) { copyFrom(ctx); }
250         override
251         public void enterRule(ParseTreeListener listener) {
252             if (cast(ExprListener) listener)
253                 (cast(ExprListener) listener).enterSub(this);
254         }
255         override
256         public void exitRule(ParseTreeListener listener) {
257             if (cast(ExprListener) listener)
258                 (cast(ExprListener) listener).exitSub(this);
259         }
260         override
261         public Variant accept(ParseTreeVisitor visitor) {
262             if (cast(ExprVisitor) visitor)
263                 return (cast(ExprVisitor) visitor).visitSub(this);
264             else
265                 return visitor.visitChildren(this);
266         }
267     }
268     public static class MulContext : ExprContext {
269         public ExprContext[] expr() {
270             return getRuleContexts!ExprContext;
271         }
272         public ExprContext expr(int i) {
273             return getRuleContext!ExprContext(i);
274         }
275             alias copyFrom = ExprContext.copyFrom;
276         public this(ExprContext ctx) { copyFrom(ctx); }
277         override
278         public void enterRule(ParseTreeListener listener) {
279             if (cast(ExprListener) listener)
280                 (cast(ExprListener) listener).enterMul(this);
281         }
282         override
283         public void exitRule(ParseTreeListener listener) {
284             if (cast(ExprListener) listener)
285                 (cast(ExprListener) listener).exitMul(this);
286         }
287         override
288         public Variant accept(ParseTreeVisitor visitor) {
289             if (cast(ExprVisitor) visitor)
290                 return (cast(ExprVisitor) visitor).visitMul(this);
291             else
292                 return visitor.visitChildren(this);
293         }
294     }
295     public static class BracketExprContext : ExprContext {
296         public ExprContext expr() {
297             return getRuleContext!ExprContext(0);
298         }
299             alias copyFrom = ExprContext.copyFrom;
300         public this(ExprContext ctx) { copyFrom(ctx); }
301         override
302         public void enterRule(ParseTreeListener listener) {
303             if (cast(ExprListener) listener)
304                 (cast(ExprListener) listener).enterBracketExpr(this);
305         }
306         override
307         public void exitRule(ParseTreeListener listener) {
308             if (cast(ExprListener) listener)
309                 (cast(ExprListener) listener).exitBracketExpr(this);
310         }
311         override
312         public Variant accept(ParseTreeVisitor visitor) {
313             if (cast(ExprVisitor) visitor)
314                 return (cast(ExprVisitor) visitor).visitBracketExpr(this);
315             else
316                 return visitor.visitChildren(this);
317         }
318     }
319     public static class IntContext : ExprContext {
320         public TerminalNode INT() { return getToken(ExprParser.INT, 0); }
321             alias copyFrom = ExprContext.copyFrom;
322         public this(ExprContext ctx) { copyFrom(ctx); }
323         override
324         public void enterRule(ParseTreeListener listener) {
325             if (cast(ExprListener) listener)
326                 (cast(ExprListener) listener).enterInt(this);
327         }
328         override
329         public void exitRule(ParseTreeListener listener) {
330             if (cast(ExprListener) listener)
331                 (cast(ExprListener) listener).exitInt(this);
332         }
333         override
334         public Variant accept(ParseTreeVisitor visitor) {
335             if (cast(ExprVisitor) visitor)
336                 return (cast(ExprVisitor) visitor).visitInt(this);
337             else
338                 return visitor.visitChildren(this);
339         }
340     }
341 
342     public ExprContext expr() {
343         return expr(0);
344     }
345 
346     private ExprContext expr(int _p) {
347         ParserRuleContext _parentctx = ctx_;
348         int _parentState = getState();
349         ExprContext _localctx = new ExprContext(ctx_, _parentState);
350         ExprContext _prevctx = _localctx;
351         int _startState = 2;
352         enterRecursionRule(_localctx, 2, RULE_expr, _p);
353         try {
354             int _alt;
355             enterOuterAlt(_localctx, 1);
356             {
357             setState(18);
358             _errHandler.sync(this);
359             switch (_input.LA(1)) {
360             case INT:
361                 {
362                 _localctx = new IntContext(_localctx);
363                 ctx_ = _localctx;
364                 _prevctx = _localctx;
365 
366                 setState(13);
367                 match(INT);
368                 }
369                 break;
370             case T__4:
371                 {
372                 _localctx = new BracketExprContext(_localctx);
373                 ctx_ = _localctx;
374                 _prevctx = _localctx;
375                 setState(14);
376                 match(T__4);
377                 setState(15);
378                 expr(0);
379                 setState(16);
380                 match(T__5);
381                 }
382                 break;
383             default:
384                 throw new NoViableAltException(this);
385             }
386             ctx_.stop = _input.LT(-1);
387             setState(34);
388             _errHandler.sync(this);
389             _alt = getInterpreter.adaptivePredict(_input,3, ctx_);
390             while ( _alt!=2 && _alt!=ATNType.INVALID_ALT_NUMBER ) {
391                 if ( _alt==1 ) {
392                     if (_parseListeners) triggerExitRuleEvent();
393                     _prevctx = _localctx;
394                     {
395                     setState(32);
396                     _errHandler.sync(this);
397                     switch ( getInterpreter.adaptivePredict(_input,2, ctx_) ) {
398                     case 1:
399                         {
400                         _localctx = new DivContext(new ExprContext(_parentctx, _parentState));
401                         pushNewRecursionContext(_localctx, _startState, RULE_expr);
402                         setState(20);
403                         if (!(precpred(ctx_, 6))) throw new FailedPredicateException(this, "precpred(ctx_, 6)");
404                         setState(21);
405                         match(T__0);
406                         setState(22);
407                         expr(7);
408                         }
409                         break;
410                     case 2:
411                         {
412                         _localctx = new MulContext(new ExprContext(_parentctx, _parentState));
413                         pushNewRecursionContext(_localctx, _startState, RULE_expr);
414                         setState(23);
415                         if (!(precpred(ctx_, 5))) throw new FailedPredicateException(this, "precpred(ctx_, 5)");
416                         setState(24);
417                         match(T__1);
418                         setState(25);
419                         expr(6);
420                         }
421                         break;
422                     case 3:
423                         {
424                         _localctx = new SubContext(new ExprContext(_parentctx, _parentState));
425                         pushNewRecursionContext(_localctx, _startState, RULE_expr);
426                         setState(26);
427                         if (!(precpred(ctx_, 4))) throw new FailedPredicateException(this, "precpred(ctx_, 4)");
428                         setState(27);
429                         match(T__2);
430                         setState(28);
431                         expr(5);
432                         }
433                         break;
434                     case 4:
435                         {
436                         _localctx = new AddContext(new ExprContext(_parentctx, _parentState));
437                         pushNewRecursionContext(_localctx, _startState, RULE_expr);
438                         setState(29);
439                         if (!(precpred(ctx_, 3))) throw new FailedPredicateException(this, "precpred(ctx_, 3)");
440                         setState(30);
441                         match(T__3);
442                         setState(31);
443                         expr(4);
444                         }
445                         break;
446                             default: {}
447                     }
448                     } 
449                 }
450                 setState(36);
451                 _errHandler.sync(this);
452                 _alt = getInterpreter.adaptivePredict(_input,3, ctx_);
453             }
454             }
455         }
456         catch (RecognitionException re) {
457             _localctx.exception = re;
458             _errHandler.reportError(this, re);
459             _errHandler.recover(this, re);
460         }
461         finally {
462             unrollRecursionContexts(_parentctx);
463         }
464         return _localctx;
465     }
466 
467     override
468     public bool sempred(InterfaceRuleContext _localctx, int ruleIndex, int predIndex) {
469         switch (ruleIndex) {
470         case 1:
471             return expr_sempred(cast(ExprContext)_localctx, predIndex);
472     default: {}
473     }
474     return true;
475 }
476     private bool expr_sempred(ExprContext _localctx, int predIndex) {
477         switch (predIndex) {
478         case 0:
479             return precpred(ctx_, 6);
480         case 1:
481             return precpred(ctx_, 5);
482         case 2:
483             return precpred(ctx_, 4);
484         case 3:
485             return precpred(ctx_, 3);
486             default: {}
487         }
488         return true;
489     }
490 
491     public static immutable wstring _serializedATN =
492         "\x03\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\x03\n(\x04"~
493     	"\x02\t\x02\x04\x03\t\x03\x03\x02\x03\x02\x03\x02\x07\x02\n\n\x02\f"~
494     	"\x02\x0e\x02\r\x0b\x02\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03"~
495     	"\x03\x05\x03\x15\n\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03"~
496     	"\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x07\x03#\n\x03"~
497     	"\f\x03\x0e\x03&\x0b\x03\x03\x03\x02\x03\x04\x04\x02\x04\x02\x02\x02"~
498     	"+\x02\x0b\x03\x02\x02\x02\x04\x14\x03\x02\x02\x02\x06\x07\x05\x04\x03"~
499     	"\x02\x07\b\x07\t\x02\x02\b\n\x03\x02\x02\x02\t\x06\x03\x02\x02\x02"~
500     	"\n\r\x03\x02\x02\x02\x0b\t\x03\x02\x02\x02\x0b\f\x03\x02\x02\x02\f"~
501     	"\x03\x03\x02\x02\x02\r\x0b\x03\x02\x02\x02\x0e\x0f\b\x03\x01\x02\x0f"~
502     	"\x15\x07\n\x02\x02\x10\x11\x07\x07\x02\x02\x11\x12\x05\x04\x03\x02"~
503     	"\x12\x13\x07\b\x02\x02\x13\x15\x03\x02\x02\x02\x14\x0e\x03\x02\x02"~
504     	"\x02\x14\x10\x03\x02\x02\x02\x15$\x03\x02\x02\x02\x16\x17\f\b\x02\x02"~
505     	"\x17\x18\x07\x03\x02\x02\x18#\x05\x04\x03\t\x19\x1a\f\x07\x02\x02\x1a"~
506     	"\x1b\x07\x04\x02\x02\x1b#\x05\x04\x03\b\x1c\x1d\f\x06\x02\x02\x1d\x1e"~
507     	"\x07\x05\x02\x02\x1e#\x05\x04\x03\x07\x1f \f\x05\x02\x02 !\x07\x06"~
508     	"\x02\x02!#\x05\x04\x03\x06\"\x16\x03\x02\x02\x02\"\x19\x03\x02\x02"~
509     	"\x02\"\x1c\x03\x02\x02\x02\"\x1f\x03\x02\x02\x02#&\x03\x02\x02\x02"~
510     	"$\"\x03\x02\x02\x02$%\x03\x02\x02\x02%\x05\x03\x02\x02\x02&$\x03\x02"~
511     	"\x02\x02\x06\x0b\x14\"$";
512     public static ATNType _ATN;
513 
514     static this() {
515         auto atnDeserializer = new ATNDeserializer;
516         _ATN = atnDeserializer.deserialize(_serializedATN);
517         _decisionToDFA.length = 0;
518         for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
519             _decisionToDFA ~= new DFA(_ATN.getDecisionState(i), i);
520         }
521     }
522 }