public override GrammarParseResult VisitNumberExpr(NumberExprContext context) { if (context == null) { return(GrammarParseResult.Unsuccessful(context.GetText())); } Debug.WriteLine($"VisitNumberExpr \"{context.GetText()}\""); int value = int.Parse(context.GetText()); return(new GrammarParseResult(context.GetText()) { Value = value }); }
private ExpressionContext expression(int _p) { ParserRuleContext _parentctx = _ctx; int _parentState = State; ExpressionContext _localctx = new ExpressionContext(_ctx, _parentState); ExpressionContext _prevctx = _localctx; int _startState = 2; EnterRecursionRule(_localctx, 2, RULE_expression, _p); int _la; try { int _alt; EnterOuterAlt(_localctx, 1); { State = 21; _errHandler.Sync(this); switch (_input.La(1)) { case LPAREN: { _localctx = new ParenthesizedExprContext(_localctx); _ctx = _localctx; _prevctx = _localctx; State = 8; Match(LPAREN); State = 9; expression(0); State = 10; Match(RPAREN); } break; case MIN: case MAX: { _localctx = new MinMaxExprContext(_localctx); _ctx = _localctx; _prevctx = _localctx; State = 12; ((MinMaxExprContext)_localctx).operatorToken = _input.Lt(1); _la = _input.La(1); if (!(_la == MIN || _la == MAX)) { ((MinMaxExprContext)_localctx).operatorToken = _errHandler.RecoverInline(this); } else { if (_input.La(1) == TokenConstants.Eof) { matchedEOF = true; } _errHandler.ReportMatch(this); Consume(); } State = 13; Match(LPAREN); State = 14; expression(0); State = 15; Match(DESP); State = 16; expression(0); State = 17; Match(RPAREN); } break; case NUMBER: { _localctx = new NumberExprContext(_localctx); _ctx = _localctx; _prevctx = _localctx; State = 19; Match(NUMBER); } break; case IDENTIFIER: { _localctx = new IdentifierExprContext(_localctx); _ctx = _localctx; _prevctx = _localctx; State = 20; Match(IDENTIFIER); } break; default: throw new NoViableAltException(this); } _ctx.stop = _input.Lt(-1); State = 34; _errHandler.Sync(this); _alt = Interpreter.AdaptivePredict(_input, 2, _ctx); while (_alt != 2 && _alt != global::Antlr4.Runtime.Atn.ATN.InvalidAltNumber) { if (_alt == 1) { if (_parseListeners != null) { TriggerExitRuleEvent(); } _prevctx = _localctx; { State = 32; _errHandler.Sync(this); switch (Interpreter.AdaptivePredict(_input, 1, _ctx)) { case 1: { _localctx = new MultiplicativeExprContext(new ExpressionContext(_parentctx, _parentState)); PushNewRecursionContext(_localctx, _startState, RULE_expression); State = 23; if (!(Precpred(_ctx, 5))) { throw new FailedPredicateException(this, "Precpred(_ctx, 5)"); } State = 24; ((MultiplicativeExprContext)_localctx).operatorToken = _input.Lt(1); _la = _input.La(1); if (!(_la == MULTIPLY || _la == DIVIDE)) { ((MultiplicativeExprContext)_localctx).operatorToken = _errHandler.RecoverInline(this); } else { if (_input.La(1) == TokenConstants.Eof) { matchedEOF = true; } _errHandler.ReportMatch(this); Consume(); } State = 25; expression(6); } break; case 2: { _localctx = new ModDivContext(new ExpressionContext(_parentctx, _parentState)); PushNewRecursionContext(_localctx, _startState, RULE_expression); State = 26; if (!(Precpred(_ctx, 4))) { throw new FailedPredicateException(this, "Precpred(_ctx, 4)"); } State = 27; ((ModDivContext)_localctx).operatorToken = _input.Lt(1); _la = _input.La(1); if (!(_la == MOD || _la == DIV)) { ((ModDivContext)_localctx).operatorToken = _errHandler.RecoverInline(this); } else { if (_input.La(1) == TokenConstants.Eof) { matchedEOF = true; } _errHandler.ReportMatch(this); Consume(); } State = 28; expression(5); } break; case 3: { _localctx = new AdditiveExprContext(new ExpressionContext(_parentctx, _parentState)); PushNewRecursionContext(_localctx, _startState, RULE_expression); State = 29; if (!(Precpred(_ctx, 3))) { throw new FailedPredicateException(this, "Precpred(_ctx, 3)"); } State = 30; ((AdditiveExprContext)_localctx).operatorToken = _input.Lt(1); _la = _input.La(1); if (!(_la == SUBTRACT || _la == ADD)) { ((AdditiveExprContext)_localctx).operatorToken = _errHandler.RecoverInline(this); } else { if (_input.La(1) == TokenConstants.Eof) { matchedEOF = true; } _errHandler.ReportMatch(this); Consume(); } State = 31; expression(4); } break; } } } State = 36; _errHandler.Sync(this); _alt = Interpreter.AdaptivePredict(_input, 2, _ctx); } } } catch (RecognitionException re) { _localctx.exception = re; _errHandler.ReportError(this, re); _errHandler.Recover(this, re); } finally { UnrollRecursionContexts(_parentctx); } return(_localctx); }
private ExprContext expr(int _p) { ParserRuleContext _parentctx = Context; int _parentState = State; ExprContext _localctx = new ExprContext(Context, _parentState); ExprContext _prevctx = _localctx; int _startState = 2; EnterRecursionRule(_localctx, 2, RULE_expr, _p); int _la; try { int _alt; EnterOuterAlt(_localctx, 1); { State = 20; ErrorHandler.Sync(this); switch (TokenStream.LA(1)) { case T__0: { _localctx = new ParensExprContext(_localctx); Context = _localctx; _prevctx = _localctx; State = 8; Match(T__0); State = 9; expr(0); State = 10; Match(T__1); } break; case OP_ADD: case OP_SUB: { _localctx = new UnaryExprContext(_localctx); Context = _localctx; _prevctx = _localctx; State = 12; ((UnaryExprContext)_localctx).op = TokenStream.LT(1); _la = TokenStream.LA(1); if (!(_la == OP_ADD || _la == OP_SUB)) { ((UnaryExprContext)_localctx).op = ErrorHandler.RecoverInline(this); } else { ErrorHandler.ReportMatch(this); Consume(); } State = 13; expr(6); } break; case ID: { _localctx = new FuncExprContext(_localctx); Context = _localctx; _prevctx = _localctx; State = 14; ((FuncExprContext)_localctx).func = Match(ID); State = 15; Match(T__0); State = 16; expr(0); State = 17; Match(T__1); } break; case NUM: { _localctx = new NumberExprContext(_localctx); Context = _localctx; _prevctx = _localctx; State = 19; ((NumberExprContext)_localctx).value = Match(NUM); } break; default: throw new NoViableAltException(this); } Context.Stop = TokenStream.LT(-1); State = 33; ErrorHandler.Sync(this); _alt = Interpreter.AdaptivePredict(TokenStream, 2, Context); while (_alt != 2 && _alt != global::Antlr4.Runtime.Atn.ATN.INVALID_ALT_NUMBER) { if (_alt == 1) { if (ParseListeners != null) { TriggerExitRuleEvent(); } _prevctx = _localctx; { State = 31; ErrorHandler.Sync(this); switch (Interpreter.AdaptivePredict(TokenStream, 1, Context)) { case 1: { _localctx = new InfixExprContext(new ExprContext(_parentctx, _parentState)); ((InfixExprContext)_localctx).left = _prevctx; PushNewRecursionContext(_localctx, _startState, RULE_expr); State = 22; if (!(Precpred(Context, 5))) { throw new FailedPredicateException(this, "Precpred(Context, 5)"); } State = 23; ((InfixExprContext)_localctx).op = Match(OP_POW); State = 24; ((InfixExprContext)_localctx).right = expr(5); } break; case 2: { _localctx = new InfixExprContext(new ExprContext(_parentctx, _parentState)); ((InfixExprContext)_localctx).left = _prevctx; PushNewRecursionContext(_localctx, _startState, RULE_expr); State = 25; if (!(Precpred(Context, 4))) { throw new FailedPredicateException(this, "Precpred(Context, 4)"); } State = 26; ((InfixExprContext)_localctx).op = TokenStream.LT(1); _la = TokenStream.LA(1); if (!(_la == OP_MUL || _la == OP_DIV)) { ((InfixExprContext)_localctx).op = ErrorHandler.RecoverInline(this); } else { ErrorHandler.ReportMatch(this); Consume(); } State = 27; ((InfixExprContext)_localctx).right = expr(5); } break; case 3: { _localctx = new InfixExprContext(new ExprContext(_parentctx, _parentState)); ((InfixExprContext)_localctx).left = _prevctx; PushNewRecursionContext(_localctx, _startState, RULE_expr); State = 28; if (!(Precpred(Context, 3))) { throw new FailedPredicateException(this, "Precpred(Context, 3)"); } State = 29; ((InfixExprContext)_localctx).op = TokenStream.LT(1); _la = TokenStream.LA(1); if (!(_la == OP_ADD || _la == OP_SUB)) { ((InfixExprContext)_localctx).op = ErrorHandler.RecoverInline(this); } else { ErrorHandler.ReportMatch(this); Consume(); } State = 30; ((InfixExprContext)_localctx).right = expr(4); } break; } } } State = 35; ErrorHandler.Sync(this); _alt = Interpreter.AdaptivePredict(TokenStream, 2, Context); } } } catch (RecognitionException re) { _localctx.exception = re; ErrorHandler.ReportError(this, re); ErrorHandler.Recover(this, re); } finally { UnrollRecursionContexts(_parentctx); } return(_localctx); }
private ExpressionContext expression(int _p) { ParserRuleContext _parentctx = _ctx; int _parentState = State; ExpressionContext _localctx = new ExpressionContext(_ctx, _parentState); ExpressionContext _prevctx = _localctx; int _startState = 2; EnterRecursionRule(_localctx, 2, RULE_expression, _p); int _la; try { int _alt; EnterOuterAlt(_localctx, 1); { State = 13; switch (_input.La(1)) { case LPAREN: { _localctx = new ParenthesizedExprContext(_localctx); _ctx = _localctx; _prevctx = _localctx; State = 8; Match(LPAREN); State = 9; expression(0); State = 10; Match(RPAREN); } break; case NUMBER: { _localctx = new NumberExprContext(_localctx); _ctx = _localctx; _prevctx = _localctx; State = 12; Match(NUMBER); } break; default: throw new NoViableAltException(this); } _ctx.stop = _input.Lt(-1); State = 26; _errHandler.Sync(this); _alt = Interpreter.AdaptivePredict(_input, 2, _ctx); while (_alt != 2 && _alt != global::Antlr4.Runtime.Atn.ATN.InvalidAltNumber) { if (_alt == 1) { if (_parseListeners != null) { TriggerExitRuleEvent(); } _prevctx = _localctx; { State = 24; switch (Interpreter.AdaptivePredict(_input, 1, _ctx)) { case 1: { _localctx = new ExponentialExprContext(new ExpressionContext(_parentctx, _parentState)); PushNewRecursionContext(_localctx, _startState, RULE_expression); State = 15; if (!(Precpred(_ctx, 4))) { throw new FailedPredicateException(this, "Precpred(_ctx, 4)"); } State = 16; Match(EXPONENT); State = 17; expression(5); } break; case 2: { _localctx = new MultiplicativeExprContext(new ExpressionContext(_parentctx, _parentState)); PushNewRecursionContext(_localctx, _startState, RULE_expression); State = 18; if (!(Precpred(_ctx, 3))) { throw new FailedPredicateException(this, "Precpred(_ctx, 3)"); } State = 19; ((MultiplicativeExprContext)_localctx).operatorToken = _input.Lt(1); _la = _input.La(1); if (!(_la == MULTIPLY || _la == DIVIDE)) { ((MultiplicativeExprContext)_localctx).operatorToken = _errHandler.RecoverInline(this); } Consume(); State = 20; expression(4); } break; case 3: { _localctx = new AdditiveExprContext(new ExpressionContext(_parentctx, _parentState)); PushNewRecursionContext(_localctx, _startState, RULE_expression); State = 21; if (!(Precpred(_ctx, 2))) { throw new FailedPredicateException(this, "Precpred(_ctx, 2)"); } State = 22; ((AdditiveExprContext)_localctx).operatorToken = _input.Lt(1); _la = _input.La(1); if (!(_la == SUBTRACT || _la == ADD)) { ((AdditiveExprContext)_localctx).operatorToken = _errHandler.RecoverInline(this); } Consume(); State = 23; expression(3); } break; } } } State = 28; _errHandler.Sync(this); _alt = Interpreter.AdaptivePredict(_input, 2, _ctx); } } } catch (RecognitionException re) { _localctx.exception = re; _errHandler.ReportError(this, re); _errHandler.Recover(this, re); } finally { UnrollRecursionContexts(_parentctx); } return(_localctx); }
private ExpressionContext expression(int _p) { ParserRuleContext _parentctx = Context; int _parentState = State; ExpressionContext _localctx = new ExpressionContext(Context, _parentState); ExpressionContext _prevctx = _localctx; int _startState = 6; EnterRecursionRule(_localctx, 6, RULE_expression, _p); int _la; try { int _alt; EnterOuterAlt(_localctx, 1); { State = 55; ErrorHandler.Sync(this); switch (TokenStream.LA(1)) { case SUBTRACT: { _localctx = new UnaryMinusExprContext(_localctx); Context = _localctx; _prevctx = _localctx; State = 40; Match(SUBTRACT); State = 41; expression(8); } break; case DICE: case ADVANTAGE: case DISADVANTAGE: { _localctx = new ModifierRollContext(_localctx); Context = _localctx; _prevctx = _localctx; State = 45; ErrorHandler.Sync(this); _la = TokenStream.LA(1); while (_la == ADVANTAGE || _la == DISADVANTAGE) { { { State = 42; _la = TokenStream.LA(1); if (!(_la == ADVANTAGE || _la == DISADVANTAGE)) { ErrorHandler.RecoverInline(this); } else { ErrorHandler.ReportMatch(this); Consume(); } } } State = 47; ErrorHandler.Sync(this); _la = TokenStream.LA(1); } State = 48; Match(DICE); State = 49; ((ModifierRollContext)_localctx).sides = expression(7); } break; case L_PAREN: { _localctx = new ParenExprContext(_localctx); Context = _localctx; _prevctx = _localctx; State = 50; Match(L_PAREN); State = 51; expression(0); State = 52; Match(R_PAREN); } break; case INTEGER: { _localctx = new NumberExprContext(_localctx); Context = _localctx; _prevctx = _localctx; State = 54; Match(INTEGER); } break; default: throw new NoViableAltException(this); } Context.Stop = TokenStream.LT(-1); State = 71; ErrorHandler.Sync(this); _alt = Interpreter.AdaptivePredict(TokenStream, 8, Context); while (_alt != 2 && _alt != global::Antlr4.Runtime.Atn.ATN.INVALID_ALT_NUMBER) { if (_alt == 1) { if (ParseListeners != null) { TriggerExitRuleEvent(); } _prevctx = _localctx; { State = 69; ErrorHandler.Sync(this); switch (Interpreter.AdaptivePredict(TokenStream, 7, Context)) { case 1: { _localctx = new SumRollContext(new ExpressionContext(_parentctx, _parentState)); ((SumRollContext)_localctx).num = _prevctx; PushNewRecursionContext(_localctx, _startState, RULE_expression); State = 57; if (!(Precpred(Context, 6))) { throw new FailedPredicateException(this, "Precpred(Context, 6)"); } State = 58; Match(DICE); State = 59; ((SumRollContext)_localctx).sides = expression(7); } break; case 2: { _localctx = new PowExprContext(new ExpressionContext(_parentctx, _parentState)); ((PowExprContext)_localctx).lhs = _prevctx; PushNewRecursionContext(_localctx, _startState, RULE_expression); State = 60; if (!(Precpred(Context, 5))) { throw new FailedPredicateException(this, "Precpred(Context, 5)"); } State = 61; ((PowExprContext)_localctx).op = Match(POW); State = 62; ((PowExprContext)_localctx).rhs = expression(5); } break; case 3: { _localctx = new MultiplicationExprContext(new ExpressionContext(_parentctx, _parentState)); ((MultiplicationExprContext)_localctx).lhs = _prevctx; PushNewRecursionContext(_localctx, _startState, RULE_expression); State = 63; if (!(Precpred(Context, 4))) { throw new FailedPredicateException(this, "Precpred(Context, 4)"); } State = 64; ((MultiplicationExprContext)_localctx).op = TokenStream.LT(1); _la = TokenStream.LA(1); if (!((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << MULTIPLY) | (1L << DIVIDE) | (1L << MOD))) != 0))) { ((MultiplicationExprContext)_localctx).op = ErrorHandler.RecoverInline(this); } else { ErrorHandler.ReportMatch(this); Consume(); } State = 65; ((MultiplicationExprContext)_localctx).rhs = expression(5); } break; case 4: { _localctx = new AdditiveExprContext(new ExpressionContext(_parentctx, _parentState)); ((AdditiveExprContext)_localctx).lhs = _prevctx; PushNewRecursionContext(_localctx, _startState, RULE_expression); State = 66; if (!(Precpred(Context, 3))) { throw new FailedPredicateException(this, "Precpred(Context, 3)"); } State = 67; ((AdditiveExprContext)_localctx).op = TokenStream.LT(1); _la = TokenStream.LA(1); if (!(_la == ADD || _la == SUBTRACT)) { ((AdditiveExprContext)_localctx).op = ErrorHandler.RecoverInline(this); } else { ErrorHandler.ReportMatch(this); Consume(); } State = 68; ((AdditiveExprContext)_localctx).rhs = expression(4); } break; } } } State = 73; ErrorHandler.Sync(this); _alt = Interpreter.AdaptivePredict(TokenStream, 8, Context); } } } catch (RecognitionException re) { _localctx.exception = re; ErrorHandler.ReportError(this, re); ErrorHandler.Recover(this, re); } finally { UnrollRecursionContexts(_parentctx); } return(_localctx); }