-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGenerator.cs
More file actions
146 lines (129 loc) · 5.1 KB
/
Generator.cs
File metadata and controls
146 lines (129 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace ObjectInitializerGenerator
{
public class Generator
{
private readonly ICodeWriter codeWriter;
List<ObjectModel> objectModelList;
public Generator(ICodeWriter codeWriter)
{
this.codeWriter = codeWriter;
}
public Generator Analyse(string input)
{
try
{
SyntaxTree tree = CSharpSyntaxTree.ParseText(input);
CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
objectModelList = new List<ObjectModel>();
foreach (SyntaxNode child in root.ChildNodes())
{
if (child.Kind() == SyntaxKind.ClassDeclaration)
{
ObjectModel parent = new ObjectModel();
foreach (SyntaxToken item in child.ChildTokens())
{
if (item.Kind() == SyntaxKind.IdentifierToken)
{
parent.TokenType = SyntaxKind.ClassDeclaration;
parent.SyntaxName = item.Value.ToString();
break;
}
}
if (child.ChildNodes().Count() > 0)
{
AnalyseChildNodes(child, parent);
}
objectModelList.Add(parent);
}
}
}
catch (Exception ex)
{
throw;
}
return this;
}
private void AnalyseChildNodes(SyntaxNode node, ObjectModel parent)
{
foreach (SyntaxNode child in node.ChildNodes())
{
if (child.Kind() == SyntaxKind.ClassDeclaration)
{
ObjectModel subParent = new ObjectModel();
foreach (SyntaxToken item in child.ChildTokens())
{
if (item.Kind() == SyntaxKind.IdentifierToken)
{
subParent.TokenType = SyntaxKind.ClassDeclaration;
subParent.SyntaxName = item.Value.ToString();
break;
}
}
if (child.ChildNodes().Count() > 0)
{
AnalyseChildNodes(child, subParent);
}
parent.Children.Add(subParent);
}
else if (child.Kind() == SyntaxKind.PropertyDeclaration)
{
ObjectModel property = new ObjectModel();
foreach (SyntaxToken item in child.ChildTokens())
{
if (item.Kind() == SyntaxKind.IdentifierToken)
{
property.TokenType = SyntaxKind.PropertyDeclaration;
property.SyntaxName = item.Value.ToString();
break;
}
}
foreach (SyntaxNode item in child.ChildNodes())
{
if (item.Kind() == SyntaxKind.PredefinedType // int, bool, string
|| item.Kind() == SyntaxKind.NullableType // Nullable kinds
|| item.Kind() == SyntaxKind.IdentifierName) // Classes, Guid, Boolean, String
{
property.PropertyType = item.ToString();
property.NodeType = item.Kind();
break;
}
if (item.Kind() == SyntaxKind.GenericName // Lists, Dictionaries
|| item.Kind() == SyntaxKind.ArrayType) // Arrays
{
// get the generic type inside the array, or list
foreach (var genericItem in item.ChildNodes())
{
if (genericItem.Kind() == SyntaxKind.PredefinedType)
{
property.GenericType = genericItem.ToString();
break;
}
}
property.PropertyType = item.ToString();
property.NodeType = item.Kind();
break;
}
}
parent.Children.Add(property);
}
}
}
public string Write()
{
try
{
return codeWriter.Write(objectModelList);
}
catch (Exception ex)
{
throw;
}
}
}
}