-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CSharpParserImpl.pas
293 lines (251 loc) · 8.22 KB
/
CSharpParserImpl.pas
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
unit CSharpParserImpl;
interface
function ParseCSharpDeclaration(const Declaration: string): string;
implementation
uses
XMLCommentsParserImpl, DeclParserIntf, TextUtils, SysUtils;
type
TCSharpRoutineDeclaration = class(TRoutineDeclaration)
protected
function IsFunction(const AText: string): Boolean; override;
end;
{ TCSharpRoutineDeclaration }
function TCSharpRoutineDeclaration.IsFunction(const AText: string): Boolean;
begin
Result := AText <> 'void';
end;
function ParseVarDeclaration(const AText: string): string;
var
Parser: IRegExp2;
MatchCollection: IMatchCollection2;
Match: IMatch2;
Submatches: ISubMatches;
Visibility, TypeName, VarName: string;
begin
Parser := CoRegExp.Create;
Parser.Global := True;
Parser.Pattern :=
'(private|protected|public)?'+ // Visibility
'\s'+ // Space
'((\w+\.)*'+ // Namespace
'\w+)'+ // Type
'\s'+ // Space
'(\w+)'+ // Variable name
'(,\w+)*'+ // Optional, more variables
'(=[^;]*)?'+ // Default Value
';';
MatchCollection := Parser.Execute(AText) as IMatchCollection2;
Match := MatchCollection.Item[0] as IMatch2;
Submatches := Match.SubMatches as ISubMatches;
Visibility := Submatches[0];
TypeName := Submatches[1];
VarName := Submatches[3];
end;
function ParseTypeName(const AText: string): string;
var
Parser: IRegExp2;
MatchCollection: IMatchCollection2;
Match: IMatch2;
Submatches: ISubMatches;
Text: string;
begin
Parser := CoRegExp.Create;
Parser.Global := True;
// Look for one of the following
// 1) : Type
// 2) : [Namespace.[Namespace. ...]]Type
Parser.Pattern := '(in|out|ref)?\s*(((\w+\.)*\w+)(\[\])?)\s?(\w+)(=[^,]*)?(,)?';
MatchCollection := Parser.Execute(AText) as IMatchCollection2;
Match := MatchCollection.Item[0] as IMatch2;
Submatches := Match.SubMatches as ISubMatches;
Text := Submatches[1];
Result := Text;
end;
function ParseDefaultValue(const AText: string): string;
var
Parser: IRegExp2;
MatchCollection: IMatchCollection2;
Match: IMatch2;
Submatches: ISubMatches;
Text: string;
begin
Result := '';
Parser := CoRegExp.Create;
Parser.Global := True;
// Look for one of
// 1) = 'some string'
// 2) = Identifier
// 3) = nnnn (where nnnn is digits) // not supporting real numbers...
Parser.Pattern := '\s*\=\s*(''.*''|\w+|(\+|\-)?\d+\.\d+)';
MatchCollection := Parser.Execute(AText) as IMatchCollection2;
if MatchCollection.Count = 0 then exit;
Match := MatchCollection.Item[0] as IMatch2;
Submatches := Match.SubMatches as ISubMatches;
Text := Submatches[0];
Result := Text;
end;
procedure ParseParamNames(Routine: TRoutineDeclaration; const AText: string);
var
Parser: IRegExp2;
MatchCollection: IMatchCollection2;
Match: IMatch2;
Submatches: ISubMatches;
I: Integer;
ParamName, Token, TypeName, Default: string;
begin
TypeName := ParseTypeName(AText);
Default := '';
// Default := ParseDefaultValue(AText);
Parser := CoRegExp.Create;
Parser.IgnoreCase := True;
// Look for identifiers, or identifiers:
Parser.Pattern := '(in|out|ref)?\s*(((\w+\.)*\w+)(\[\d*\])?)\s?(\w+)(=[^,]*)?(,)?';
Parser.Global := False;
MatchCollection := Parser.Execute(AText) as IMatchCollection2;
for I := 0 to MatchCollection.Count - 1 do
begin
Match := MatchCollection[I] as IMatch2;
Token := Match.Value;
Submatches := Match.SubMatches as ISubMatches;
ParamName := Submatches[5];
Routine.AddParam(pkByVal, ParamName, TypeName, Default);
end;
end;
/// Parse parameters for methods
function ParseTypeDeclaration(Routine: TRoutineDeclaration; const AText: string): TParamType;
var
Parser: IRegExp2;
MatchCollection: IMatchCollection2;
Match: IMatch2;
I: Integer;
Text: string;
begin
Parser := CoRegExp.Create;
Parser.Global := True; Parser.IgnoreCase := True;
Parser.Pattern := '(in|out|ref)?\s*(((\w+\.)*\w+)(\[\d*\])?)\s?(\w+)(=[^,]*)?(,)?';
MatchCollection := Parser.Execute(AText) as IMatchCollection2;
for I := 0 to MatchCollection.Count - 1 do
begin
Match := MatchCollection[I] as IMatch2;
Text := Match.Value;
if Text <> '' then
ParseParamNames(Routine, Text);
end;
end;
function ParseReturnType(const AText: string): string;
var
Parser: IRegExp2;
MatchCollection: IMatchCollection2;
Match: IMatch2;
begin
Result := '';
Parser := CoRegExp.Create;
Parser.Global := True;
// Look for one of the following
// 1) : [Namespace.[Namespace. ...]]Type
Parser.Pattern := '((\w+)\.)*\w+';
MatchCollection := Parser.Execute(AText) as IMatchCollection2;
if MatchCollection.Count = 0 then
exit;
Match := MatchCollection.Item[0] as IMatch2;
Result := Match.Value;
end;
function Parse(const AText: string): TRoutineDeclaration;
var
Parser: IRegExp2;
MatchCollection: IMatchCollection2;
Match: IMatch2;
Submatches: ISubMatches;
Pattern, ReturnType,
IsClass, ClassName, RoutineType, RoutineName, Text: string;
Routine: TRoutineDeclaration;
begin
Routine := nil;
Parser := CoRegExp.Create;
try
Parser.IgnoreCase := True;
Parser.Global := False;
Pattern :=
'(((new|public|protected|internal|private|static|'+
'virtual|sealed|override|abstract|extern)*\s)*)'+ // Visibility
'((\w+\.)*(\w+))'+ // Return type, either a local type, or a namespace type
'\s?'+
'(\w*)'+ // Method name
'\('+ // Left bracket
'([^\)]*)'+ // optional parameters
'\)'; // right bracket
Parser.Pattern := Pattern;
if Parser.Test(AText) then // try method parsing
begin
MatchCollection := Parser.Execute(AText) as IMatchCollection2;
Match := MatchCollection.Item[0] as IMatch2;
Submatches := Match.SubMatches as ISubMatches;
IsClass := '';
ReturnType := Submatches[3];
RoutineType := ReturnType;
RoutineName := Submatches[6];
// Check for constructor
if (ReturnType <> '') and (RoutineName = '') then
ReturnType := '';
Routine := TCSharpRoutineDeclaration.Create(IsClass, RoutineType,
ClassName, RoutineName, ReturnType);
if Submatches.Count > 7 then
begin
Text := Submatches[7];
if Length(Text) > 0 then
begin
ParseTypeDeclaration(Routine, Text);
end;
end;
end else
begin // try parameter parsing
Routine := TRoutineDeclaration.Create('', '', '', '', '');
// raise Exception.CreateFmt('Unable to parse declaration: %s', [AText]);
end;
finally
Result := Routine;
end;
end;
function ParseCSharpDeclaration(const Declaration: string): string;
var
RoutineDeclaration: TRoutineDeclaration;
begin
RoutineDeclaration := Parse(StripSpaces(Declaration));
Result := RoutineDeclaration.XML;
RoutineDeclaration.Free;
end;
procedure TestVarDeclaration;
begin
ParseVarDeclaration('private System.Windows.Forms.Panel panel1;');
ParseVarDeclaration(StripSpaces('private System.Windows.Forms.Panel panel1, x;'));
end;
procedure TestParser;
procedure TestParse(const AText: string);
var
RoutineDecl: TRoutineDeclaration;
begin
if IsConsole then
begin
WriteLn(AText);
RoutineDecl := Parse(StripSpaces(AText));
WriteLn(RoutineDecl.XML);
RoutineDecl.Free;
ReadLn;
end;
end;
begin
if not IsConsole then exit;
TestParse('private System.Windows.Forms.MainMenu mainMenu1;');
TestParse('public override void IDERegister(out string[] aMenuNames, out int[] aMenuShortCuts)');
TestParse('protected override void Dispose (bool disposing)');
TestParse('public frmRemoteBossClient()');
TestParse('private blah1.blah2.blah3 WinForm3_Load()');
TestParse('public static blah1.blah2.blah3 WinForm3_Load(object sender)');
TestParse('public static blah1 WinForm3_Load(object sender)');
TestParse('public static void WinForm3_Load(object sender)');
TestParse('private void WinForm3_Load(object sender, System.EventArgs e)');
end;
initialization
TestParser;
finalization
end.