-
-
Notifications
You must be signed in to change notification settings - Fork 924
/
SimpleExpression.pas
318 lines (289 loc) · 9.65 KB
/
SimpleExpression.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
unit SimpleExpression;
{
Inno Setup
Copyright (C) 1997-2024 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Evaluator for simple boolean expressions
Grammar:
-expression = term ('or' term)*
-term = factor ('and' factor)*
-factor = '(' expression ')' | 'not' factor | identifier ( '(' parameters ')' )
-identifier = letter | '_' (letter | number | '_' | '\')*
-parameters = string | number | boolean (',' string | number | boolean )*
As a special optional rule it can insert an 'or' if an identifier is encountered
at the place where an 'or' could be.
Function calls within parameter lists are currently not supported, except for calls
to the special ExpandConstant function.
}
interface
type
TSimpleExpression = class;
TSimpleExpressionOnEvalIdentifier = function(Sender: TSimpleExpression;
const Name: String; const Parameters: array of const): Boolean of object;
TSimpleExpressionOnExpandConstant = function(Sender: TSimpleExpression;
const Constant: String): String of object;
TSimpleExpression = class
private
FExpression: String;
FLazy: Boolean;
FOnEvalIdentifier: TSimpleExpressionOnEvalIdentifier;
FOnExpandConstant: TSimpleExpressionOnExpandConstant;
FParametersAllowed: Boolean;
FSingleIdentifierMode: Boolean;
FSilentOrAllowed: Boolean;
FTag: LongInt;
FText: PChar;
FTokenId: (tiEOF, tiOpenRound, tiCloseRound, tiComma, tiNot, tiAnd, tiOr, tiIdentifier, tiString, tiInteger, tiBoolean);
FToken: String;
function FReadParameters(var Parameters: array of const): Integer;
function FEvalIdentifier(const InLazyBranch: Boolean): Boolean;
function FEvalFactor(const InLazyBranch: Boolean): Boolean;
function FEvalTerm(const InLazyBranch: Boolean): Boolean;
function FEvalExpression(const InLazyBranch: Boolean): Boolean;
procedure Next;
public
function Eval: Boolean;
property Expression: String read FExpression write FExpression;
property Lazy: Boolean read FLazy write FLazy;
property OnEvalIdentifier: TSimpleExpressionOnEvalIdentifier read FOnEvalIdentifier write FOnEvalIdentifier;
property OnExpandConstant: TSimpleExpressionOnExpandConstant read FOnExpandConstant write FOnExpandConstant;
property ParametersAllowed: Boolean read FParametersAllowed write FParametersAllowed;
property SilentOrAllowed: Boolean read FSilentOrAllowed write FSilentOrAllowed;
property SingleIdentifierMode: Boolean read FSingleIdentifierMode write FSingleIdentifierMode;
property Tag: LongInt read FTag write FTag;
end;
implementation
uses
SysUtils;
procedure AssignStringToVarRec(var VarRec: TVarRec; const S: String);
begin
VarRec.VType := vtUnicodeString;
UnicodeString(VarRec.VUnicodeString) := S;
end;
{---}
procedure TSimpleExpression.Next;
var
P: PChar;
begin
{ Ignore whitespace }
while CharInSet(FText^ , [#1..#32]) do
Inc(FText);
case FText^ of
#0:
begin
FToken := '';
FTokenId := tiEOF;
end;
'(':
begin
FToken := FText^;
FTokenId := tiOpenRound;
Inc(FText);
end;
')':
begin
FToken := FText^;
FTokenId := tiCloseRound;
Inc(FText);
end;
',':
begin
FToken := FText^;
FTokenId := tiComma;
Inc(FText);
end;
'A'..'Z', 'a'..'z', '_':
begin
P := FText;
Inc(FText);
while CharInSet(FText^ , ['0'..'9', 'A'..'Z', 'a'..'z', '_', '\']) do
Inc(FText);
SetString(FToken, P, FText - P);
if CompareText(FToken, 'not') = 0 then
FTokenId := tiNot
else if CompareText(FToken, 'and') = 0 then
FTokenId := tiAnd
else if CompareText(FToken, 'or') = 0 then
FTokenId := tiOr
else if CompareText(FToken, 'true') = 0 then
FTokenId := tiBoolean
else if CompareText(FToken, 'false') = 0 then
FTokenId := tiBoolean
else
FTokenId := tiIdentifier;
end;
'0'..'9':
begin
P := FText;
Inc(FText);
while CharInSet(FText^ , ['0'..'9']) do
Inc(FText);
SetString(FToken, P, FText - P);
FTokenId := tiInteger;
end;
'''':
begin
FToken := '';
while True do begin
Inc(FText);
case FText^ of
#0: raise Exception.Create('Unexpected end of expression while reading string constant');
#10, #13: raise Exception.Create('Unterminated string');
else
if FText^ = '''' then begin
Inc(FText);
if FText^ <> '''' then
Break;
end;
FToken := FToken + FText^;
end;
end;
FTokenId := tiString;
end;
else
raise Exception.CreateFmt('Invalid symbol ''%s'' found', [FText^]);
end;
end;
function TSimpleExpression.FReadParameters(var Parameters: array of const): Integer;
var
I: Integer;
begin
I := 0;
while FTokenId in [tiIdentifier, tiString, tiInteger, tiBoolean] do begin
if I <= High(Parameters) then begin
if FTokenId = tiIdentifier then begin
{ Currently only calls to 'ExpandConstant' are supported in parameter lists }
if CompareText(FToken, 'ExpandConstant') <> 0 then
raise Exception.Create('Can only call function "ExpandConstant" within parameter lists');
Next;
if FTokenId <> tiOpenRound then
raise Exception.CreateFmt('Invalid token ''%s'' found', [FToken]);
Next;
if FTokenId <> tiString then
raise Exception.CreateFmt('Invalid token ''%s'' found', [FToken]);
if Assigned(FOnExpandConstant) then
AssignStringToVarRec(Parameters[I], FOnExpandConstant(Self, FToken))
else
AssignStringToVarRec(Parameters[I], FToken);
Next;
if FTokenId <> tiCloseRound then
raise Exception.CreateFmt('Invalid token ''%s'' found', [FToken]);
end else if FTokenId = tiString then begin
AssignStringToVarRec(Parameters[I], FToken);
end else if FTokenId = tiInteger then begin
Parameters[I].VType := vtInteger;
Parameters[I].vInteger := StrToInt(FToken);
end else begin
Parameters[I].VType := vtBoolean;
Parameters[I].vBoolean := CompareText(FToken, 'true') = 0;
end;
Inc(I);
end else
raise Exception.Create('Maximum number of parameters exceeded');
Next;
if FTokenId <> tiComma then
Break
else
Next;
end;
Result := I;
end;
function TSimpleExpression.FEvalIdentifier(const InLazyBranch: Boolean): Boolean;
var
Name: String;
Parameters: array[0..9] of TVarRec;
ParameterCount: Integer;
I: Integer;
begin
Name := FToken;
Next;
FillChar(Parameters, SizeOf(Parameters), 0);
try
if FParametersAllowed and (FTokenId = tiOpenRound) then begin
Next;
ParameterCount := FReadParameters(Parameters);
if FTokenId <> tiCloseRound then
raise Exception.CreateFmt('Invalid token ''%s'' found', [FToken]);
Next;
end else
ParameterCount := 0;
if not Lazy or not InLazyBranch then begin
if Assigned(FOnEvalIdentifier) then
Result := FOnEvalIdentifier(Self, Name, Slice(Parameters, ParameterCount))
else
Result := True;
end else
Result := True; { Lazy and in lazy branch, just return something }
finally
for I := High(Parameters) downto Low(Parameters) do
if Parameters[I].VType = vtUnicodeString then
AssignStringToVarRec(Parameters[I], '');
end
end;
function TSimpleExpression.FEvalFactor(const InLazyBranch: Boolean): Boolean;
begin
case FTokenId of
tiOpenRound:
begin
Next;
Result := FEvalExpression(InLazyBranch);
if FTokenId <> tiCloseRound then
raise Exception.Create('Invalid token');
Next;
end;
tiNot:
begin
Next;
Result := not FEvalFactor(InLazyBranch);
end;
tiIdentifier:
begin
Result := FEvalIdentifier(InLazyBranch);
end;
else
raise Exception.CreateFmt('Invalid token ''%s'' found', [FToken]);
end;
end;
function TSimpleExpression.FEvalTerm(const InLazyBranch: Boolean): Boolean;
begin
Result := FEvalFactor(InLazyBranch);
while FTokenId = tiAnd do begin
Next;
if not Result then begin
{ End term result known, but continue parsing }
FEvalFactor(True)
end else
Result := FEvalFactor(InLazyBranch);
end;
end;
function TSimpleExpression.FEvalExpression(const InLazyBranch: Boolean): Boolean;
begin
Result := FEvalTerm(InLazyBranch);
while (FTokenId = tiOr) or
(FSilentOrAllowed and (FTokenId = tiIdentifier)) do begin
if FTokenId = tiOr then
Next;
if Result then begin
{ End expression result known, but continue parsing }
FEvalTerm(True)
end else
Result := FEvalTerm(InLazyBranch);
end;
end;
{---}
function TSimpleExpression.Eval: Boolean;
begin
FText := PChar(FExpression);
Next;
if not FSingleIdentifierMode then
Result := FEvalExpression(False)
else begin
if FTokenId <> tiIdentifier then
raise Exception.CreateFmt('Invalid token ''%s'' found', [FToken]);
Result := FEvalIdentifier(False);
end;
if FTokenID <> tiEOF then
raise Exception.CreateFmt('Invalid token ''%s'' found', [FToken]);
end;
end.