Exemple #1
0
        internal NodePredAtom Conjoin(NodePredAtom a)
        {
            if (PredicateKind == NodePredicateKind.False ||
                a.PredicateKind == NodePredicateKind.False)
            {
                return(NodePredFactory.Instance.False);
            }

            if (TargetKind != NodeKind.AnyNodeKind &&
                a.TargetKind != NodeKind.AnyNodeKind &&
                TargetKind != a.TargetKind)
            {
                return(NodePredFactory.Instance.False);
            }

            if (ChildContext != ChildContextKind.AnyChildContext &&
                a.ChildContext != ChildContextKind.AnyChildContext &&
                ChildContext != a.ChildContext)
            {
                return(NodePredFactory.Instance.False);
            }

            if (ChildIndexLower < a.ChildIndexLower &&
                ChildIndexUpper < a.ChildIndexUpper)
            {
                return(NodePredFactory.Instance.False);
            }

            if (a.ChildIndexLower < ChildIndexLower &&
                a.ChildIndexUpper < ChildIndexUpper)
            {
                return(NodePredFactory.Instance.False);
            }

            Func <AttributeKind, object, bool> newAP;

            if (AttributePredicate != null && a.AttributePredicate != null)
            {
                var ap1 = AttributePredicate;
                var ap2 = a.AttributePredicate;
                newAP = (at, obj) => ap1(at, obj) && ap2(at, obj);
            }
            else
            {
                newAP = AttributePredicate == null ? a.AttributePredicate : AttributePredicate;
            }

            return(new NodePredAtom(
                       TargetKind == NodeKind.AnyNodeKind ? a.TargetKind : TargetKind,
                       ChildContext == ChildContextKind.AnyChildContext ? a.ChildContext : ChildContext,
                       Math.Max(ChildIndexLower, a.ChildIndexLower),
                       Math.Min(ChildIndexUpper, a.ChildIndexUpper),
                       newAP));
        }
Exemple #2
0
        public NodePredFactory()
        {
            Star = new NodePredStar();

            True = new NodePredAtom(NodeKind.AnyNodeKind, ChildContextKind.AnyChildContext, -1, int.MaxValue, null);

            False = new NodePredFalse();

            Module = MkPredicate(NodeKind.Model) |
                     MkPredicate(NodeKind.Domain) |
                     MkPredicate(NodeKind.Transform) |
                     MkPredicate(NodeKind.TSystem) |
                     MkPredicate(NodeKind.Machine);

            TypeDecl = MkPredicate(NodeKind.UnnDecl) |
                       MkPredicate(NodeKind.ConDecl) |
                       MkPredicate(NodeKind.MapDecl);
        }
Exemple #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);
        }