当前位置: 首页>>代码示例>>C#>>正文


C# Block.InsertRange方法代码示例

本文整理汇总了C#中Block.InsertRange方法的典型用法代码示例。如果您正苦于以下问题:C# Block.InsertRange方法的具体用法?C# Block.InsertRange怎么用?C# Block.InsertRange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Block的用法示例。


在下文中一共展示了Block.InsertRange方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RemoveUnreachableCode

 public static int RemoveUnreachableCode(Block block, Statement statement, int index)
 {
     if (statement is Statement.If)
     {
         Statement.If _if = (Statement.If)statement;
         int v;
         if (_if.Condition.TryGetIntValue(out v))
         {
             if (v == 0)
             {
                 block.RemoveAt(index);
                 block.InsertRange(index, _if.ElseDoThis);
                 return index;
             }
             else
             {
                 block.RemoveAt(index);
                 block.InsertRange(index, _if.ThenDoThis);
                 return index;
             }
         }
     }
     else if (statement is Statement.While)
     {
         Statement.While loop = (Statement.While)statement;
         int v;
         if (loop.WhileThisIsTrue.TryGetIntValue(out v) && v == 0)
         {
             block.RemoveAt(index);
             return index;
         }
     }
     else if (statement is Statement.Return)
     {
         block.RemoveRange(index + 1, block.Count - (index + 1));
     }
     return index + 1;
 }
开发者ID:duncanc,项目名称:Red-Herring-Farm,代码行数:38,代码来源:CodeFilter.cs

示例2: UnnestBlocks

        // unnest any child blocks
        private void UnnestBlocks(Block node)
        {
            // before we unnest the blocks or do anything else, let's identify any directive prologues.
            // if the parent is null (we are the program) or a function object...
            if (node.Parent == null || node.Parent is FunctionObject)
            {
                // directive prologues are the first n statements of a program or function body that
                // are expression statements consisting entirely of a string literal. So let's walk from
                // the beginning until we find the first statement that is not a ConstantWrapper of primitive
                // type string. Mark the ones we find as directive prologues
                for (var ndx = 0; ndx < node.Count; ++ndx)
                {
                    var constWrapper = node[ndx] as ConstantWrapper;
                    if (constWrapper == null || constWrapper.PrimitiveType != PrimitiveType.String)
                    {
                        // not a constant, not a string -- we are done searching!
                        break;
                    }

                    // otherwise it is a directive prologue. Mark it as such
                    constWrapper.IsDirectivePrologue = true;

                    // if it's a "use strict" prologue, let's mark the appropriate scope as strict
                    if (string.CompareOrdinal(constWrapper.ToString(), "use strict") == 0)
                    {
                        // the actual code cannot contain any escape sequences or line-contunations,
                        // so check the context code to make sure it ALSO is the right text. Be sure
                        // not to compare with the quote delimiters.
                        if (constWrapper.Context == null
                            || string.CompareOrdinal(constWrapper.Context.Code, 1, "use strict", 0, 10) == 0)
                        {
                            var funcObject = node.Parent as FunctionObject;
                            if (funcObject != null)
                            {
                                // function scope
                                funcObject.FunctionScope.UseStrict = true;
                            }
                            else
                            {
                                // global scope
                                node.Parser.GlobalScope.UseStrict = true;
                            }
                        }
                    }
                }
            }

            // walk the list of items backwards -- if we come
            // to any blocks, unnest the block recursively. We walk backwards because
            // we could be adding any number of statements and we don't want to have
            // to modify the counter
            for (int ndx = node.Count - 1; ndx >= 0; --ndx)
            {
                var nestedBlock = node[ndx] as Block;
                if (nestedBlock != null)
                {
                    // unnest recursively
                    UnnestBlocks(nestedBlock);

                    // remove the nested block
                    node.RemoveAt(ndx);

                    // then start adding the statements in the nested block to our own.
                    // go backwards so we can just keep using the same index
                    node.InsertRange(ndx, nestedBlock.Children);
                }
                else if (ndx > 0)
                {
                    // see if the previous node is a conditional-compilation comment, because
                    // we will also combine adjacent those
                    var previousComment = node[ndx - 1] as ConditionalCompilationComment;
                    if (previousComment != null)
                    {
                        ConditionalCompilationComment thisComment = node[ndx] as ConditionalCompilationComment;
                        if (thisComment != null)
                        {
                            // two adjacent conditional comments -- combine them into the first.
                            previousComment.Statements.Append(thisComment.Statements);

                            // and remove the second one (which is now a duplicate)
                            node.RemoveAt(ndx);
                        }
                    }
                }
            }
        }
开发者ID:nuxleus,项目名称:ajaxmin,代码行数:87,代码来源:ReorderScopeVisitor.cs

示例3: Visit

        public override void Visit(Block node)
        {
            if (node != null)
            {
                // we might things differently if these statements are the body collection for a function
                // because we can assume the implicit return statement at the end of it
                bool isFunctionLevel = (node.Parent is FunctionObject);

                // if we want to remove debug statements...
                if (m_parser.Settings.StripDebugStatements && m_parser.Settings.IsModificationAllowed(TreeModifications.StripDebugStatements))
                {
                    // do it now before we try doing other things
                    StripDebugStatements(node);
                }

                // analyze all the statements in our block and recurse them
                if (node.BlockScope != null)
                {
                    ScopeStack.Push(node.BlockScope);
                }
                try
                {
                    // call the base class to recurse
                    base.Visit(node);
                }
                finally
                {
                    if (node.BlockScope != null)
                    {
                        ScopeStack.Pop();
                    }
                }

                if (m_parser.Settings.RemoveUnneededCode)
                {
                    // go forward, and check the count each iteration because we might be ADDING statements to the block.
                    // let's look at all our if-statements. If a true-clause ends in a return, then we don't
                    // need the else-clause; we can pull its statements out and stick them after the if-statement.
                    // also, if we encounter a return-, break- or continue-statement, we can axe everything after it
                    for (var ndx = 0; ndx < node.Count; ++ndx)
                    {
                        // see if it's an if-statement with both a true and a false block
                        var ifNode = node[ndx] as IfNode;
                        if (ifNode != null
                            && ifNode.TrueBlock != null
                            && ifNode.TrueBlock.Count > 0
                            && ifNode.FalseBlock != null)
                        {
                            // now check to see if the true block ends in a return statement
                            if (ifNode.TrueBlock[ifNode.TrueBlock.Count - 1] is ReturnNode)
                            {
                                // transform: if(cond){statements1;return}else{statements2} to if(cond){statements1;return}statements2
                                // it does. insert all the false-block statements after the if-statement
                                node.InsertRange(ndx + 1, ifNode.FalseBlock.Children);

                                // and then remove the false block altogether
                                ifNode.ReplaceChild(ifNode.FalseBlock, null);
                            }
                        }
                        else if (node[ndx] is ReturnNode
                            || node[ndx] is Break
                            || node[ndx] is ContinueNode)
                        {
                            // we have a return node -- no statments afterwards will be executed, so clear them out.
                            // transform: {...;return;...} to {...;return}
                            // transform: {...;break;...} to {...;break}
                            // transform: {...;continue;...} to {...;continue}
                            // we've found a return statement, and it's not the last statement in the function.
                            // walk the rest of the statements and delete anything that isn't a function declaration
                            // or a var statement.
                            for (var ndxRemove = node.Count - 1; ndxRemove > ndx; --ndxRemove)
                            {
                                var funcObject = node[ndxRemove] as FunctionObject;
                                if (funcObject == null || funcObject.FunctionType != FunctionType.Declaration)
                                {
                                    var varStatement = node[ndxRemove] as Var;
                                    if (varStatement != null)
                                    {
                                        // var statements can't be removed, but any initializers should
                                        // be deleted since they won't get executed.
                                        for (var ndxDecl = 0; ndxDecl < varStatement.Count; ++ndxDecl)
                                        {
                                            if (varStatement[ndxDecl].Initializer != null)
                                            {
                                                varStatement[ndxDecl].ReplaceChild(varStatement[ndxDecl].Initializer, null);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // not a function declaration, and not a var statement -- get rid of it
                                        node.RemoveAt(ndxRemove);
                                    }
                                }
                            }
                        }
                    }
                }

                // now check the last statement -- if it's an if-statement where the true-block is a single return
//.........这里部分代码省略.........
开发者ID:nuxleus,项目名称:ajaxmin,代码行数:101,代码来源:AnalyzeNodeVisitor.cs


注:本文中的Block.InsertRange方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。