Ejemplo n.º 1
0
        private object EqualsNode(EqualsNode e)
        {
            var l = Evaluate(e.l);
            var r = Evaluate(e.r);

            return((decimal)l == (decimal)r);
        }
Ejemplo n.º 2
0
        public void Parse_WhenQueryPassedIn_ExpectParsedCorrectly()
        {
            // Arrange
            IDynamicQueryParser queryParser = new DynamicQueryParser();

            // Act
            var stopwatch = Stopwatch.StartNew();
            var query     = queryParser.Parse <TestEntity>("where::Name:\"test name\" and age:asdorderby::age asc page::1,10");

            stopwatch.Stop();

            query.Where(new WhereNode {
                Statement = new WhereStatement {
                    Value = EqualsNode.CreateEquals("test", "bob")
                }
            });
            query.Where("val:bob");

            // Assert
            this.WriteTimeElapsed(stopwatch);
        }
Ejemplo n.º 3
0
        protected SyntaxNode buildSyntaxTree(List <ISyntaxUnit> postfixForm)
        {
            Queue <ISyntaxUnit> inputQueue   = new Queue <ISyntaxUnit>(postfixForm);
            Stack <SyntaxNode>  operandStack = new Stack <SyntaxNode>();

            while (inputQueue.Count > 0)
            {
                ISyntaxUnit input = inputQueue.Dequeue();
                if (input is Lexeme)
                {
                    Lexeme            token = input as Lexeme;
                    Lexeme.LexemeType ttype = token.Type;
                    if (properties.IsVariable(token.Value))
                    {
                        VariableIdentifierNode variable = new VariableIdentifierNode(token.Value);
                        operandStack.Push(variable);
                    }
                    else if (properties.IsConstant(token.Value))
                    {
                        double constantValue            = properties.getConstantValue(token.Value);
                        ConstantIdentifierNode constant = new ConstantIdentifierNode(token.Value, constantValue);
                        operandStack.Push(constant);
                    }
                    else if (properties.IsFunctionName(token.Value))
                    {
                        int nArguments = properties.getFunctionArgumentsCount(token.Value);
                        FunctionApplyNode.FunctionBody funcBody = properties.getFunctionDefinition(token.Value);
                        ArgumentListNode argumentList           = new ArgumentListNode();
                        try
                        {
                            for (int i = 0; i < nArguments; i++)
                            {
                                argumentList.addArgument(operandStack.Pop());
                            }
                        }
                        catch (InvalidOperationException ex)
                        {
                            throw new ParseException("Not enough operands on operand stack for function call.");
                        }

                        FunctionApplyNode functionCall = new FunctionApplyNode(argumentList, funcBody, token.Value);
                        operandStack.Push(functionCall);
                    }
                    else if (ttype == Lexeme.LexemeType.REAL_VALUE)
                    {
                        double value;
                        if (!double.TryParse(token.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out value))
                        {
                            throw new ParseException("Couldn't parse literal value: " + token.Value);
                        }
                        LiteralNode literal = new LiteralNode(value);
                        operandStack.Push(literal);
                    }
                    else if (ttype == Lexeme.LexemeType.OP_PLUS)
                    {
                        try
                        {
                            SyntaxNode   right    = operandStack.Pop();
                            SyntaxNode   left     = operandStack.Pop();
                            AdditionNode addition = new AdditionNode(left, right);
                            operandStack.Push(addition);
                        }
                        catch (InvalidOperationException ex)
                        {
                            throw new ParseException("Missing operand(s) for addition.");
                        }
                    }
                    else if (ttype == Lexeme.LexemeType.OP_MINUS)
                    {
                        try
                        {
                            SyntaxNode      right       = operandStack.Pop();
                            SyntaxNode      left        = operandStack.Pop();
                            SubtractionNode subtraction = new SubtractionNode(left, right);
                            operandStack.Push(subtraction);
                        }
                        catch (InvalidOperationException ex)
                        {
                            throw new ParseException("Missing operand(s) for subtraction.");
                        }
                    }
                    else if (ttype == Lexeme.LexemeType.OP_MUL)
                    {
                        try
                        {
                            SyntaxNode         right          = operandStack.Pop();
                            SyntaxNode         left           = operandStack.Pop();
                            MultiplicationNode multiplication = new MultiplicationNode(left, right);
                            operandStack.Push(multiplication);
                        }
                        catch (InvalidOperationException ex)
                        {
                            throw new ParseException("Missing operand(s) for multiplication.");
                        }
                    }
                    else if (ttype == Lexeme.LexemeType.OP_DIV)
                    {
                        try
                        {
                            SyntaxNode   right    = operandStack.Pop();
                            SyntaxNode   left     = operandStack.Pop();
                            DivisionNode division = new DivisionNode(left, right);
                            operandStack.Push(division);
                        }
                        catch (InvalidOperationException ex)
                        {
                            throw new ParseException("Missing operand(s) for division.");
                        }
                    }
                    else if (ttype == Lexeme.LexemeType.OP_POW)
                    {
                        try
                        {
                            SyntaxNode exponent = operandStack.Pop();
                            SyntaxNode baseNode = operandStack.Pop();
                            PowerNode  power    = new PowerNode(baseNode, exponent);
                            operandStack.Push(power);
                        }
                        catch (InvalidOperationException ex)
                        {
                            throw new ParseException("Missing operand(s) for exponentiation.");
                        }
                    }
                    else if (ttype == Lexeme.LexemeType.EQ_SIGN)
                    {
                        try
                        {
                            SyntaxNode right  = operandStack.Pop();
                            SyntaxNode left   = operandStack.Pop();
                            EqualsNode eqNode = new EqualsNode(left, right);
                            operandStack.Push(eqNode);
                        }
                        catch (InvalidOperationException ex)
                        {
                            throw new ParseException("Missing operand(s) for assignment.");
                        }
                    }
                    else if (ttype == Lexeme.LexemeType.OP_PLUS_UNARY)
                    {
                        try
                        {
                            SyntaxNode    child     = operandStack.Pop();
                            UnaryPlusNode unaryPlus = new UnaryPlusNode(child);
                            operandStack.Push(unaryPlus);
                        }
                        catch (InvalidOperationException ex)
                        {
                            throw new ParseException("Missing operand for unary plus.");
                        }
                    }
                    else if (ttype == Lexeme.LexemeType.OP_MINUS_UNARY)
                    {
                        try
                        {
                            SyntaxNode     child      = operandStack.Pop();
                            UnaryMinusNode unaryMinus = new UnaryMinusNode(child);
                            operandStack.Push(unaryMinus);
                        }
                        catch (InvalidOperationException ex)
                        {
                            throw new ParseException("Missing operand for unary minus.");
                        }
                    }
                    else
                    {
                        throw new ParseException("Unexpected token in postfix expression: " + token.simpleRepresentation());
                    }
                }
                else if (input is SyntaxNode)
                {
                    operandStack.Push(input as SyntaxNode);
                }
                else
                {
                    throw new ParseException("Unexpected object type in postfix expression.");
                }
            }

            if (operandStack.Count == 1)
            {
                return(operandStack.Pop());
            }
            else
            {
                throw new ParseException("Too many operands in postfix expression.");
            }
        }
Ejemplo n.º 4
0
    // $ANTLR start "equalityExpression"
    // JavaScript.g:271:1: equalityExpression : relationalExpression ( ( LT )* ( '==' ( LT )* relationalExpression | '!=' ( LT )* relationalExpression | '===' ( LT )* relationalExpression | '!==' ( LT )* relationalExpression ) )* ;
    public JavaScriptParser.equalityExpression_return equalityExpression() // throws RecognitionException [1]
    {   
        JavaScriptParser.equalityExpression_return retval = new JavaScriptParser.equalityExpression_return();
        retval.Start = input.LT(1);

        object root_0 = null;

        IToken LT312 = null;
        IToken string_literal313 = null;
        IToken LT314 = null;
        IToken string_literal316 = null;
        IToken LT317 = null;
        IToken string_literal319 = null;
        IToken LT320 = null;
        IToken string_literal322 = null;
        IToken LT323 = null;
        JavaScriptParser.relationalExpression_return relationalExpression311 = default(JavaScriptParser.relationalExpression_return);

        JavaScriptParser.relationalExpression_return relationalExpression315 = default(JavaScriptParser.relationalExpression_return);

        JavaScriptParser.relationalExpression_return relationalExpression318 = default(JavaScriptParser.relationalExpression_return);

        JavaScriptParser.relationalExpression_return relationalExpression321 = default(JavaScriptParser.relationalExpression_return);

        JavaScriptParser.relationalExpression_return relationalExpression324 = default(JavaScriptParser.relationalExpression_return);


        object LT312_tree=null;
        object string_literal313_tree=null;
        object LT314_tree=null;
        object string_literal316_tree=null;
        object LT317_tree=null;
        object string_literal319_tree=null;
        object LT320_tree=null;
        object string_literal322_tree=null;
        object LT323_tree=null;

        try 
    	{
            // JavaScript.g:272:2: ( relationalExpression ( ( LT )* ( '==' ( LT )* relationalExpression | '!=' ( LT )* relationalExpression | '===' ( LT )* relationalExpression | '!==' ( LT )* relationalExpression ) )* )
            // JavaScript.g:272:4: relationalExpression ( ( LT )* ( '==' ( LT )* relationalExpression | '!=' ( LT )* relationalExpression | '===' ( LT )* relationalExpression | '!==' ( LT )* relationalExpression ) )*
            {
            	root_0 = (object)adaptor.GetNilNode();

            	PushFollow(FOLLOW_relationalExpression_in_equalityExpression2399);
            	relationalExpression311 = relationalExpression();
            	state.followingStackPointer--;
            	if (state.failed) return retval;
            	if ( state.backtracking == 0 ) adaptor.AddChild(root_0, relationalExpression311.Tree);
            	// JavaScript.g:273:4: ( ( LT )* ( '==' ( LT )* relationalExpression | '!=' ( LT )* relationalExpression | '===' ( LT )* relationalExpression | '!==' ( LT )* relationalExpression ) )*
            	do 
            	{
            	    int alt169 = 2;
            	    alt169 = dfa169.Predict(input);
            	    switch (alt169) 
            		{
            			case 1 :
            			    // JavaScript.g:273:5: ( LT )* ( '==' ( LT )* relationalExpression | '!=' ( LT )* relationalExpression | '===' ( LT )* relationalExpression | '!==' ( LT )* relationalExpression )
            			    {
            			    	// JavaScript.g:273:7: ( LT )*
            			    	do 
            			    	{
            			    	    int alt163 = 2;
            			    	    int LA163_0 = input.LA(1);

            			    	    if ( (LA163_0 == LT) )
            			    	    {
            			    	        alt163 = 1;
            			    	    }


            			    	    switch (alt163) 
            			    		{
            			    			case 1 :
            			    			    // JavaScript.g:273:7: LT
            			    			    {
            			    			    	LT312=(IToken)Match(input,LT,FOLLOW_LT_in_equalityExpression2406); if (state.failed) return retval;

            			    			    }
            			    			    break;

            			    			default:
            			    			    goto loop163;
            			    	    }
            			    	} while (true);

            			    	loop163:
            			    		;	// Stops C# compiler whining that label 'loop163' has no statements

            			    	// JavaScript.g:273:10: ( '==' ( LT )* relationalExpression | '!=' ( LT )* relationalExpression | '===' ( LT )* relationalExpression | '!==' ( LT )* relationalExpression )
            			    	int alt168 = 4;
            			    	switch ( input.LA(1) ) 
            			    	{
            			    	case 87:
            			    		{
            			    	    alt168 = 1;
            			    	    }
            			    	    break;
            			    	case 88:
            			    		{
            			    	    alt168 = 2;
            			    	    }
            			    	    break;
            			    	case 89:
            			    		{
            			    	    alt168 = 3;
            			    	    }
            			    	    break;
            			    	case 90:
            			    		{
            			    	    alt168 = 4;
            			    	    }
            			    	    break;
            			    		default:
            			    		    if ( state.backtracking > 0 ) {state.failed = true; return retval;}
            			    		    NoViableAltException nvae_d168s0 =
            			    		        new NoViableAltException("", 168, 0, input);

            			    		    throw nvae_d168s0;
            			    	}

            			    	switch (alt168) 
            			    	{
            			    	    case 1 :
            			    	        // JavaScript.g:274:6: '==' ( LT )* relationalExpression
            			    	        {
            			    	        	string_literal313=(IToken)Match(input,87,FOLLOW_87_in_equalityExpression2417); if (state.failed) return retval;
            			    	        	if ( state.backtracking == 0 )
            			    	        	{string_literal313_tree = new EqualsNode(string_literal313) ;
            			    	        		root_0 = (object)adaptor.BecomeRoot(string_literal313_tree, root_0);
            			    	        	}
            			    	        	// JavaScript.g:274:26: ( LT )*
            			    	        	do 
            			    	        	{
            			    	        	    int alt164 = 2;
            			    	        	    int LA164_0 = input.LA(1);

            			    	        	    if ( (LA164_0 == LT) )
            			    	        	    {
            			    	        	        alt164 = 1;
            			    	        	    }


            			    	        	    switch (alt164) 
            			    	        		{
            			    	        			case 1 :
            			    	        			    // JavaScript.g:274:26: LT
            			    	        			    {
            			    	        			    	LT314=(IToken)Match(input,LT,FOLLOW_LT_in_equalityExpression2423); if (state.failed) return retval;

            			    	        			    }
            			    	        			    break;

            			    	        			default:
            			    	        			    goto loop164;
            			    	        	    }
            			    	        	} while (true);

            			    	        	loop164:
            			    	        		;	// Stops C# compiler whining that label 'loop164' has no statements

            			    	        	PushFollow(FOLLOW_relationalExpression_in_equalityExpression2427);
            			    	        	relationalExpression315 = relationalExpression();
            			    	        	state.followingStackPointer--;
            			    	        	if (state.failed) return retval;
            			    	        	if ( state.backtracking == 0 ) adaptor.AddChild(root_0, relationalExpression315.Tree);

            			    	        }
            			    	        break;
            			    	    case 2 :
            			    	        // JavaScript.g:275:6: '!=' ( LT )* relationalExpression
            			    	        {
            			    	        	string_literal316=(IToken)Match(input,88,FOLLOW_88_in_equalityExpression2435); if (state.failed) return retval;
            			    	        	if ( state.backtracking == 0 )
            			    	        	{string_literal316_tree = new NotEqualsNode(string_literal316) ;
            			    	        		root_0 = (object)adaptor.BecomeRoot(string_literal316_tree, root_0);
            			    	        	}
            			    	        	// JavaScript.g:275:29: ( LT )*
            			    	        	do 
            			    	        	{
            			    	        	    int alt165 = 2;
            			    	        	    int LA165_0 = input.LA(1);

            			    	        	    if ( (LA165_0 == LT) )
            			    	        	    {
            			    	        	        alt165 = 1;
            			    	        	    }


            			    	        	    switch (alt165) 
            			    	        		{
            			    	        			case 1 :
            			    	        			    // JavaScript.g:275:29: LT
            			    	        			    {
            			    	        			    	LT317=(IToken)Match(input,LT,FOLLOW_LT_in_equalityExpression2441); if (state.failed) return retval;

            			    	        			    }
            			    	        			    break;

            			    	        			default:
            			    	        			    goto loop165;
            			    	        	    }
            			    	        	} while (true);

            			    	        	loop165:
            			    	        		;	// Stops C# compiler whining that label 'loop165' has no statements

            			    	        	PushFollow(FOLLOW_relationalExpression_in_equalityExpression2445);
            			    	        	relationalExpression318 = relationalExpression();
            			    	        	state.followingStackPointer--;
            			    	        	if (state.failed) return retval;
            			    	        	if ( state.backtracking == 0 ) adaptor.AddChild(root_0, relationalExpression318.Tree);

            			    	        }
            			    	        break;
            			    	    case 3 :
            			    	        // JavaScript.g:276:6: '===' ( LT )* relationalExpression
            			    	        {
            			    	        	string_literal319=(IToken)Match(input,89,FOLLOW_89_in_equalityExpression2453); if (state.failed) return retval;
            			    	        	if ( state.backtracking == 0 )
            			    	        	{string_literal319_tree = new EqualsNodeEx(string_literal319) ;
            			    	        		root_0 = (object)adaptor.BecomeRoot(string_literal319_tree, root_0);
            			    	        	}
            			    	        	// JavaScript.g:276:29: ( LT )*
            			    	        	do 
            			    	        	{
            			    	        	    int alt166 = 2;
            			    	        	    int LA166_0 = input.LA(1);

            			    	        	    if ( (LA166_0 == LT) )
            			    	        	    {
            			    	        	        alt166 = 1;
            			    	        	    }


            			    	        	    switch (alt166) 
            			    	        		{
            			    	        			case 1 :
            			    	        			    // JavaScript.g:276:29: LT
            			    	        			    {
            			    	        			    	LT320=(IToken)Match(input,LT,FOLLOW_LT_in_equalityExpression2459); if (state.failed) return retval;

            			    	        			    }
            			    	        			    break;

            			    	        			default:
            			    	        			    goto loop166;
            			    	        	    }
            			    	        	} while (true);

            			    	        	loop166:
            			    	        		;	// Stops C# compiler whining that label 'loop166' has no statements

            			    	        	PushFollow(FOLLOW_relationalExpression_in_equalityExpression2463);
            			    	        	relationalExpression321 = relationalExpression();
            			    	        	state.followingStackPointer--;
            			    	        	if (state.failed) return retval;
            			    	        	if ( state.backtracking == 0 ) adaptor.AddChild(root_0, relationalExpression321.Tree);

            			    	        }
            			    	        break;
            			    	    case 4 :
            			    	        // JavaScript.g:277:6: '!==' ( LT )* relationalExpression
            			    	        {
            			    	        	string_literal322=(IToken)Match(input,90,FOLLOW_90_in_equalityExpression2471); if (state.failed) return retval;
            			    	        	if ( state.backtracking == 0 )
            			    	        	{string_literal322_tree = new NotEqualsNodeEx(string_literal322) ;
            			    	        		root_0 = (object)adaptor.BecomeRoot(string_literal322_tree, root_0);
            			    	        	}
            			    	        	// JavaScript.g:277:32: ( LT )*
            			    	        	do 
            			    	        	{
            			    	        	    int alt167 = 2;
            			    	        	    int LA167_0 = input.LA(1);

            			    	        	    if ( (LA167_0 == LT) )
            			    	        	    {
            			    	        	        alt167 = 1;
            			    	        	    }


            			    	        	    switch (alt167) 
            			    	        		{
            			    	        			case 1 :
            			    	        			    // JavaScript.g:277:32: LT
            			    	        			    {
            			    	        			    	LT323=(IToken)Match(input,LT,FOLLOW_LT_in_equalityExpression2477); if (state.failed) return retval;

            			    	        			    }
            			    	        			    break;

            			    	        			default:
            			    	        			    goto loop167;
            			    	        	    }
            			    	        	} while (true);

            			    	        	loop167:
            			    	        		;	// Stops C# compiler whining that label 'loop167' has no statements

            			    	        	PushFollow(FOLLOW_relationalExpression_in_equalityExpression2481);
            			    	        	relationalExpression324 = relationalExpression();
            			    	        	state.followingStackPointer--;
            			    	        	if (state.failed) return retval;
            			    	        	if ( state.backtracking == 0 ) adaptor.AddChild(root_0, relationalExpression324.Tree);

            			    	        }
            			    	        break;

            			    	}


            			    }
            			    break;

            			default:
            			    goto loop169;
            	    }
            	} while (true);

            	loop169:
            		;	// Stops C# compiler whining that label 'loop169' has no statements


            }

            retval.Stop = input.LT(-1);

            if ( (state.backtracking==0) )
            {	retval.Tree = (object)adaptor.RulePostProcessing(root_0);
            	adaptor.SetTokenBoundaries(retval.Tree, (IToken) retval.Start, (IToken) retval.Stop);}
        }
        catch (RecognitionException re) 
    	{
            ReportError(re);
            Recover(input,re);
    	// Conversion of the second argument necessary, but harmless
    	retval.Tree = (object)adaptor.ErrorNode(input, (IToken) retval.Start, input.LT(-1), re);

        }
        finally 
    	{
        }
        return retval;
    }