-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cs
231 lines (202 loc) · 8.38 KB
/
Parser.cs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using FlatBufferEx.Model;
using System.Text.RegularExpressions;
using Enum = FlatBufferEx.Model.Enum;
namespace FlatBufferEx
{
public static class Parser
{
private static readonly Regex FieldRegEx = new Regex(@"\s*(?<name>[_a-zA-Z][_a-zA-Z0-9]*)\s*:\s*(?<type>\[*[_a-zA-Z][_a-zA-Z0-9\.]*\??\]*\??)(?:\s*=\s*(?<init>.+))?\s*(?<deprecated>\(deprecated\))?\s*;");
private static readonly Regex TableRegEx = new Regex(@"(?<type>struct|table)\s+(?<name>[_a-zA-Z][_a-zA-Z0-9]*)\s*{(?<contents>[\s\S]*?)}");
private static readonly Regex EnumRegEx = new Regex(@"enum\s+(?<name>[_a-zA-Z][_a-zA-Z0-9]*)\s*:\s*(?<type>[a-zA-Z]+)\s+{\s*(?<contents>.+)\s*}");
private static readonly Regex NamespaceRegEx = new Regex(@"namespace\s+(?<name>[_a-zA-Z][_a-zA-Z0-9\.]*);");
private static readonly Regex IncludeRegEx = new Regex(@"include\s*""(?<file>.+)\.fbs""\s*;");
private static (List<string> Namespace, string Type) SplitNamespace(string value)
{
if (value.Contains('.'))
{
var splitted = value.Split('.').ToList();
return (splitted.GetRange(0, splitted.Count - 1).ToList(), splitted.Last());
}
else
{
return (null, value);
}
}
private static bool RemoveNullableType(ref string type)
{
if (type.EndsWith('?'))
{
type = type.Substring(0, type.Length - 1);
return true;
}
else
{
return false;
}
}
private static bool RemoveArrayType(ref string type)
{
if (type.StartsWith('[') && type.EndsWith(']'))
{
type = type.Substring(1, type.Length - 2);
return true;
}
else
{
return false;
}
}
private static Field GetField(Context context, Scope scope, Table table, string type)
{
var field = new Field
{
Context = context,
Scope = scope,
Table = table,
};
field.IsNullable = RemoveNullableType(ref type);
if (RemoveArrayType(ref type))
{
if (field.IsNullable)
throw new Exception("array cannot be null type");
field.Type = "array";
field.ArrayElement = GetField(context, scope, table, type);
}
else
{
(field.ReferNamespace, field.Type) = SplitNamespace(type);
}
return field;
}
private static IEnumerable<Field> GetFields(Context context, Scope scope, Table table, string contents)
{
foreach (Match match in FieldRegEx.Matches(contents))
{
if (match.Groups["deprecated"].Success)
continue;
var field = GetField(context, scope, table, match.Groups["type"].Value);
field.Name = match.Groups["name"].Value;
field.Init = match.Groups["init"].Success ? match.Groups["init"].Value : null;
yield return field;
}
}
private static IEnumerable<Enum> GetEnums(Context context, Scope scope, string contents)
{
foreach (Match match in EnumRegEx.Matches(contents))
{
yield return new Enum
{
Context = context,
Scope = scope,
Type = match.Groups["type"].Value,
Name = match.Groups["name"].Value,
Values = match.Groups["contents"].Value.Split(',').Select(x => x.Split('=')[0].Trim()).ToList(),
};
}
}
private static IEnumerable<Table> GetTables(Context context, Scope scope, string contents)
{
foreach (Match match in TableRegEx.Matches(contents))
{
var table = new Table
{
Context = context,
Scope = scope,
Type = match.Groups["type"].Value,
Name = match.Groups["name"].Value,
};
table.Fields = GetFields(context, scope, table, match.Groups["contents"].Value).ToList();
yield return table;
}
}
private static IEnumerable<string> GetIncludeFiles(string contents)
{
foreach (Match match in IncludeRegEx.Matches(contents))
{
yield return match.Groups["file"].Value;
}
}
private static List<string> GetNamespace(string contents)
{
var matched = NamespaceRegEx.Match(contents);
if (matched.Success == false)
return new List<string>();
return matched.Groups["name"].Value.Split('.').ToList();
}
public static Scope GetScope(Context context, string file)
{
var contents = File.ReadAllText(file);
var scope = new Scope
{
Context = context,
FileName = Path.GetFileNameWithoutExtension(file),
IncludeFiles = GetIncludeFiles(contents).ToList(),
Namespace = GetNamespace(contents),
};
scope.Tables = GetTables(context, scope, contents).ToList();
scope.Enums = GetEnums(context, scope, contents).ToList();
return scope;
}
public static Context Parse(string path, string wildcard)
{
var context = new Context
{
Scopes = new List<Model.Scope>()
};
foreach (var file in Directory.GetFiles(path, wildcard))
{
context.Scopes.Add(GetScope(context, file));
}
return context;
}
//public static IEnumerable<string> CreateOriginTempFiles(string path, string to, string wildcard, string lang)
//{
// if (Directory.Exists(to) == false)
// Directory.CreateDirectory(to);
// foreach (var file in Directory.GetFiles(to, wildcard))
// {
// File.Delete(file);
// }
// foreach (var file in Directory.GetFiles(path, wildcard))
// {
// var contents = File.ReadAllText(file);
// var matchNamespace = NamespaceRegEx.Match(contents);
// contents = contents.Remove(matchNamespace.Groups["name"].Index, matchNamespace.Groups["name"].Value.Length).Insert(matchNamespace.Groups["name"].Index, string.Join('.', matchNamespace.Groups["name"].Value.Split('.').Select(x =>
// {
// switch (lang)
// {
// case "c#":
// return ScribanEx.CsReplaceReservedKeyword(x);
// default:
// return x;
// }
// }).Concat(new[] { "origin" })));
// var matchFields = FieldRegEx.Matches(contents);
// foreach (var match in matchFields.Cast<Match>().Reverse())
// {
// var values = match.Groups["type"].Value.Split('.').ToList();
// if (values.Count == 1)
// continue;
// switch (lang)
// {
// case "c#":
// values = values.Select(x => ScribanEx.CsReplaceReservedKeyword(x)).ToList();
// break;
// }
// values.Insert(values.Count - 1, "origin");
// contents = contents.Remove(match.Groups["type"].Index, match.Groups["type"].Value.Length).Insert(match.Groups["type"].Index, string.Join('.', values));
// }
// var fileName = Path.GetFileName(file);
// File.WriteAllText(Path.Join(to, Path.GetFileName(file)), contents);
// yield return fileName;
// }
//}
//public static IEnumerable<FlatBufferFileInfo> Parse(string path, string wildcard)
//{
// foreach (var file in Directory.GetFiles(path, wildcard))
// {
// yield return Parse(file);
// }
//}
}
}