public AstNode GetNodeAt(AstLocation location, Predicate <AstNode> pred = null)
        {
            AstNode result = null;
            AstNode node   = this;

            while (node.FirstChild != null)
            {
                var child = node.FirstChild;
                while (child != null)
                {
                    if (child.StartLocation <= location && location < child.EndLocation)
                    {
                        if (pred == null || pred(child))
                        {
                            result = child;
                        }
                        node = child;
                        break;
                    }
                    child = child.NextSibling;
                }
                // found no better child node - therefore the parent is the right one.
                if (child == null)
                {
                    break;
                }
            }
            return(result);
        }
        public IEnumerable <AstNode> GetNodesBetween(AstLocation start, AstLocation end)
        {
            AstNode node = this;

            while (node != null)
            {
                AstNode next;
                if (start <= node.StartLocation && node.EndLocation <= end)
                {
                    // Remember next before yielding node.
                    // This allows iteration to continue when the caller removes/replaces the node.
                    next = node.NextSibling;
                    yield return(node);
                }
                else
                {
                    if (node.EndLocation <= start)
                    {
                        next = node.NextSibling;
                    }
                    else
                    {
                        next = node.FirstChild;
                    }
                }

                if (next != null && next.StartLocation > end)
                {
                    yield break;
                }
                node = next;
            }
        }
        /// <summary>
        /// Gets the node specified by T at location. This is useful for getting a specific node from the tree. For example searching
        /// the current method declaration.
        /// </summary>
        public T GetNodeAt <T> (AstLocation location) where T : AstNode
        {
            T       result = null;
            AstNode node   = this;

            while (node.FirstChild != null)
            {
                var child = node.FirstChild;
                while (child != null)
                {
                    if (child.StartLocation <= location && location < child.EndLocation)
                    {
                        if (child is T)
                        {
                            result = (T)child;
                        }
                        node = child;
                        break;
                    }
                    child = child.NextSibling;
                }
                // found no better child node - therefore the parent is the right one.
                if (child == null)
                {
                    break;
                }
            }
            return(result);
        }
 protected Identifier(string name, AstLocation location)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     this.Name          = name;
     this.startLocation = location;
 }
        /// <summary>
        /// Gets a node that can be resolved at location.
        /// </summary>
        public AstNode GetResolveableNodeAt(AstLocation location)
        {
            return(GetNodeAt(location, delegate(AstNode n) {
                if (n is TypeDeclaration)
                {
                    var decl = (TypeDeclaration)n;
                    return decl.NameToken.StartLocation <= location && location <= decl.NameToken.EndLocation;
                }

                if (n is DelegateDeclaration)
                {
                    var decl = (DelegateDeclaration)n;
                    return decl.NameToken.StartLocation <= location && location <= decl.NameToken.EndLocation;
                }

                if (n is MemberDeclaration)
                {
                    var decl = (MemberDeclaration)n;
                    return decl.NameToken.StartLocation <= location && location <= decl.NameToken.EndLocation;
                }

                if (n is ConstructorDeclaration)
                {
                    var decl = (ConstructorDeclaration)n;
                    return decl.IdentifierToken.StartLocation <= location && location <= decl.IdentifierToken.EndLocation;
                }

                if (n is DestructorDeclaration)
                {
                    var decl = (DestructorDeclaration)n;
                    return decl.IdentifierToken.StartLocation <= location && location <= decl.IdentifierToken.EndLocation;
                }

                if (n is VariableInitializer)
                {
                    var decl = (VariableInitializer)n;
                    return decl.NameToken.StartLocation <= location && location <= decl.NameToken.EndLocation;
                }

                if (n is ParameterDeclaration)
                {
                    var decl = (ParameterDeclaration)n;
                    return decl.NameToken.StartLocation <= location && location <= decl.NameToken.EndLocation;
                }

                if (n is MemberReferenceExpression)
                {
                    var decl = (MemberReferenceExpression)n;
                    return decl.MemberNameToken.StartLocation <= location && location <= decl.MemberNameToken.EndLocation;
                }

                return n is IdentifierExpression || n is AstType;
            }));
        }
 public static Identifier Create(string name, AstLocation location)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (name.Length > 0 && name[0] == '@')
     {
         return(new VerbatimIdentifier(name.Substring(1), location));
     }
     return(new Identifier(name, location));
 }
        public static Identifier Create(string name, AstLocation location, bool isVerbatim)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (isVerbatim)
            {
                return(new VerbatimIdentifier(name, location));
            }
            return(new Identifier(name, location));
        }
 public bool Contains(AstLocation location)
 {
     return(this.StartLocation <= location && location < this.EndLocation);
 }
Exemplo n.º 9
0
 public Comment(CommentType commentType, AstLocation startLocation, AstLocation endLocation)
 {
     this.CommentType   = commentType;
     this.startLocation = startLocation;
     this.endLocation   = endLocation;
 }
 public VerbatimIdentifier(string name, AstLocation location) : base(name, location)
 {
 }
Exemplo n.º 11
0
 public CSharpTokenNode(AstLocation location, int tokenLength)
 {
     this.startLocation = location;
     this.tokenLength   = tokenLength;
 }
 public CSharpModifierToken(AstLocation location, Modifiers modifier) : base(location, 0)
 {
     this.Modifier = modifier;
 }
 public PrimitiveType(string keyword, AstLocation location)
 {
     this.Keyword  = keyword;
     this.Location = location;
 }
 public SimpleType(string identifier, AstLocation location)
 {
     SetChildByRole(Roles.Identifier, CSharp.Ast.Identifier.Create (identifier, location));
 }