CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
GeneratorConf.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 
5 namespace CodeGen.generators
6 {
11  public abstract class Generator
12  {
16  protected static bool UseTabs = true;
17 
23  public Dictionary<string, string> Generate(Package pkg)
24  {
25  var data = new Dictionary<string, string>();
26  UseTabs = !pkg.UseSpaces;
27  foreach (var @class in pkg.Classes)
28  {
29  if (@class?.Name != null)
30  data[@class.Name] = GenerateClass(@class) + '\n';
31  }
32 
33  return data;
34  }
35 
40  public virtual string GetIndent()
41  {
42  return "\t";
43  }
44 
50  protected abstract string GenerateClass(Class @class);
51 
57  public abstract string GenerateField(Field field);
58 
64  public abstract string GenerateMethod(Method method);
65  }
66 
71  public abstract class Normalizer
72  {
78  public virtual Package NormalizePackage(ref Package pkg)
79  {
80  if (pkg == null)
81  return null;
82  for (var i = 0; i < pkg.Classes?.Length; i++)
83  NormalizeClass(ref pkg.Classes[i]);
84  for (var i = 0; i < pkg.Packages?.Length; i++)
85  if (pkg.Packages[i] != pkg)
86  NormalizePackage(ref pkg.Packages[i]);
87  return pkg;
88  }
89 
95  protected virtual Class NormalizeClass(ref Class @class)
96  {
97  if (@class == null)
98  return null;
99  for (var i = 0; i < @class.Fields?.Length; i++)
100  NormalizeField(ref @class.Fields[i]);
101  for (var i = 0; i < @class.Methods?.Length; i++)
102  NormalizeMethod(ref @class.Methods[i]);
103  for (var i = 0; i < @class.Classes?.Length; i++)
104  NormalizeClass(ref @class.Classes[i]);
105  return @class;
106  }
107 
113  protected virtual Field NormalizeField(ref Field field)
114  {
115  field.Type = NormalizeType(field.Type);
116  return field;
117  }
118 
124  protected virtual Method NormalizeMethod(ref Method method)
125  {
126  method.Type = NormalizeType(method.Type);
127  for (var i = 0; i < method.Parameters?.Length; i++)
128  NormalizeParameter(method.Parameters[i]);
129  return method;
130  }
131 
137  protected virtual Parameter NormalizeParameter(Parameter parameter)
138  {
139  parameter.Type = NormalizeType(parameter.Type);
140  return parameter;
141  }
142 
148  protected abstract string NormalizeType(string type);
149  }
150 
154  public abstract class Validator
155  {
156  private string[] AccessModifiers { get; } = {"", "public", "private", "protected", "default"};
157 
163  public virtual bool FieldIsValid(Field field)
164  {
165  if (field == null)
166  return false;
167  var isInvalid = string.IsNullOrWhiteSpace(field.Name);
168  isInvalid |= string.IsNullOrWhiteSpace(field.Type);
169  isInvalid |= string.IsNullOrWhiteSpace(field.Type);
170  isInvalid |= field.Name.Any(char.IsWhiteSpace);
171  isInvalid |= field.Type.Any(char.IsWhiteSpace);
172  if (field.Access == null) return !isInvalid;
173  isInvalid |= field.Access.Any(char.IsWhiteSpace);
174  isInvalid |= !AccessModifiers.Any(field.Access.Equals);
175  return !isInvalid;
176  }
177  }
178 
182  public struct Languange
183  {
187  public readonly Generator Generator;
188 
192  public readonly string Extension;
193 
197  public readonly string Comment;
198 
202  public readonly Normalizer Normalizer;
203 
207  private readonly int _indentSize;
208 
217  public Languange(Generator generator, string extension = "", string comment = "", Normalizer normalizer = null,
218  int indentSize = 4)
219  {
220  Generator = generator;
221  Extension = extension;
222  Comment = comment;
223  Normalizer = normalizer;
224  _indentSize = indentSize;
225  }
226  }
227 
231  // ReSharper disable once ClassNeverInstantiated.Global
232  public class GeneratorConf
233  {
237  public static readonly Package ExamplePkg = new Package
238  {
239  Name = "main",
240  UseSpaces = true,
241  Classes = new[]
242  {
243  new Class {Name = "Fruit"},
244  new Class
245  {
246  Name = "Apple",
247  Parent = "Fruit",
248  Fields = new[]
249  {
250  new Field
251  {
252  Access = "public",
253  Type = "string",
254  Name = "colour",
255  Default = "\"red\""
256  },
257  new Field
258  {
259  Access = "public",
260  Type = "string",
261  Static = true,
262  Name = "sort",
263  Default = "\"Golden\"",
264  Getter = true,
265  Setter = true
266  },
267  new Field
268  {
269  Access = "private",
270  Type = "int",
271  Name = "size",
272  Default = "1",
273  Getter = true
274  }
275  },
276  Methods = new[]
277  {
278  new Method
279  {
280  Access = "private",
281  Type = "",
282  Name = "print",
283  Parameters = new[]
284  {
285  new Parameter
286  {
287  Name = "colour",
288  Type = "string"
289  }
290  }
291  },
292  new Method
293  {
294  Access = "protected",
295  Type = "int",
296  Static = true,
297  Name = "getSizeValue"
298  },
299  new Method
300  {
301  Access = "public",
302  Type = "string",
303  Name = "getColorName",
304  Const = true
305  }
306  },
307  Classes = new[]
308  {
309  new Class
310  {
311  Access = "private",
312  Name = "Seed",
313  Fields = new[]
314  {
315  new Field
316  {
317  Access = "public",
318  Type = "int",
319  Name = "size"
320  }
321  },
322  Methods = new[]
323  {
324  new Method
325  {
326  Static = true,
327  Access = "public",
328  Type = "int",
329  Name = "transform",
330  Const = true
331  }
332  }
333  }
334  }
335  },
336  }
337  };
338 
345  public static readonly Dictionary<string, Languange> Languanges = new Dictionary<string, Languange>
346  {
347  {"java", new Languange(new JavaGenerator(), "java", "/* {0} */", JavaNormalizer.GetNormalizer())},
348  {"go", new Languange(new GoGenerator(), "go", "/* {0} */")},
349  {"ruby", new Languange(new RubyGenerator(), "rb", "# {0}")},
350  {"python", new Languange(new PythonGenerator(), "py", "# {0}\n")},
351  {"vb", new Languange(new VbGenerator(), "vb", "' {0}\n", VbNormalizer.GetNormalizer())},
352  {"csharp", new Languange(new CSharpGenerator(), "cs", "/* {0} */", CSharpNormalizer.GetNormalizer())},
353  {"js_es6", new Languange(new Es6Generator(), "js", "/* {0} */")},
354  {"groovy", new Languange(new GroovyGenerator(), "groovy", "/* {0} */")},
355  {"cpp", new Languange(new CppGenerator(), "cpp", "/* {0} */")},
356  };
357  //{"es6", new Languange(new Es6Generator(), "es2015", "/* {0} */")},
358 // {"js", new Languange(new Es6Generator(), "js5", "/* {0} */")},
359 
360 
367  public static string GetIndent(bool tabs, int tabStop)
368  {
369  return tabs ? "\t" : new string(' ', tabStop);
370  }
371 
379  public static string ShiftCode(string code, int num, string indent)
380  {
381  indent = string.Concat(Enumerable.Repeat(indent, num));
382  return indent + code.Replace("\n", "\n" + indent);
383  }
384 
390  public static string NormalizeLang(string lang)
391  {
392  if (lang == "js")
393  {
394  lang = "js_es6";
395  }
396  else if (lang == "c#" || lang == "cs")
397  {
398  lang = "csharp";
399  }
400  else if (lang == "yml")
401  {
402  lang = "yaml";
403  }
404  else if (lang == "c++")
405  {
406  lang = "cpp";
407  }
408 
409  return lang;
410  }
411 
418  public static Languange GetLanguage(string name)
419  {
420  name = NormalizeLang(name);
421  Languange lang;
422  try
423  {
424  lang = Languanges[name];
425  }
426  catch (Exception)
427  {
428  throw new IndexOutOfRangeException("this language doesn't exist");
429  }
430 
431  return lang;
432  }
433  }
434 }
virtual Method NormalizeMethod(ref Method method)
Method normalizer: normalizes method
Generator for Ruby
Definition: Ruby.cs:10
Generator for Go
Definition: Go.cs:9
static Normalizer GetNormalizer()
Method for getting a singleton
Definition: VB.cs:109
The class that describes programming language and has a generator for it
The structure that describes class. Contains name, array of fields, methods and subclasses, parent class name, access specifier. Overrides ToString() method.
Definition: Models.cs:48
string Type
Represents the type of the variable. Type: string
Definition: Models.cs:108
Generator for Python
Definition: Python.cs:10
virtual string GetIndent()
Gets indentation of current generator
static string GetIndent(bool tabs, int tabStop)
Creates indent using given parameters
The structure that describes package. Contains classes and subpackages
Definition: Models.cs:9
Languange(Generator generator, string extension="", string comment="", Normalizer normalizer=null, int indentSize=4)
Constructor for language, used to avoid struct initializers
Generator for JavaScript ES6
Definition: ES6.cs:10
static Normalizer GetNormalizer()
Method for getting a singleton
Definition: Java.cs:169
static Normalizer GetNormalizer()
Method for getting a singleton
Definition: CSharp.cs:213
readonly string Comment
Holds comment format. Field is read only
Holds the configuration of generator
virtual Package NormalizePackage(ref Package pkg)
Package normalizer: normalizes package with classes and subpackages
Dictionary< string, string > Generate(Package pkg)
Package generator: generates package with classes and subpackages from given package object ...
Normalizer for Visual Basic
Definition: VB.cs:96
virtual bool FieldIsValid(Field field)
Checks if field is valid
Generator for Java
Definition: Java.cs:11
Interface of language normalizer: normalizes package data according to specified language ...
virtual Field NormalizeField(ref Field field)
Field normalizer: normalizes field
static string NormalizeLang(string lang)
Converts given language into language which is used for identification of generator ...
static Languange GetLanguage(string name)
Creates generator if it exists, else throws an error
Generator for Visual Basic
Definition: VB.cs:10
Normalizer for Java
Definition: Java.cs:145
readonly Generator Generator
Holds the generator of the language. Field is read only
Generator for Groovy
Definition: Groovy.cs:9
static string ShiftCode(string code, int num, string indent)
Shifts code using given parameters
string Access
Represents access level of the field. Type: string
Definition: Models.cs:127
Interface of language generator
readonly string Extension
Holds the extension of the file. Field is read only
virtual Parameter NormalizeParameter(Parameter parameter)
Parameter normalizer: normalizes parameter
virtual Class NormalizeClass(ref Class @class)
Class normalizer: normalizes class with fields, methods and subclasses
The structure that describes method. Contains name, return type, access level, const and static prope...
Definition: Models.cs:166
The structure that describes parameter. Inherits from Variable
Definition: Models.cs:158
readonly Normalizer Normalizer
Holds language normalizer. Field is read only
The structure that describes field. Contains access, const and static properties. Inherits from Varia...
Definition: Models.cs:121