-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKM_ScriptingTypes.pas
More file actions
746 lines (601 loc) · 20.8 KB
/
KM_ScriptingTypes.pas
File metadata and controls
746 lines (601 loc) · 20.8 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
unit KM_ScriptingTypes;
interface
uses
System.Classes, System.SysUtils, System.Types, System.Generics.Collections, System.Generics.Defaults,
System.Math, System.StrUtils,
KM_ParserTypes;
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;
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;
end;
// Single type info
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;
end;
// List of types
TKMScriptTypes = class
private
fOnLog: TProc<string>;
fList: TObjectList<TKMScriptType>;
procedure AssignSortOrder;
function ExportWikiBody: string;
function ExportWikiLinks: string;
function GetCount: Integer;
procedure LoadFromFile(const aInputFile: string);
procedure SortByName(aSortBy: TKMSortType);
public
constructor Create(aOnLog: TProc<string>);
destructor Destroy; override;
procedure Clear;
property Count: Integer read GetCount;
procedure LoadFromFiles(const aSourceMask: string);
procedure ExportCode(const aCodeFile: string);
procedure ExportWiki(const aTemplateFile, aOutputFile: string);
end;
implementation
uses
System.IOUtils,
KM_ScriptingConsts, KM_StringUtils;
{ 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('//* 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('//*', srcLine) do
begin
details.Add(RightStrAfter(srcLine, '* '));
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;
{ TKMScriptTypes }
procedure TKMScriptTypes.Clear;
begin
fList.Clear;
end;
constructor TKMScriptTypes.Create(aOnLog: TProc<string>);
begin
inherited Create;
fOnLog := aOnLog;
fList := TObjectList<TKMScriptType>.Create(
TComparer<TKMScriptType>.Construct(
function (const A, B: TKMScriptType): Integer
begin
Result := CompareValue(A.SortPriority, B.SortPriority);
if Result = 0 then
// Case-sensitive compare, since we use CamelCase and it looks nicer that way
Result := CompareText(A.fName, B.fName);
end));
end;
destructor TKMScriptTypes.Destroy;
begin
FreeAndNil(fList);
inherited;
end;
// Scans source contents and puts it all in proper formatting for most wikis.
procedure TKMScriptTypes.LoadFromFile(const aInputFile: string);
var
slSource: TStringList;
I: Integer;
srcLine: string;
sl: TStringList;
sectionStarted: Boolean;
recordStarted: Boolean;
begin
slSource := TStringList.Create;
try
slSource.LoadFromFile(aInputFile);
// Assemble method sections 1 by 1
{
//* Enum
TKMSomeType = (stNone,
//
stSomething);
//* Record
// ignore this comment
TKMSomeType = record
A,B: Integer;
function Some: Byte;
end;
//* Array of
TKMSomeType = array of TKMSomething;
//* Set of
TKMSomeType = set of TKMSomething;
}
sectionStarted := False;
recordStarted := False;
sl := TStringList.Create;
for I := 0 to slSource.Count - 1 do
begin
srcLine := Trim(slSource[I]);
if not sectionStarted and StartsStr('//* ', srcLine) then
begin
sectionStarted := True;
sl.Clear;
end;
if sectionStarted then
if not StartsStr('//', srcLine) or StartsStr('//*', srcLine) then
sl.Append(srcLine);
if sectionStarted and not StartsStr('//', srcLine) and (Pos('record', srcLine) > 0) then
recordStarted := True;
if sectionStarted and not recordStarted and (StartsStr('procedure', srcLine) or StartsStr('function', srcLine)) then
sectionStarted := False;
if sectionStarted and recordStarted and (Pos('end;', srcLine) > 0) then
recordStarted := False;
if sectionStarted and not recordStarted and not StartsStr('//*', srcLine) and (Pos(';', srcLine) > 0) then
begin
sectionStarted := False;
fList.Add(TKMScriptType.Create);
fList.Last.LoadFromStringList(sl);
end;
end;
sl.Free;
finally
slSource.Free;
end;
end;
procedure TKMScriptTypes.LoadFromFiles(const aSourceMask: string);
var
s: TStringDynArray;
I: Integer;
begin
Clear;
// Get all files matching the mask
s := TDirectory.GetFiles(ExtractFilePath(aSourceMask), ExtractFileName(aSourceMask), TSearchOption.soAllDirectories);
for I := Low(s) to High(s) do
LoadFromFile(s[I]);
fOnLog(Format('%d %s parsed', [GetCount, AREA_INFO[paTypes].Short]));
end;
function TKMScriptTypes.ExportWikiBody: string;
var
I: Integer;
begin
Result := '';
for I := 0 to fList.Count - 1 do
Result := Result + IfThen(I > 0, sLineBreak) + fList[I].ExportWikiBody;
end;
function TKMScriptTypes.ExportWikiLinks: string;
var
I: Integer;
begin
Result := '';
for I := 0 to fList.Count - 1 do
Result := Result + IfThen(I > 0, sLineBreak) + fList[I].ExportWikiLink;
end;
function TKMScriptTypes.GetCount: Integer;
begin
Result := fList.Count;
end;
procedure TKMScriptTypes.AssignSortOrder;
function FindType(aName: string): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to fList.Count - 1 do
if fList[I].fName = aName then
Exit(I);
end;
var
I, K: Integer;
use: array of TList<Integer>;
order: array of Integer;
id: Integer;
s: string;
orderLoop: Integer;
needsAnotherLoop: Boolean;
begin
SetLength(use, fList.Count);
for I := 0 to fList.Count - 1 do
use[I] := TList<Integer>.Create;
SetLength(order, fList.Count);
for I := 0 to fList.Count - 1 do
order[I] := -1;
for I := 0 to fList.Count - 1 do
case fList[I].fType of
ttRecord: begin
for K := 0 to fList[I].fElements.fList.Count - 1 do
begin
s := RightStrAfter(fList[I].fElements.fList[K].fName, ': ');
id := FindType(s);
if id <> -1 then
use[I].Add(id);
end;
end;
ttArray: begin
s := ReplaceStr(fList[I].fElements.fList[0].fName, 'array of ', '');
id := FindType(s);
if id <> -1 then
use[I].Add(id);
end;
ttSetOfType: begin
s := ReplaceStr(fList[I].fElements.fList[0].fName, 'set of ', '');
id := FindType(s);
if id <> -1 then
use[I].Add(id);
end;
end;
orderLoop := 0;
repeat
// Demark items without dependencies
for I := 0 to fList.Count - 1 do
if (order[I] = -1) and (use[I].Count = 0) then
order[I] := orderLoop;
// Trim demarked items
for I := 0 to fList.Count - 1 do
for K := use[I].Count - 1 downto 0 do
if order[use[I][K]] = 0 then
use[I].Delete(K);
// Check if all items are demarked
needsAnotherLoop := False;
for I := 0 to fList.Count - 1 do
if (order[I] = -1) then
needsAnotherLoop := True;
Inc(orderLoop);
until not needsAnotherLoop or (orderLoop = 9);
for I := 0 to fList.Count - 1 do
fList[I].SortPriority := order[I];
end;
procedure TKMScriptTypes.SortByName(aSortBy: TKMSortType);
begin
case aSortBy of
stByAlphabet: ; // Already sorted by default
stByDependancy: AssignSortOrder;
end;
fList.Sort;
end;
procedure TKMScriptTypes.ExportCode(const aCodeFile: string);
var
sl: TStringList;
secStart, secEnd, pad: Integer;
I: Integer;
begin
if not FileExists(aCodeFile) then Exit;
SortByName(stByDependancy);
sl := TStringList.Create;
try
sl.LoadFromFile(aCodeFile);
FindStartAndFinish(sl, AREA_INFO[paTypes].RegTag, secStart, secEnd, pad);
if secStart <> -1 then
begin
for I := secEnd downto secStart do
sl.Delete(I);
for I := fList.Count - 1 downto 0 do
begin
sl.Insert(secStart, DupeString(' ', pad) + fList[I].ExportCode);
if (I > 0) and (fList[I].SortPriority <> fList[I-1].SortPriority) then
sl.Insert(secStart, DupeString(' ', pad) + '// Dependent types of level ' + IntToStr(fList[I].SortPriority));
end;
end;
sl.SaveToFile(aCodeFile);
finally
sl.Free;
end;
fOnLog(Format('%d %s exported into Code checks', [fList.Count, AREA_INFO[paTypes].Short]));
end;
procedure TKMScriptTypes.ExportWiki(const aTemplateFile, aOutputFile: string);
var
sl: TStringList;
exportPath: string;
begin
// Without template we cant generate output
if aTemplateFile = '' then Exit;
SortByName(stByAlphabet);
sl := TStringList.Create;
sl.LoadFromFile(aTemplateFile);
sl.Text := StringReplace(sl.Text, '{LINKS}', ExportWikiLinks, []);
sl.Text := StringReplace(sl.Text, '{BODY}', ExportWikiBody, []);
exportPath := ExpandFileName(ExtractFilePath(ParamStr(0)) + aOutputFile);
if not DirectoryExists(ExtractFileDir(exportPath)) then
ForceDirectories(ExtractFileDir(exportPath));
sl.SaveToFile(aOutputFile);
sl.Free;
fOnLog(Format('%d %s exported into Wiki', [fList.Count, AREA_INFO[paTypes].Short]));
end;
end.