forked from JOGAsoft/EBC-controller
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lineparser.pas
executable file
·417 lines (363 loc) · 11.5 KB
/
lineparser.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
{ simple parser to parse the script file for ebcc
Armin Diehl <ad@ardiehl.de> 09/2023
May be somewhat inadequate since i have not programmed in pascal for more
than 25 years }
{$mode objfpc}
unit lineparser;
// if set, any chars (a..z) appended to numbers (w/o white spaces) will be
// ignored, e.g. Cells=6Peaces or curr=6Amps
// or even curr=6Amps.And23Milliamps
{$define AllowAppendCharsToNumber}
interface
uses sysutils,classes;
const
TK_ERR = -100;
TK_ERR_COMMENT = -101;
TK_EOL = -102;
TK_COMMENT = -103;
TK_LAST = -99;
TK_EQUAL = -104;
TK_VALUE = -105;
TK_UNKNOWN = -1;
type
TTokenType = (TTokenNone,TTokenCommand,TTokenParam);
TLineParserException = Class(Exception)
Constructor create (aLineNumber, aLinePos:integer; aMessage: string);
public
err:integer;
LinePos:integer;
LineNo:integer;
end;
TLineParserObject=class
constructor create(aName:string; aValue:integer; aValueRequired : boolean);
public
name :string;
value:integer;
valueRequired : boolean;
end;
TLineParserObjectList=class(TFPList)
destructor destroy; override;
public
function findName (name : string) : integer;
function requiresValue (name : string) : boolean;
end;
TLineParser=class
constructor create;
destructor destroy; override;
private
//public
commands : TLineParserObjectList;
params : TLineParserObjectList;
line : string;
lineNo : integer;
linePos : integer;
lineLen : integer;
currWord : string;
public
tkType : TTokenType;
tkValue : extended;
procedure raiseException(Message : string);
function getTokenText : string;
procedure addCommand (name : string; value : integer); // add command names and values
procedure addParam (name : string; value : integer; // add parameter names and values
valueRequired : boolean); // true if param=number is expected
procedure addParamAlias (currName,aliasName : string);
procedure beginLine (aLineNumber : integer; aLine : string); // start parsing a new line
function findCommand (name : string) : integer;
function findParam (name : string) : integer;
function next : integer; // returns next token
function expectCommandOrEOL : integer; // returns command or raises exception
function expectParamOrEOL : integer; // expects param=value, value in tkValue
function expectParamOrValueOrEOL : integer;
function expectValue : integer;
function expectEOL : integer;
private
function nextWord : integer;
function skipNoise : boolean;
end;
implementation
Resourcestring
cUnableToAddAlias = 'unable to add alias, "%s" not found';
cUnableToConvertToNumber = 'unable to convert "%s" to a number';
cCommentNotClosed = 'comment not closed, } missing';
cSlashExpected = '/ expected';
cCommandExpected = 'command expected';
cParameterNameExpected = 'parameter name expected';
cEqualExpected = '= expected';
cValueExpected = 'value expected';
cParamDoesNotSupValue = 'parameter %s does not support a value';
cParamNameOrValueExpected = 'parameter name or value expected';
cEndOfLineExpected = 'end of line expected';
constructor TLineParserException.create(aLineNumber, aLinePos:integer; aMessage : string);
begin
inherited create(aMessage);
LinePos := aLinePos;
LineNo := aLineNumber;
end;
constructor TLineParserObject.create(aName:string; aValue:integer; aValueRequired : boolean);
begin
inherited create;
name := aName;
value := aValue;
valueRequired := aValueRequired;
end;
//******************************************************************************
destructor TLineParserObjectList.destroy;
var i : integer;
begin
for i:= 0 to count-1 do
begin
TLineParserObject(items[i]).free;
items[i] := NIL;
end;
inherited;
end;
function TLineParserObjectList.findName (name : string) : integer;
var o : TLineParserObject;
nameU : string;
begin
nameU := UpperCase(name);
for pointer(o) in self do
if nameU = o.name then exit(o.value);
result := TK_ERR;
end;
function TLineParserObjectList.requiresValue (name : string) : boolean;
var o : TLineParserObject;
nameU : string;
begin
nameU := UpperCase(name);
for pointer(o) in self do
if nameU = o.name then exit(o.valueRequired);
result := false;
end;
//*****************************************************************************
const
cNoise : set of char = [' ',#9];
cWordSep : set of char = [' ',#9,'=','/','{','}'];
constructor TLineParser.create;
begin
commands := TLineParserObjectList.create;
params := TLineParserObjectList.create;
end;
destructor TLineParser.destroy;
begin
commands.free;
params.free;
inherited;
end;
function TLineParser.getTokenText : string;
begin
result := currWord;
end;
procedure TLineParser.addCommand (name : string; value : integer);
begin
commands.add(TLineParserObject.create(UpperCase(name),value,false));
end;
procedure TLineParser.addParam (name : string; value : integer; valueRequired : boolean);
begin
params.add(TlineParserObject.create(UpperCase(name),value,valueRequired));
end;
procedure TLineParser.addParamAlias (currName,aliasName : string);
var i : integer;
nameU : string;
begin
nameU := UpperCase(currName);
for i := 0 to params.count-1 do
if TlineParserObject(params.items[i]).name = nameU then
begin
params.add(TlineParserObject.create(upperCase(aliasName),TlineParserObject(params.items[i]).value,TlineParserObject(params.items[i]).valueRequired));
exit;
end;
raise (TLineParserException.create(0, 0,format(cUnableToAddAlias,[currName])));
end;
procedure TLineParser.beginLine (aLineNumber : integer; aLine : string);
begin
Line := aLine;
LinePos := 1;
LineLen := length(aLine);
LineNo := aLineNumber;
end;
function TLineParser.findCommand (name : string) : integer;
begin
result := commands.findName(name);
end;
function TLineParser.findParam (name : string) : integer;
begin
result := params.findName(name);
end;
function TLineParser.nextWord : integer;
var i : integer;
DecimalSeparatorSave : char;
convOk : boolean;
{$ifdef AllowAppendCharsToNumber}
tempS : string;
{$endif}
begin
currWord := '';
if not skipNoise then exit(TK_EOL);
if LinePos > LineLen then exit (TK_EOL);
if Line[LinePos] = '=' then
begin
inc(LinePos); exit (TK_EQUAL);
end;
while (LinePos <= LineLen) and (not (Line[LinePos] in cWordSep)) do
begin
currWord := currWord + Line[LinePos];
inc(LinePos);
end;
result := TK_UNKNOWN;
if length(currWord) = 0 then exit;
if (currWord[1] < '0') or (currWord[1] > '9') then exit; // not a number
{$ifdef AllowAppendCharsToNumber}
tempS := currWord; currWord := '';
for i := 1 to length(tempS) do
//if tempS[i] in ['0'..'9','.'] then currWord := currWord + tempS[i];
if ((tempS[i] >= '0') and (tempS[i] <= '9')) or (tempS[i] = '.') then currWord := currWord + tempS[i];
{$else}
for i:=1 to length(currWord) do // check for number
if not (currWord[i] in ['0'..'9','.']) then exit;
{$endif}
// convert to number
convOk := TryStrToFloat(currWord,tkValue);
if not convOk then
begin
DecimalSeparatorSave := FormatSettings.DecimalSeparator;
if (DecimalSeparatorSave <> '.') then
begin
FormatSettings.DecimalSeparator := '.';
convOk := TryStrToFloat(currWord,tkValue);
FormatSettings.DecimalSeparator := DecimalSeparatorSave;
end;
end;
if not convOk then
raise (TLineParserException.create(LineNo, LinePos,format(cUnableToConvertToNumber,[currWord])));
result := TK_VALUE;
end;
procedure TLineParser.raiseException(Message : string);
begin
raise (TLineParserException.create(LineNo, LinePos,Message));
end;
function TLineParser.skipNoise : boolean;
var lastWasComment : boolean;
begin
repeat
lastWasComment := false;
if LinePos <= LineLen then
begin
while (LinePos < LineLen) and (Line[LinePos] in cNoise) do inc(LinePos);
if LinePos < LineLen then
if Line[LinePos] = '{' then // {} comment
begin
inc(LinePos);
while (LinePos <= LineLen) and (Line[LinePos] <> '}') do
inc(LinePos);
if (LinePos > LineLen) then raise (TLineParserException.create(LineNo, LinePos,cCommentNotClosed));
inc(LinePos);
lastWasComment := true;
end;
if LinePos < LineLen then // // comment
if Line[LinePos] = '/' then
begin
inc(LinePos);
if LinePos < LineLen then
if Line[LinePos] = '/' then exit(false);
raise (TLineParserException.create(LineNo, LinePos,cSlashExpected));
end;
end else
exit(false);
until (not lastWasComment);
result := (LinePos <= LineLen);
end;
function TLineParser.next : integer;
var tk : integer;
begin
tkType := TTokenNone;
tkValue := 0;
tk := nextWord;
if tk < TK_LAST then exit (tk);
// search for a command or a parameter
tk := findCommand(currWord);
if tk >= 0 then
begin
tkType := TTokenCommand; exit (tk);
end;
tk := findParam(currWord);
if tk >= 0 then
tkType := TTokenParam;
exit (tk);
end;
function TLineParser.expectCommandOrEOL : integer;
var tk:integer;
begin
tk := next;
if (tk = TK_EOL) or ((tkType = TTokenCommand) and (tk >= 0)) then exit(tk);
raise (TLineParserException.create(LineNo, LinePos,cCommandExpected));
end;
function TLineParser.expectParamOrEOL : integer;
var tk : integer;
tkNameBuf : string;
begin
result := next;
if (result = TK_EOL) then exit(result);
if tkType <> TTokenParam then
raise (TLineParserException.create(LineNo, LinePos,cParameterNameExpected));
tkNameBuf := currWord;
if params.requiresValue(tkNameBuf) then
begin
tk := next;
if tk <> TK_EQUAL then
raise (TLineParserException.create(LineNo, LinePos,cEqualExpected));
tk := next;
if tk <> TK_VALUE then
raise (TLineParserException.create(LineNo, LinePos,cValueExpected));
currWord := tkNameBuf;
end else
begin
skipNoise;
currWord := tkNameBuf;
if LinePos <= LineLen then
if Line[LinePos] = '=' then
raise (TLineParserException.create(LineNo, LinePos,format(cParamDoesNotSupValue,[currWord])));
end;
end;
function TLineParser.expectParamOrValueOrEOL : integer;
var tk : integer;
tkNameBuf : string;
begin
result := next;
if (result = TK_EOL) then exit(result);
if (result = TK_VALUE) then exit (result);
if tkType <> TTokenParam then
raise (TLineParserException.create(LineNo, LinePos,cParamNameOrValueExpected));
tkNameBuf := currWord;
if params.requiresValue(tkNameBuf) then
begin
tk := next;
if tk <> TK_EQUAL then
raise (TLineParserException.create(LineNo, LinePos,cEqualExpected));
tk := next;
if tk <> TK_VALUE then
raise (TLineParserException.create(LineNo, LinePos,cValueExpected));
currWord := tkNameBuf;
end else
begin
skipNoise;
currWord := tkNameBuf;
if LinePos <= LineLen then
if Line[LinePos] = '=' then
raise (TLineParserException.create(LineNo, LinePos,format(cParamDoesNotSupValue,[currWord])));
end;
end;
function TLineParser.expectValue : integer;
begin
result := next;
if result <> TK_VALUE then
raise (TLineParserException.create(LineNo, LinePos,cValueExpected));
end;
function TLineParser.expectEOL : integer;
begin
result := next;
if (result = TK_EOL) then exit(result);
raise (TLineParserException.create(LineNo,LinePos,cEndOfLineExpected));
end;
end.