CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
ES6.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using System.Linq;
3 
4 namespace CodeGen.generators
5 {
10  public class Es6Generator : Generator
11  {
12  private const string ClassFormat = "class {0} {1}{{{2}{3}}}{4}";
13  private string Indent { get; set; } = GeneratorConf.GetIndent(true, 4);
14 
16  protected override string GenerateClass(Class @class)
17  {
18  string fields = "", inherits = "", methods = "", classes = "";
19  if (!string.IsNullOrWhiteSpace(@class.Parent))
20  {
21  inherits = "extends " + @class.Parent + " ";
22  }
23 
24  if (@class.Fields?.Length > 0)
25  {
26  fields = "\n" + GeneratorConf.ShiftCode(GenerateInit(@class), 1, Indent) + "\n";
27  }
28 
29  methods = @class.Methods?.Aggregate("\n" + methods,
30  (current, method) => current + GeneratorConf.ShiftCode(GenerateMethod(method), 1, Indent) + "\n");
31 
32  var staticFields = @class.Fields?.Aggregate("",
33  (current, field) =>
34  {
35  if (!field.Static) return current;
36  current += "\n" + @class.Name + "." + field.Name + " = ";
37  current += (string.IsNullOrEmpty(field.Default) ? "null" : field.Default) + ";";
38  return current;
39  });
40 
41  classes = @class.Classes?.Aggregate(classes,
42  (current, cls) => current + "\n" + @class.Name + "." + cls.Name + " = " + GenerateClass(cls));
43 
44  return string.Format(ClassFormat, @class.Name, inherits, fields, methods, staticFields + classes);
45  }
46 
48  public override string GenerateField(Field field)
49  {
50  var result = Indent;
51 
52  result += field.Type + " ";
53  result += field.Name;
54  if (field.Default != "")
55  {
56  result += " = " + field.Default;
57  }
58 
59  result += ";";
60 
61  return result;
62  }
63 
65  public override string GenerateMethod(Method method)
66  {
67  return GenerateMethodWithBody(method, "");
68  }
69 
76  private string GenerateMethodWithBody(Method method, string body)
77  {
78  var result = "";
79  if (method.Static)
80  {
81  result += "static ";
82  }
83 
84  result += method.Name;
85  result += "(";
86 
87  for (var i = 0; i < method.Parameters?.Length; i++)
88  {
89  var parameter = method.Parameters[i];
90 
91  result += parameter.Name;
92  if (!string.IsNullOrEmpty(parameter.Default))
93  {
94  result += "=" + parameter.Default;
95  }
96 
97  if (i + 1 < method.Parameters.Length)
98  {
99  result += ", ";
100  }
101  }
102 
103  result += ") {";
104 
105  if (body != "")
106  {
107  result += "\n" + GeneratorConf.ShiftCode(body, 1, Indent) + "\n";
108  }
109 
110  if (method.Type != "" && method.Name != "constructor")
111  {
112  result += "\n" + Indent + "return null;\n";
113  }
114 
115  result += "}";
116 
117  return result;
118  }
119 
125  private string GenerateInit(Class @class)
126  {
127  var result = "";
128  var body = "";
129 
130  var init = new Method
131  {
132  Name = "constructor"
133  };
134 
135  var previousIsStatic = false;
136 
137  var paramList = new List<Parameter>();
138 
139  for (var i = 0; i < @class.Fields?.Length; i++)
140  {
141  var field = @class.Fields[i];
142 
143  if (field.Static)
144  {
145  previousIsStatic = true;
146  continue;
147  }
148 
149  paramList.Add(new Parameter
150  {
151  Name = field.Name,
152  Default = field.Default
153  });
154 
155  body += "this." + field.Name + " = " + field.Name + ";";
156 
157  if (i + 1 < @class.Fields.Length && !previousIsStatic)
158  {
159  body += "\n";
160  }
161 
162  previousIsStatic = false;
163  }
164 
165  init.Parameters = paramList.ToArray();
166 
167  if (previousIsStatic && body.Length > Indent.Length)
168  {
169  body = body.Substring(0, body.Length - Indent.Length + 1);
170  }
171 
172  if (!string.IsNullOrWhiteSpace(@class.Parent))
173  {
174  body = "super();\n" + body;
175  }
176 
177  result += GenerateMethodWithBody(init, body);
178 
179  return result;
180  }
181  }
182 }
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
Definition: ES6.cs:16
string Type
Represents return type of the method. Type: string
Definition: Models.cs:178
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 Name
Represents the name of the method. Type: string
Definition: Models.cs:172
bool Static
Denotes if the mathod is static or not. Type: boolean
Definition: Models.cs:196
Generator for JavaScript ES6
Definition: ES6.cs:10
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
Definition: ES6.cs:65
Parameter[] Parameters
Represents parameters of the method. Type: array of type Parameter
Definition: Models.cs:202
Holds the configuration of generator
static string ShiftCode(string code, int num, string indent)
Shifts code using given parameters
string Default
Represents default value of the varibale. Type: string
Definition: Models.cs:114
override string GenerateField(Field field)
Field generator: generates field from given field object Field objectString of generated code of fiel...
Definition: ES6.cs:48
Interface of language generator
The structure that describes method. Contains name, return type, access level, const and static prope...
Definition: Models.cs:166
The structure that describes field. Contains access, const and static properties. Inherits from Varia...
Definition: Models.cs:121