本文整理汇总了C#中Block.Insert方法的典型用法代码示例。如果您正苦于以下问题:C# Block.Insert方法的具体用法?C# Block.Insert怎么用?C# Block.Insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Block
的用法示例。
在下文中一共展示了Block.Insert方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RelocateVar
//.........这里部分代码省略.........
{
// create a vardecl with the same name and no initializer
var copyDecl = new VariableDeclaration(
varDecl.Context,
varDecl.Parser,
varDecl.Identifier,
varDecl.Field.OriginalContext,
null,
0,
true);
// replace the special vardecl with the copy
varStatement.ReplaceChild(varDecl, copyDecl);
// add the original vardecl to the list of "assignments"
assignments.Add(varDecl);
}
else
{
// hold on to the object so we don't lose it to the GC
var initializer = varDecl.Initializer;
// remove it from the vardecl
varDecl.ReplaceChild(initializer, null);
// create an assignment operator for a lookup to the name
// as the left, and the initializer as the right, and add it to the list
assignments.Add(new BinaryOperator(
varDecl.Context,
varDecl.Parser,
new Lookup(varDecl.Identifier, varDecl.Field.OriginalContext, varDecl.Parser),
initializer,
JSToken.Assign));
}
}
}
// now if there were any initializers...
if (assignments.Count > 0)
{
// we want to create one big expression from all the assignments and replace the
// var statement with the assignment(s) expression. Start at position n=1 and create
// a binary operator of n-1 as the left, n as the right, and using a comma operator.
var expression = assignments[0];
for (var ndx = 1; ndx < assignments.Count; ++ndx)
{
expression = new BinaryOperator(
null,
expression.Parser,
expression,
assignments[ndx],
JSToken.Comma);
}
// replace the var with the expression.
// we still have a pointer to the var, so we can insert it back into the proper
// place next.
varStatement.Parent.ReplaceChild(varStatement, expression);
}
else
{
// no initializers.
// if the parent is a for-in statement...
var forInParent = varStatement.Parent as ForIn;
if (forInParent != null)
{
// we want to replace the var statement with a lookup for the var
// there should be only one vardecl
var varDecl = varStatement[0];
varStatement.Parent.ReplaceChild(
varStatement,
new Lookup(varDecl.Identifier, varDecl.Field.OriginalContext, varStatement.Parser));
}
else
{
// just remove the var statement altogether
varStatement.Parent.ReplaceChild(varStatement, null);
}
}
// if the statement at the insertion point is a var-statement already,
// then we just need to append our vardecls to it. Otherwise we'll insert our
// var statement at the right point
if (existingVar != null)
{
// append the varstatement we want to move to the existing var, which will
// transfer all the vardecls to it.
existingVar.Append(varStatement);
}
else
{
// move the var to the insert point, incrementing the position or next time
block.Insert(insertAt, varStatement);
}
}
}
}
return insertAt;
}
示例2: RelocateFunction
private static int RelocateFunction(Block block, int insertAt, FunctionObject funcDecl)
{
if (block[insertAt] != funcDecl)
{
// technically function declarations can only be direct children of the program or a function block.
// and since we are passing in such a block, the parent of the function declaration better be that
// block. If it isn't, we don't want to move it because it's not in an allowed place, and different
// browsers treat that situation differently. Some browsers would process such funcdecls as if
// they were a direct child of the main block. Others will treat it like a function expression with
// an external name, and only assign the function to the name if that line of code is actually
// executed. So since there's a difference, just leave them as-is and only move valid funcdecls.
if (funcDecl.Parent == block && !InsideConditionalComment(funcDecl))
{
// remove the function from it's parent, which will take it away from where it is right now.
funcDecl.Parent.ReplaceChild(funcDecl, null);
// now insert it into the block at the new location, incrementing the location so the next function
// will be inserted after it. It is important that they be in the same order as the source, or the semantics
// will change when there are functions with the same name.
block.Insert(insertAt++, funcDecl);
}
}
else
{
// we're already in the right place. Just increment the pointer to move to the next position
// for next time
++insertAt;
}
// return the new position
return insertAt;
}