public void Apply(AstNode rootNode)
 {
     if (rootNode == null)
     {
         throw new ArgumentNullException("rootNode");
     }
     rootNode.AcceptVisitor(new GenerateCodeVisitior(this));
 }
Exemple #2
0
        /// <summary>
        /// Expands all occurances of query patterns in the specified node. Returns a clone of the node with all query patterns expanded, or null if there was no query pattern to expand.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public QueryExpressionExpansionResult ExpandQueryExpressions(AstNode node)
        {
            var visitor = new Visitor();
            var astNode = node.AcceptVisitor(visitor);

            if (astNode != null)
            {
                astNode.Freeze();
                return(new QueryExpressionExpansionResult(astNode, visitor.rangeVariables, visitor.expressions));
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Applies the <paramref name="visitor"/> to all nodes in this collection.
        /// </summary>
        public void AcceptVisitor(IAstVisitor visitor)
        {
            AstNode next;

            for (AstNode cur = node.FirstChild; cur != null; cur = next)
            {
                Debug.Assert(cur.Parent == node);
                // Remember next before yielding cur.
                // This allows removing/replacing nodes while iterating through the list.
                next = cur.NextSibling;
                if (cur.Role == role)
                {
                    cur.AcceptVisitor(visitor);
                }
            }
        }
        void FixEmbeddedStatment(BraceStyle braceStyle, CSharpTokenNode token, bool allowInLine, AstNode node, bool statementAlreadyIndented = false)
        {
            if (node == null)
            {
                return;
            }
            bool isBlock = node is BlockStatement;

            FormattingChanges.TextReplaceAction beginBraceAction = null;
            FormattingChanges.TextReplaceAction endBraceAction   = null;
            BlockStatement closeBlockToBeFixed = null;

            if (isBlock)
            {
                BlockStatement block = node as BlockStatement;
                if (allowInLine && block.StartLocation.Line == block.EndLocation.Line && block.Statements.Count() <= 1)
                {
                    if (block.Statements.Count() == 1)
                    {
                        nextStatementIndent = " ";
                    }
                }
                else
                {
                    if (!statementAlreadyIndented)
                    {
                        FixOpenBrace(braceStyle, block.LBraceToken);
                    }
                    closeBlockToBeFixed = block;
                }

                if (braceStyle == BraceStyle.NextLineShifted2)
                {
                    curIndent.Push(IndentType.Block);
                }
            }
            else
            {
                if (allowInLine && token.StartLocation.Line == node.EndLocation.Line)
                {
                    nextStatementIndent = " ";
                }
            }
            bool pushed = false;

            if (policy.IndentBlocks && !(policy.AlignEmbeddedIfStatements && node is IfElseStatement && node.Parent is IfElseStatement || policy.AlignEmbeddedUsingStatements && node is UsingStatement && node.Parent is UsingStatement))
            {
                curIndent.Push(IndentType.Block);
                pushed = true;
            }
            if (isBlock)
            {
                VisitBlockWithoutFixingBraces((BlockStatement)node, false);
            }
            else
            {
                if (!statementAlreadyIndented)
                {
                    FixStatementIndentation(node.StartLocation);
                }
                node.AcceptVisitor(this);
            }
            if (pushed)
            {
                curIndent.Pop();
            }
            if (beginBraceAction != null && endBraceAction != null)
            {
                beginBraceAction.DependsOn = endBraceAction;
                endBraceAction.DependsOn   = beginBraceAction;
            }

            if (isBlock && braceStyle == BraceStyle.NextLineShifted2)
            {
                curIndent.Pop();
            }
            if (closeBlockToBeFixed != null)
            {
                FixClosingBrace(braceStyle, closeBlockToBeFixed.RBraceToken);
            }
        }