// ---------------------------------------------------------- // 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; } }
// ---------------------------------------------------------- // 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); }
private XTFormulaToken AddOptToken(XTOperatorToken token) { if (token.Pri <= this.Pri) // 新加的运算符,优先级低 { token.m_lToken = this; // 则将新运算符置顶 return(token); } XTOperatorToken rToken = this; while (true) { if ((rToken.m_rToken == null) || !(rToken.m_rToken is XTOperatorToken) || ((XTOperatorToken)rToken.m_rToken).Pri >= token.Pri) { token.m_lToken = rToken.m_rToken; rToken.m_rToken = token; break; } rToken = rToken.m_rToken as XTOperatorToken; } return(this); }
// ---------------------------------------------------------- // 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(); } }