Repository navigation
Expand file tree
/
Copy pathKM_ScriptingMethodParameters.pas
More file actions
279 lines (223 loc) · 7.66 KB
/
Copy pathKM_ScriptingMethodParameters.pas
File metadata and controls
279 lines (223 loc) · 7.66 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
unit KM_ScriptingMethodParameters;
interface
uses
System.Classes, System.SysUtils, System.Types, System.Generics.Collections,
System.StrUtils;
type
// Single parameter(argument) info
TKMScriptingMethodParameter = 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
TKMScriptingMethodParameters = class
private
fList: TObjectList<TKMScriptingMethodParameter>;
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): TKMScriptingMethodParameter;
public
constructor Create;
destructor Destroy; override;
property Count: Integer read GetCount;
property Parameters[aIndex: Integer]: TKMScriptingMethodParameter read GetParameter; default;
function ExportWikiBody: string;
function ExportCodeSignature: string;
procedure AdjoinPairs;
procedure DowngradeTypes;
procedure ParseFromString(const aArguments: string; aDescriptions: TStringList);
end;
implementation
uses
KM_ScriptingMethodConsts, KM_StringUtils;
{ TKMScriptingMethodParameter }
constructor TKMScriptingMethodParameter.Create(const aName, aModifier, aVarType, aDesc: string);
begin
inherited Create;
fName := aName;
fModifier := aModifier;
fVarType := aVarType;
fDesc := aDesc;
end;
function TKMScriptingMethodParameter.ExportCodeSignature: string;
begin
Result := IfThen(fModifier <> '', fModifier + ' ') + fName + ': ' + fVarType;
end;
function TKMScriptingMethodParameter.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;
{ TKMScriptingMethodParameters }
constructor TKMScriptingMethodParameters.Create;
begin
inherited;
fList := TObjectList<TKMScriptingMethodParameter>.Create;
end;
destructor TKMScriptingMethodParameters.Destroy;
begin
FreeAndNil(fList);
inherited;
end;
function TKMScriptingMethodParameters.GetCount: Integer;
begin
Result := fList.Count;
end;
function TKMScriptingMethodParameters.GetParameter(aIndex: Integer): TKMScriptingMethodParameter;
begin
Result := fList[aIndex];
end;
function TKMScriptingMethodParameters.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 TKMScriptingMethodParameters.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 TKMScriptingMethodParameters.SplitIntoTokens(const aArguments: string; aTokenList: TStringList);
var
args: string;
begin
args := ReplaceStr(aArguments, ',', ' , ');
args := ReplaceStr(args, ':', ' : ');
args := ReplaceStr(args, ';', ' ; ');
var sa := SplitString(args, ' ');
// Assemble the "[array] + [of] + [something]"
// Do it first, cos we can have [array of const]
for var I := High(sa) downto 0 do
if SameText(sa[I], 'array') then
begin
sa[I] := sa[I] + ' ' + sa[I + 1] + ' ' + sa[I + 2];
sa[I + 2] := '';
sa[I + 1] := '';
end;
// Skip single 'const' modifiers
for var I := 0 to High(sa) do
if (sa[I] <> '') and not SameText(sa[I], 'const') then
aTokenList.Add(sa[I]);
end;
function TKMScriptingMethodParameters.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 := Trim(RightStrAfter(aDescriptions[I], ':'));
aDescriptions.Delete(I);
Exit;
end;
end;
procedure TKMScriptingMethodParameters.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(TKMScriptingMethodParameter.Create(list[I].Name, list[I].Modifier, list[I].&Type, FindDescription(aTokenList[I], aDescriptions)));
end;
// Adjoin pairs wherever possible
procedure TKMScriptingMethodParameters.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 TKMScriptingMethodParameters.DowngradeTypes;
var
I: Integer;
begin
for I := 0 to fList.Count - 1 do
fList[I].fVarType := TryEventTypeToAlias(fList[I].fVarType);
end;
procedure TKMScriptingMethodParameters.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.