Beispiel #1
0
        /// <summary>
        /// Makes a predicate that matches folders to the program, and then matches the remainder
        /// </summary>
        public NodePred[] MkProgramPredicate(ProgramName prog, params NodePred[] remainder)
        {
            Contract.Requires(prog != null);
            var segs = prog.Uri.Segments;

            Contract.Assert(segs.Length > 1 && segs[0] == "/");
            var preds = new NodePred[segs.Length + (remainder == null ? 0 : remainder.Length)];

            preds[0] = MkPredicate(NodeKind.Folder) & MkNamePredicate("/");
            for (int i = 1; i < segs.Length - 1; ++i)
            {
                var seg = segs[i];
                preds[i] = MkPredicate(NodeKind.Folder) & MkNamePredicate(seg.Substring(0, seg.Length - 1));
            }

            preds[segs.Length - 1] = MkPredicate(NodeKind.Program) & MkNamePredicate(prog.ToString());

            if (remainder != null)
            {
                for (int i = 0; i < remainder.Length; ++i)
                {
                    preds[segs.Length + i] = remainder[i];
                }
            }

            return(preds);
        }
Beispiel #2
0
        internal NodePredOr(NodePred p1, NodePred p2)
        {
            Contract.Requires(p1 != null && p1.PredicateKind != NodePredicateKind.Star);
            Contract.Requires(p2 != null && p2.PredicateKind != NodePredicateKind.Star);

            Arg1 = p1;
            Arg2 = p2;
        }
Beispiel #3
0
        public AST <Node> Substitute(
            ASTQueries.NodePredAtom filter,
            Func <IEnumerable <ChildInfo>, NodeKind> subKind,
            Func <IEnumerable <ChildInfo>, Node> sub,
            CancellationToken cancel = default(CancellationToken))
        {
            var query = new ASTQueries.NodePred[]
            {
                ASTQueries.NodePredFactory.Instance.Star,
                ASTQueries.NodePredFactory.Instance.MkPredicate(NodeKind.Id) & filter
            };

            if (Root.NodeKind == NodeKind.Id)
            {
                if (Root.Eval(query[1], ChildContextKind.AnyChildContext, 0, 0))
                {
                    var rootPath = new ChildInfo[] { new ChildInfo(Root, ChildContextKind.AnyChildContext, -1, -1) };
                    var kind     = subKind(rootPath);
                    var m        = sub(rootPath);
                    return(m.NodeKind != kind ? this : Factory.Instance.ToAST(m));
                }
                else
                {
                    return(this);
                }
            }

            AST <Node> crntAst = Factory.Instance.ToAST(Root);

            this.FindAll(
                query,
                (pt, x) =>
            {
                var list  = (LinkedList <ChildInfo>)pt;
                var prev  = list.Last.Previous;
                var rkind = subKind(pt);
                if (!ASTQueries.ASTSchema.Instance.CanReplace(
                        prev.Value.Node,
                        list.Last.Value.Node,
                        list.Last.Value.Context,
                        rkind))
                {
                    return;
                }

                var rep = sub(pt);
                if (rep.NodeKind != rkind)
                {
                    return;
                }

                var newAST  = crntAst == this ? this : Factory.Instance.FromAbsPositions(crntAst.Root, pt);
                var crnt    = ((LinkedList <ChildInfo>)newAST.Path).Last;
                var subPath = new LinkedList <ChildInfo>();
                Node n      = null, p = null;
                int pos     = -1;
                while (crnt != null)
                {
                    n   = pos == -1 ? rep : crnt.Value.Node.ShallowClone(p, pos);
                    pos = crnt.Value.AbsolutePos;
                    p   = n;

                    subPath.AddFirst(new ChildInfo(n, crnt.Value.Context, pos, crnt.Value.RelativePos));
                    crnt = crnt.Previous;
                }

                crntAst = Factory.Instance.ToAST(n);
            },
                cancel);

            crntAst.GetHashCode();
            return(crntAst);
        }