Exemple #1
0
        public override bool PreVisit(UnaryOpNode node)
        {
            Contracts.AssertValue(node);

            // Cursor is inside the operation token.
            if (node.Token.Kind == TokKind.PercentSign)
            {
                var span = node.GetSourceBasedSpan();

                if (node.Token.Span.Min <= _cursorPosition && _cursorPosition <= node.Token.Span.Lim || _cursorPosition <= span.Min || _cursorPosition >= span.Lim)
                {
                    _result = node;
                    return(false);
                }
            }
            else
            {
                if (_cursorPosition <= node.Token.Span.Lim)
                {
                    _result = node;
                    return(false);
                }
            }

            // Cursor is inside the child.
            node.Child.Accept(this);
            return(false);
        }
        public Code Neg(Code opd)
        {
            Node expr = (Node)opd;

            if (expr.type == NodeType.tpInt)
            {
                if (expr.tag == NodeTag.opIntConst)
                {
                    IntLiteralNode ic = (IntLiteralNode)expr;
                    ic.val = -ic.val;
                }
                else
                {
                    expr = new UnaryOpNode(NodeType.tpInt, NodeTag.opIntNeg, expr);
                }
            }
            else if (expr.type == NodeType.tpReal)
            {
                if (expr.tag == NodeTag.opRealConst)
                {
                    RealLiteralNode fc = (RealLiteralNode)expr;
                    fc.val = -fc.val;
                }
                else
                {
                    expr = new UnaryOpNode(NodeType.tpReal, NodeTag.opRealNeg, expr);
                }
            }
            else
            {
                throw new CodeGeneratorException("Invalid argument types");
            }
            return(expr);
        }
        public virtual bool IsSupportedOpNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding)
        {
            Contracts.AssertValue(node);
            Contracts.AssertValue(metadata);
            Contracts.AssertValue(binding);

            UnaryOpNode unaryOpNode = node.AsUnaryOpLit();

            if (unaryOpNode == null)
            {
                return(false);
            }

            if (!IsValidNode(node, binding))
            {
                return(false);
            }

            IOpDelegationStrategy opDelStrategy = _function.GetOpDelegationStrategy(unaryOpNode.Op);
            var unaryOpDelStrategy = (opDelStrategy as UnaryOpDelegationStrategy).VerifyValue();

            Contracts.Assert(unaryOpDelStrategy.Op == unaryOpNode.Op);

            if ((unaryOpNode.Child.Kind != NodeKind.FirstName) && !opDelStrategy.IsOpSupportedByTable(metadata, node, binding))
            {
                var telemetryMessage = string.Format("{0} operator not supported at table level", unaryOpNode.Op.ToString());
                SuggestDelegationHintAndAddTelemetryMessage(node, binding, telemetryMessage);
                TrackingProvider.Instance.SetDelegationTrackerStatus(DelegationStatus.UnaryOpNotSupportedByTable, node, binding, _function, DelegationTelemetryInfo.CreateUnaryOpNoSupportedInfoTelemetryInfo(unaryOpNode.Op));
                return(false);
            }

            return(IsSupportedNode(unaryOpNode.Child, metadata, binding, opDelStrategy));
        }
        public Code Not(Code opd)
        {
            Node expr = (Node)opd;

            if (expr.type == NodeType.tpInt)
            {
                if (expr.tag == NodeTag.opIntConst)
                {
                    IntLiteralNode ic = (IntLiteralNode)expr;
                    ic.val = ~ic.val;
                }
                else
                {
                    expr = new UnaryOpNode(NodeType.tpInt, NodeTag.opIntNot, expr);
                }
                return(expr);
            }
            else if (expr.type == NodeType.tpBool)
            {
                return(new UnaryOpNode(NodeType.tpBool, NodeTag.opBoolNot, expr));
            }
            else
            {
                throw new CodeGeneratorException("Invalid argument types");
            }
        }
Exemple #5
0
        public override dynamic Visit(UnaryOpNode node)
        {
            node.Expression = Replace(node.Expression);
            node.Expression.Accept(this);

            return(null);
        }
Exemple #6
0
            internal override bool TryAddSuggestionsForNodeKind(IntellisenseData.IntellisenseData intellisenseData)
            {
                Contracts.AssertValue(intellisenseData);

                TexlNode curNode   = intellisenseData.CurNode;
                int      cursorPos = intellisenseData.CursorPos;
                // Cursor is in the operation token or before.
                // Suggest all value possibilities.
                UnaryOpNode unaryOpNode = curNode.CastUnaryOp();
                var         tokenSpan   = unaryOpNode.Token.Span;

                if (cursorPos < tokenSpan.Min)
                {
                    return(false);
                }

                Contracts.Assert(cursorPos >= tokenSpan.Min || cursorPos <= tokenSpan.Lim);

                string keyword = TexlParser.GetTokString(unaryOpNode.Token.Kind);

                Contracts.Assert(intellisenseData.MatchingLength <= keyword.Length);

                var replacementLength = tokenSpan.Min == cursorPos ? 0 : tokenSpan.Lim - tokenSpan.Min;

                intellisenseData.SetMatchArea(tokenSpan.Min, cursorPos, replacementLength);
                intellisenseData.BoundTo = intellisenseData.MatchingLength == 0 ? string.Empty : keyword;
                IntellisenseHelper.AddSuggestionsForValuePossibilities(intellisenseData, curNode);

                return(true);
            }
Exemple #7
0
 public override void PostVisit(UnaryOpNode node)
 {
     Contracts.AssertValue(node);
     if (node.Token.Kind == TokKind.PercentSign)
     {
         _unaryOperators.Add(node);
     }
 }
Exemple #8
0
 public ExpressionNode Optimize(ExpressionNode expression)
 {
     return(expression switch
     {
         UnaryOpNode unaryOp => Optimize(unaryOp),
         BinaryOpNode binOp => Optimize(binOp),
         FunctionNode func => Optimize(func),
         _ => expression
     });
Exemple #9
0
        public ObjectBase VisitUnaryOpNode(UnaryOpNode node)
        {
            var number = this.Visit(node.node);

            if (node.token.type == Token.TokenType.TokenMinus)
            {
                number = number.MulBy(new Integer(-1));
            }

            number.SetPosition(node.start, node.end);
            return(number);
        }
Exemple #10
0
        private FilterLogicalOperator GetCompositeFilterDescriptorLogicalOperator(
            ExpressionNode binaryNode)
        {
            ExpressionNode expressionNode = binaryNode;

            for (UnaryOpNode unaryOpNode = expressionNode as UnaryOpNode; unaryOpNode != null && unaryOpNode.Op == Operator.Noop; unaryOpNode = expressionNode as UnaryOpNode)
            {
                expressionNode = unaryOpNode.Right;
            }
            BinaryOpNode binaryOpNode = expressionNode as BinaryOpNode;

            return(binaryOpNode != null && binaryOpNode.Op == Operator.Or ? FilterLogicalOperator.Or : FilterLogicalOperator.And);
        }
Exemple #11
0
        public IValue VisitUnaryNode(UnaryOpNode node, Context context)
        {
            var value = TraverseTree(node.Node, context);
            var operation = node.Operation;

            if (operation.TokenType == TokenType.PLUS)
            {
                return value;
            }
            else if (operation.TokenType == TokenType.MINUS)
            {
                var number = ((float)value.Value) * (-1);
                return new NumberValue { Value = number};
            }
            throw new Exception();
        }
Exemple #12
0
 public bool ApplyBehaviour(List <ASTNode> nodes, int opIndex, OrderMethod caller)
 {
     this.m_advanceBy = 0;
     if (this.m_isPostOp)
     {
         nodes[opIndex - 1] = new UnaryOpNode(nodes[opIndex].Pos, nodes[opIndex - 1], this.m_symbol, true);
         nodes.RemoveAt(opIndex);
         this.m_advanceBy = 0;
     }
     else
     {
         nodes[opIndex] = new UnaryOpNode(nodes[opIndex].Pos, nodes[opIndex + 1], this.m_symbol, false);
         nodes.RemoveAt(opIndex + 1);
         this.m_advanceBy = 1;
     }
     return(true);
 }
Exemple #13
0
        public override LazyList <string> Visit(UnaryOpNode node, Precedence parentPrecedence)
        {
            Contracts.AssertValue(node);

            var child = node.Child.Accept(this, Precedence.PrefixUnary);

            LazyList <string> result;

            switch (node.Op)
            {
            case UnaryOp.Not:
                if (node.Token.Kind == TokKind.KeyNot)
                {
                    result = LazyList <string> .Of(TexlLexer.KeywordNot, " ").With(child);
                }
                else
                {
                    result = LazyList <string> .Of(TexlLexer.PunctuatorBang).With(child);
                }
                break;

            case UnaryOp.Minus:
                result = LazyList <string> .Of(TexlLexer.PunctuatorSub).With(child);

                break;

            case UnaryOp.Percent:
                result = LazyList <string> .Of(child).With(TexlLexer.PunctuatorPercent);

                break;

            default:
                Contracts.Assert(false);
                result = LazyList <string> .Of("<error>").With(child);

                break;
            }

            return(ApplyPrecedence(parentPrecedence, Precedence.PrefixUnary, result));
        }
Exemple #14
0
        public override dynamic Visit(UnaryOpNode node)
        {
            var op   = node.Op;
            var type = node.Expression.Accept(this);

            if (type != PrimitiveType.Boolean && op == OperatorType.Not ||
                new[] { OperatorType.Add, OperatorType.Sub }.Contains(op) && type != PrimitiveType.Integer &&
                type != PrimitiveType.Real)
            {
                Context.ErrorService.Add(
                    ErrorType.Unknown,
                    node.Token,
                    $"invalid op {op} on {type}");
                type = PrimitiveType.Error;
            }

            // throw new Exception($"invalid op {op} on {type}");

            node.Type = new SimpleTypeNode
            {
                PrimitiveType = type
            };
            return(type);
        }
Exemple #15
0
 public override Result Visit(UnaryOpNode node, Context context)
 {
     return(Default);
 }
Exemple #16
0
        public override bool IsRowScopedServerDelegatable(CallNode callNode, TexlBinding binding, OperationCapabilityMetadata metadata)
        {
            Contracts.AssertValue(callNode);
            Contracts.AssertValue(binding);
            Contracts.AssertValue(metadata);

            if (binding.ErrorContainer.HasErrors(callNode) ||
                !CheckArgsCount(callNode, binding) ||
                !binding.IsRowScope(callNode))
            {
                return(false);
            }

            TexlNode[] args    = callNode.Args.Children.VerifyValue();
            NodeKind   argKind = args[0].VerifyValue().Kind;

            var opStrategy        = GetOpDelegationStrategy(UnaryOp.Not);
            var firstNameStrategy = GetFirstNameNodeDelegationStrategy();
            var dottedStrategy    = GetDottedNameNodeDelegationStrategy();
            var cNodeStrategy     = GetCallNodeDelegationStrategy();

            switch (argKind)
            {
            case NodeKind.FirstName:
                return(firstNameStrategy.IsValidFirstNameNode(args[0].AsFirstName(), binding, opStrategy));

            case NodeKind.Call:
            {
                if (!opStrategy.IsOpSupportedByTable(metadata, callNode, binding))
                {
                    return(false);
                }

                return(cNodeStrategy.IsValidCallNode(args[0].AsCall(), binding, metadata));
            }

            case NodeKind.BinaryOp:
            {
                if (!opStrategy.IsOpSupportedByTable(metadata, callNode, binding))
                {
                    return(false);
                }

                BinaryOpNode          opNode = args[0].AsBinaryOp();
                IOpDelegationStrategy binaryOpNodeValidationStrategy = GetOpDelegationStrategy(opNode.Op, opNode);
                return(binaryOpNodeValidationStrategy.IsSupportedOpNode(opNode, metadata, binding));
            }

            case NodeKind.UnaryOp:
            {
                if (!opStrategy.IsOpSupportedByTable(metadata, callNode, binding))
                {
                    return(false);
                }

                UnaryOpNode opNode = args[0].AsUnaryOpLit();
                var         unaryOpNodeValidationStrategy = GetOpDelegationStrategy(opNode.Op);
                return(unaryOpNodeValidationStrategy.IsSupportedOpNode(opNode, metadata, binding));
            }

            case NodeKind.DottedName:
                return(dottedStrategy.IsValidDottedNameNode(args[0].AsDottedName(), binding, metadata, opStrategy));

            default:
                return(argKind == NodeKind.BoolLit);
            }
        }
Exemple #17
0
 public abstract dynamic Visit(UnaryOpNode node);
Exemple #18
0
        public override bool IsRowScopedServerDelegatable(CallNode callNode, TexlBinding binding, OperationCapabilityMetadata metadata)
        {
            Contracts.AssertValue(callNode);
            Contracts.AssertValue(binding);
            Contracts.AssertValue(metadata);

            if (binding.ErrorContainer.HasErrors(callNode) ||
                !CheckArgsCount(callNode, binding) ||
                !binding.IsRowScope(callNode))
            {
                return(false);
            }

            TexlNode[] args = callNode.Args.Children.VerifyValue();
            Contracts.Assert(args.Length >= MinArity);

            DelegationCapability funcDelegationCapability = FunctionDelegationCapability | (_isAnd ? DelegationCapability.And : DelegationCapability.Or);

            if (!metadata.IsDelegationSupportedByTable(funcDelegationCapability))
            {
                return(false);
            }

            foreach (var arg in args)
            {
                NodeKind argKind = arg.VerifyValue().Kind;
                switch (argKind)
                {
                case NodeKind.FirstName:
                {
                    var firstNameStrategy = GetFirstNameNodeDelegationStrategy();
                    if (!firstNameStrategy.IsValidFirstNameNode(arg.AsFirstName(), binding, null))
                    {
                        return(false);
                    }

                    break;
                }

                case NodeKind.Call:
                {
                    var cNodeStrategy = GetCallNodeDelegationStrategy();
                    if (!cNodeStrategy.IsValidCallNode(arg.AsCall(), binding, metadata))
                    {
                        SuggestDelegationHint(arg, binding);
                        return(false);
                    }

                    break;
                }

                case NodeKind.DottedName:
                {
                    var dottedStrategy = GetDottedNameNodeDelegationStrategy();
                    if (!dottedStrategy.IsValidDottedNameNode(arg.AsDottedName(), binding, metadata, null))
                    {
                        SuggestDelegationHint(arg, binding);
                        return(false);
                    }

                    break;
                }

                case NodeKind.BinaryOp:
                {
                    BinaryOpNode opNode = arg.AsBinaryOp();
                    var          binaryOpNodeValidationStrategy = GetOpDelegationStrategy(opNode.Op, opNode);
                    if (!binaryOpNodeValidationStrategy.IsSupportedOpNode(opNode, metadata, binding))
                    {
                        SuggestDelegationHint(arg, binding);
                        return(false);
                    }

                    break;
                }

                case NodeKind.UnaryOp:
                {
                    UnaryOpNode opNode = arg.AsUnaryOpLit();
                    var         unaryOpNodeValidationStrategy = GetOpDelegationStrategy(opNode.Op);
                    if (!unaryOpNodeValidationStrategy.IsSupportedOpNode(opNode, metadata, binding))
                    {
                        SuggestDelegationHint(arg, binding);
                        return(false);
                    }

                    break;
                }

                case NodeKind.BoolLit:
                    break;

                default:
                    return(false);
                }
            }

            return(true);
        }
        private static Node listToTree(Node expr, BinOpNode list)
        {
            BinOpNode tree = null;

            do
            {
                Node    elem = list.right;
                NodeTag cop  = NodeTag.opNop;
                if (elem.type == NodeType.tpUnknown)
                {
                    elem.type = expr.type;
                }
                if (expr.type == NodeType.tpInt)
                {
                    if (elem.type == NodeType.tpReal)
                    {
                        expr = new UnaryOpNode(NodeType.tpReal, NodeTag.opIntToReal, expr);
                        cop  = NodeTag.opRealEq;
                    }
                    else if (elem.type == NodeType.tpInt)
                    {
                        cop = NodeTag.opIntEq;
                    }
                }
                else if (expr.type == NodeType.tpReal)
                {
                    if (elem.type == NodeType.tpReal)
                    {
                        cop = NodeTag.opRealEq;
                    }
                    else if (elem.type == NodeType.tpInt)
                    {
                        cop  = NodeTag.opRealEq;
                        elem = QueryImpl.int2real(elem);
                    }
                }
                else if (expr.type == NodeType.tpDate && elem.type == NodeType.tpDate)
                {
                    cop = NodeTag.opDateEq;
                }
                else if (expr.type == NodeType.tpStr && elem.type == NodeType.tpStr)
                {
                    cop = NodeTag.opStrEq;
                }
                else if (expr.type == NodeType.tpObj && elem.type == NodeType.tpObj)
                {
                    cop = NodeTag.opObjEq;
                }
                else if (expr.type == NodeType.tpBool && elem.type == NodeType.tpBool)
                {
                    cop = NodeTag.opBoolEq;
                }
                if (cop == NodeTag.opNop)
                {
                    throw new CodeGeneratorException("Invalid argument types");
                }
                BinOpNode cmp = new BinOpNode(NodeType.tpBool, cop, expr, elem);
                if (tree == null)
                {
                    tree = cmp;
                }
                else
                {
                    tree = new BinOpNode(NodeType.tpBool, NodeTag.opBoolOr, cmp, tree);
                }
            } while ((list = (BinOpNode)list.left) != null);
            return(tree);
        }
Exemple #20
0
 public virtual bool PreVisit(UnaryOpNode node)
 {
     return(true);
 }
Exemple #21
0
        // Parses the next (maximal) expression with precedence >= precMin.
        private TexlNode ParseExpr(Precedence precMin, TexlNode node = null)
        {
            // ParseOperand may accept PrefixUnary and higher, so ParseExpr should never be called
            // with precMin > Precedence.PrefixUnary - it will not correctly handle those cases.
            Contracts.Assert(Precedence.None <= precMin && precMin <= Precedence.PrefixUnary);

            try
            {
                // The parser is recursive. Deeply nested invocations (over 200 deep) and other
                // intentionally miscrafted rules can throw it off, causing stack overflows.
                // Ensure the product doesn't crash in such situations, but instead post
                // corresponding parse errors.
                if (node == null)
                {
                    if (++_depth > _maxAllowedExpressionDepth)
                    {
                        return(CreateError(_curs.TokMove(), TexlStrings.ErrRuleNestedTooDeeply));
                    }

                    // Get the left operand.
                    node = ParseOperand();
                }

                // Process operators and right operands as long as the precedence bound is satisfied.
                for (;;)
                {
                    var leftTrivia = ParseTrivia();
                    Contracts.AssertValue(node);
                    Token       tok;
                    TexlNode    right;
                    Identifier  identifier;
                    ITexlSource rightTrivia;
                    switch (_curs.TidCur)
                    {
                    case TokKind.PercentSign:
                        Contracts.Assert(precMin <= Precedence.PostfixUnary);
                        tok  = _curs.TokMove();
                        node = new UnaryOpNode(
                            ref _idNext,
                            tok,
                            new SourceList(new NodeSource(node), new TokenSource(tok)),
                            UnaryOp.Percent,
                            node);
                        break;

                    case TokKind.Dot:
                    case TokKind.Bang:
                        Contracts.Assert(precMin <= Precedence.Primary);
                        DottedNameNode leftDotted;
                        if ((leftDotted = node as DottedNameNode) != null && leftDotted.Token.Kind != _curs.TidCur && leftDotted.Token.Kind != TokKind.BracketOpen)
                        {
                            // Can't mix and match separators. E.g. A.B!C is invalid.
                            goto case TokKind.False;
                        }
                        tok         = _curs.TokMove();
                        rightTrivia = ParseTrivia();
                        identifier  = ParseIdentifier();
                        node        = new DottedNameNode(
                            ref _idNext,
                            tok,
                            new SourceList(
                                new NodeSource(node),
                                new TokenSource(tok),
                                new SpreadSource(rightTrivia),
                                new IdentifierSource(identifier)),
                            node,
                            identifier,
                            null);
                        if (node.Depth > _maxAllowedExpressionDepth)
                        {
                            return(CreateError(node.Token, TexlStrings.ErrRuleNestedTooDeeply));
                        }
                        break;

                    case TokKind.Caret:
                        Contracts.Assert(precMin <= Precedence.Power);
                        node = ParseBinary(node, leftTrivia, BinaryOp.Power, Precedence.PrefixUnary);
                        break;

                    case TokKind.Mul:
                        if (precMin > Precedence.Mul)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.Mul, Precedence.Mul + 1);
                        break;

                    case TokKind.Div:
                        if (precMin > Precedence.Mul)
                        {
                            goto default;
                        }
                        tok         = _curs.TokMove();
                        rightTrivia = ParseTrivia();
                        right       = ParseExpr(Precedence.Mul + 1);
                        node        = MakeBinary(BinaryOp.Div, node, leftTrivia, tok, rightTrivia, right);
                        break;

                    case TokKind.Sub:
                        if (precMin > Precedence.Add)
                        {
                            goto default;
                        }
                        tok         = _curs.TokMove();
                        rightTrivia = ParseTrivia();
                        right       = ParseExpr(Precedence.Add + 1);
                        right       = new UnaryOpNode(ref _idNext, tok, right.SourceList, UnaryOp.Minus, right);
                        node        = MakeBinary(BinaryOp.Add, node, leftTrivia, tok, rightTrivia, right);
                        break;

                    case TokKind.Add:
                        if (precMin > Precedence.Add)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.Add, Precedence.Add + 1);
                        break;

                    case TokKind.Ampersand:
                        if (precMin > Precedence.Concat)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.Concat, Precedence.Concat + 1);
                        break;

                    case TokKind.KeyAnd:
                    case TokKind.And:
                        if (precMin > Precedence.And)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.And, Precedence.And + 1);
                        break;

                    case TokKind.KeyOr:
                    case TokKind.Or:
                        if (precMin > Precedence.Or)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.Or, Precedence.Or + 1);
                        break;

                    // Comparison operators
                    // expr = expr
                    // expr <> expr
                    case TokKind.Equ:
                        if (precMin > Precedence.Compare)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.Equal, Precedence.Compare + 1);
                        break;

                    case TokKind.LssGrt:
                        if (precMin > Precedence.Compare)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.NotEqual, Precedence.Compare + 1);
                        break;

                    // expr < expr
                    case TokKind.Lss:
                        if (precMin > Precedence.Compare)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.Less, Precedence.Compare + 1);
                        break;

                    // expr <= expr
                    case TokKind.LssEqu:
                        if (precMin > Precedence.Compare)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.LessEqual, Precedence.Compare + 1);
                        break;

                    // expr > expr
                    case TokKind.Grt:
                        if (precMin > Precedence.Compare)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.Greater, Precedence.Compare + 1);
                        break;

                    // expr >= expr
                    case TokKind.GrtEqu:
                        if (precMin > Precedence.Compare)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.GreaterEqual, Precedence.Compare + 1);
                        break;

                    case TokKind.Ident:
                    case TokKind.NumLit:
                    case TokKind.StrLit:
                    case TokKind.True:
                    case TokKind.False:
                        PostError(_curs.TokCur, TexlStrings.ErrOperatorExpected);
                        tok         = _curs.TokMove();
                        rightTrivia = ParseTrivia();
                        right       = ParseExpr(Precedence.Error);
                        node        = MakeBinary(BinaryOp.Error, node, leftTrivia, tok, rightTrivia, right);
                        break;

                    case TokKind.ParenOpen:
                        DottedNameNode dotted;
                        if ((dotted = node as DottedNameNode) == null ||
                            !dotted.HasPossibleNamespaceQualifier)
                        {
                            goto default;
                        }
                        node = ParseInvocationWithNamespace(dotted);
                        break;

                    case TokKind.In:
                        if (precMin > Precedence.In)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.In, Precedence.In + 1);
                        break;

                    case TokKind.Exactin:
                        if (precMin > Precedence.In)
                        {
                            goto default;
                        }
                        node = ParseBinary(node, leftTrivia, BinaryOp.Exactin, Precedence.In + 1);
                        break;


                    case TokKind.As:
                        if (precMin > Precedence.As)
                        {
                            goto default;
                        }
                        node = ParseAs(node, leftTrivia);
                        break;

                    case TokKind.Semicolon:
                        // Only allow this when expression chaining is enabled (e.g. in behavior rules).
                        if ((_flags & Flags.EnableExpressionChaining) == 0)
                        {
                            goto case TokKind.False;
                        }
                        if (precMin > Precedence.None)
                        {
                            goto default;
                        }
                        node = ParseExprChain(node, leftTrivia);
                        break;

                    case TokKind.BracketOpen:
                        // Note we explicitly forbid [@foo][@bar], and also A!B!C[@foo], since these are syntactically nonsensical at the moment.
                        FirstNameNode first;
                        if ((first = node as FirstNameNode) == null || first.Ident.AtToken != null || _curs.TidPeek() != TokKind.At)
                        {
                            goto default;
                        }
                        node = ParseScopeField(first);
                        break;

                    case TokKind.Comment:
                        Contracts.Assert(false, "A stray comment was found");
                        _curs.TokMove();
                        return(node);

                    case TokKind.Eof:
                        if (_after == null)
                        {
                            _after = new SourceList(leftTrivia);
                        }
                        else
                        {
                            _after = new SourceList(new SpreadSource(_after.Sources), new SpreadSource(leftTrivia));
                        }
                        return(node);

                    default:
                        AddExtraTrivia(leftTrivia);
                        return(node);
                    }
                }
            }
            finally
            {
                --_depth;
            }
        }
Exemple #22
0
 public virtual TResult Visit(UnaryOpNode node) => this.VisitChildren(node);
 public override void PostVisit(UnaryOpNode node)
 {
 }
Exemple #24
0
 public abstract void PostVisit(UnaryOpNode node);
 public abstract Result Visit(UnaryOpNode node, Context context);
 private static Node listToTree(Node expr, BinOpNode list)
 {
     BinOpNode tree = null; 
     do { 
         Node elem = list.right;
         NodeTag cop = NodeTag.opNop;
         if (elem.type == NodeType.tpUnknown) { 
             elem.type = expr.type;
         }
         if (expr.type == NodeType.tpInt) { 
             if (elem.type == NodeType.tpReal) { 
                 expr = new UnaryOpNode(NodeType.tpReal, NodeTag.opIntToReal, expr);
                 cop = NodeTag.opRealEq;
             } else if (elem.type == NodeType.tpInt) { 
                 cop = NodeTag.opIntEq;
             }
         } else if (expr.type == NodeType.tpReal) {
             if (elem.type == NodeType.tpReal) { 
                 cop = NodeTag.opRealEq;
             } else if (elem.type == NodeType.tpInt) { 
                 cop = NodeTag.opRealEq;
                 elem = QueryImpl.int2real(elem);
             }
         } else if (expr.type == NodeType.tpDate && elem.type == NodeType.tpDate) {
             cop = NodeTag.opDateEq;
         } else if (expr.type == NodeType.tpStr && elem.type == NodeType.tpStr) {
             cop = NodeTag.opStrEq;
         } else if (expr.type == NodeType.tpObj && elem.type == NodeType.tpObj) {
             cop = NodeTag.opObjEq;
         } else if (expr.type == NodeType.tpBool && elem.type == NodeType.tpBool) {
             cop = NodeTag.opBoolEq;
         }
         if (cop == NodeTag.opNop) { 
             throw new CodeGeneratorException("Invalid argument types");
         }
         BinOpNode cmp = new BinOpNode(NodeType.tpBool, cop, expr, elem);
         if (tree == null) { 
             tree = cmp; 
         } else {
             tree = new BinOpNode(NodeType.tpBool, NodeTag.opBoolOr, cmp, tree);
         }
     } while ((list = (BinOpNode)list.left) != null);
     return tree;
 }
 public Code Neg(Code opd) {
     Node expr = (Node)opd;
     if (expr.type == NodeType.tpInt) { 
         if (expr.tag == NodeTag.opIntConst) { 
             IntLiteralNode ic = (IntLiteralNode)expr;
             ic.val = -ic.val;
         } else {
             expr = new UnaryOpNode(NodeType.tpInt, NodeTag.opIntNeg, expr);
         } 
     } else if (expr.type == NodeType.tpReal) { 
         if (expr.tag == NodeTag.opRealConst) { 
             RealLiteralNode fc = (RealLiteralNode)expr;
             fc.val = -fc.val;
         } else {
             expr = new UnaryOpNode(NodeType.tpReal, NodeTag.opRealNeg, expr);
         } 
     } else { 
         throw new CodeGeneratorException("Invalid argument types");
     }        
     return expr;
 }
Exemple #28
0
        public override LazyList <string> Visit(UnaryOpNode node, Context context)
        {
            Contracts.AssertValue(node);

            return(Basic(node, context));
        }
Exemple #29
0
        public void ComplexAssignmentTests()
        {
            this.Compare(
                new SourceNode(
                    new DeclStatNode(1,
                                     new DeclSpecsNode(1, "int"),
                                     new DeclListNode(1, new VarDeclNode(1, new IdNode(1, "x"), new LitExprNode(1, 1)))
                                     ),
                    new ExprStatNode(1,
                                     new ExprListNode(1,
                                                      new AssignExprNode(1,
                                                                         new IdNode(1, "x"),
                                                                         AssignOpNode.FromSymbol(1, "+="),
                                                                         new LitExprNode(1, 2)
                                                                         ),
                                                      new AssignExprNode(1,
                                                                         new IdNode(1, "x"),
                                                                         AssignOpNode.FromSymbol(1, "+="),
                                                                         new LitExprNode(1, 3)
                                                                         )
                                                      )
                                     )
                    ),
                new SourceNode(
                    new DeclStatNode(1,
                                     new DeclSpecsNode(1, "int"),
                                     new DeclListNode(1, new VarDeclNode(1, new IdNode(1, "x"), new LitExprNode(1, 1)))
                                     ),
                    new ExprStatNode(1,
                                     new AssignExprNode(1,
                                                        new IdNode(1, "x"),
                                                        AssignOpNode.FromSymbol(1, "+="),
                                                        new ArithmExprNode(1,
                                                                           new IdNode(1, "x"),
                                                                           ArithmOpNode.FromSymbol(1, "+"),
                                                                           new LitExprNode(1, 4)
                                                                           )
                                                        )
                                     )
                    )
                );

            this.Compare(
                new SourceNode(
                    new DeclStatNode(1,
                                     new DeclSpecsNode(1, "int"),
                                     new DeclListNode(1, new VarDeclNode(1, new IdNode(1, "x"), new LitExprNode(1, 1)))
                                     ),
                    new DeclStatNode(1,
                                     new DeclSpecsNode(1, "int"),
                                     new DeclListNode(1, new VarDeclNode(1, new IdNode(1, "y"), new LitExprNode(1, 1)))
                                     ),
                    new ExprStatNode(1,
                                     new AssignExprNode(1,
                                                        new IdNode(1, "x"),
                                                        AssignOpNode.FromSymbol(1, "+="),
                                                        new IdNode(1, "y")
                                                        )
                                     )
                    ),
                new SourceNode(
                    new DeclStatNode(1,
                                     new DeclSpecsNode(1, "int"),
                                     new DeclListNode(1, new VarDeclNode(1, new IdNode(1, "x"), new LitExprNode(1, 1)))
                                     ),
                    new DeclStatNode(1,
                                     new DeclSpecsNode(1, "int"),
                                     new DeclListNode(1, new VarDeclNode(1, new IdNode(1, "y"), new LitExprNode(1, 1)))
                                     ),
                    new ExprStatNode(1,
                                     new AssignExprNode(1,
                                                        new IdNode(1, "x"),
                                                        AssignOpNode.FromSymbol(1, "-="),
                                                        new UnaryExprNode(1,
                                                                          UnaryOpNode.FromSymbol(1, "-"),
                                                                          new IdNode(1, "y")
                                                                          )
                                                        )
                                     )
                    )
                );

            this.Compare(
                new SourceNode(
                    new DeclStatNode(1,
                                     new DeclSpecsNode(1, "int"),
                                     new DeclListNode(1, new VarDeclNode(1, new IdNode(1, "x"), new LitExprNode(1, 1)))
                                     ),
                    new DeclStatNode(1,
                                     new DeclSpecsNode(1, "int"),
                                     new DeclListNode(1, new VarDeclNode(1, new IdNode(1, "y"), new LitExprNode(1, 1)))
                                     ),
                    new ExprStatNode(1,
                                     new AssignExprNode(1,
                                                        new IdNode(1, "x"),
                                                        AssignOpNode.FromSymbol(1, "+="),
                                                        new IdNode(1, "y")
                                                        )
                                     )
                    ),
                new SourceNode(
                    new DeclStatNode(1,
                                     new DeclSpecsNode(1, "int"),
                                     new DeclListNode(1, new VarDeclNode(1, new IdNode(1, "x"), new LitExprNode(1, 1)))
                                     ),
                    new DeclStatNode(1,
                                     new DeclSpecsNode(1, "int"),
                                     new DeclListNode(1, new VarDeclNode(1, new IdNode(1, "y"), new LitExprNode(1, 1)))
                                     ),
                    new ExprStatNode(1,
                                     new ExprListNode(1,
                                                      new AssignExprNode(1,
                                                                         new IdNode(1, "x"),
                                                                         AssignOpNode.FromSymbol(1, "+="),
                                                                         new UnaryExprNode(1,
                                                                                           UnaryOpNode.FromSymbol(1, "-"),
                                                                                           new IdNode(1, "y")
                                                                                           )
                                                                         ),
                                                      new AssignExprNode(1,
                                                                         new IdNode(1, "y"),
                                                                         AssignOpNode.FromSymbol(1, "+="),
                                                                         new LitExprNode(1, 1)
                                                                         )
                                                      )
                                     )
                    ),
                new MatchIssues()
                .AddError(new BlockEndValueMismatchError("x", 1, "2", "0"))
                .AddError(new BlockEndValueMismatchError("y", 1, "1", "2"))
                );
        }
 public Code Not(Code opd) {
     Node expr = (Node)opd;
     if (expr.type == NodeType.tpInt) { 
         if (expr.tag == NodeTag.opIntConst) { 
             IntLiteralNode ic = (IntLiteralNode)expr;
             ic.val = ~ic.val;
         } else {
             expr = new UnaryOpNode(NodeType.tpInt, NodeTag.opIntNot, expr);
         } 
         return expr;
     } else if (expr.type == NodeType.tpBool) { 
         return new UnaryOpNode(NodeType.tpBool, NodeTag.opBoolNot, expr);
     } else { 
         throw new CodeGeneratorException("Invalid argument types");
     }
 }
Exemple #31
0
 public override Result Visit(UnaryOpNode node, Context context)
 {
     return(node.Child.Accept(this, context));
 }
Exemple #32
0
        //private void Parse(string expression)
        //{
        //    ExpressionNode root = ExpressionParser.Parse(expression, false);
        //    //validate name nodes

        //    this.BeginUpdate();
        //    this.Clear();

        //    Stack<ExpressionNode> nodeStack = new Stack<ExpressionNode>();
        //    nodeStack.Push(root);

        //    Stack<FilterExpressionCollection> collectionStack = new Stack<FilterExpressionCollection>();
        //    collectionStack.Push(this);

        //    while (nodeStack.Count > 0)
        //    {
        //        ExpressionNode current = nodeStack.Pop();
        //        FilterExpressionCollection expressions = collectionStack.Pop();

        //        UnaryOpNode unaryNode = current as UnaryOpNode;
        //        if (unaryNode != null)
        //        {
        //            if (unaryNode.Op == Operator.Noop)
        //            {
        //                nodeStack.Push(((UnaryOpNode)current).Right);
        //                collectionStack.Push(expressions);
        //                continue;
        //            }
        //        }

        //        BinaryOpNode binaryNode = current as BinaryOpNode;
        //        if (binaryNode != null)
        //        {
        //            if (this.IsPredicate(binaryNode))
        //            {
        //                FilterExpression filterExpression = new FilterExpression();
        //                expressions.Add(filterExpression);
        //                continue;
        //            }

        //            nodeStack.Push(binaryNode.Right);
        //            FilterExpressionCollection collection = expressions;
        //            if(!this.IsPredicate(binaryNode.Right))
        //            {
        //                CompositeFilterExpression  compositeExpression = new CompositeFilterExpression();
        //                expressions.Add(compositeExpression);
        //                collection = compositeExpression.FilterExpressions;
        //            }
        //            collectionStack.Push(collection);

        //            nodeStack.Push(binaryNode.Left);
        //            collection = expressions;
        //            if (!this.IsPredicate(binaryNode.Left))
        //            {
        //                CompositeFilterExpression compositeExpression = new CompositeFilterExpression();
        //                compositeExpression.BinaryOperator = (binaryNode.Op == Operator.And) ? FilterExpression.BinaryOperation.AND : FilterExpression.BinaryOperation.OR;
        //                expressions.Add(compositeExpression);
        //                collection = compositeExpression.FilterExpressions;
        //            }
        //            collectionStack.Push(collection);
        //        }
        //    }

        //    this.EndUpdate();
        //}

        //private bool IsPredicate(ExpressionNode node)
        //{
        //    while (node is UnaryOpNode)
        //    {
        //        node = ((UnaryOpNode)node).Right;
        //    }

        //    BinaryOpNode binaryNode = node as BinaryOpNode;
        //    if (binaryNode == null)
        //    {
        //        return false;
        //    }

        //    if (binaryNode.Left is NameNode || binaryNode.Right is NameNode)
        //    {
        //        return true;
        //    }

        //    return false;
        //}

        private void Parse(string expression)
        {
            ExpressionNode root = ExpressionParser.Parse(expression, false);

            //validate name nodes

            this.BeginUpdate();
            this.Clear();

            Stack <ExpressionNode> nodeStack = new Stack <ExpressionNode>();

            nodeStack.Push(root);

            Stack <FilterDescriptorCollection> collectionStack = new Stack <FilterDescriptorCollection>();

            collectionStack.Push(this);

            while (nodeStack.Count > 0)
            {
                ExpressionNode             current     = nodeStack.Pop();
                FilterDescriptorCollection expressions = collectionStack.Pop();

                UnaryOpNode unaryNode = current as UnaryOpNode;
                if (unaryNode != null)
                {
                    if (unaryNode.Op == Operator.Noop)
                    {
                        nodeStack.Push(((UnaryOpNode)current).Right);
                        collectionStack.Push(expressions);
                        continue;
                    }
                }

                BinaryOpNode binaryNode = current as BinaryOpNode;
                if (binaryNode != null)
                {
                    if (this.IsPredicate(binaryNode))
                    {
                        FilterDescriptor filterDescriptor = this.CreateFilterDescriptor(binaryNode);

                        expressions.Add(filterDescriptor);
                        continue;
                    }

                    nodeStack.Push(binaryNode.Right);
                    FilterDescriptorCollection collection = expressions;
                    if (!this.IsPredicate(binaryNode.Right))
                    {
                        CompositeFilterDescriptor compositeExpression = new CompositeFilterDescriptor();
                        expressions.Add(compositeExpression);
                        collection = compositeExpression.FilterDescriptors;
                    }
                    collectionStack.Push(collection);

                    nodeStack.Push(binaryNode.Left);
                    collection = expressions;
                    if (!this.IsPredicate(binaryNode.Left))
                    {
                        CompositeFilterDescriptor compositeExpression = new CompositeFilterDescriptor();
                        compositeExpression.LogicalOperator = (binaryNode.Op == Operator.And) ? FilterLogicalOperator.And : FilterLogicalOperator.Or;
                        expressions.Add(compositeExpression);
                        collection = compositeExpression.FilterDescriptors;
                    }
                    collectionStack.Push(collection);
                }
            }

            this.EndUpdate();
        }
        private bool IsSupportedNode(TexlNode node, OperationCapabilityMetadata metadata, TexlBinding binding, IOpDelegationStrategy opDelStrategy)
        {
            Contracts.AssertValue(node);
            Contracts.AssertValue(metadata);
            Contracts.AssertValue(binding);
            Contracts.AssertValue(opDelStrategy);

            if (!binding.IsRowScope(node))
            {
                return(true);
            }

            switch (node.Kind)
            {
            case NodeKind.DottedName:
            {
                if (!opDelStrategy.IsOpSupportedByTable(metadata, node, binding))
                {
                    return(false);
                }

                var dottedNodeValStrategy = _function.GetDottedNameNodeDelegationStrategy();
                return(dottedNodeValStrategy.IsValidDottedNameNode(node.AsDottedName(), binding, metadata, opDelStrategy));
            }

            case NodeKind.Call:
            {
                if (!opDelStrategy.IsOpSupportedByTable(metadata, node, binding))
                {
                    return(false);
                }

                var cNodeValStrategy = _function.GetCallNodeDelegationStrategy();
                return(cNodeValStrategy.IsValidCallNode(node.AsCall(), binding, metadata));
            }

            case NodeKind.FirstName:
            {
                var firstNameNodeValStrategy = _function.GetFirstNameNodeDelegationStrategy();
                return(firstNameNodeValStrategy.IsValidFirstNameNode(node.AsFirstName(), binding, opDelStrategy));
            }

            case NodeKind.UnaryOp:
            {
                if (!opDelStrategy.IsOpSupportedByTable(metadata, node, binding))
                {
                    return(false);
                }

                UnaryOpNode unaryOpNode = node.AsUnaryOpLit().VerifyValue();
                opDelStrategy = _function.GetOpDelegationStrategy(unaryOpNode.Op).VerifyValue();

                var unaryOpDelStrategy = (opDelStrategy as UnaryOpDelegationStrategy).VerifyValue();
                Contracts.Assert(unaryOpDelStrategy.Op == unaryOpNode.Op);

                if (!opDelStrategy.IsSupportedOpNode(node, metadata, binding))
                {
                    SuggestDelegationHint(node, binding);
                    return(false);
                }

                return(true);
            }

            case NodeKind.BinaryOp:
            {
                if (!opDelStrategy.IsOpSupportedByTable(metadata, node, binding))
                {
                    return(false);
                }

                var binaryOpNode = node.AsBinaryOp().VerifyValue();
                IOpDelegationStrategy binaryOpNodeDelValidationStrategy = _function.GetOpDelegationStrategy(binaryOpNode.Op, binaryOpNode);
                return(binaryOpNodeDelValidationStrategy.IsSupportedOpNode(node.AsBinaryOp(), metadata, binding));
            }
            }

            SuggestDelegationHint(node, binding);
            return(false);
        }