-
Notifications
You must be signed in to change notification settings - Fork 1
/
Parser.cs
198 lines (151 loc) · 7.29 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
using System;
using System.Collections.Generic;
using System.Xml;
namespace SimpleDocs
{
public class Parser
{
public Parser()
{
}
public static object[] Parse(string xmlComments, bool includePrivate)
{
List<object> elements = new List<object>();
XmlDocument doc = new XmlDocument();
try
{
doc.LoadXml("<?xml version=\"1.0\"?>\n<documentation>" + xmlComments + "</documentation>");
}
catch (Exception ex)
{
Console.Out.WriteLine("XML error found: " + ex.Message);
string[] commentLines = xmlComments.Split(new char[] { '\n' });
for (int i = 1 ; i <= commentLines.Length ; i++)
Console.Out.WriteLine(i + ": " + commentLines[i-1]);
throw ex;
}
XmlNodeList containers = doc.GetElementsByTagName("container");
XmlNodeList members = doc.GetElementsByTagName("member");
XmlNodeList functions = doc.GetElementsByTagName("function");
XmlNodeList parameters;
Container con;
Member mem;
Function func;
Parameter parm;
// Get containers
for (int i = 0 ; i < containers.Count ; i++)
{
if (getAttributeValue(containers[i].Attributes["name"]) == "")
continue;
con = new Container(getAttributeValue(containers[i].Attributes["name"]));
con.Description = getNodeText(containers[i].FirstChild != null && containers[i].FirstChild.Name == "description" ? containers[i].FirstChild : containers[i]);
con.Extends = getAttributeValue(containers[i].Attributes["extends"]);
elements.Add(con);
}
// Get members
for (int i = 0 ; i < members.Count ; i++)
{
if (getAttributeValue(members[i].Attributes["name"]) == "")
continue;
if (members[i].Attributes["access"] != null && getAttributeValue(members[i].Attributes["access"]).ToLower() != "private" && getAttributeValue(members[i].Attributes["access"]).ToLower() != "public" && getAttributeValue(members[i].Attributes["access"]).ToLower() != "protected")
continue;
if (members[i].Attributes["static"] != null && getAttributeValue(members[i].Attributes["static"]).ToLower() != "true" && getAttributeValue(members[i].Attributes["static"]).ToLower() != "false")
continue;
if (includePrivate == false && members[i].Attributes["access"] != null && getAttributeValue(members[i].Attributes["access"]).ToLower() == "private")
continue;
string container = "";
if (getAttributeValue(members[i].Attributes["container"]) != "")
container = getAttributeValue(members[i].Attributes["container"]);
else if (members[i].ParentNode != null && members[i].ParentNode.Name == "container" && getAttributeValue(members[i].ParentNode.Attributes["name"]) != "")
container = getAttributeValue(members[i].ParentNode.Attributes["name"]);
else
container = "@GlobalScope";
mem = new Member(getAttributeValue(members[i].Attributes["name"]));
mem.Container = container;
mem.Access = getAttributeValue(members[i].Attributes["access"]);
mem.Type = getAttributeValue(members[i].Attributes["type"]);
mem.Default = getAttributeValue(members[i].Attributes["default"]);
mem.Nullable = (getAttributeValue(members[i].Attributes["nullable"]).ToLower() == "true");
mem.Static = (getAttributeValue(members[i].Attributes["static"]).ToLower() == "true");
mem.Description = getNodeText(members[i]);
elements.Add(mem);
// Ensure container
if (containerExists(elements.ToArray(), mem.Container) == false)
{
con = new Container(mem.Container);
if (con.Name == "@GlobalScope")
con.Description = "@GlobalScope is a special container listing members and functions not associated with any specific container - they are most likely globally available.";
elements.Add(con);
}
}
// Get functions
for (int i = 0 ; i < functions.Count ; i++)
{
if (getAttributeValue(functions[i].Attributes["name"]) == "")
continue;
if (functions[i].Attributes["access"] != null && getAttributeValue(functions[i].Attributes["access"]).ToLower() != "private" && getAttributeValue(functions[i].Attributes["access"]).ToLower() != "public" && getAttributeValue(functions[i].Attributes["access"]).ToLower() != "protected")
continue;
if (functions[i].Attributes["static"] != null && getAttributeValue(functions[i].Attributes["static"]).ToLower() != "true" && getAttributeValue(functions[i].Attributes["static"]).ToLower() != "false")
continue;
if (includePrivate == false && functions[i].Attributes["access"] != null && getAttributeValue(functions[i].Attributes["access"]).ToLower() == "private")
continue;
string container = "";
if (getAttributeValue(functions[i].Attributes["container"]) != "")
container = getAttributeValue(functions[i].Attributes["container"]);
else if (functions[i].ParentNode != null && functions[i].ParentNode.Name == "container" && getAttributeValue(functions[i].ParentNode.Attributes["name"]) != "")
container = getAttributeValue(functions[i].ParentNode.Attributes["name"]);
else
container = "@GlobalScope";
func = new Function(getAttributeValue(functions[i].Attributes["name"]));
func.Container = container;
func.Access = getAttributeValue(functions[i].Attributes["access"]);
func.Returns = getAttributeValue(functions[i].Attributes["returns"]);
func.Static = (getAttributeValue(functions[i].Attributes["static"]).ToLower() == "true");
func.Virtual = (getAttributeValue(functions[i].Attributes["virtual"]).ToLower() == "true");
func.Description = getElementText(functions[i]["description"]);
func.SortSequence = i;
parameters = ((XmlElement)functions[i]).GetElementsByTagName("param");
for (int j = 0 ; j < parameters.Count ; j++)
{
if (getAttributeValue(parameters[j].Attributes["name"]) == "")
continue;
parm = new Parameter(getAttributeValue(parameters[j].Attributes["name"]));
parm.Type = getAttributeValue(parameters[j].Attributes["type"]);
parm.Default = getAttributeValue(parameters[j].Attributes["default"]);
parm.Description = getNodeText(parameters[j]);
parm.Nullable = (getAttributeValue(parameters[j].Attributes["nullable"]).ToLower() == "true");
func.Parameters.Add(parm);
}
elements.Add(func);
// Ensure container
if (containerExists(elements.ToArray(), func.Container) == false)
{
con = new Container(func.Container);
if (con.Name == "@GlobalScope")
con.Description = "@GlobalScope is a special container listing members and functions not associated with any specific container - they are most likely globally available.";
elements.Add(con);
}
}
return elements.ToArray();
}
private static bool containerExists(object[] elements, string name)
{
foreach (object elm in elements)
if (elm is Container && ((Container)elm).Name.Equals(name))
return true;
return false;
}
private static string getAttributeValue(XmlAttribute att)
{
return (att != null && att.Value != null ? att.Value.Trim() : "");
}
private static string getNodeText(XmlNode n)
{
return (n != null && n.InnerText != null ? n.InnerText.Trim() : "");
}
private static string getElementText(XmlElement e)
{
return (e != null && e.InnerText != null ? e.InnerText.Trim() : "");
}
}
}