-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableParser.cs
242 lines (201 loc) · 6.44 KB
/
TableParser.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
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Collections.Generic;
using System.Linq;
using HtmlAgilityPack;
namespace DotGLFW.Generator;
public class TableParser
{
private readonly IGLFWProvider _glfwProvider;
private readonly HtmlNode _table;
private int _rowIndex = -1;
public TableParser(IGLFWProvider glfwProvider, HtmlNode itemTable)
{
_glfwProvider = glfwProvider;
_table = itemTable;
}
public ParsedAPI Parse()
{
// Parsing
var api = new ParsedAPI();
var rowsInTable = _table.Descendants("tr").Count();
while (_rowIndex + 1 < rowsInTable)
{
ParseItem(ref api);
}
return api;
}
// Parsing helpers
private HtmlNode PreviousRow()
{
return _table.Descendants("tr").Skip(_rowIndex - 1).First();
}
private HtmlNode CurrentRow()
{
return _table.Descendants("tr").Skip(_rowIndex).First();
}
private HtmlNode ConsumeRow()
{
return _table.Descendants("tr").Skip(++_rowIndex).First();
}
private bool TryConsumeNextRow(Predicate<HtmlNode> predicate, out HtmlNode row)
{
if (_rowIndex + 1 >= _table.Descendants("tr").Count())
{
row = null;
return false;
}
row = _table.Descendants("tr").Skip(_rowIndex + 1).First();
if (predicate(row))
{
_rowIndex++;
return true;
}
return false;
}
private bool IsRowSeparator(HtmlNode row)
{
return row.GetAttributeValue("class", "").StartsWith("separator");
}
private string ReadDocsFromRow(HtmlNode row)
{
var cells = row.Descendants("td").ToArray();
return cells.Skip(1).First().InnerText.Trim().Remove("\n").Remove(" ").FixSpaces();
}
// Parsers
private void ParseItem(ref ParsedAPI api)
{
var row = ConsumeRow();
if (row.HasClass("heading") || IsRowSeparator(row) || !row.Attributes.Any())
{
return;
}
var cells = row.Descendants("td").ToArray();
var firstCell = cells.First();
var cellType = firstCell.InnerText;
if (cellType.StartsWith("#define"))
{
ParseMacro(ref api);
}
else if (cellType.StartsWith("typedef"))
{
}
else
{
// Assume it's a function
}
}
private void ParseMacro(ref ParsedAPI api)
{
var row = CurrentRow();
var cells = row.Descendants("td").ToArray();
var linkThatContainsName = cells.Skip(1).First().Descendants("a").First();
var name = linkThatContainsName.InnerText;
// var value = ParseMacroValue(linkThatContainsName.NextSibling)?.Trim();
var macro = new Macro
{
Name = name,
// Value = value,
Type = new CppType { Name = "int" }
};
if (TryConsumeNextRow(row => !IsRowSeparator(row), out var docsRow))
{
macro.Documentation = ReadDocsFromRow(docsRow);
}
api.Macros.Add(macro);
}
// private void ParseTypeDef(ref ParsedAPI api)
// {
// var row = CurrentRow();
// var typedefString = row.InnerText.Trim();
// if (typedefString.StartsWith("typedef struct"))
// {
// ParseStructTypeDef(ref api);
// }
// else
// {
// ParseDelegateTypeDef(ref api, typedefString);
// }
// }
// private void ParseDelegateTypeDef(ref ParsedAPI api, string typedefString)
// {
// var withoutTypedef = typedefString.Substring("typedef ".Length).Trim();
// var returnType = withoutTypedef.Split("(*").First().Trim();
// var name = withoutTypedef.Split(" ").Where(x => x != "").Skip(1).First()[..^1].Trim();
// var parameters = withoutTypedef.Split(")").Skip(1).First().Trim().Substring(1);
// var delegateTypeDef = new DelegateTypeDef
// {
// Name = name,
// // ReturnType = returnType,
// // Parameters = ParseParameters(parameters)
// };
// if (TryConsumeNextRow(row => !IsRowSeparator(row), out var docsRow))
// {
// delegateTypeDef.Documentation = ReadDocsFromRow(docsRow);
// }
// api.TypeDefs.Add(delegateTypeDef);
// }
// private List<(string Name, string Type)> ParseParameters(string parametersString)
// {
// if (parametersString == "void")
// {
// return [];
// }
// return parametersString.Split(", ").Select(param =>
// {
// var split = param.Split(" ");
// return (split[1], split[0]);
// }).ToList();
// }
// private void ParseStructTypeDef(ref ParsedAPI api)
// {
// var row = CurrentRow();
// var name = row.Descendants("td").First().Descendants("a").First().InnerText;
// var href = row.Descendants("td").First().Descendants("a").First()
// .GetAttributeValue("href", "");
// bool isOpaque = href.StartsWith("group_");
// string docs = null;
// if (TryConsumeNextRow(row => !IsRowSeparator(row), out var docsRow))
// {
// docs = ReadDocsFromRow(docsRow);
// }
// if (isOpaque)
// {
// api.TypeDefs.Add(new OpaqueObjectTypeDef { Name = name, Documentation = docs });
// }
// else
// {
// string structHtml = _glfwProvider.ReadGLFWRepoFile($"docs/html/{href}");
// var structHtmlDoc = new HtmlDocument();
// structHtmlDoc.LoadHtml(structHtml);
// var structTypeDef = ParseStructTypeDefWithNameAndInFile(name, structHtmlDoc.DocumentNode);
// structTypeDef.Documentation = docs;
// api.TypeDefs.Add(structTypeDef);
// }
// }
// private StructTypeDef ParseStructTypeDefWithNameAndInFile(string name, HtmlNode structHtmlBody)
// {
// var table = structHtmlBody.Descendants("table").Where(table => table.HasClass("memberdecls")).First();
// var rows = table.Descendants("tr").ToArray();
// int rowIndex = 0;
// var fields = new Dictionary<string, string>();
// while (rowIndex < rows.Length)
// {
// var row = rows[rowIndex];
// if (row.HasClass("heading") || IsRowSeparator(row) || !row.Attributes.Any())
// {
// rowIndex++;
// continue;
// }
// var fieldType = row.Descendants("td").First().InnerText.Trim().Remove(" ");
// var fieldName = row.Descendants("td").Skip(1).First().InnerText.Trim().Remove(" ");
// fields.Add(fieldName, fieldType);
// rowIndex++;
// }
// return new StructTypeDef
// {
// Name = name,
// Fields = fields,
// Documentation = "DOCS FOR STRUCT FIELDS ARE NOT SUPPORTED YET"
// };
// }
}