-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
260 lines (231 loc) · 9.49 KB
/
Program.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using OfficeOpenXml;
using OfficeOpenXml.Table;
using OfficeOpenXml.Style;
using System.Drawing;
namespace PasParser
{
class Program
{
static void Main(string[] args)
{
//Path to file should be in args
if(args.Length != 0)
{
var parser = new PascalParser(args[0]);
parser.Parse();
}
}
private class PascalParser
{
private string _filePath = string.Empty;
private KeyWordType _currentKeyWord = KeyWordType.NULL;
private KeyWordType _currentAccessType = KeyWordType.PRIVATE;
public PascalParser(string path)
{
_filePath = path;
}
public void Parse()
{
try
{
using(var stream = new StreamReader(_filePath))
{
string line = string.Empty;
while(!(line = stream.ReadLine()).Contains("implementation"))
{
if(ParseCodeLine(KeyWordType.CLASS,line,ClassDocumentation)) continue;
if(ParseCodeLine(KeyWordType.FIELD,line,FieldDocumentation)) continue;
if(ParseCodeLine(KeyWordType.PROCEDURE,line,ProcedureDocumentation)) continue;
if(ParseCodeLine(KeyWordType.FUNCTION,line,ProcedureDocumentation)) continue;
if(ParseCodeLine(KeyWordType.PUBLIC,line,(x)=>{_currentAccessType = KeyWordType.PUBLIC;} )) continue;
if(ParseCodeLine(KeyWordType.PRIVATE,line,(x)=>{_currentAccessType = KeyWordType.PRIVATE;} )) continue;
if(ParseCodeLine(KeyWordType.PROTECTED,line,(x)=>{_currentAccessType = KeyWordType.PROTECTED;} )) continue;
}
}
}
catch(Exception e)
{
throw e;
}
}
private bool ParseCodeLine(KeyWordType wordType,string line,Action<string> documentationAction)
{
var checker = new TextChecker();
var matches = checker.CheckText(wordType,line);
if(matches.Count > 0)
{
foreach(Match val in matches)
{
documentationAction(val.Value);
}
return true;
}
else
{
return false;
}
}
private void ProcedureDocumentation(string val)
{
if(_currentKeyWord != KeyWordType.PROCEDURE)
{
string[] headers = {"Тип","Название","Аргументы","Возвращаемое значение","Видимость","Описание"};
ExcelExporter.AppendLine(Color.GreenYellow,headers);
_currentKeyWord = KeyWordType.PROCEDURE;
}
val = val.Replace(";",string.Empty);
var docParams = new List<string>();
var strings = val.Split('(');
if(strings.Length > 1)
{
var typeAndName = strings[0].Split(' ');
docParams.Add(typeAndName[0]);
docParams.Add(typeAndName[1]);
var args = strings[1].Split(')');
docParams.Add(args[0]);
if(args.Length > 1)
{
var res = args[1].Split(':');
if(res.Length > 1)
{
docParams.Add(res[1]);
}
else
{
docParams.Add("-");
}
}
}
else
{
var res = strings[0].Split(':');
var typeAndName = res[0].Split(' ');
docParams.Add(typeAndName[0]);
docParams.Add(typeAndName[1]);
docParams.Add("-");
if(res.Length > 1)
{
docParams.Add(res[1]);
}
else
{
docParams.Add("-");
}
}
docParams.Add(_currentAccessType.ToString().ToLower());
ExcelExporter.AppendLine(Color.White,docParams.ToArray());
}
private void FieldDocumentation(string val)
{
if(_currentKeyWord != KeyWordType.FIELD)
{
string[] headers = {"Название","Тип","Видимость","Описание"};
ExcelExporter.AppendLine(Color.GreenYellow,headers);
_currentKeyWord = KeyWordType.FIELD;
}
val = val.Replace(";",string.Empty);
var docParams = new List<string>();
var res = val.Split(':');
docParams.Add(res[0]);
docParams.Add(res[1]);
docParams.Add(_currentAccessType.ToString().ToLower());
ExcelExporter.AppendLine(Color.White,docParams.ToArray());
}
private void ClassDocumentation(string val)
{
if(_currentKeyWord != KeyWordType.CLASS)
{
string[] headers = {"Название","Тип","Базовый класс и интерфейсы","Описание"};
ExcelExporter.AppendLine(Color.GreenYellow,headers);
_currentKeyWord = KeyWordType.CLASS;
_currentAccessType = KeyWordType.PRIVATE;
}
val = val.Replace(";",string.Empty);
var docParams = new List<string>();
var typeAndName = val.Split('=');
docParams.Add(typeAndName[0]);
var parent = typeAndName[1].Split('(');
docParams.Add(parent[0]);
if(parent.Length > 1) docParams.Add(parent[1].Replace(")",string.Empty));
ExcelExporter.AppendLine(Color.White,docParams.ToArray());
}
}
private static class ExcelExporter
{
private static int _currentLine = 0;
public static void AppendLine(Color fillColor,params string[] values)
{
_currentLine++;
var fileInfo = new FileInfo("Unit.xlsx");
using(var package = new ExcelPackage(fileInfo))
{
int i = 1;
ExcelWorksheet worksheet = null;
if(package.Workbook.Worksheets.Count == 0)
{
worksheet = package.Workbook.Worksheets.Add("New");
}
else
{
worksheet = package.Workbook.Worksheets["New"];
}
foreach(var val in values)
{
worksheet.Cells[_currentLine,i].Value = val;
worksheet.Cells[_currentLine,i].Style.Fill.PatternType =ExcelFillStyle.Solid;
worksheet.Cells[_currentLine,i].Style.Fill.BackgroundColor.SetColor(fillColor);
worksheet.Cells[_currentLine,i].Style.Border.BorderAround(ExcelBorderStyle.Thin);
i++;
}
package.Save();
}
}
}
private class TextChecker
{
private Dictionary<KeyWordType,string> _keyWords = new Dictionary<KeyWordType, string>
{
{KeyWordType.UNIT,@"^unit\s*\w*;"},
{KeyWordType.CLASS,@"^\w*\s*=\s*class\D*"},
{KeyWordType.PROCEDURE,@"^procedure\s*\D*"},
{KeyWordType.FUNCTION,@"^function\s*\D*"},
{KeyWordType.FIELD,@"^\w*\s*:\s*\w*"},
{KeyWordType.PUBLIC,@"^public"},
{KeyWordType.PROTECTED,@"^protected"},
{KeyWordType.PRIVATE,@"^private"}
};
public MatchCollection CheckText(KeyWordType wordType,string text)
{
var reg = string.Empty;
text = text.Trim();
_keyWords.TryGetValue(wordType,out reg);
if(reg != null)
{
var regex = new Regex(reg,RegexOptions.IgnoreCase);
var matches = regex.Matches(text);
return matches;
}
else
{
return null;
}
}
}
private enum KeyWordType
{
UNIT,
CLASS,
PROCEDURE,
FUNCTION,
FIELD,
PUBLIC,
PRIVATE,
PROTECTED,
NULL
}
}
}