Ejemplo n.º 1
0
        public RptUnit Parser(bool needParens)
        {
            int     token;
            RptUnit rpnUnit = null, cur;
            bool    meedParens = false;


            while ((token = _lexer.GetToken()) != Token.EOF)
            {
                cur = null;

                if (token == Token.INTEGER)
                {
                    int number = (int)_lexer.CurrentValue;

                    if (_checkIdentifier != null && !_checkIdentifier(number))
                    {
                        throw new APRptConditionParseException(APResource.GetString(APResource.APRptFilter_LogicIdentifierInvalidate, number));
                    }

                    cur = new OperandRpnUnit(number.ToString());
                }
                else if (token == Token.IDENTIFIER)
                {
                    string identifier = (string)_lexer.CurrentValue;

                    if (_checkIdentifier != null && !_checkIdentifier(identifier))
                    {
                        throw new APRptConditionParseException(APResource.GetString(APResource.APRptFilter_LogicIdentifierInvalidate, identifier));
                    }

                    cur = new OperandRpnUnit(identifier);
                }
                else if (token == Token.BITWISE_AND || token == Token.BITWISE_OR)
                {
                    if (rpnUnit == null || IsUncompletedRpnUnit(rpnUnit))
                    {
                        throw new APRptConditionParseException(APResource.APRptFilter_LogicExpressionInvalidate);
                    }

                    cur = new BinaryRpnUnit(rpnUnit, token == Token.BITWISE_AND ? RpnOperator.AND : RpnOperator.OR);

                    rpnUnit = null;
                }
                else if (token == Token.LOGIC_NOT)
                {
                    if (rpnUnit != null && !IsUncompletedRpnUnit(rpnUnit))
                    {
                        throw new APRptConditionParseException(APResource.APRptFilter_LogicExpressionInvalidate);
                    }

                    cur = new UnaryNotRpnUnit();
                }
                else if (token == Token.OPEN_PARENS)
                {
                    cur = Parser(true);
                }
                else if (token == Token.CLOSE_PARENS)
                {
                    meedParens = true;

                    if (!needParens)
                    {
                        throw new APRptConditionParseException(APResource.APRptFilter_LogicExpressionInvalidate);
                    }

                    break;
                }
                else
                {
                    throw new APRptConditionParseException(APResource.APRptFilter_LogicExpressionInvalidate);
                }

                if (cur == null)
                {
                    throw new APRptConditionParseException(APResource.APRptFilter_LogicExpressionInvalidate);
                }

                if (rpnUnit == null)
                {
                    rpnUnit = cur;
                }
                else if (IsUncompletedRpnUnit(rpnUnit))
                {
                    (rpnUnit as IRightRpnUnit).SetRight(cur);
                }
                else
                {
                    throw new APRptConditionParseException(APResource.APRptFilter_LogicExpressionInvalidate);
                }
            }

            if (needParens && !meedParens)
            {
                throw new APRptConditionParseException(APResource.APRptFilter_LogicExpressionInvalidate);
            }

            if (IsUncompletedRpnUnit(rpnUnit))
            {
                throw new APRptConditionParseException(APResource.APRptFilter_LogicExpressionInvalidate);
            }

            return(rpnUnit);
        }
Ejemplo n.º 2
0
 private bool IsUncompletedRpnUnit(RptUnit rpnUnit)
 {
     return(rpnUnit is IRightRpnUnit && !(rpnUnit as IRightRpnUnit).IsCompleted);
 }