Microsoft.R.Core.AST.Operators.TokenOperator.Parse C# (CSharp) Method

Parse() public method

public Parse ( ParseContext context, IAstNode parent ) : bool
context Microsoft.R.Core.Parser.ParseContext
parent IAstNode
return bool
        public override bool Parse(ParseContext context, IAstNode parent) {
            Debug.Assert(context.Tokens.CurrentToken.TokenType == RTokenType.Operator);

            OperatorType = TokenOperator.GetOperatorType(context.TextProvider.GetText(context.Tokens.CurrentToken));
            OperatorToken = RParser.ParseToken(context, this);
            Associativity = OperatorAssociativity.GetAssociativity(OperatorType);

            // If operator is preceded by an operator, it is then unary
            // Look back two tokens since operator parsing already consumed its token.
            if (IsUnary || IsUnaryOperator(context.Tokens, context.TextProvider, OperatorType, -2)) {
                OperatorType = Operator.GetUnaryForm(OperatorType);
                IsUnary = true;
                Associativity = Associativity.Right;
            }
            return base.Parse(context, parent);
        }

Usage Example

Ejemplo n.º 1
0
        private ParseErrorType HandleOperator(ParseContext context, IAstNode parent, out bool isUnary) {
            ParseErrorType errorType = ParseErrorType.None;

            // If operands stack is empty the operator is unary.
            // If operator is preceded by another operator, 
            // it is interpreted as unary.

            TokenOperator currentOperator = new TokenOperator(_operands.Count == 0);

            currentOperator.Parse(context, null);
            isUnary = currentOperator.IsUnary;

            IOperator lastOperator = _operators.Peek();
            if (isUnary && lastOperator != null && lastOperator.IsUnary) {
                // !!!x is treated as !(!(!x)))
                // Step back and re-parse as expression
                context.Tokens.Position -= 1;
                var exp = new Expression(inGroup: false);
                if (exp.Parse(context, null)) {
                    _operands.Push(exp);
                    return ParseErrorType.None;
                }
            }

            if (currentOperator.Precedence <= lastOperator.Precedence &&
                !(currentOperator.OperatorType == lastOperator.OperatorType && currentOperator.Association == Association.Right)) {
                // New operator has lower or equal precedence. We need to make a tree from
                // the topmost operator and its operand(s). Example: a*b+c. + has lower priority
                // and a and b should be on the stack along with * on the operator stack.
                // Repeat until there are no more higher precendece operators on the stack.

                errorType = this.ProcessHigherPrecendenceOperators(context, currentOperator);
            }

            if (errorType == ParseErrorType.None) {
                _operators.Push(currentOperator);
            }

            return errorType;
        }
All Usage Examples Of Microsoft.R.Core.AST.Operators.TokenOperator::Parse