Repository navigation
Expand file tree
/
Copy pathKM_ScriptingType.pas
More file actions
420 lines (343 loc) · 12.7 KB
/
Copy pathKM_ScriptingType.pas
File metadata and controls
420 lines (343 loc) · 12.7 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
unit KM_ScriptingType;
interface
uses
System.Classes, System.Generics.Collections;
type
// There are these base types we want to share in the Wiki:
TKMTypeType = (ttEnum, ttRecord, ttArray, ttSetOfType, ttSetOfEnum);
TKMSortType = (stByAlphabet, stByDependancy);
// Single type element (
TKMScriptTypeElement = class
private
fName: string; // Name of the element
fDesc: string; // Description of the element
public
constructor Create(const aName, aDesc: string);
function ExportWikiBody: string;
property Name: string read fName;
property Desc: string read fDesc;
end;
// Collection of elements (e.g. enum)
TKMScriptTypeElements = class
private
fList: TList<TKMScriptTypeElement>;
public
constructor Create;
destructor Destroy; override;
procedure ParseFromStringList(aType: TKMTypeType; aStrings: TStringList);
function ExportCode(aType: TKMTypeType): string;
function ExportWikiBody: string;
property List: TList<TKMScriptTypeElement> read fList;
end;
// Single type info
// Documenter > Scripting > Type
TKMScriptType = class
private
fName: string;
fType: TKMTypeType;
fDescription: string;
fElements: TKMScriptTypeElements;
public
SortPriority: Integer;
constructor Create;
destructor Destroy; override;
procedure LoadFromStringList(aSource: TStringList);
function ExportCode: string;
function ExportWikiBody: string;
function ExportWikiLink: string;
property Name: string read fName;
property Typ: TKMTypeType read fType;
property Elements: TKMScriptTypeElements read fElements;
end;
implementation
uses
System.Math, System.StrUtils, System.SysUtils, System.Types,
KM_StringUtils,
KM_DocumenterTypes;
{ TKMScriptTypeElement }
constructor TKMScriptTypeElement.Create(const aName, aDesc: string);
begin
inherited Create;
fName := aName;
fDesc := aDesc;
end;
function TKMScriptTypeElement.ExportWikiBody: string;
begin
Result := '<sub>**' + fName + '**' + IfThen(fDesc <> '', ' // ' + fDesc) + '</sub>';
end;
{ TKMScriptTypeElements }
constructor TKMScriptTypeElements.Create;
begin
inherited;
fList := TList<TKMScriptTypeElement>.Create;
end;
destructor TKMScriptTypeElements.Destroy;
begin
FreeAndNil(fList);
inherited;
end;
procedure TKMScriptTypeElements.ParseFromStringList(aType: TKMTypeType; aStrings: TStringList);
var
I, K: Integer;
comment: string;
colonPos, commentPos: Integer;
declaration: string;
elements: TStringDynArray;
begin
case aType of
ttEnum: begin
aStrings[0] := ReplaceStr(aStrings[0], '(', '');
aStrings[aStrings.Count - 1] := ReplaceStr(aStrings[aStrings.Count - 1], ');', '');
for I := 0 to aStrings.Count - 1 do
if aStrings[I] <> '' then
begin
commentPos := Pos('//', aStrings[I]);
if commentPos > 0 then
begin
comment := Trim(RightStr(aStrings[I], Length(aStrings[I]) - commentPos - 1));
declaration := LeftStr(aStrings[I], commentPos - 1);
end else
begin
comment := '';
declaration := aStrings[I];
end;
elements := SplitString(declaration, ',');
for K := Low(elements) to High(elements) do
if Trim(elements[K]) <> '' then
fList.Add(TKMScriptTypeElement.Create(Trim(elements[K]), comment));
end;
end;
ttRecord: for I := 0 to aStrings.Count - 1 do
if (aStrings[I] <> '') and (Pos(':', aStrings[I]) > 0) then
begin
colonPos := Pos(':', aStrings[I]);
commentPos := Pos('//', aStrings[I]);
if (Pos('(', aStrings[I]) > colonPos)
or (Pos(')', aStrings[I]) > colonPos)
or InRange(Pos('function', aStrings[I]), 1, colonPos)
or InRange(Pos('procedure', aStrings[I]), 1, colonPos) then
Continue;
if commentPos > 0 then
begin
comment := Trim(RightStr(aStrings[I], Length(aStrings[I]) - commentPos - 1));
declaration := LeftStr(aStrings[I], commentPos - 1);
end else
begin
comment := '';
declaration := aStrings[I];
end;
// Trim trailing ";" for nicer look
declaration := Trim(ReplaceStr(declaration, ';', ''));
fList.Add(TKMScriptTypeElement.Create(declaration, comment));
end;
ttArray: begin
Assert(aStrings.Count = 1);
commentPos := Pos('//', aStrings[0]);
if commentPos > 0 then
begin
comment := Trim(RightStr(aStrings[0], Length(aStrings[0]) - commentPos - 1));
declaration := LeftStr(aStrings[0], commentPos - 1);
end else
begin
comment := '';
declaration := aStrings[0];
end;
declaration := Trim(ReplaceStr(declaration, ';', ''));
fList.Add(TKMScriptTypeElement.Create(declaration, comment));
end;
ttSetOfType:begin
Assert(aStrings.Count = 1);
// Single type declaration needs no comments
declaration := ReplaceStr(aStrings[0], ';', '');
fList.Add(TKMScriptTypeElement.Create(declaration, ''));
end;
ttSetOfEnum:begin
aStrings[0] := ReplaceStr(aStrings[0], 'set of', '');
aStrings[0] := ReplaceStr(aStrings[0], '(', '');
aStrings[aStrings.Count - 1] := ReplaceStr(aStrings[aStrings.Count - 1], ');', '');
for I := 0 to aStrings.Count - 1 do
if aStrings[I] <> '' then
begin
commentPos := Pos('//', aStrings[I]);
if commentPos > 0 then
begin
comment := Trim(RightStr(aStrings[I], Length(aStrings[I]) - commentPos - 1));
declaration := LeftStr(aStrings[I], commentPos - 1);
end else
begin
comment := '';
declaration := aStrings[I];
end;
elements := SplitString(declaration, ',');
for K := Low(elements) to High(elements) do
if Trim(elements[K]) <> '' then
fList.Add(TKMScriptTypeElement.Create(Trim(elements[K]), comment));
end;
end;
end;
end;
function TKMScriptTypeElements.ExportCode(aType: TKMTypeType): string;
const
WRAP_AROUND_COUNT = 5;
var
I: Integer;
begin
case aType of
ttEnum: begin
Result := #39'(';
for I := 0 to fList.Count - 1 do
begin
if (I > 0) and (I mod WRAP_AROUND_COUNT = 0) then
Result := Result + #39' +' + sLineBreak + ' '#39;
Result := Result + fList[I].fName + IfThen(I < fList.Count - 1, ', ');
end;
Result := Result + ')'#39;
end;
ttRecord: begin
Result := #39 + 'record '#39 + ' +' + sLineBreak;
for I := 0 to fList.Count - 1 do
Result := Result + ' '#39 + fList[I].fName + '; '#39 + ' +' + sLineBreak;
Result := Result + ' '#39'end;'#39;
end;
ttArray: Result := #39 + fList[0].fName + #39;
ttSetOfType: Result := #39 + fList[0].fName + #39;
ttSetOfEnum: begin
Result := #39'set of (';
for I := 0 to fList.Count - 1 do
begin
if (I > 0) and (I mod WRAP_AROUND_COUNT = 0) then
Result := Result + #39' +' + sLineBreak + ' '#39;
Result := Result + fList[I].fName + IfThen(I < fList.Count - 1, ', ');
end;
Result := Result + ')'#39;
end;
end;
end;
function TKMScriptTypeElements.ExportWikiBody: string;
var
I: Integer;
begin
Result := '';
for I := 0 to fList.Count - 1 do
Result := Result + IfThen(I > 0, '<br/>') + fList[I].ExportWikiBody;
end;
{ TKMScriptType }
constructor TKMScriptType.Create;
begin
inherited;
fElements := TKMScriptTypeElements.Create;
end;
destructor TKMScriptType.Destroy;
begin
FreeAndNil(fElements);
inherited;
end;
procedure TKMScriptType.LoadFromStringList(aSource: TStringList);
var
I: Integer;
srcLine: string;
enumStr: TStringList; // Needs to be a list because of comments
details: TStringList;
begin
details := TStringList.Create;
enumStr := TStringList.Create;
try
I := 0;
srcLine := aSource[I];
// if StartsStr(DOC_TAG_VERSION, srcLine) then
// begin
// fVersion := Trim(RightStrAfter(srcLine, ':'));
// Inc(I);
// srcLine := aSource[I];
// end;
// Descriptions are only added by lines starting with "//*"
// Repeat until no description tags are found
while StartsStr(DOC_TAG, srcLine) do
begin
details.Add(RightStrAfter(srcLine, DOC_TAG + ' '));
Inc(I);
srcLine := aSource[I];
end;
// Skip empty or "faulty" lines (e.g. comments not intended for wiki)
// until we get a type declaration (must start with T)
while not StartsStr('T', srcLine) do
begin
Inc(I);
srcLine := aSource[I];
end;
// Parse enum - detected by "("
if Pos('(', srcLine) > 0 then
begin
fType := ttEnum;
enumStr.Text := srcLine;
if I < aSource.Count - 1 then
repeat
Inc(I);
srcLine := aSource[I];
enumStr.Append(srcLine);
// Ignore closing brackets if they are in comments
until ((Pos(')', srcLine) <> 0) and (Pos('//', srcLine) = 0))
or ((Pos(')', srcLine) <> 0) and (Pos('//', srcLine) <> 0) and (Pos(')', srcLine) < Pos('//', srcLine)));
end;
// Parse record - detected by "record"
if Pos('record', srcLine) > 0 then
begin
fType := ttRecord;
enumStr.Text := srcLine;
if I < aSource.Count - 1 then
repeat
Inc(I);
srcLine := aSource[I];
enumStr.Append(srcLine);
until Pos('end;', srcLine) <> 0;
end;
// Parse array - detected by "array of"
if Pos('array of', srcLine) > 0 then
begin
fType := ttArray;
enumStr.Text := srcLine;
end;
// Parse set of type - detected by "set of A;"
if (Pos('set of', srcLine) > 0) and ((Pos('(', srcLine) = 0)) then
begin
fType := ttSetOfType;
enumStr.Text := srcLine;
end;
// Parse set of enum - detected by "set of (a, b);"
if (Pos('set of', srcLine) > 0) and ((Pos('(', srcLine) > 0)) then
begin
fType := ttSetOfEnum;
enumStr.Text := srcLine;
end;
// Name is the first word
fName := Trim(LeftStr(enumStr[0], Pos('=', enumStr[0]) - 1));
enumStr[0] := Trim(RightStrAfter(enumStr[0], '='));
fElements.ParseFromStringList(fType, enumStr);
// Now we can assemble Description, after we have detected and removed fParameters descriptions from it
for I := 0 to details.Count - 1 do
fDescription := fDescription + '<br/>' + details[I];
finally
details.Free;
enumStr.Free;
end;
end;
function TKMScriptType.ExportCode: string;
const
TEMPLATE = 'Sender.AddTypeS(''%s'', %s);';
begin
Result := Format(TEMPLATE, [fName, fElements.ExportCode(fType)]);
end;
function TKMScriptType.ExportWikiBody: string;
const
TEMPLATE = '| - | <sub>%s</sub> | <a id="%s">%s</a><sub>%s</sub> |';
TYPE_NAME: array [TKMTypeType] of string = ('enum', 'record', 'array', 'set', 'set');
begin
Result := Format(TEMPLATE, [TYPE_NAME[fType], fName, fName, fDescription]) + fElements.ExportWikiBody;
end;
function TKMScriptType.ExportWikiLink: string;
const
TEMPLATE = '* <a href="#%s">%s</a>';
begin
Result := Format(TEMPLATE, [fName, fName]);
end;
end.