-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKM_ScriptingParameters.pas
More file actions
280 lines (224 loc) · 7.52 KB
/
KM_ScriptingParameters.pas
File metadata and controls
280 lines (224 loc) · 7.52 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
unit KM_ScriptingParameters;
interface
uses
System.Classes, System.SysUtils, System.Types, System.Generics.Collections,
System.StrUtils;
type
// Single parameter(argument) info
TKMScriptParameter = class
private
fName: string;
fModifier: string;
fVarType: string;
fDesc: string;
public
constructor Create(const aName, aModifier, aVarType, aDesc: string);
property Modifier: string read fModifier;
property VarType: string read fVarType;
function ExportWikiBody: string;
function ExportCodeSignature: string;
end;
// List of parameters(arguments) of a method
TKMScriptParameters = class
private
fList: TObjectList<TKMScriptParameter>;
procedure SplitIntoTokens(const aArguments: string; aTokenList: TStringList);
function FindDescription(const aName: string; aDescriptions: TStringList): string;
procedure CollectParameters(aTokenList: TStringList; aDescriptions: TStringList);
function GetCount: Integer;
function GetParameter(aIndex: Integer): TKMScriptParameter;
public
constructor Create;
destructor Destroy; override;
property Count: Integer read GetCount;
property Parameters[aIndex: Integer]: TKMScriptParameter read GetParameter; default;
function ExportWikiBody: string;
function ExportCodeSignature: string;
procedure AdjoinPairs;
procedure DowngradeTypes;
procedure ParseFromString(const aArguments: string; aDescriptions: TStringList);
end;
implementation
uses
KM_ScriptingConsts, KM_StringUtils;
{ TKMScriptParameter }
constructor TKMScriptParameter.Create(const aName, aModifier, aVarType, aDesc: string);
begin
inherited Create;
fName := aName;
fModifier := aModifier;
fVarType := aVarType;
fDesc := aDesc;
end;
function TKMScriptParameter.ExportCodeSignature: string;
begin
Result := IfThen(fModifier <> '', fModifier + ' ') + fName + ': ' + fVarType;
end;
function TKMScriptParameter.ExportWikiBody: string;
const
TEMPLATE = '**%s%s**: %s;%s';
begin
//todo -cPractical: add link instead of text when our custom script type is mentioned
Result := Format(TEMPLATE, [IfThen(fModifier <> '', fModifier + ' '), fName, fVarType, IfThen(fDesc <> '', ' // _' + fDesc + '_')]);
end;
{ TKMScriptParameters }
constructor TKMScriptParameters.Create;
begin
inherited;
fList := TObjectList<TKMScriptParameter>.Create;
end;
destructor TKMScriptParameters.Destroy;
begin
FreeAndNil(fList);
inherited;
end;
function TKMScriptParameters.GetCount: Integer;
begin
Result := fList.Count;
end;
function TKMScriptParameters.GetParameter(aIndex: Integer): TKMScriptParameter;
begin
Result := fList[aIndex];
end;
function TKMScriptParameters.ExportWikiBody: string;
var
I: Integer;
begin
Result := '';
for I := 0 to fList.Count - 1 do
Result := Result + fList[I].ExportWikiBody + IfThen(I <> fList.Count - 1, ' <br/> ');
end;
// Method signature for the "RegisterMethodCheck(c, '...');" in PS engine
function TKMScriptParameters.ExportCodeSignature: string;
const
ROUGH_LINE_LIMIT = 60;
var
I: Integer;
line: Integer;
begin
Result := '';
line := 0;
for I := 0 to fList.Count - 1 do
begin
Result := Result + IfThen(I = 0, '(') + fList[I].ExportCodeSignature + IfThen(I <> fList.Count - 1, '; ', ')');
Inc(line, Length(fList[I].ExportCodeSignature));
if (I <> fList.Count - 1) and (line > ROUGH_LINE_LIMIT) then
begin
Result := Result + #39' +' + sLineBreak + ' '#39;
line := 0;
end;
end;
end;
// Take a string of arguments and split it into list of tokens
procedure TKMScriptParameters.SplitIntoTokens(const aArguments: string; aTokenList: TStringList);
var
I: Integer;
args: string;
begin
args := ReplaceStr(aArguments, ',', ' , ');
args := ReplaceStr(args, ':', ' : ');
args := ReplaceStr(args, ';', ' ; ');
StrSplit(args, ' ', aTokenList);
// Assemble the "[array] + [of] + [something]"
// Do it first, cos we can have [array of const]
for I := aTokenList.Count - 1 downto 0 do
if SameText(aTokenList[I], 'array') then
begin
aTokenList[I] := aTokenList[I] + ' ' + aTokenList[I + 1] + ' ' + aTokenList[I + 2];
aTokenList.Delete(I + 2);
aTokenList.Delete(I + 1);
end;
// Remove single 'const' modifiers
for I := aTokenList.Count - 1 downto 0 do
if SameText(aTokenList[I], 'const') then
aTokenList.Delete(I);
end;
function TKMScriptParameters.FindDescription(const aName: string; aDescriptions: TStringList): string;
var
I: Integer;
begin
// Find the parameter description (and remove it from source)
Result := '';
// Parameter names are identified by the [Name:]
for I := 0 to aDescriptions.Count - 1 do
if StartsStr(aName + ':', aDescriptions[I]) then
begin
Result := StrSubstring(aDescriptions[I], Pos(':', aDescriptions[I]) + 1);
aDescriptions.Delete(I);
Exit;
end;
end;
procedure TKMScriptParameters.CollectParameters(aTokenList: TStringList; aDescriptions: TStringList);
var
I: Integer;
varModifier, varType: string;
list: array of record Modifier, Name, &Type: string; end;
begin
SetLength(list, aTokenList.Count);
varModifier := '';
varType := '';
// Forward pass to assign modifiers (modifier applies to all vars until ":")
for I := 1 to aTokenList.Count - 1 do
begin
if TokenIsModifier(aTokenList[I-1]) then
varModifier := FixModifierCase(aTokenList[I-1]);
if aTokenList[I] = ':' then
varModifier := '';
list[I].Modifier := varModifier;
end;
// Backward pass to assign types (type applies to all vars until ";")
for I := aTokenList.Count - 1 downto 0 do
begin
if (I < aTokenList.Count - 1)and (aTokenList[I+1] = ':') then
varType := FixTypeCase(aTokenList[I+2]);
if (aTokenList[I] = ';')
or TokenIsModifier(aTokenList[I]) then
varType := '';
list[I].&Type := varType;
end;
// Now we can collect names (separate step to simplify the debug process)
for I := 0 to aTokenList.Count - 1 do
if (aTokenList[I] <> ',')
and (list[I].&Type <> '') then
list[I].Name := aTokenList[I];
// Finally add to the list
for I := 0 to aTokenList.Count - 1 do
if (list[I].Name <> '') then
fList.Add(TKMScriptParameter.Create(list[I].Name, list[I].Modifier, list[I].&Type, FindDescription(aTokenList[I], aDescriptions)));
end;
// Adjoin pairs wherever possible
procedure TKMScriptParameters.AdjoinPairs;
var
I: Integer;
begin
for I := fList.Count - 1 downto 1 do
if ((fList[I].fName = 'Y') and (fList[I-1].fName = 'X')
or (fList[I].fName = 'aY') and (fList[I-1].fName = 'aX')
or (fList[I].fName = 'B') and (fList[I-1].fName = 'A')
or (fList[I].fName = 'aMax') and (fList[I-1].fName = 'aMin'))
and (fList[I].fDesc = fList[I-1].fDesc) then
begin
fList[I-1].fName := fList[I-1].fName + ', ' + fList[I].fName;
fList.Delete(I);
end;
end;
procedure TKMScriptParameters.DowngradeTypes;
var
I: Integer;
begin
for I := 0 to fList.Count - 1 do
fList[I].fVarType := TryEventTypeToAlias(fList[I].fVarType);
end;
procedure TKMScriptParameters.ParseFromString(const aArguments: string; aDescriptions: TStringList);
var
tokenList: TStringList;
begin
tokenList := TStringList.Create;
try
SplitIntoTokens(aArguments, tokenList);
CollectParameters(tokenList, aDescriptions);
finally
FreeAndNil(tokenList);
end;
end;
end.