本文整理汇总了C#中CSharpSyntaxGenerator.Node类的典型用法代码示例。如果您正苦于以下问题:C# Node类的具体用法?C# Node怎么用?C# Node使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Node类属于CSharpSyntaxGenerator命名空间,在下文中一共展示了Node类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DetermineMinimalFactoryFields
private IEnumerable<Field> DetermineMinimalFactoryFields(Node nd)
{
// special case to allow a single optional argument if there would have been no arguments
// and we can determine a best single argument.
Field allowOptionalField = null;
var optionalCount = OptionalFactoryArgumentCount(nd);
if (optionalCount == 0)
{
return null; // no fields...
}
var requiredCount = RequiredFactoryArgumentCount(nd, includeKind: false);
if (requiredCount == 0 && optionalCount > 1)
{
allowOptionalField = DetermineMinimalOptionalField(nd);
}
return nd.Fields.Where(f => IsRequiredFactoryField(nd, f) || allowOptionalField == f);
}
示例2: DetermineMinimalOptionalField
private Field DetermineMinimalOptionalField(Node nd)
{
// first if there is a single list, then choose the list because it would not have been optional
int listCount = nd.Fields.Count(f => IsAnyNodeList(f.Type));
if (listCount == 1)
{
return nd.Fields.First(f => IsAnyNodeList(f.Type));
}
else
{
// otherwise, if there is a single optional node, use that..
int nodeCount = nd.Fields.Count(f => IsNode(f.Type) && f.Type != "SyntaxToken");
if (nodeCount == 1)
{
return nd.Fields.First(f => IsNode(f.Type) && f.Type != "SyntaxToken");
}
else
{
return null;
}
}
}
示例3: WriteGreenFactoryParameters
private void WriteGreenFactoryParameters(Node nd)
{
if (nd.Kinds.Count > 1)
{
Write("SyntaxKind kind, ");
}
for (int i = 0, n = nd.Fields.Count; i < n; i++)
{
var field = nd.Fields[i];
if (i > 0)
Write(", ");
var type = field.Type;
if (type == "SyntaxNodeOrTokenList")
{
type = "Microsoft.CodeAnalysis.Syntax.InternalSyntax.SyntaxList<CSharpSyntaxNode>";
}
else if (IsSeparatedNodeList(field.Type) ||
IsNodeList(field.Type))
{
type = "Microsoft.CodeAnalysis.Syntax.InternalSyntax." + type;
}
Write("{0} {1}", type, CamelCase(field.Name));
}
}
示例4: WriteGreenUpdateMethod
private void WriteGreenUpdateMethod(Node node)
{
WriteLine();
Write(" public {0} Update(", node.Name);
// parameters
for (int f = 0; f < node.Fields.Count; f++)
{
var field = node.Fields[f];
if (f > 0)
Write(", ");
var type =
field.Type == "SyntaxNodeOrTokenList" ? "Microsoft.CodeAnalysis.Syntax.InternalSyntax.SyntaxList<CSharpSyntaxNode>" :
field.Type == "SyntaxTokenList" ? "Microsoft.CodeAnalysis.Syntax.InternalSyntax.SyntaxList<SyntaxToken>" :
IsNodeList(field.Type) ? "Microsoft.CodeAnalysis.Syntax.InternalSyntax." + field.Type :
IsSeparatedNodeList(field.Type) ? "Microsoft.CodeAnalysis.Syntax.InternalSyntax." + field.Type :
field.Type;
Write("{0} {1}", type, CamelCase(field.Name));
}
WriteLine(")");
WriteLine(" {");
Write(" if (");
int nCompared = 0;
for (int f = 0; f < node.Fields.Count; f++)
{
var field = node.Fields[f];
if (IsDerivedOrListOfDerived("SyntaxNode", field.Type) || IsDerivedOrListOfDerived("SyntaxToken", field.Type) || field.Type == "SyntaxNodeOrTokenList")
{
if (nCompared > 0)
Write(" || ");
Write("{0} != this.{1}", CamelCase(field.Name), field.Name);
nCompared++;
}
}
if (nCompared > 0)
{
WriteLine(")");
WriteLine(" {");
Write(" var newNode = SyntaxFactory.{0}(", StripPost(node.Name, "Syntax"));
if (node.Kinds.Count > 1)
{
Write("this.Kind, ");
}
for (int f = 0; f < node.Fields.Count; f++)
{
var field = node.Fields[f];
if (f > 0)
Write(", ");
Write(CamelCase(field.Name));
}
WriteLine(");");
WriteLine(" var diags = this.GetDiagnostics();");
WriteLine(" if (diags != null && diags.Length > 0)");
WriteLine(" newNode = newNode.WithDiagnosticsGreen(diags);");
WriteLine(" var annotations = this.GetAnnotations();");
WriteLine(" if (annotations != null && annotations.Length > 0)");
WriteLine(" newNode = newNode.WithAnnotationsGreen(annotations);");
WriteLine(" return newNode;");
WriteLine(" }");
}
WriteLine();
WriteLine(" return this;");
WriteLine(" }");
}
示例5: WriteSetDiagnostics
private void WriteSetDiagnostics(Node node)
{
WriteLine();
WriteLine(" internal override GreenNode SetDiagnostics(DiagnosticInfo[] diagnostics)");
WriteLine(" {");
Write(" return new {0}(", node.Name);
Write("this.Kind, ");
for (int f = 0; f < node.Fields.Count; f++)
{
var field = node.Fields[f];
if (f > 0)
Write(", ");
Write("this.{0}", CamelCase(field.Name));
}
WriteLine(", diagnostics, GetAnnotations());");
WriteLine(" }");
}
示例6: WriteRedMinimalFactory
// creates a factory with only the required arguments (everything else is defaulted)
private void WriteRedMinimalFactory(Node nd, bool withStringNames = false)
{
var optionalCount = OptionalFactoryArgumentCount(nd);
if (optionalCount == 0)
return; // already handled w/ general factory method
var minimalFactoryfields = new HashSet<Field>(DetermineMinimalFactoryFields(nd));
if (withStringNames && minimalFactoryfields.Count(f => IsRequiredFactoryField(nd, f) && CanAutoConvertFromString(f)) == 0)
return; // no string-name overload necessary
this.WriteLine();
WriteComment(string.Format("<summary>Creates a new {0} instance.</summary>", nd.Name), " ");
Write(" {0} static {1} {2}(", "public", nd.Name, StripPost(nd.Name, "Syntax"));
bool hasPreviousParameter = false;
if (nd.Kinds.Count > 1)
{
Write("SyntaxKind kind");
hasPreviousParameter = true;
}
for (int i = 0, n = nd.Fields.Count; i < n; i++)
{
var field = nd.Fields[i];
if (minimalFactoryfields.Contains(field))
{
var type = GetRedPropertyType(field);
if (IsRequiredFactoryField(nd, field))
{
if (hasPreviousParameter)
Write(", ");
if (withStringNames && CanAutoConvertFromString(field))
{
type = "string";
}
Write("{0} {1}", type, CamelCase(field.Name));
hasPreviousParameter = true;
}
else
{
if (hasPreviousParameter)
Write(", ");
Write("{0} {1} = default({0})", type, CamelCase(field.Name));
hasPreviousParameter = true;
}
}
}
WriteLine(")");
WriteLine(" {");
Write(" return SyntaxFactory.{0}(", StripPost(nd.Name, "Syntax"));
bool hasPreviousArgument = false;
if (nd.Kinds.Count > 1)
{
Write("kind");
hasPreviousArgument = true;
}
for (int i = 0, n = nd.Fields.Count; i < n; i++)
{
var field = nd.Fields[i];
if (hasPreviousArgument)
Write(", ");
if (minimalFactoryfields.Contains(field))
{
if (IsRequiredFactoryField(nd, field))
{
if (withStringNames && CanAutoConvertFromString(field))
{
Write("{0}({1})", GetStringConverterMethod(field), CamelCase(field.Name));
}
else
{
Write("{0}", CamelCase(field.Name));
}
}
else
{
if (IsOptional(field) || IsAnyList(field.Type))
{
Write("{0}", CamelCase(field.Name));
}
else
{
Write("{0} ?? {1}", CamelCase(field.Name), GetDefaultValue(nd, field));
}
//.........这里部分代码省略.........
示例7: DetermineRedFactoryWithNoAutoCreatableTokenFields
private IEnumerable<Field> DetermineRedFactoryWithNoAutoCreatableTokenFields(Node nd)
{
return nd.Fields.Where(f => !IsAutoCreatableToken(nd, f));
}
示例8: GetDefaultValue
private string GetDefaultValue(Node nd, Field field)
{
System.Diagnostics.Debug.Assert(!IsRequiredFactoryField(nd, field));
if (IsOptional(field) || IsAnyList(field.Type))
{
return string.Format("default({0})", GetRedPropertyType(field));
}
else if (field.Type == "SyntaxToken")
{
// auto construct token?
if (field.Kinds.Count == 1)
{
return string.Format("SyntaxFactory.Token(SyntaxKind.{0})", field.Kinds[0].Name);
}
else
{
return string.Format("SyntaxFactory.Token(Get{0}{1}Kind(kind))", StripPost(nd.Name, "Syntax"), StripPost(field.Name, "Opt"));
}
}
else
{
var referencedNode = GetNode(field.Type);
return string.Format("SyntaxFactory.{0}()", StripPost(referencedNode.Name, "Syntax"));
}
}
示例9: WriteRedListHelperMethods
private void WriteRedListHelperMethods(Node node, Field field)
{
var argType = GetElementType(field.Type);
WriteLine();
WriteLine(" public {0} Add{1}(params {2}[] items)", node.Name, field.Name, argType);
WriteLine(" {");
WriteLine(" return this.With{0}(this.{1}.AddRange(items));", StripPost(field.Name, "Opt"), field.Name);
WriteLine(" }");
}
示例10: WriteRedSetters
private void WriteRedSetters(Node node)
{
for (int f = 0; f < node.Fields.Count; f++)
{
var field = node.Fields[f];
var type = this.GetRedPropertyType(field);
WriteLine();
WriteLine(" {0} {1} With{2}({3} {4})", "public", node.Name, StripPost(field.Name, "Opt"), type, CamelCase(field.Name));
WriteLine(" {");
// call update inside each setter
Write(" return this.Update(");
for (int f2 = 0; f2 < node.Fields.Count; f2++)
{
var field2 = node.Fields[f2];
if (f2 > 0)
Write(", ");
if (field2 == field)
{
this.Write("{0}", CamelCase(field2.Name));
}
else
{
this.Write("this.{0}", field2.Name);
}
}
WriteLine(");");
WriteLine(" }");
}
}
示例11: WriteRedWithMethod
private void WriteRedWithMethod(Node node)
{
WriteLine();
Write(" public {0} With(", node.Name);
// parameters
for (int f = 0; f < node.Fields.Count; f++)
{
var field = node.Fields[f];
var type = this.GetRedPropertyType(field);
Write("Optional<{0}> {1} = default(Optional<{0}>)", type, CamelCase(field.Name));
if (f < node.Fields.Count - 1)
Write(", ");
}
WriteLine(")");
WriteLine(" {");
Write(" return this.Update(");
for (int f = 0; f < node.Fields.Count; f++)
{
var field = node.Fields[f];
var parameterName = CamelCase(field.Name);
WriteLine();
Write(" {0}.HasValue ? {0}.Value : this.{1}", parameterName, field.Name);
if (f < node.Fields.Count - 1)
Write(",");
}
WriteLine();
WriteLine(" );");
WriteLine(" }");
}
示例12: WriteRedUpdateMethod
private void WriteRedUpdateMethod(Node node)
{
WriteLine();
Write(" {0} {1} Update(", "public", node.Name);
// parameters
for (int f = 0; f < node.Fields.Count; f++)
{
var field = node.Fields[f];
if (f > 0)
Write(", ");
var type = field.Type == "SyntaxList<SyntaxToken>" ? "SyntaxTokenList" : field.Type;
Write("{0} {1}", type, CamelCase(field.Name));
}
WriteLine(")");
WriteLine(" {");
Write(" if (");
int nCompared = 0;
for (int f = 0; f < node.Fields.Count; f++)
{
var field = node.Fields[f];
if (IsDerivedOrListOfDerived("SyntaxNode", field.Type) || IsDerivedOrListOfDerived("SyntaxToken", field.Type) || field.Type == "SyntaxNodeOrTokenList")
{
if (nCompared > 0)
Write(" || ");
Write("{0} != this.{1}", CamelCase(field.Name), field.Name);
nCompared++;
}
}
if (nCompared > 0)
{
WriteLine(")");
WriteLine(" {");
Write(" var newNode = SyntaxFactory.{0}(", StripPost(node.Name, "Syntax"));
if (node.Kinds.Count > 1)
{
Write("this.Kind(), ");
}
for (int f = 0; f < node.Fields.Count; f++)
{
var field = node.Fields[f];
if (f > 0)
Write(", ");
Write(CamelCase(field.Name));
}
WriteLine(");");
WriteLine(" var annotations = this.GetAnnotations();");
WriteLine(" if (annotations != null && annotations.Length > 0)");
WriteLine(" return newNode.WithAnnotations(annotations);");
WriteLine(" return newNode;");
WriteLine(" }");
}
WriteLine();
WriteLine(" return this;");
WriteLine(" }");
}
示例13: WriteRedAcceptMethod
private void WriteRedAcceptMethod(Node node, bool genericArgument, bool genericResult)
{
string genericArgs =
(genericResult && genericArgument) ? "<TArgument, TResult>" :
genericResult ? "<TResult>" : "";
WriteLine();
WriteLine(" public override " + (genericResult ? "TResult" : "void") + " Accept" + genericArgs + "(CSharpSyntaxVisitor" + genericArgs + " visitor{0})", genericArgument ? ", TArgument argument" : "");
WriteLine(" {");
WriteLine(" " + (genericResult ? "return " : "") + "visitor.Visit{0}(this{1});", StripPost(node.Name, "Syntax"), genericArgument ? ", argument" : "");
WriteLine(" }");
}
示例14: WriteRedAcceptMethods
private void WriteRedAcceptMethods(Node node)
{
//WriteRedAcceptMethod(node, true, true);
WriteRedAcceptMethod(node, false, true);
WriteRedAcceptMethod(node, false, false);
}
示例15: WriteRedFactory
// full factory signature with nothing optional
private void WriteRedFactory(Node nd)
{
this.WriteLine();
var valueFields = nd.Fields.Where(n => IsValueField(n)).ToList();
var nodeFields = nd.Fields.Where(n => !IsValueField(n)).ToList();
WriteComment(string.Format("<summary>Creates a new {0} instance.</summary>", nd.Name), " ");
Write(" {0} static {1} {2}(", "public", nd.Name, StripPost(nd.Name, "Syntax"));
WriteRedFactoryParameters(nd);
WriteLine(")");
WriteLine(" {");
// validate kinds
if (nd.Kinds.Count > 1)
{
WriteLine(" switch (kind)");
WriteLine(" {");
foreach (var kind in nd.Kinds)
{
WriteLine(" case SyntaxKind.{0}:", kind.Name);
}
WriteLine(" break;");
WriteLine(" default:");
WriteLine(" throw new ArgumentException(\"kind\");");
WriteLine(" }");
}
// validate parameters
for (int i = 0, n = nodeFields.Count; i < n; i++)
{
var field = nodeFields[i];
var pname = CamelCase(field.Name);
if (field.Type == "SyntaxToken")
{
if (field.Kinds != null && field.Kinds.Count > 0)
{
WriteLine(" switch ({0}.Kind())", pname);
WriteLine(" {");
foreach (var kind in field.Kinds)
{
WriteLine(" case SyntaxKind.{0}:", kind.Name);
}
if (IsOptional(field))
{
WriteLine(" case SyntaxKind.None:");
}
WriteLine(" break;");
WriteLine(" default:");
WriteLine(" throw new ArgumentException(\"{0}\");", pname);
WriteLine(" }");
}
}
else if (!IsAnyList(field.Type) && !IsOptional(field))
{
WriteLine(" if ({0} == null)", CamelCase(field.Name));
WriteLine(" throw new ArgumentNullException(nameof({0}));", CamelCase(field.Name));
}
}
Write(" return ({0})Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxFactory.{1}(", nd.Name, StripPost(nd.Name, "Syntax"));
if (nd.Kinds.Count > 1)
{
Write("kind, ");
}
for (int i = 0, n = nodeFields.Count; i < n; i++)
{
var field = nodeFields[i];
if (i > 0)
Write(", ");
if (field.Type == "SyntaxToken")
{
Write("(Syntax.InternalSyntax.SyntaxToken){0}.Node", CamelCase(field.Name));
}
else if (field.Type == "SyntaxList<SyntaxToken>")
{
Write("{0}.Node.ToGreenList<Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxToken>()", CamelCase(field.Name));
}
else if (IsNodeList(field.Type))
{
Write("{0}.Node.ToGreenList<Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.{1}>()", CamelCase(field.Name), GetElementType(field.Type));
}
else if (IsSeparatedNodeList(field.Type))
{
Write("{0}.Node.ToGreenSeparatedList<Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.{1}>()", CamelCase(field.Name), GetElementType(field.Type));
}
else if (field.Type == "SyntaxNodeOrTokenList")
{
Write("{0}.Node.ToGreenList<Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.CSharpSyntaxNode>()", CamelCase(field.Name));
}
else
{
Write("{0} == null ? null : (Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.{1}){0}.Green", CamelCase(field.Name), field.Type);
}
}
//.........这里部分代码省略.........