예제 #1
0
        /**
         * 将切分的元素转化成ExpressionToken,并验证减号还是负号
         * @param previousToken
         * @param ele
         * @return
         * @throws ParseException
         */
        public ExpressionToken ChangeToToken(ExpressionToken previousToken, Element ele)
        {
            if (ele == null)
            {
                throw new ArgumentException();
            }
            ExpressionToken token = null;

            //转成ExpressionToken
            if (ElementType.NULL == ele.Type)
            {
                token = ExpressionToken.CreateConstantToken(DataType.DATATYPE_NULL, null);
            }
            else if (ElementType.STRING == ele.Type)
            {
                token = ExpressionToken.CreateConstantToken(DataType.DATATYPE_STRING, ele.Text);
            }
            else if (ElementType.BOOLEAN == ele.Type)
            {
                token = ExpressionToken.CreateConstantToken(DataType.DATATYPE_BOOLEAN, Convert.ToBoolean(ele.Text));
            }
            else if (ElementType.INT == ele.Type)
            {
                token = ExpressionToken.CreateConstantToken(DataType.DATATYPE_INT, Convert.ToInt32(ele.Text));
            }
            else if (ElementType.LONG == ele.Type)
            {
                token = ExpressionToken.CreateConstantToken(DataType.DATATYPE_LONG, Convert.ToInt64(ele.Text));
            }
            else if (ElementType.FLOAT == ele.Type)
            {
                token = ExpressionToken.CreateConstantToken(DataType.DATATYPE_FLOAT, Convert.ToSingle(ele.Text));
            }
            else if (ElementType.DOUBLE == ele.Type)
            {
                token = ExpressionToken.CreateConstantToken(DataType.DATATYPE_DOUBLE, Convert.ToDouble(ele.Text));
            }
            else if (ElementType.DATE == ele.Type)
            {
                token = ExpressionToken.CreateConstantToken(DataType.DATATYPE_DATE, Convert.ToDateTime(ele.Text));
            }
            else if (ElementType.VARIABLE == ele.Type)
            {
                token = ExpressionToken.CreateVariableToken(ele.Text);
            }
            else if (ElementType.OPERATOR == ele.Type)
            {
                //区分负号
                if (ele.Text.Equals("-") && (
                        previousToken == null || //以“-”开头肯定是负号
                        previousToken.TokenType == ETokenType.ETOKEN_TYPE_OPERATOR || //运算符后面肯定是负号
                        previousToken.TokenType == ETokenType.ETOKEN_TYPE_SPLITOR && //“(”或“,”后面肯定是负号
                        !")".Equals(previousToken.GetSplitor())
                        ))
                {
                    token = ExpressionToken.CreateOperatorToken(Operator.NG);
                }
                else
                {
                    token = ExpressionToken.CreateOperatorToken(GetOperator(ele.Text));
                }
            }
            else if (ElementType.FUNCTION == ele.Type)
            {
                token = ExpressionToken.CreateFunctionToken(ele.Text);
            }
            else if (ElementType.SPLITOR == ele.Type)
            {
                token = ExpressionToken.CreateSplitorToken(ele.Text);
            }
            token.StartPosition = ele.Index;

            return(token);
        }