-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLEXICAL.PAS
347 lines (303 loc) · 9.35 KB
/
LEXICAL.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
{********************************************************}
{* *}
{* LEX & YACC tools generic lexical analyzer unit *}
{* *}
{* File name : LEXICAL.PAS *}
{* *}
{* Programming: Russlan Kafri - 1999 *}
{* *}
{********************************************************}
unit Lexical;
interface
uses Generics;
const BUFFERBLOCKSIZE = $FFF0;
MAXLEXEMESTRLENGTH = 8192;
type PBufferBlock = ^TBufferBlock;
TBufferBlock = record
Data: array[0..BUFFERBLOCKSIZE] of char;
Next: PBufferBlock;
end;
PBufferPtr = ^TBufferPtr;
TBufferPtr = record
Block: PBufferBlock;
Ptr: PChar;
end;
PLexical= ^TLexical;
TLexical = object( TErrGeneric)
private
Source: PStream;
FirstBlock,
LastBlock: PBufferBlock;
LexemeStartPtr,
ForwardPtr,
LastMatchPtr: TBufferPtr;
ErrorOcured: Boolean;
ForwardState: Word;
LastMatchState: Word;
procedure Initialize;
function AllocateNewBlock: PBufferBlock;
function ReadNewBlock: PBufferBlock;
procedure FreePassedBlocks;
public
constructor Create( AOwner: PErrGeneric; ASource: PStream);
destructor Destroy; virtual;
function NextChar: Char;
procedure ScanPassed;
function GetMatchedLexeme( var ABuf; ABufferSize: Word): Word;
procedure DisplayBlocks;
function Lexeme( var ALexemeInfo): Boolean;
procedure InputFilter( AChar: Char); virtual;
function CallPatternProcedure( AState: Word; var ALexemeInfo): Boolean; virtual;
function GetNextState( AState: Word; AChar: Char): Word; virtual;
function IsAcceptingState( AState: Word): Boolean; virtual;
procedure BadLexeme( var ALexemeInfo); virtual;
function TerminateLexeme( var ALexemeInfo): Boolean; virtual;
end;
implementation
{******************************************************************}
{* *}
{******************************************************************}
constructor TLexical.Create( AOwner: PErrGeneric; ASource: PStream);
begin
inherited Create( AOwner);
Source := ASource;
FirstBlock := nil;
LastBlock := nil;
ForwardPtr.Block := nil;
ForwardPtr.Ptr := nil;
LexemeStartPtr.Block := nil;
LexemeStartPtr.Ptr := nil;
LastMatchPtr.Block := nil;
LastMatchPtr.Ptr := nil;
ForwardState := 1;
LastMatchState := 1;
ErrorOcured := False;
Initialize;
end;
destructor TLexical.Destroy;
var ScanBlock: PBufferBlock;
begin
ScanBlock := FirstBlock;
while ( ScanBlock <> nil)
do begin
ScanBlock := ScanBlock^.Next;
Dispose( FirstBlock);
FirstBlock := ScanBlock;
end;
inherited Destroy;
end;
procedure TLexical.Initialize;
begin
if ErrorOcured
then Exit;
ForwardPtr.Block := ReadNewBlock;
ForwardPtr.Ptr := @ForwardPtr.Block^.Data[0];
LexemeStartPtr := ForwardPtr;
LastMatchPtr := ForwardPtr;
end;
function TLexical.AllocateNewBlock: PBufferBlock;
var NewBlock: PBufferBlock;
begin
New( NewBlock);
NewBlock^.Next := nil;
if ( FirstBlock <> nil)
then begin
LastBlock^.Next := NewBlock;
LastBlock := NewBlock;
end
else begin
FirstBlock := NewBlock;
LastBlock := NewBlock;
end;
AllocateNewBlock := NewBlock;
end;
function TLexical.ReadNewBlock: PBufferBlock;
var NewBlock: PBufferBlock;
Readen: Word;
begin
if ErrorOcured
then begin
ReadNewBlock := nil;
Exit;
end;
if ( Source <> nil)
then begin
NewBlock := AllocateNewBlock;
Readen := Source^.Read( NewBlock^.Data, BUFFERBLOCKSIZE);
NewBlock^.Data[Readen] := #0;
ReadNewBlock := NewBlock;
end
else ReadNewBlock := nil;
end;
procedure TLexical.FreePassedBlocks;
var ScanBlock: PBufferBlock;
begin
ScanBlock := FirstBlock;
while (( ScanBlock <> nil) and ( ScanBlock <> LexemeStartPtr.Block))
do begin
ScanBlock := ScanBlock^.Next;
Dispose( FirstBlock);
FirstBlock := ScanBlock;
end;
end;
function TLexical.NextChar: Char;
begin
if ErrorOcured
then begin
NextChar := #0;
Exit;
end;
if (ForwardPtr.Ptr^ <> #0)
then begin
NextChar := ForwardPtr.Ptr^;
ForwardPtr.Ptr := ForwardPtr.Ptr + 1;
end
else begin
if (ForwardPtr.Ptr = @ForwardPtr.Block^.Data[BUFFERBLOCKSIZE])
then begin
if (ForwardPtr.Block^.Next <> nil)
then ForwardPtr.Block := ForwardPtr.Block^.Next
else ForwardPtr.Block := ReadNewBlock;
ForwardPtr.Ptr := @ForwardPtr.Block^.Data[0];
if ForwardPtr.Ptr^ <> #0
then begin
NextChar := ForwardPtr.Ptr^;
ForwardPtr.Ptr := ForwardPtr.Ptr + 1;
end
else NextChar := #0;
end
else NextChar := #0;
end;
end;
procedure TLexical.DisplayBlocks;
var ScanBlock: PBufferBlock;
I: Longint;
begin
I := 1;
ScanBlock := FirstBlock;
while ( ScanBlock <> nil)
do begin
WriteLn(I:4,'>',ScanBlock^.Data,'<');
ScanBlock := ScanBlock^.Next;
I := I + 1;
end;
end;
function TLexical.GetNextState( AState: Word; AChar: Char): Word;
begin
GetNextState := 0;
end;
function TLexical.IsAcceptingState( AState: Word): Boolean;
begin
IsAcceptingState := False;
end;
procedure TLexical.InputFilter( AChar: Char);
begin
end;
function TLexical.CallPatternProcedure( AState: Word; var ALexemeInfo): Boolean;
begin
CallPatternProcedure := False;
end;
procedure TLexical.BadLexeme( var ALexemeInfo);
begin
end;
function TLexical.TerminateLexeme( var ALexemeInfo): Boolean;
begin
TerminateLexeme := False;
end;
function TLexical.Lexeme( var ALexemeInfo): Boolean;
var ForwardChar: Char;
ThereIsAPattern: Boolean;
begin
repeat
ScanPassed;
FreePassedBlocks;
ForwardState := 1;
LastMatchState := 1;
ThereIsAPattern := False;
while True
do begin
ForwardChar := NextChar;
if ( ForwardChar <> #0)
then ForwardState := GetNextState( ForwardState, ForwardChar)
else ForwardState := 0;
if ( ForwardState > 0)
then begin
if IsAcceptingState( ForwardState)
then begin
LastMatchState := ForwardState;
LastMatchPtr := ForwardPtr;
end;
end
else Break;
end;
if ( LastMatchPtr.Ptr = LexemeStartPtr.Ptr)
then begin
if (( ForwardChar = #0) and ( LexemeStartPtr.Ptr = ForwardPtr.Ptr))
then ThereIsAPattern := TerminateLexeme( ALexemeInfo)
else begin
LastMatchPtr := ForwardPtr;
BadLexeme( ALexemeInfo);
ThereIsAPattern := False;
end;
Break;
end
else ThereIsAPattern := CallPatternProcedure( LastMatchState, ALexemeInfo);
until ( ThereIsAPattern);
Lexeme := ThereIsAPattern;
end;
procedure TLexical.ScanPassed;
var ScanPtr: TBufferPtr;
begin
ScanPtr := LexemeStartPtr;
while ( ScanPtr.Ptr <> LastMatchPtr.Ptr)
do begin
if (ScanPtr.Ptr^ <> #0)
then begin
InputFilter( ScanPtr.Ptr^);
ScanPtr.Ptr := ScanPtr.Ptr + 1;
end
else begin
if ( ScanPtr.Ptr = @ScanPtr.Block^.Data[BUFFERBLOCKSIZE])
then begin
ScanPtr.Block := ScanPtr.Block^.Next;
if ( ScanPtr.Block <> nil)
then ScanPtr.Ptr := @ScanPtr.Block^.Data[0]
else Break;
end
else Break;
end;
end;
LexemeStartPtr := ScanPtr;
ForwardPtr := ScanPtr;
end;
function TLexical.GetMatchedLexeme( var ABuf; ABufferSize: Word): Word;
var Buf: PChar;
LexemeSize: Word;
ScanPtr: TBufferPtr;
begin
Buf := @ABuf;
LexemeSize := 0;
ScanPtr := LexemeStartPtr;
while (( ScanPtr.Ptr <> LastMatchPtr.Ptr) and ( LexemeSize < ABufferSize))
do begin
if (ScanPtr.Ptr^ <> #0)
then begin
Buf^ := ScanPtr.Ptr^;
Buf := Buf + 1;
ScanPtr.Ptr := ScanPtr.Ptr + 1;
LexemeSize := LexemeSize + 1;
end
else begin
if ( ScanPtr.Ptr = @ScanPtr.Block^.Data[BUFFERBLOCKSIZE])
then begin
ScanPtr.Block := ScanPtr.Block^.Next;
if ( ScanPtr.Block <> nil)
then ScanPtr.Ptr := @ScanPtr.Block^.Data[0]
else Break;
end
else Break;
end;
end;
GetMatchedLexeme := LexemeSize;
end;
end.