本文整理汇总了C#中Scope.GetDocument方法的典型用法代码示例。如果您正苦于以下问题:C# Scope.GetDocument方法的具体用法?C# Scope.GetDocument怎么用?C# Scope.GetDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Scope
的用法示例。
在下文中一共展示了Scope.GetDocument方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessAsynch
private static SyntaxNode ProcessAsynch(SyntaxNode node, Scope scope, SyntacticalExtension<SyntaxNode> extension)
{
if (extension.Kind == ExtensionKind.Code)
{
var result = AsynchTemplate
.ReplaceNodes(AsynchTemplate
.DescendantNodes()
.OfType<BlockSyntax>(),
(oldNode, newNode) => extension.Body);
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
document.change(node.Parent, RoslynCompiler.AddStatement(ContextVariable, before: node));
return result;
}
scope.AddError("asynch01", "asynch does not return a value", node);
return node;
}
示例2: FixMissingType
private static void FixMissingType(SyntaxNode node, Scope scope)
{
var type = node
.Ancestors()
.OfType<TypeDeclarationSyntax>()
.FirstOrDefault();
if (type != null)
{
var typeScope = scope.GetScope<SyntaxToken, SyntaxNode, SemanticModel>(type);
if (typeScope != null)
{
SyntaxNode realType = typeScope.get<SyntaxNode>("__tdef" + node.ToString());
if (realType != null)
{
realType = RoslynCompiler.Mark(realType); //make sure not to duplicate nodes
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
document.change(node, RoslynCompiler.ReplaceNode(realType));
}
}
}
}
示例3: Transform
private static SyntaxNode Transform(SyntaxNode oldNode, SyntaxNode newNode, Scope scope, LexicalExtension<SyntaxToken> extension)
{
Debug.Assert(newNode is BlockSyntax);
var isAssignment = oldNode is LocalDeclarationStatementSyntax;
if (!isAssignment && oldNode is BinaryExpressionSyntax)
{
var expr = oldNode as BinaryExpressionSyntax;
isAssignment = expr.Kind() == SyntaxKind.SimpleAssignmentExpression;
}
if (isAssignment)
{
scope.AddError("r01", "R does not return", oldNode);
return newNode;
}
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
document.change(oldNode.Parent, RoslynCompiler.ExplodeBlock(newNode));
return newNode;
}
示例4: Method
private static SyntaxNode Method(SyntaxNode node, Scope scope)
{
if (!(node is MethodDeclarationSyntax))
{
//td: error
return node;
}
var method = node as MethodDeclarationSyntax;
if (!RoslynCompiler.HasVisibilityModifier(method.Modifiers))
method = method.AddModifiers(CSharp.Token(SyntaxKind.PublicKeyword));
if (method.ReturnType.IsMissing)
{
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
document.change(method, FixReturnType);
return method.WithReturnType([email protected]);
}
return method;
}
示例5: Property
private static SyntaxNode Property(SyntaxNode node, Scope scope)
{
var field = node.AncestorsAndSelf()
.OfType<MemberDeclarationSyntax>()
.FirstOrDefault()
as FieldDeclarationSyntax;
if (field == null)
{
//td: error, malformed property
return node;
}
if (field.Declaration.Variables.Count != 1)
{
//td: error, malformed property
return node;
}
var variable = field
.Declaration
.Variables[0];
var initializer = variable.Initializer;
var type = field.Declaration.Type;
if (type == null || type.IsMissing || type.ToString() == "property") //untyped
{
if (initializer != null)
type = RoslynCompiler.ConstantType(initializer.Value);
}
if (type == null)
type = [email protected];
var property = _property
.WithIdentifier(variable.Identifier)
.WithType(type);
if (!RoslynCompiler.HasVisibilityModifier(field.Modifiers))
property = property.AddModifiers(CSharp.Token(SyntaxKind.PublicKeyword));
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
//schedule the field replacement
//td: coud be done in this pass with the right info from lexical
document.change(field, RoslynCompiler.ReplaceNode(property));
//must be initialized
if (initializer != null)
{
var expr = (AssignmentExpressionSyntax)_assignment.Expression;
document.change(field.Parent, RoslynCompiler
.AddInitializers(_assignment.WithExpression(expr
.WithLeft(CSharp.IdentifierName(variable.Identifier))
.WithRight(initializer.Value))));
}
return node;
}
示例6: Typedef
private static SyntaxNode Typedef(SyntaxNode node, Scope scope)
{
var field = node
.AncestorsAndSelf()
.OfType<FieldDeclarationSyntax>()
.FirstOrDefault();
if (field == null)
{
scope.AddError("xs01", "malformed typedef", node);
//td: error, malformed typedef
return node;
}
if (field.Declaration.Variables.Count != 1)
{
scope.AddError("xs01", "malformed typedef", node);
return node;
}
var variable = field
.Declaration
.Variables[0];
Debug.Assert(variable.Initializer == null || variable.Initializer.IsMissing);
var type = RoslynCompiler.UnMark(field.Declaration.Type);
var identifier = variable.Identifier;
var parentScope = scope.CreateScope<SyntaxToken, SyntaxNode, SemanticModel>(field.Parent);
Debug.Assert(parentScope != null);
parentScope.set("__tdef" + identifier.ToString(), type);
//schedule deletion
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
document.change(field.Parent, RoslynCompiler.RemoveMember(field));
//return intact
return node;
}
示例7: TypedefAssignment
private static IEnumerable<SyntaxToken> TypedefAssignment(IEnumerable<SyntaxToken> tokens, Scope scope)
{
dynamic context = scope;
SyntaxToken identifier = context.id;
bool found = false;
foreach (var token in tokens)
{
if (found)
{
if (token.Kind() == SyntaxKind.SemicolonToken)
break;
yield return token;
}
else
found = token.Kind() == SyntaxKind.EqualsToken;
}
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
yield return identifier.WithLeadingTrivia(CSharp.ParseLeadingTrivia(" "));
yield return document.change(CSharp.Token(SyntaxKind.SemicolonToken), Typedef);
}
示例8: FunctionType
private static void FunctionType(SyntaxNode node, SemanticModel model, Scope scope)
{
TypeSyntax newType = null;
if (node is GenericNameSyntax)
{
var generic = node as GenericNameSyntax;
if (generic.Identifier.ToString() == "function")
{
List<TypeSyntax> arguments = new List<TypeSyntax>();
TypeSyntax returnType = null;
bool first = true;
foreach (var arg in generic.TypeArgumentList.Arguments)
{
if (first)
{
first = false;
if (arg.ToString() != "void")
returnType = arg;
}
else
arguments.Add(arg);
}
if (returnType == null)
newType = generic
.WithIdentifier(CSharp.Identifier("Action"))
.WithTypeArgumentList(CSharp.TypeArgumentList(CSharp.SeparatedList(
arguments)));
else
newType = generic
.WithIdentifier(CSharp.Identifier("Func"))
.WithTypeArgumentList(CSharp.TypeArgumentList(CSharp.SeparatedList(
arguments
.Union(new[] { returnType}))));
}
}
else if (node.ToString() == "function")
{
newType = CSharp.ParseTypeName("Action");
}
if (newType != null)
{
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
document.change(node, RoslynCompiler.ReplaceNode(newType));
}
}
示例9: ProcessMemberFunction
private static SyntaxNode ProcessMemberFunction(SyntaxNode node, Scope scope)
{
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
if (node is MethodDeclarationSyntax)
{
var method = node as MethodDeclarationSyntax;
if (method.ReturnType.IsMissing)
{
document.change(method, ReturnType);
return method.WithReturnType([email protected]);
}
return node;
}
//handle functions declared inside code blocks
var statement = node
.AncestorsAndSelf()
.OfType<StatementSyntax>()
.FirstOrDefault();
Debug.Assert(statement != null); //td: error, maybe?
Debug.Assert(statement is ExpressionStatementSyntax);
var invocation = (statement as ExpressionStatementSyntax)
.Expression as InvocationExpressionSyntax;
Debug.Assert(invocation != null);
var function = invocation.Expression as IdentifierNameSyntax;
Debug.Assert(function != null);
BlockSyntax parent = statement.Parent as BlockSyntax;
Debug.Assert(parent != null); //td: error, maybe?
var body = RoslynCompiler.NextStatement(parent, statement) as BlockSyntax;
if (body == null)
{
//td: error, function declaration must be followed by a block of code
return node;
}
//We are not allowed to modify parents, so schedule the removal of the code
//And its insertion in the final lambda variable
document.change(parent, RoslynCompiler.RemoveStatement(body));
document.change(statement, ProcessCodeFunction(function, body));
return node;
}
示例10: EventHandler
private static SyntaxNode EventHandler(SyntaxNode node, Scope scope)
{
MethodDeclarationSyntax method = (MethodDeclarationSyntax)node;
if (method.Modifiers.Any())
{
//td: error, no modifiers allowed
return node;
}
var args = method.ParameterList.ReplaceNodes(method.ParameterList.Parameters, (oldParam, newParam) =>
{
if (oldParam.Identifier.IsMissing)
return newParam
.WithType([email protected])
.WithIdentifier(CSharp.Identifier(newParam.Type.ToString()));
return newParam;
});
var result = method
.WithReturnType([email protected])
.WithParameterList(args)
.WithModifiers([email protected]);
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
return document.change(result, SemanticEventHandler);
}
示例11: SemanticEventHandler
private static SyntaxNode SemanticEventHandler(SyntaxNode oldNode, SyntaxNode node, SemanticModel model, Scope scope)
{
var mthd = (MethodDeclarationSyntax)node;
var methdArgs = mthd.ParameterList;
var methodName = "on_" + mthd.Identifier.ToString();
ISymbol self = model.GetDeclaredSymbol(oldNode);
ITypeSymbol type = (ITypeSymbol)self.ContainingSymbol;
string evName = mthd.Identifier.ToString();
string typeName = type.Name;
bool found = false;
while (type != null && !found)
{
foreach (var ev in type.GetMembers().OfType<IEventSymbol>())
{
if (ev.Name.Equals(evName))
{
//arguments
foreach (var syntax in ev.Type.DeclaringSyntaxReferences)
{
var refNode = (DelegateDeclarationSyntax)syntax.GetSyntax();
int pCount = methdArgs.Parameters.Count;
int idx = 0;
bool match = true;
methdArgs = refNode.ParameterList.ReplaceNodes(refNode.ParameterList.Parameters, (oldArg, newArg) =>
{
if (match)
{
if (idx >= pCount && match)
return newArg;
ParameterSyntax arg = methdArgs.Parameters[idx++];
string argName = arg.Identifier.ToString();
if (argName == oldArg.Identifier.ToString())
{
//coincident parameters, fix missing type or return same
if (arg.Identifier.IsMissing || arg.Type.ToString() == "void")
return newArg.WithIdentifier(arg.Identifier);
return arg;
}
else
{
match = false;
if (!refNode.ParameterList.Parameters.Any(p => p.Identifier.ToString().Equals(arg.Identifier.ToString())))
{
//name change?
if (oldArg.Identifier.IsMissing)
return newArg.WithIdentifier(CSharp.Identifier(arg.Type.ToString()));
return arg;
}
}
}
return newArg;
});
}
//event initialization
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
document.change(mthd.Parent, RoslynCompiler.AddInitializers(EventInitializer(ev.Name, methodName)));
found = true;
break;
}
}
type = type.BaseType;
}
if (!found)
{
//td: error, no such event
}
return mthd.WithIdentifier(CSharp.Identifier(methodName)).
WithParameterList(methdArgs);
}
示例12: LexicalEventDeclaration
private static IEnumerable<SyntaxToken> LexicalEventDeclaration(IEnumerable<SyntaxToken> tokens, Scope scope)
{
dynamic context = scope;
SyntaxToken keyword = context.ev;
SyntaxToken identifier = context.id;
IEnumerable<SyntaxToken> args = context.args;
var document = scope.GetDocument<SyntaxToken, SyntaxNode, SemanticModel>();
yield return document.change(keyword, EventDeclaration(args));
yield return CSharp.Identifier(" useless ");
yield return identifier;
}