コード例 #1
0
        // ----------------------------------------------------------
        // private
        // ----------------------------------------------------------
        private void Verify(XTFormulaParser parser, XTFormulaToken token)
        {
            XTOperatorToken optToken = token as XTOperatorToken;

            if (this.m_currIsOpt == null)                                                                       // 第一个元素
            {
                this.m_currIsOpt = false;
                if (optToken == null)
                {
                    return;                                                                                     // 不是运算符
                }
                this.m_currIsOpt = true;
                if (optToken.AllowEmptyLToken)
                {
                    return;                                                                             // 允许空左子树的运算符
                }
                parser.RaiseFormulaException();                                                         // 以双目运算符开头(语法错误)
            }
            else if (this.m_currIsOpt == true)                                                          // 前一个元素是运算符
            {
                if (optToken != null)                                                                   // 紧接着又是一个运算符
                {
                    parser.RaiseFormulaException();                                                     // 两个双目运算符连一起(语法错误)
                }
                this.m_currIsOpt = false;
            }
            else                                                                                                        // 前一个元素是值元素
            {
                if (optToken == null)                                                                                   // 紧接着又一个值元素(语法错误)
                {
                    parser.RaiseFormulaException();
                }
                this.m_currIsOpt = true;
            }
        }
コード例 #2
0
        // 解释嵌套表达式
        private XTFormula Parse(HashSet <string> argNames, XTFormula formula)
        {
            XTFormulaToken token = null;

            while (this.CurrChar() > 0)
            {
                token = null;
                foreach (XTFormulaTokenParser parser in m_tokenParsers)
                {
                    token = parser.Parse(this, argNames);
                    if (token == null)
                    {
                        continue;
                    }
                    formula.AddToken(this, token);
                    break;
                }
                if (token == null)
                {
                    break;
                }
            }
            if (!formula.IsVaild)
            {
                this.RaiseFormulaException();
            }
            return(formula);
        }
コード例 #3
0
 // -------------------------------------------
 // 添加节点,并调整语法树
 public XTFormulaToken AddToken(XTFormulaToken token)
 {
     if (token is XTOperatorToken)
     {
         return(this.AddOptToken((XTOperatorToken)token));
     }
     return(this.AddValueToken(token));
 }
コード例 #4
0
        private readonly HashSet <string> m_argNames;           // 公式中实际包含的所有参数

        internal XTFormula(string formula, HashSet <string> argNames)
        {
            this.m_formula   = formula;
            this.m_root      = null;
            this.m_currIsOpt = null;
            this.m_result    = null;
            this.m_argNames  = argNames;
        }
コード例 #5
0
 public override XTFormulaToken Parse(XTFormulaParser parser, HashSet <string> argNames)
 {
     foreach (NumericParser func in sm_parsers)
     {
         XTFormulaToken token = func(parser, argNames);
         if (token == null)
         {
             continue;
         }
         return(token);
     }
     return(null);
 }
コード例 #6
0
        // ----------------------------------------------------------
        // private
        // ----------------------------------------------------------
        private XTFormulaToken AddValueToken(XTFormulaToken token)
        {
            XTOperatorToken rToken = this;

            while (true)
            {
                if (rToken.m_rToken is XTOperatorToken)
                {
                    rToken = rToken.m_rToken as XTOperatorToken;
                }
                else
                {
                    rToken.m_rToken = token;
                    break;
                }
            }
            return(this);
        }
コード例 #7
0
        // ----------------------------------------------------------
        // internal
        // ----------------------------------------------------------
        internal virtual void AddToken(XTFormulaParser parser, XTFormulaToken token)
        {
            this.Verify(parser, token);

            XTOperatorToken optToken = token as XTOperatorToken;

            if (this.m_root == null)
            {
                this.m_root = token;
            }
            else if (this.m_root is XTOperatorToken)                                                            // 重新调整语法树
            {
                this.m_root = ((XTOperatorToken)this.m_root).AddToken(token);
            }
            else if (optToken != null)
            {
                optToken.SetLToken(this.m_root);
                this.m_root = optToken;
            }
            else
            {
                parser.RaiseFormulaException();
            }
        }
コード例 #8
0
        private XTFormulaToken m_rToken;                        // 右表达式

        public XTOperatorToken(Operator opt)
        {
            this.m_opt    = opt;
            this.m_lToken = null;
            this.m_rToken = null;
        }
コード例 #9
0
 // ----------------------------------------------------------
 // public
 // ----------------------------------------------------------
 // 添加左节点
 public void SetLToken(XTFormulaToken token)
 {
     this.m_lToken = token;
 }