public static ISyntaxToken GetLastToken(SyntaxNodeBase node, bool includeZeroLength, bool includeSkippedTokens)
        {
            var tokenPredicate  = GetTokenPredicate(includeZeroLength);
            var triviaPredicate = GetTriviaPredicate(includeSkippedTokens);

            return(GetLastToken(node, tokenPredicate, triviaPredicate));
        }
        private static SyntaxTreeBase ComputeSyntaxTree(SyntaxNodeBase node)
        {
            ArrayBuilder <SyntaxNodeBase> nodes = null;
            SyntaxTreeBase tree = null;

            // Find the nearest parent with a non-null syntax tree
            while (true)
            {
                tree = node._syntaxTree;
                if (tree != null)
                {
                    break;
                }

                var parent = node.Parent;
                if (parent == null)
                {
                    // root node: unexpected, root node should have a tree.
                    //Interlocked.CompareExchange(ref node._syntaxTree, CSharpSyntaxTree.CreateWithoutClone(node), null);
                    //tree = node._syntaxTree;
                    break;
                }

                tree = parent._syntaxTree;
                if (tree != null)
                {
                    node._syntaxTree = tree;
                    break;
                }

                (nodes ?? (nodes = ArrayBuilder <SyntaxNodeBase> .GetInstance())).Add(node);
                node = parent;
            }

            // Propagate the syntax tree downwards if necessary
            if (nodes != null)
            {
                Debug.Assert(tree != null);

                foreach (var n in nodes)
                {
                    var existingTree = n._syntaxTree;
                    if (existingTree != null)
                    {
                        Debug.Assert(existingTree == tree, "how could this node belong to a different tree?");

                        // yield the race
                        break;
                    }
                    n._syntaxTree = tree;
                }

                nodes.Free();
            }

            return(tree);
        }
        private static ISyntaxToken GetPreviousToken(SyntaxNodeBase node, Func <ISyntaxToken, bool> tokenPredicate, Func <SyntaxNodeBase, bool> triviaPredicate)
        {
            if (node.Parent != null && !node.IsStructuredTrivia)
            {
                var returnNext = false;

                foreach (var nodeOrToken in node.Parent.ChildNodes.Reverse())
                {
                    if (returnNext)
                    {
                        if (nodeOrToken.IsToken)
                        {
                            var t = GetLastToken((ISyntaxToken)nodeOrToken, tokenPredicate, triviaPredicate);
                            if (t != null)
                            {
                                return(t);
                            }
                        }
                        else
                        {
                            var t = GetLastToken(nodeOrToken, tokenPredicate, triviaPredicate);
                            if (t != null)
                            {
                                return(t);
                            }
                        }
                    }

                    if (!nodeOrToken.IsToken && nodeOrToken == node)
                    {
                        returnNext = true;
                    }
                }

                return(GetPreviousToken(node.Parent, tokenPredicate, triviaPredicate));
            }

            if (!node.IsStructuredTrivia)
            {
                return(null);
            }

            var trivia = node.Parent as ISyntaxToken;

            return(trivia == null
                ? null
                : GetPreviousTokenFromTrivia(trivia, tokenPredicate, triviaPredicate));
        }
        public static IEnumerable <SyntaxNodeBase> DescendantNodesAndSelf(this SyntaxNodeBase root)
        {
            var stack = new Stack <SyntaxNodeBase>();

            stack.Push(root);

            while (stack.Count > 0)
            {
                var current = stack.Pop();
                yield return(current);

                foreach (var child in current.ChildNodes.Reverse())
                {
                    stack.Push(child);
                }
            }
        }
        private static ISyntaxToken GetNextToken(SyntaxNodeBase node, Func <ISyntaxToken, bool> tokenPredicate, Func <SyntaxNodeBase, bool> triviaPredicate)
        {
            if (node.Parent != null)
            {
                var returnNext = false;

                foreach (var nodeOrToken in node.Parent.ChildNodes)
                {
                    if (returnNext)
                    {
                        if (nodeOrToken.IsToken)
                        {
                            var t = GetFirstToken((ISyntaxToken)nodeOrToken, tokenPredicate, triviaPredicate);
                            if (t != null)
                            {
                                return(t);
                            }
                        }
                        else
                        {
                            var t = GetFirstToken(nodeOrToken, tokenPredicate, triviaPredicate);
                            if (t != null)
                            {
                                return(t);
                            }
                        }
                    }

                    if (!nodeOrToken.IsToken && nodeOrToken == node)
                    {
                        returnNext = true;
                    }
                }

                return(GetNextToken(node.Parent, tokenPredicate, triviaPredicate));
            }

            return(null);
        }
        private static ISyntaxToken GetLastToken(SyntaxNodeBase node, Func <ISyntaxToken, bool> tokenPredicate, Func <SyntaxNodeBase, bool> triviaPredicate)
        {
            foreach (var nodeOrToken in node.ChildNodes.Reverse())
            {
                if (nodeOrToken.IsToken)
                {
                    var t = GetLastToken((ISyntaxToken)nodeOrToken, tokenPredicate, triviaPredicate);
                    if (t != null)
                    {
                        return(t);
                    }
                }
                else
                {
                    var t = GetLastToken(nodeOrToken, tokenPredicate, triviaPredicate);
                    if (t != null)
                    {
                        return(t);
                    }
                }
            }

            return(null);
        }
 public static IEnumerable <SyntaxNodeBase> DescendantNodes(this SyntaxNodeBase root)
 {
     return(root.DescendantNodesAndSelf().Skip(1));
 }
 public static ISyntaxToken GetLastToken(this SyntaxNodeBase token, bool includeZeroLength = false, bool includeSkippedTokens = false)
 {
     return(SyntaxNavigator.GetLastToken(token, includeZeroLength, includeSkippedTokens));
 }