Exemplo n.º 1
0
        internal void FindAll(
            LinkedList <ChildInfo> pathToNode,
            ASTQueries.NodePred[] query,
            Action <IEnumerable <ChildInfo>, Node> visitor,
            CancellationToken cancel = default(CancellationToken))
        {
            Contract.Requires(query != null);
            Contract.Requires(pathToNode != null && pathToNode.Last.Value.Node == this);

            if (query.Length == 0)
            {
                return;
            }

            var initContext = ChildContextKind.AnyChildContext;
            int initAbsPos = 0, initRelPos = 0;

            if (pathToNode.Count > 1)
            {
                var prev = pathToNode.Last.Previous;
                foreach (var c in prev.Value.Node.ChildrenInfo)
                {
                    if (c.AbsolutePos == pathToNode.Last.Value.AbsolutePos)
                    {
                        initContext = c.Context;
                        initAbsPos  = c.AbsolutePos;
                        initRelPos  = c.RelativePos;
                        break;
                    }
                }
            }

            if (!Eval(query[0], initContext, initAbsPos, initRelPos) ||
                !ASTQueries.ASTSchema.Instance.IsQueryFeasible(NodeKind, initContext, query, 0))
            {
                return;
            }

            var qpath   = new LinkedList <ChildInfo>(pathToNode);
            var ctok    = cancel == default(CancellationToken) ? null : ASTComputationBase.MkControlToken(cancel, CancelCheckFreq);
            var astComp = new ASTComputationUpDown <int, bool>(
                this,
                (n, pos) => FindAllUnfold(n == this, n, pos, query, qpath, visitor),
                (n, v, c) =>
            {
                if (qpath.Last.Value.Node != this)
                {
                    qpath.RemoveLast();
                }

                return(true);
            },
                ctok);

            astComp.Compute(0);
        }
Exemplo n.º 2
0
        internal AST <Node> FindAny(
            LinkedList <ChildInfo> pathToNode,
            ASTQueries.NodePred[] query,
            CancellationToken cancel = default(CancellationToken))
        {
            Contract.Requires(query != null);
            Contract.Requires(pathToNode != null && pathToNode.Last.Value.Node == this);

            if (query.Length == 0)
            {
                return(null);
            }

            var initContext = ChildContextKind.AnyChildContext;
            int initAbsPos = 0, initRelPos = 0;

            if (pathToNode.Count > 1)
            {
                var prev = pathToNode.Last.Previous;
                foreach (var c in prev.Value.Node.ChildrenInfo)
                {
                    if (c.AbsolutePos == pathToNode.Last.Value.AbsolutePos)
                    {
                        initContext = c.Context;
                        initAbsPos  = c.AbsolutePos;
                        initRelPos  = c.RelativePos;
                        break;
                    }
                }
            }

            if (!Eval(query[0], initContext, initAbsPos, initRelPos) ||
                !ASTQueries.ASTSchema.Instance.IsQueryFeasible(NodeKind, initContext, query, 0))
            {
                return(null);
            }

            var qpath   = new LinkedList <ChildInfo>(pathToNode);
            var ctok    = cancel == default(CancellationToken) ? ASTComputationBase.MkControlToken() : ASTComputationBase.MkControlToken(cancel, CancelCheckFreq);
            var astComp = new ASTComputationUpDown <int, bool>(
                this,
                (n, pos) => FindAnyUnfold(n == this, n, pos, query, ctok, qpath),
                (n, v, c) =>
            {
                if (qpath.Last.Value.Node != this)
                {
                    qpath.RemoveLast();
                }

                return(true);
            },
                ctok);

            astComp.Compute(0);

            if (!ctok.IsSuspended)
            {
                return(null);
            }
            else if (cancel != default(CancellationToken) && cancel.IsCancellationRequested)
            {
                return(null);
            }

            Action <ChildInfo, bool> extender;
            var ast  = Factory.Instance.MkEmptyAST(qpath.Last.Value.Node.NodeKind, out extender);
            var crnt = qpath.First;

            while (crnt != null)
            {
                extender(crnt.Value, crnt.Next == null);
                crnt = crnt.Next;
            }

            return(ast);
        }