public void testVisitPowerExp()
        {
            setup("4^3^2");

            PowerExpContext context = parser.expression() as PowerExpContext;

            DoubleSpreadsheetVisitor visitor = new DoubleSpreadsheetVisitor();
            double result = visitor.VisitPowerExp(context);

            Assert.Equal(double.Parse("262144"), result);
        }
        public void testExpressionPow()
        {
            setup("5^3^2");

            PowerExpContext context = parser.expression() as PowerExpContext;

            CommonTokenStream ts = (CommonTokenStream)parser.InputStream;

            Assert.Equal(SpreadsheetLexer.NUMBER, ts.Get(0).Type);
            Assert.Equal(SpreadsheetLexer.T__2, ts.Get(1).Type);
            Assert.Equal(SpreadsheetLexer.NUMBER, ts.Get(2).Type);
            Assert.Equal(SpreadsheetLexer.T__2, ts.Get(3).Type);
            Assert.Equal(SpreadsheetLexer.NUMBER, ts.Get(4).Type);
        }
示例#3
0
    private ExpressionContext expression(int _p)
    {
        ParserRuleContext _parentctx = Context;
        int _parentState             = State;
        ExpressionContext _localctx  = new ExpressionContext(Context, _parentState);
        ExpressionContext _prevctx   = _localctx;
        int _startState = 0;

        EnterRecursionRule(_localctx, 0, RULE_expression, _p);
        int _la;

        try {
            int _alt;
            EnterOuterAlt(_localctx, 1);
            {
                State = 14;
                ErrorHandler.Sync(this);
                switch (TokenStream.LA(1))
                {
                case T__0:
                {
                    _localctx = new ParenthesisExpContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;

                    State = 3; Match(T__0);
                    State = 4; expression(0);
                    State = 5; Match(T__1);
                }
                break;

                case NAME:
                {
                    _localctx = new FunctionExpContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;
                    State     = 7; Match(NAME);
                    State     = 8; Match(T__0);
                    State     = 9; expression(0);
                    State     = 10; Match(T__1);
                }
                break;

                case NUMBER:
                {
                    _localctx = new NumericAtomExpContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;
                    State     = 12; Match(NUMBER);
                }
                break;

                case ID:
                {
                    _localctx = new IdAtomExpContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;
                    State     = 13; Match(ID);
                }
                break;

                default:
                    throw new NoViableAltException(this);
                }
                Context.Stop = TokenStream.LT(-1);
                State        = 27;
                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 = 25;
                            ErrorHandler.Sync(this);
                            switch (Interpreter.AdaptivePredict(TokenStream, 1, Context))
                            {
                            case 1:
                            {
                                _localctx = new MulDivExpContext(new ExpressionContext(_parentctx, _parentState));
                                PushNewRecursionContext(_localctx, _startState, RULE_expression);
                                State = 16;
                                if (!(Precpred(Context, 6)))
                                {
                                    throw new FailedPredicateException(this, "Precpred(Context, 6)");
                                }
                                State = 17;
                                _la   = TokenStream.LA(1);
                                if (!(_la == ASTERISK || _la == SLASH))
                                {
                                    ErrorHandler.RecoverInline(this);
                                }
                                else
                                {
                                    ErrorHandler.ReportMatch(this);
                                    Consume();
                                }
                                State = 18; expression(7);
                            }
                            break;

                            case 2:
                            {
                                _localctx = new AddSubExpContext(new ExpressionContext(_parentctx, _parentState));
                                PushNewRecursionContext(_localctx, _startState, RULE_expression);
                                State = 19;
                                if (!(Precpred(Context, 5)))
                                {
                                    throw new FailedPredicateException(this, "Precpred(Context, 5)");
                                }
                                State = 20;
                                _la   = TokenStream.LA(1);
                                if (!(_la == PLUS || _la == MINUS))
                                {
                                    ErrorHandler.RecoverInline(this);
                                }
                                else
                                {
                                    ErrorHandler.ReportMatch(this);
                                    Consume();
                                }
                                State = 21; expression(6);
                            }
                            break;

                            case 3:
                            {
                                _localctx = new PowerExpContext(new ExpressionContext(_parentctx, _parentState));
                                PushNewRecursionContext(_localctx, _startState, RULE_expression);
                                State = 22;
                                if (!(Precpred(Context, 4)))
                                {
                                    throw new FailedPredicateException(this, "Precpred(Context, 4)");
                                }
                                State = 23; Match(T__2);
                                State = 24; expression(4);
                            }
                            break;
                            }
                        }
                    }
                    State = 29;
                    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);
    }