Exemplo n.º 1
0
        private bool IsUnaryPrefix(StringToken token, Operator[] possibleOperators)
        {
            if (!possibleOperators.Any(x => x.Arity == Arity.Unary && x.Fixity == Fixity.Prefix))
            {
                return(false);
            }

            var isPrecededByOpenBracket = token.Previous != default &&
                                          grammarService.OpenPriorityBracket.Denotations.Contains(token.Previous);

            var isFollowedByOpenBracket = token.Next != default &&
                                          grammarService.OpenPriorityBracket.Denotations.Contains(token.Next);

            // like -1 - 2
            //      ^
            var isPrefixCase1 = grammarService.IsOperand(token.Next) &&
                                token.Previous == StringToken.NonExistingOperator;

            // like !a && !b
            //            ^
            var isPrefixCase2 = grammarService.IsOperand(token.Next) &&
                                grammarService.IsOperator(token.Previous) &&
                                grammarService.IsUniqueByArity(token.Previous, Arity.Unary);

            // like -(1 - 2)
            //      ^
            var isPrefixCase3 = isFollowedByOpenBracket &&
                                token.Previous == StringToken.NonExistingOperator;

            // like ( -(1 - 2))
            //        ^
            var isPrefixCase4 = isPrecededByOpenBracket &&
                                grammarService.IsOperator(token.Current) &&
                                isFollowedByOpenBracket;

            // like (-5)
            //       ^
            var isPrefixCase5 = isPrecededByOpenBracket &&
                                grammarService.IsOperator(token.Current) &&
                                grammarService.IsOperand(token.Next);

            return(isPrefixCase1 ||
                   isPrefixCase2 ||
                   isPrefixCase3 ||
                   isPrefixCase4 ||
                   isPrefixCase5);
        }