Esempio n. 1
0
        public override void DefaultVisit(SyntaxNode node)
        {
            var childCnt = node.ChildNodesAndTokens().Count;
            int i        = 0;

            do
            {
                var child = ChildSyntaxList.ItemInternal((CSharpSyntaxNode)node, i);
                i++;

                var asNode = child.AsNode();
                if (asNode != null)
                {
                    if (this.Depth >= SyntaxWalkerDepth.Node)
                    {
                        this.Visit(asNode);
                    }
                }
                else
                {
                    if (this.Depth >= SyntaxWalkerDepth.Token)
                    {
                        this.VisitToken(child.AsToken());
                    }
                }
            } while (i < childCnt);
        }
Esempio n. 2
0
        internal SyntaxToken(CSharpSyntaxNode parent, Syntax.InternalSyntax.SyntaxToken node, int position, int index)
        {
            Debug.Assert(parent == null || parent.Kind != SyntaxKind.List);
            Debug.Assert(node != null || (position == 0 && index == 0 && parent == null));
            Debug.Assert(position >= 0);

            this.position = position;
            this.parent   = parent;
            this.node     = node;
            this.index    = index;

#if DEBUG
            if (parent != null && node != null)
            {
                var nodeOrToken = ChildSyntaxList.ItemInternal(parent, index, fromTokenCtor: true);

                Debug.Assert(nodeOrToken.UnderlyingNode == node, "node was not found at given index");
                Debug.Assert(nodeOrToken.Position == position, "position mismatch");
            }
            else
            {
                Debug.Assert(parent == null || position >= parent.Position);
            }
#endif
        }
Esempio n. 3
0
        /// <summary>
        /// Returns child node or token that contains given position.
        /// </summary>
        public override SyntaxNodeOrToken ChildThatContainsPosition(int position)
        {
            //PERF: it is very important to keep this method fast.
            //      if there are ways to make it faster through some use of green nodes and such -
            //      it would be a welcome change.
            var childList = this.ChildNodesAndTokens();

            int left  = 0;
            int right = childList.Count - 1;

            while (left <= right)
            {
                int middle             = left + ((right - left) / 2);
                SyntaxNodeOrToken node = ChildSyntaxList.ItemInternal(childList.Node, middle);

                if (position < node.Position)
                {
                    right = middle - 1;
                }
                else
                {
                    if (position >= node.EndPosition)
                    {
                        left = middle + 1;
                        continue;
                    }

                    return(node);
                }
            }

            // we could check up front that index is within FullSpan,
            // but we want to optimize for the common case where position is valid.
            Debug.Assert(!FullSpan.Contains(position), "Position is valid. How could we not find a child?");
            throw new ArgumentOutOfRangeException("position");
        }