Exemplo n.º 1
0
        private bool TryParse(bool enableExceptions, out ExpressionNode expressionNode)
        {
            expressionNode = null;
            Token token;
            int   parenCount = 0;

            this.StartParse();
            do
            {
                try
                {
                    token = this.lexer.Read();
                }
                catch (LexicalException e)
                {
                    if (enableExceptions)
                    {
                        throw e;
                    }
                    return(false);
                }
                Debug.WriteLine(Utils.TokenToString(token) + " : " + this.lexer.TokenString);

                switch (token)
                {
                case Token.Name:
                case Token.Numeric:
                case Token.Decimal:
                case Token.Float:
                case Token.NumericHex:
                case Token.StringConst:
                case Token.Date:
                    if (OperandType.None != this.prevOperand)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.MissingOperator(this.lexer.TokenString);
                        }
                        return(false);
                    }

                    /*if (this.opStack.Count > 0)
                     * {
                     *      OperatorInfo info = this.opStack.Peek();
                     *      if (info.Operator == Operator.In)
                     *      {
                     *              throw ParserException.InWithoutParentheses();
                     *      }
                     * }*/

                    this.prevOperand = OperandType.Scalar;
                    this.nodeStack.Push(this.CreateScalarNode(token, this.lexer.TokenString));
                    break;

                case Token.ListSeparator:
                {
                    if (this.prevOperand == OperandType.None)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.MissingOperandBefore(",");
                        }
                        return(false);
                    }
                    this.BuildExpression(3);
                    OperatorInfo info = this.opStack.Peek();
                    if (info.Type != NodeType.Call)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.SystaxError();
                        }
                        return(false);
                    }
                    ExpressionNode arg  = this.nodeStack.Pop();
                    FunctionNode   func = (FunctionNode)this.nodeStack.Pop();
                    func.AddArgument(arg);
                    this.nodeStack.Push(func);
                    this.prevOperand = OperandType.None;
                }
                break;

                case Token.LeftParen:
                    parenCount++;
                    if (OperandType.None != this.prevOperand)
                    {
                        this.BuildExpression(22);
                        this.prevOperand = OperandType.None;

                        ExpressionNode node = this.nodeStack.Peek();
                        if (!(node is NameNode))
                        {
                            if (enableExceptions)
                            {
                                throw ParserException.SystaxError();
                            }
                            return(false);
                        }

                        NameNode node1 = (NameNode)this.nodeStack.Pop();

                        ExpressionNode func;
                        if (AggregateNode.IsAggregare(node1.Name))
                        {
                            func = new AggregateNode(node1.Name);
                        }
                        else
                        {
                            func = new FunctionNode(node1.Parent, node1.Name);
                        }

                        this.nodeStack.Push(func);
                        this.opStack.Push(new OperatorInfo(NodeType.Call, Operator.Proc));
                    }
                    else
                    {
                        OperatorInfo info = this.opStack.Peek();
                        if (info.Type != NodeType.Binop || info.Operator != Operator.In)
                        {
                            this.opStack.Push(new OperatorInfo(NodeType.Paren, Operator.Proc));
                        }
                        else
                        {
                            this.nodeStack.Push(new FunctionNode(null, "In"));
                            this.opStack.Push(new OperatorInfo(NodeType.Call, Operator.Proc));
                        }
                    }
                    break;

                case Token.RightParen:
                {
                    if (this.prevOperand != OperandType.None)
                    {
                        this.BuildExpression(3);
                    }

                    if (this.opStack.Count <= 1)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.TooManyRightParentheses();
                        }
                        return(false);
                    }

                    OperatorInfo info = this.opStack.Pop();
                    if (this.prevOperand == OperandType.None && info.Type != NodeType.Call)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.MissingOperand(info.Operator.ToString());
                        }
                        return(false);
                    }

                    if (info.Type == NodeType.Call)
                    {
                        if (this.prevOperand != OperandType.None)
                        {
                            ExpressionNode argument = this.nodeStack.Pop();
                            FunctionNode   func     = (FunctionNode)this.nodeStack.Pop();
                            func.AddArgument(argument);
                            func.Check();

                            this.nodeStack.Push(func);
                        }
                    }
                    else
                    {
                        ExpressionNode node = this.nodeStack.Pop();
                        node = new UnaryOpNode(Operator.Noop, node);                                            // noop -- return value
                        this.nodeStack.Push(node);
                    }
                    this.prevOperand = OperandType.Expr;
                    parenCount--;
                }
                break;

                case Token.ZeroOp:
                    if (OperandType.None != this.prevOperand)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.MissingOperator(this.lexer.TokenString);
                        }
                        return(false);
                    }
                    this.opStack.Push(new OperatorInfo(NodeType.Zop, this.lexer.Operator));
                    this.prevOperand = OperandType.Expr;
                    break;

                case Token.UnaryOp:
                    this.opStack.Push(new OperatorInfo(NodeType.Unop, this.lexer.Operator));
                    break;

                case Token.BinaryOp:
                    if (OperandType.None != this.prevOperand)
                    {
                        this.prevOperand = OperandType.None;

                        Operator op       = this.lexer.Operator;
                        NodeType nodeType = NodeType.Binop;

                        if (op == Operator.And)
                        {
                            this.BuildExpression(Operator.BetweenAnd.Priority);                                       // eat all ops higher than BETWEEN AND: the unary +/-; comparison ops, etc.

                            OperatorInfo lastOp = this.opStack.Peek();

                            if (op == Operator.And &&
                                lastOp.Operator == Operator.Between)
                            {
                                op       = Operator.BetweenAnd;
                                nodeType = NodeType.TernaryOp2;
                            }
                        }

                        this.BuildExpression(op.Priority);
                        this.opStack.Push(new OperatorInfo(nodeType, op));
                    }
                    else
                    {
                        if (Operator.Plus == this.lexer.Operator)
                        {
                            this.opStack.Push(new OperatorInfo(NodeType.Unop, Operator.UnaryPlus));
                        }
                        else if (Operator.Minus == this.lexer.Operator)
                        {
                            this.opStack.Push(new OperatorInfo(NodeType.Unop, Operator.Negative));
                        }
                        else
                        {
                            if (enableExceptions)
                            {
                                throw ParserException.MissingOperandBefore(this.lexer.Operator.ToString());
                            }
                            return(false);
                        }
                    }
                    break;

                case Token.TernaryOp:
                    if (OperandType.None != this.prevOperand)
                    {
                        Operator op = this.lexer.Operator;

                        this.prevOperand = OperandType.None;
                        this.BuildExpression(op.Priority);
                        this.opStack.Push(new OperatorInfo(NodeType.TernaryOp, op));
                    }
                    break;

                case Token.Dot:
                {
                    ExpressionNode node = this.nodeStack.Peek();
                    if (!(node is NameNode) &&
                        !(node is FunctionNode) &&
                        !(node is ConstNode))
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos);
                        }
                        return(false);
                    }

                    try
                    {
                        token = this.lexer.Read();
                    }
                    catch (LexicalException e)
                    {
                        if (enableExceptions)
                        {
                            throw e;
                        }
                        return(false);
                    }

                    if (Token.Name != token)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos);
                        }
                        return(false);
                    }

                    ExpressionNode node1 = this.nodeStack.Pop();                                     //NameNode node1 = (NameNode)this.nodeStack.Pop();
                    //string name = node1.Name + "." + this.lexer.TokenString;
                    this.nodeStack.Push(new NameNode(node1, this.lexer.TokenString));
                }
                break;

                case Token.EOF:
                    if (OperandType.None != this.prevOperand)
                    {
                        this.BuildExpression(3);
                        if (this.opStack.Count != 1)
                        {
                            if (enableExceptions)
                            {
                                throw ParserException.MissingRightParen();
                            }
                            return(false);
                        }
                    }
                    else if (this.nodeStack.Count > 0)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.MissingOperand(this.opStack.Peek().Operator.ToString());
                        }
                        return(false);
                    }
                    break;

                case Token.Parameter:

                    try
                    {
                        token = this.lexer.Read();
                    }
                    catch (LexicalException e)
                    {
                        if (enableExceptions)
                        {
                            throw e;
                        }
                        return(false);
                    }

                    if (Token.Name != token)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos);
                        }
                        return(false);
                    }

                    NameNode parametersNode = new NameNode(null, "Parameters");
                    this.nodeStack.Push(new NameNode(parametersNode, this.lexer.TokenString));
                    this.prevOperand = OperandType.Scalar;
                    break;

                default:
                    if (enableExceptions)
                    {
                        throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos);
                    }
                    return(false);
                }
            } while (Token.EOF != token);


            //Debug.Assert(1 == this.nodeStack.Count);

            if (this.nodeStack.Count != 1)
            {
                return(false);
            }

            expressionNode = this.nodeStack.Peek();

            if (expressionNode == null)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        private bool TryParse(bool enableExceptions, out ExpressionNode expressionNode)
        {
            expressionNode = (ExpressionNode)null;
            int num = 0;

            this.StartParse();
            Token token;

            do
            {
                try
                {
                    token = this.lexer.Read();
                }
                catch (LexicalException ex)
                {
                    if (enableExceptions)
                    {
                        throw ex;
                    }
                    return(false);
                }
                switch (token)
                {
                case Token.Name:
                case Token.Numeric:
                case Token.Decimal:
                case Token.Float:
                case Token.NumericHex:
                case Token.StringConst:
                case Token.Date:
                    if (this.prevOperand != OperandType.None)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.MissingOperator(this.lexer.TokenString);
                        }
                        return(false);
                    }
                    this.prevOperand = OperandType.Scalar;
                    this.nodeStack.Push(this.CreateScalarNode(token, this.lexer.TokenString));
                    break;

                case Token.ListSeparator:
                    if (this.prevOperand == OperandType.None)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.MissingOperandBefore(",");
                        }
                        return(false);
                    }
                    this.BuildExpression(3);
                    if (this.opStack.Peek().Type != ExpressionParser.NodeType.Call)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.SystaxError();
                        }
                        return(false);
                    }
                    ExpressionNode expressionNode1 = this.nodeStack.Pop();
                    FunctionNode   functionNode1   = (FunctionNode)this.nodeStack.Pop();
                    functionNode1.AddArgument(expressionNode1);
                    this.nodeStack.Push((ExpressionNode)functionNode1);
                    this.prevOperand = OperandType.None;
                    break;

                case Token.LeftParen:
                    ++num;
                    if (this.prevOperand != OperandType.None)
                    {
                        this.BuildExpression(22);
                        this.prevOperand = OperandType.None;
                        if (!(this.nodeStack.Peek() is NameNode))
                        {
                            if (enableExceptions)
                            {
                                throw ParserException.SystaxError();
                            }
                            return(false);
                        }
                        NameNode nameNode = (NameNode)this.nodeStack.Pop();
                        this.nodeStack.Push(!AggregateNode.IsAggregare(nameNode.Name) ? (ExpressionNode) new FunctionNode(nameNode.Parent, nameNode.Name) : (ExpressionNode) new AggregateNode(nameNode.Name));
                        this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Call, Operator.Proc));
                        break;
                    }
                    ExpressionParser.OperatorInfo operatorInfo1 = this.opStack.Peek();
                    if (operatorInfo1.Type != ExpressionParser.NodeType.Binop || operatorInfo1.Operator != Operator.In)
                    {
                        this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Paren, Operator.Proc));
                        break;
                    }
                    this.nodeStack.Push((ExpressionNode) new FunctionNode((ExpressionNode)null, "In"));
                    this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Call, Operator.Proc));
                    break;

                case Token.RightParen:
                    if (this.prevOperand != OperandType.None)
                    {
                        this.BuildExpression(3);
                    }
                    if (this.opStack.Count <= 1)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.TooManyRightParentheses();
                        }
                        return(false);
                    }
                    ExpressionParser.OperatorInfo operatorInfo2 = this.opStack.Pop();
                    if (this.prevOperand == OperandType.None && operatorInfo2.Type != ExpressionParser.NodeType.Call)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.MissingOperand(operatorInfo2.Operator.ToString());
                        }
                        return(false);
                    }
                    if (operatorInfo2.Type == ExpressionParser.NodeType.Call)
                    {
                        if (this.prevOperand != OperandType.None)
                        {
                            ExpressionNode expressionNode2 = this.nodeStack.Pop();
                            FunctionNode   functionNode2   = (FunctionNode)this.nodeStack.Pop();
                            functionNode2.AddArgument(expressionNode2);
                            functionNode2.Check();
                            this.nodeStack.Push((ExpressionNode)functionNode2);
                        }
                    }
                    else
                    {
                        ExpressionNode right = this.nodeStack.Pop();
                        this.nodeStack.Push((ExpressionNode) new UnaryOpNode(Operator.Noop, right));
                    }
                    this.prevOperand = OperandType.Expr;
                    --num;
                    break;

                case Token.ZeroOp:
                    if (this.prevOperand != OperandType.None)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.MissingOperator(this.lexer.TokenString);
                        }
                        return(false);
                    }
                    this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Zop, this.lexer.Operator));
                    this.prevOperand = OperandType.Expr;
                    break;

                case Token.UnaryOp:
                    this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Unop, this.lexer.Operator));
                    break;

                case Token.BinaryOp:
                    if (this.prevOperand != OperandType.None)
                    {
                        this.prevOperand = OperandType.None;
                        Operator betweenAnd            = this.lexer.Operator;
                        ExpressionParser.NodeType type = ExpressionParser.NodeType.Binop;
                        if (betweenAnd == Operator.And)
                        {
                            this.BuildExpression(Operator.BetweenAnd.Priority);
                            ExpressionParser.OperatorInfo operatorInfo3 = this.opStack.Peek();
                            if (betweenAnd == Operator.And && operatorInfo3.Operator == Operator.Between)
                            {
                                betweenAnd = Operator.BetweenAnd;
                                type       = ExpressionParser.NodeType.TernaryOp2;
                            }
                        }
                        this.BuildExpression(betweenAnd.Priority);
                        this.opStack.Push(new ExpressionParser.OperatorInfo(type, betweenAnd));
                        break;
                    }
                    if (Operator.Plus == this.lexer.Operator)
                    {
                        this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Unop, Operator.UnaryPlus));
                        break;
                    }
                    if (Operator.Minus == this.lexer.Operator)
                    {
                        this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.Unop, Operator.Negative));
                        break;
                    }
                    if (enableExceptions)
                    {
                        throw ParserException.MissingOperandBefore(this.lexer.Operator.ToString());
                    }
                    return(false);

                case Token.TernaryOp:
                    if (this.prevOperand != OperandType.None)
                    {
                        Operator op = this.lexer.Operator;
                        this.prevOperand = OperandType.None;
                        this.BuildExpression(op.Priority);
                        this.opStack.Push(new ExpressionParser.OperatorInfo(ExpressionParser.NodeType.TernaryOp, op));
                        break;
                    }
                    break;

                case Token.Dot:
                    ExpressionNode expressionNode3 = this.nodeStack.Peek();
                    if (!(expressionNode3 is NameNode))
                    {
                        if (!(expressionNode3 is FunctionNode))
                        {
                            if (!(expressionNode3 is ConstNode))
                            {
                                if (enableExceptions)
                                {
                                    throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos);
                                }
                                return(false);
                            }
                        }
                    }
                    try
                    {
                        token = this.lexer.Read();
                    }
                    catch (LexicalException ex)
                    {
                        if (enableExceptions)
                        {
                            throw ex;
                        }
                        return(false);
                    }
                    if (Token.Name != token)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos);
                        }
                        return(false);
                    }
                    this.nodeStack.Push((ExpressionNode) new NameNode(this.nodeStack.Pop(), this.lexer.TokenString));
                    break;

                case Token.Parameter:
                    try
                    {
                        token = this.lexer.Read();
                    }
                    catch (LexicalException ex)
                    {
                        if (enableExceptions)
                        {
                            throw ex;
                        }
                        return(false);
                    }
                    if (Token.Name != token)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos);
                        }
                        return(false);
                    }
                    this.nodeStack.Push((ExpressionNode) new NameNode((ExpressionNode) new NameNode((ExpressionNode)null, "Parameters"), this.lexer.TokenString));
                    this.prevOperand = OperandType.Scalar;
                    break;

                case Token.EOF:
                    if (this.prevOperand != OperandType.None)
                    {
                        this.BuildExpression(3);
                        if (this.opStack.Count != 1)
                        {
                            if (enableExceptions)
                            {
                                throw ParserException.MissingRightParen();
                            }
                            return(false);
                        }
                        break;
                    }
                    if (this.nodeStack.Count > 0)
                    {
                        if (enableExceptions)
                        {
                            throw ParserException.MissingOperand(this.opStack.Peek().Operator.ToString());
                        }
                        return(false);
                    }
                    break;

                default:
                    if (enableExceptions)
                    {
                        throw ParserException.UnknownToken(this.lexer.TokenString, this.lexer.StartPos);
                    }
                    return(false);
                }
            }while (Token.EOF != token);
            if (this.nodeStack.Count != 1)
            {
                return(false);
            }
            expressionNode = this.nodeStack.Peek();
            return(expressionNode != null);
        }