public void Build(CodeFragmentBuilder builder) { builder.AppendLine("public enum {0}", Name); builder.AppendLine("{"); builder.Indent(); var names = new List<string>(FieldNames); names.Sort(); int none = names.IndexOf("None"); if (none != -1) { names.RemoveAt(none); names.Insert(0, "None"); } for (int i = 0; i < names.Count; i++) { builder.Append(names[i]); if (i != names.Count - 1) builder.Append(","); builder.AppendLine(); } builder.Unindent(); builder.AppendLine("}"); }
public void Build(CodeFragmentBuilder builder) { builder.AppendLine("public enum {0}", Name); builder.AppendLine("{"); builder.Indent(); var names = new List <string>(FieldNames); names.Sort(); int none = names.IndexOf("None"); if (none != -1) { names.RemoveAt(none); names.Insert(0, "None"); } for (int i = 0; i < names.Count; i++) { builder.Append(names[i]); if (i != names.Count - 1) { builder.Append(","); } builder.AppendLine(); } builder.Unindent(); builder.AppendLine("}"); }
public void Build(CodeFragmentBuilder builder) { builder.AppendLine("[Flags]"); builder.AppendLine("public enum {0}", Name); builder.AppendLine("{"); builder.Indent(); builder.AppendLine("None = 0,"); for (int i = 0; i < FieldNames.Length; i++) { builder.Append(FieldNames[i]); builder.Append(" = 1 << "); builder.Append(i.ToString(CultureInfo.InvariantCulture)); if (i != FieldNames.Length - 1 || AddAll) { builder.Append(","); } builder.AppendLine(); } if (AddAll) { builder.Append("All = (1 << "); builder.Append(FieldNames.Length.ToString(CultureInfo.InvariantCulture)); builder.AppendLine(") - 1"); } builder.Unindent(); builder.AppendLine("}"); }
public void Build(CodeFragmentBuilder builder) { builder.AppendLine("[Flags]"); builder.AppendLine("public enum {0}", Name); builder.AppendLine("{"); builder.Indent(); builder.AppendLine("None = 0,"); for (int i = 0; i < FieldNames.Length; i++) { builder.Append(FieldNames[i]); builder.Append(" = 1 << "); builder.Append(i.ToString(CultureInfo.InvariantCulture)); if (i != FieldNames.Length - 1 || AddAll) builder.Append(","); builder.AppendLine(); } if (AddAll) { builder.Append("All = (1 << "); builder.Append(FieldNames.Length.ToString(CultureInfo.InvariantCulture)); builder.AppendLine(") - 1"); } builder.Unindent(); builder.AppendLine("}"); }
private void BuildFactories(string target, Dictionary<SyntaxType, List<Factory>> factories, HashSet<SyntaxType> writtenTypes) { var builder = new CodeFragmentBuilder(); builder.AppendLine("using System;"); builder.AppendLine("using System.Collections.Generic;"); builder.AppendLine("using System.Linq;"); builder.AppendLine("using System.Text;"); builder.AppendLine(); builder.AppendLine("namespace CSharpSyntax"); builder.AppendLine("{"); builder.Indent(); builder.AppendLine("public static partial class Syntax"); builder.AppendLine("{"); builder.Indent(); bool hadOne = false; foreach (var item in factories.Where(p => writtenTypes.Contains(p.Key)).OrderBy(p => p.Key.Name)) { foreach (var factory in item.Value) { factory.Parameters.RemoveAll(p => p.Type == "SyntaxKind"); } } SeedParamsFactories(factories); SeedParseNameFactories(factories); foreach (var item in factories.Where(p => writtenTypes.Contains(p.Key)).OrderBy(p => p.Key.Name)) { for (int i = 0; i < item.Value.Count; i++) { var factory = item.Value[i]; bool duplicate = false; for (int j = i + 1; j < item.Value.Count; j++) { if (factory.Equals(item.Value[j])) { duplicate = true; break; } } if (duplicate) continue; if (factory.Parameters.Count == 0 && item.Value.Any(p => p.Parameters.Count > 0)) continue; if (FactoryWithMoreParametersExists(factory, item.Value)) continue; if (hadOne) builder.AppendLine(); else hadOne = true; builder.Append( "public static {0} {1}(", item.Key.Name, factory.Name ); bool hadOneChild = false; foreach (var parameter in factory.Parameters) { if (hadOneChild) builder.Append(", "); else hadOneChild = true; if (parameter.IsParams) builder.Append("params "); string type = parameter.Type; switch (type) { case "String": type = "string"; break; case "Object": type = "object"; break; } if (parameter.IsParams && !type.EndsWith("[]")) type += "[]"; if (parameter.IsList) builder.Append("IEnumerable<{0}> {1}", type, EscapeParameterName(parameter)); else builder.Append("{0} {1}", type, EscapeParameterName(parameter)); if ( (parameter.IsOptional || true) && // All parameters are currently generated as default !parameter.IsParams ) { if ( parameter.OriginalType.IsValueType || parameter.OriginalType == typeof(AttributeTargetSpecifierSyntax) ) builder.Append(" = default({0})", parameter.Type); else builder.Append(" = null"); } } builder.AppendLine(")"); builder.AppendLine("{"); builder.Indent(); builder.AppendLine("var result = new {0}();", item.Key.Name); builder.AppendLine(); foreach (var parameter in factory.Parameters) { if (parameter.Type == "SyntaxKind") continue; string propertyName = parameter.Name.Substring(0, 1).ToUpperInvariant() + parameter.Name.Substring(1); string parameterName = EscapeParameterName(parameter); string parseName = parameter.ParseNameType; if (item.Key.Name == "AliasQualifiedNameSyntax") { if (parameter.Name == "alias" && parameter.Type == "String") parseName = "IdentifierNameSyntax"; } else if (item.Key.Name == "IdentifierNameSyntax") { if (parameter.Name == "name") propertyName = "Identifier"; } else if (new[] { "NameColonSyntax", "NameEqualsSyntax", "TypeParameterConstraintClauseSyntax" }.Contains(item.Key.Name)) { if (parameter.Name == "name" && parameter.Type == "String") parseName = "IdentifierNameSyntax"; } if (parseName != null) { builder.AppendLine("if ({0} != null)", parameterName); builder.Indent(); if (parseName == "TypeSyntax") { if (parameter.IsList || parameter.IsParams) { builder.AppendLine( "result.{0}.AddRange(ParseNames<TypeSyntax>({1}));", propertyName, parameterName ); } else { builder.AppendLine( "result.{0} = ParseName({1});", propertyName, parameterName ); } } else { if (parameter.IsList || parameter.IsParams) { builder.AppendLine( "result.{0}.AddRange(ParseNames<{2}>({1}));", propertyName, parameterName, parseName ); } else { builder.AppendLine( "result.{0} = ({2})ParseName({1});", propertyName, parameterName, parseName ); } } builder.Unindent(); } else if (parameter.IsList || parameter.IsParams) { builder.AppendLine("if ({0} != null)", parameterName); builder.Indent(); builder.AppendLine("result.{0}.AddRange({1});", propertyName, parameterName); builder.Unindent(); } else { builder.AppendLine( "result.{0} = {1};", propertyName, parameterName ); } } builder.AppendLine(); builder.AppendLine("return result;"); builder.Unindent(); builder.AppendLine("}"); } } builder.Unindent(); builder.AppendLine("}"); builder.Unindent(); builder.AppendLine("}"); WriteFile(target, "Syntax", builder); }