Exemplo n.º 1
0
        private static Expression ParseFactor(ExpressionLexer lexer)
        {
            ExprTokenType tokenType = lexer.PeekNextToken();
            if (tokenType == ExprTokenType.Number)
                return new PrimitiveExpression((int)lexer.GetNextToken(ExprTokenType.Number));
            if (tokenType == ExprTokenType.String)
                return new PrimitiveExpression((string)lexer.GetNextToken(ExprTokenType.String));
            if (tokenType == ExprTokenType.Symbol)
            {
                string symbol = (string) lexer.GetNextToken(ExprTokenType.Symbol);
                if (lexer.PeekNextToken() == ExprTokenType.Dot)
                {
                    lexer.GetNextToken(ExprTokenType.Dot);
                    Expression exprInContext = ParseFactor(lexer);
                    return new ContextExpression(symbol, exprInContext, new Expression[0]);
                }

                if (lexer.PeekNextToken() == ExprTokenType.Open)
                {
                    lexer.GetNextToken(ExprTokenType.Open);
                    var parameters = new List<Expression>();
                    while (lexer.PeekNextToken() != ExprTokenType.Close)
                    {
                        if (parameters.Count > 0)
                            lexer.GetNextToken(ExprTokenType.Comma);
                        parameters.Add(ParseCondCombo(lexer));
                    }
                    lexer.GetNextToken(ExprTokenType.Close);
                    if (lexer.PeekNextToken() == ExprTokenType.Dot)
                    {
                        lexer.GetNextToken(ExprTokenType.Dot);
                        Expression exprInContext = ParseFactor(lexer);
                        return new ContextExpression(symbol, exprInContext, parameters.ToArray());
                    }
                    return new FunctionExpression(symbol, parameters.ToArray());
                }
                return new SymbolExpression(symbol);
            }
            if (tokenType == ExprTokenType.Open)
            {
                lexer.GetNextToken(ExprTokenType.Open);
                Expression result = ParseCondCombo(lexer);
                lexer.GetNextToken(ExprTokenType.Close);
                return result;
            }
            if (tokenType == ExprTokenType.Minus || tokenType == ExprTokenType.NOT)
            {
                lexer.GetNextToken(tokenType);
                return new UnaryExpression(ParseFactor(lexer), tokenType);
            }
            throw new ParseException("Unexpected token " + tokenType, lexer.CurrentPosition);
        }
Exemplo n.º 2
0
        public void PeekingShouldThrowWhenIncorrectCharacterAtStart()
        {
            ExpressionLexer lexer = new ExpressionLexer("#$*@#", false, false);
            Action          peek  = () => lexer.PeekNextToken();

            peek.Throws <ODataException>(ODataErrorStrings.ExpressionLexer_InvalidCharacter("#", "0", "#$*@#"));
        }
Exemplo n.º 3
0
 public void ShouldOutputTokenWhenNoError()
 {
     ExpressionLexer lexer = new ExpressionLexer("null", false, false);
     ExpressionToken result = lexer.PeekNextToken();
     lexer.CurrentToken.Should().NotBe(result);
     result.Kind.Should().Be(ExpressionTokenKind.NullLiteral);
 }
Exemplo n.º 4
0
        public void ShouldOutputTokenWhenNoError()
        {
            ExpressionLexer lexer  = new ExpressionLexer("null", false, false);
            ExpressionToken result = lexer.PeekNextToken();

            Assert.NotEqual(result, lexer.CurrentToken);
            Assert.Equal(ExpressionTokenKind.NullLiteral, result.Kind);
        }
Exemplo n.º 5
0
 private static Expression ParseTernary(ExpressionLexer lexer)
 {
     var expr = ParseCondCombo(lexer);
     if (lexer.PeekNextToken() == ExprTokenType.QuestionMark)
     {
         lexer.GetNextToken(ExprTokenType.QuestionMark);
         var trueValue = ParseTernary(lexer);
         lexer.GetNextToken(ExprTokenType.Colon);
         var falseValue = ParseTernary(lexer);
         return new TernaryExpression(expr, trueValue, falseValue);
     }
     return expr;
 }