-
Notifications
You must be signed in to change notification settings - Fork 1
/
se_scriptengine.pas
397 lines (352 loc) · 8.52 KB
/
se_scriptengine.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
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
unit se_scriptengine;
interface
uses
Classes;
//
// JVAL
//
// Script Engine
//
// TScriptEngine
const
MAX_STRING_SIZE = 1024;
type
TScriptEngine = class
private
sc_String: PChar;
sc_Integer: integer;
sc_Line: integer;
sc_End: boolean;
fNewLine: boolean;
fBracketLevel: integer;
fParenthesisLevel: integer;
ScriptBuffer: PChar;
ScriptPtr: PChar;
ScriptEndPtr: PChar;
StringBuffer: array [0..MAX_STRING_SIZE] of char;
ScriptSize: integer;
AlreadyGot: boolean;
lasterror: string;
protected
function fToken: string;
public
constructor Create(const tx: string); virtual;
destructor Destroy; override;
procedure Clear;
procedure SetText(const tx: string); virtual;
procedure ScriptError(const err: string); overload;
procedure ScriptError(const Fmt: string; const Args: array of const); overload;
function GetString: boolean;
function GetStringEOL: string;
procedure MustGetString;
procedure MustGetStringName(const name: string);
function GetInteger: boolean;
procedure MustGetInteger;
procedure UnGet;
function MatchString(const strs: TStringList): integer; overload;
function MatchString(const str: string): boolean; overload;
function MatchPosString(const str: string): boolean;
function MustMatchString(strs: TStringList): integer;
function Compare(const txt: string): boolean;
function GetLastError: string;
property _Integer: integer read sc_Integer;
property _String: string read fToken;
property _Finished: boolean read sc_End;
property _Line: integer read sc_Line;
property NewLine: boolean read fNewLine;
property BracketLevel: integer read fBracketLevel;
property ParenthesisLevel: integer read fParenthesisLevel;
end;
implementation
uses
SysUtils;
const
ASCII_QUOTE = '"';
ASCII_COMMENT1 = '/';
ASCII_COMMENT = $2F2F; // = '//'
// TScriptEngine
constructor TScriptEngine.Create(const tx: string);
begin
Inherited Create;
lasterror := '';
fBracketLevel := 0;
fParenthesisLevel := 0;
fNewLine := false;
Clear;
SetText(tx);
end;
destructor TScriptEngine.Destroy;
begin
Clear;
Inherited;
end;
function TScriptEngine.fToken: string;
begin
result := sc_String;
end;
procedure TScriptEngine.Clear;
begin
if ScriptSize > 0 then
FreeMem(ScriptBuffer, ScriptSize);
ScriptBuffer := nil;
ScriptSize := 0;
ScriptPtr := ScriptBuffer;
ScriptEndPtr := ScriptPtr + ScriptSize;
sc_Line := 1;
sc_End := false;
sc_String := @StringBuffer[0];
AlreadyGot := false;
end;
procedure TScriptEngine.SetText(const tx: string);
var
p: Pointer;
size: integer;
begin
size := Length(tx);
GetMem(p, size);
Move(tx[1], p^, size);
Clear;
ScriptBuffer := p;
ScriptSize := size;
ScriptPtr := ScriptBuffer;
ScriptEndPtr := ScriptPtr + ScriptSize;
sc_Line := 1;
sc_End := false;
sc_String := @StringBuffer[0];
AlreadyGot := false;
end;
procedure TScriptEngine.ScriptError(const err: string);
begin
lasterror := err;
end;
procedure TScriptEngine.ScriptError(const fmt: string; const Args: array of const);
var
s: string;
begin
s := Format(fmt, Args);
ScriptError(s);
end;
procedure TScriptEngine.MustGetString;
begin
if not GetString then
ScriptError('TScriptEngine.MustGetString(): Missing string at Line %d', [sc_Line]);
end;
procedure TScriptEngine.MustGetStringName(const name: string);
begin
MustGetString;
if not Compare(name) then
ScriptError('TScriptEngine.MustGetStringName(): "%s" expected at Line %d', [name, sc_Line]);
end;
function TScriptEngine.GetInteger: boolean;
var
code: integer;
begin
if GetString then
begin
val(sc_String, sc_Integer, code);
if code <> 0 then
begin
ScriptError(
'TScriptEngine.GetInteger(): Bad numeric constant "%s" at Line %d',
[sc_String, sc_Line]);
result := false;
exit;
end;
result := true;
end
else
result := false;
end;
procedure TScriptEngine.MustGetInteger;
begin
if not GetInteger then
ScriptError('TScriptEngine.MustGetInteger(): Missing integer at Line %d', [sc_Line]);
end;
procedure TScriptEngine.UnGet;
// Assumes there is a valid string in sc_String.
begin
AlreadyGot := true;
end;
function TScriptEngine.MatchString(const str: string): boolean;
begin
result := Compare(str);
end;
function TScriptEngine.MatchString(const strs: TStringList): integer;
// Returns the index of the first match to sc_String from the passed
// array of strings, or -1 if not found.
var
i: integer;
begin
for i := 0 to strs.Count - 1 do
begin
if Compare(strs.Strings[i]) then
begin
result := i;
exit;
end;
end;
result := -1;
end;
function TScriptEngine.MatchPosString(const str: string): boolean;
begin
result := Pos(UpperCase(str), strupper(sc_String)) > 0;
end;
function TScriptEngine.MustMatchString(strs: TStringList): integer;
var
i: integer;
begin
i := MatchString(strs);
if i = -1 then
ScriptError('TScriptEngine.MustMatchString(): List'#13#10'%s'#13#10'expected at Line %d', [strs.Text, sc_Line]);
result := i;
end;
function TScriptEngine.Compare(const txt: string): boolean;
begin
result := UpperCase(txt) = strupper(sc_String);
end;
function TScriptEngine.GetLastError: string;
begin
result := lasterror;
end;
function TScriptEngine.GetString: boolean;
var
txt: PChar;
foundToken: boolean;
begin
if AlreadyGot then
begin
AlreadyGot := false;
result := true;
exit;
end;
fNewLine := false;
foundToken := false;
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
result := false;
exit;
end;
while not foundToken do
begin
while (ScriptPtr^ <= Chr(32)) or (ScriptPtr^ in ['{', '}', '(', ')', ',']) do
begin
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
result := false;
exit;
end;
if ScriptPtr^ = '{' then
inc(fBracketLevel)
else if ScriptPtr^ = '}' then
begin
dec(fBracketLevel);
if fBracketLevel < 0 then
ScriptError('TScriptEngine.GetString(): Closing bracket "}" found at line %d without opening bracket "{"', [sc_Line]);
end
else if ScriptPtr^ = '(' then
inc(fParenthesisLevel)
else if ScriptPtr^ = ')' then
begin
dec(fParenthesisLevel);
if fParenthesisLevel < 0 then
ScriptError('TScriptEngine.GetString(): Closing parenthesis ")" found at line %d without opening parenthesis "("', [sc_Line]);
end
else if ScriptPtr^ = Chr(10) then
begin
inc(sc_Line);
fNewLine := true;
end;
inc(ScriptPtr);
end;
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
result := false;
exit;
end;
if ScriptPtr^ <> ASCII_COMMENT1 then
begin // Found a token
foundToken := true;
end
else
begin // Skip comment
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
result := false;
exit;
end;
inc(ScriptPtr);
if ScriptPtr^ = ASCII_COMMENT1 then
begin
while (ScriptPtr^ <> Chr(13)) and (ScriptPtr^ <> Chr(10)) do
begin
if ScriptPtr >= ScriptEndPtr then
begin
sc_End := true;
result := false;
exit;
end;
inc(ScriptPtr);
end;
inc(sc_Line);
fNewLine := true;
end;
end;
end;
txt := sc_String;
if ScriptPtr^ = ASCII_QUOTE then
begin // Quoted string
inc(ScriptPtr);
while ScriptPtr^ <> ASCII_QUOTE do
begin
txt^ := ScriptPtr^;
inc(txt);
inc(ScriptPtr);
if (ScriptPtr = ScriptEndPtr) or
(txt = @sc_String[MAX_STRING_SIZE - 1]) then
break;
end;
inc(ScriptPtr);
end
else
begin // Normal string
while (ScriptPtr^ > Chr(32)) and (ScriptPtr < ScriptEndPtr) and
(PWord(ScriptPtr)^ <> ASCII_COMMENT) and (not (ScriptPtr^ in ['{', '}', '(', ')', ','])) do
begin
txt^ := ScriptPtr^;
inc(txt);
inc(ScriptPtr);
if (ScriptPtr = ScriptEndPtr) or
(txt = @sc_String[MAX_STRING_SIZE - 1]) then
break;
end;
end;
txt^ := Chr(0);
result := true;
end;
function TScriptEngine.GetStringEOL: string;
begin
result := '';
if not GetString then
exit;
if fNewLine then
begin
AlreadyGot := true;
exit;
end;
result := sc_string;
while not sc_End and not fNewLine do
begin
GetString;
if fNewLine then
begin
AlreadyGot := true;
exit;
end;
result := result + ' ' + sc_string;
end;
end;
end.