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
418
|
(* M2Lex.mod provides a non tokenised lexical analyser.
Copyright (C) 2001-2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Modula-2 is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Modula-2; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. *)
IMPLEMENTATION MODULE M2Lex ;
FROM FIO IMPORT File, OpenToRead, ReadChar, Close, IsNoError ;
FROM StrIO IMPORT ReadString, WriteString, WriteLn ;
FROM StdIO IMPORT Write ;
FROM NumberIO IMPORT WriteCard ;
FROM ASCII IMPORT nul, lf, cr, EOL ;
FROM StrLib IMPORT StrCopy, StrEqual, StrLen ;
CONST
LineBuf = 1 ;
Wrap = LineBuf+1 ;
eof = 032C ;
MaxStack= 10 ;
VAR
f: File ;
Opened : BOOLEAN ;
CurrentChar : CHAR ;
NextChar : CHAR ;
FileName : ARRAY [0..MaxLine] OF CHAR ;
Lines : ARRAY [0..LineBuf] OF ARRAY [0..255] OF CHAR ;
(* Need two lines since the delimiter of the CurrentSymbol *)
(* maybe on the next line. *)
HighNext : CARDINAL ; (* Length of the NextChar line. *)
CurLine : CARDINAL ; (* Line number of the Current Char Line. *)
NextLine : CARDINAL ; (* Line number of the Next Char Line. *)
IndexCur : CARDINAL ; (* Index to the Lines array for Current Ln *)
IndexNext : CARDINAL ; (* Index to the Lines array for NextChar Ln *)
CurSym : CARDINAL ; (* Character start of the CurrentSymbol *)
CurSymLine : CARDINAL ; (* Line number of the CurrentSymbol *)
CurCharIndex : CARDINAL ; (* Character number of CurChar. *)
NextCharIndex : CARDINAL ; (* Character number of NextChar. *)
Eof : BOOLEAN ; (* End of source file. *)
InQuotes : BOOLEAN ; (* If we are in quotes. *)
QuoteChar : CHAR ; (* Quote character expected. *)
Stack : ARRAY [0..MaxStack] OF ARRAY [0..255] OF CHAR ;
StackPtr : CARDINAL ;
(*
IsSym - returns the result of the comparison between CurrentSymbol
and Name.
*)
PROCEDURE IsSym (Name: ARRAY OF CHAR) : BOOLEAN ;
BEGIN
RETURN( StrEqual(CurrentSymbol, Name) )
END IsSym ;
(*
SymIs - if Name is equal to the CurrentSymbol the next Symbol is read
and true is returned, otherwise false is returned.
*)
PROCEDURE SymIs (Name: ARRAY OF CHAR) : BOOLEAN ;
BEGIN
IF StrEqual(CurrentSymbol, Name)
THEN
GetSymbol ;
RETURN( TRUE )
ELSE
RETURN( FALSE )
END
END SymIs ;
(*
WriteError - displays the source line and points to the symbol in error.
The message, a, is displayed.
*)
PROCEDURE WriteError (a: ARRAY OF CHAR) ;
VAR
i: CARDINAL ;
BEGIN
WriteString(FileName) ; Write(':') ; WriteCard(CurSymLine, 0) ; Write(':') ; WriteString(a) ;
WriteLn ;
WriteString( Lines[IndexCur] ) ; WriteLn ;
i := CurSym ;
WHILE i>0 DO
Write(' ') ;
DEC(i)
END ;
i := StrLen(CurrentSymbol) ;
WHILE i>0 DO
Write('^') ;
DEC(i)
END ;
WriteLn ;
WriteString(a) ; WriteLn ;
END WriteError ;
(*
OpenSource - Attempts to open the source file, a.
The success of the operation is returned.
*)
PROCEDURE OpenSource (a: ARRAY OF CHAR) : BOOLEAN ;
BEGIN
f := OpenToRead(a) ;
IF IsNoError(f)
THEN
StrCopy(a, FileName) ;
Opened := TRUE ;
Init ;
RETURN( TRUE )
ELSE
Opened := FALSE ;
Eof := TRUE ;
RETURN( FALSE )
END
END OpenSource ;
(*
CloseSource - Closes the current open file.
*)
PROCEDURE CloseSource ;
BEGIN
IF Opened=TRUE
THEN
Opened := FALSE ;
Close( f )
END
END CloseSource ;
(*
GetSymbol - gets the next Symbol into CurrentSymbol.
*)
PROCEDURE GetSymbol ;
BEGIN
StrCopy( CurrentSymbol, LastSymbol ) ;
IF StackPtr>0
THEN
DEC(StackPtr) ;
StrCopy( Stack[StackPtr], CurrentSymbol )
ELSE
ReadSymbol( CurrentSymbol )
END
END GetSymbol ;
(*
PutSymbol - pushes a symbol, Name, back onto the input.
GetSymbol will set CurrentSymbol to, Name.
*)
PROCEDURE PutSymbol (Name: ARRAY OF CHAR) ;
BEGIN
IF StackPtr=MaxStack
THEN
WriteError('Maximum push back symbol exceeded - Increase CONST MaxStack')
ELSE
StrCopy(Name, Stack[StackPtr]) ;
INC(StackPtr)
END
END PutSymbol ;
PROCEDURE ReadSymbol (VAR a: ARRAY OF CHAR) ;
VAR
high,
i : CARDINAL ;
ok : BOOLEAN ;
BEGIN
high := HIGH(a) ;
IF NOT Eof
THEN
IF InQuotes
THEN
i := 0 ;
IF CurrentChar=QuoteChar
THEN
InQuotes := FALSE ;
a[i] := QuoteChar ;
INC(i) ;
AdvanceChar
ELSE
(* Fill in string or character *)
i := 0 ;
REPEAT
a[i] := CurrentChar ;
INC(i) ;
AdvanceChar
UNTIL (CurrentChar=QuoteChar) OR Eof OR (i>high) ;
END
ELSE
(* Get rid of all excess spaces *)
REPEAT
IF CurrentChar=' '
THEN
WHILE (CurrentChar=' ') AND (NOT Eof) DO
AdvanceChar
END ;
ok := FALSE
ELSIF (CurrentChar='(') AND (NextChar='*')
THEN
ConsumeComments ;
ok := FALSE
ELSE
ok := TRUE
END
UNTIL ok ;
i := 0 ;
CurSym := CurCharIndex ;
CurSymLine := CurLine ;
IF (CurrentChar='"') OR (CurrentChar="'")
THEN
InQuotes := TRUE ;
QuoteChar := CurrentChar ;
a[i] := CurrentChar ;
AdvanceChar ;
INC(i)
ELSIF DoubleDelimiter()
THEN
a[i] := CurrentChar ;
AdvanceChar ;
INC(i) ;
a[i] := CurrentChar ;
AdvanceChar ;
INC(i)
ELSIF Delimiter()
THEN
a[i] := CurrentChar ;
AdvanceChar ;
INC(i)
ELSE
REPEAT
a[i] := CurrentChar ;
AdvanceChar ;
INC(i)
UNTIL Delimiter() OR (i>high) OR (CurrentChar=' ') OR Eof
END
END
ELSE
(* eof *)
i := 0 ;
a[i] := eof ;
INC(i)
END ;
IF i<=HIGH(a)
THEN
a[i] := nul
END
END ReadSymbol ;
(*
ConsumeComments - consumes Modula-2 comments.
*)
PROCEDURE ConsumeComments ;
VAR
Level: CARDINAL ;
BEGIN
Level := 0 ;
REPEAT
IF (CurrentChar='(') AND (NextChar='*')
THEN
INC(Level)
ELSIF (CurrentChar='*') AND (NextChar=')')
THEN
DEC(Level)
END ;
AdvanceChar ;
UNTIL (Level=0) OR Eof ;
AdvanceChar
END ConsumeComments;
(* Delimiter returns true if and only if CurrentChar is a delimiter *)
PROCEDURE Delimiter() : BOOLEAN ;
BEGIN
IF (CurrentChar='-') OR
(CurrentChar='+') OR (CurrentChar='*') OR (CurrentChar='\') OR
(CurrentChar='|') OR (CurrentChar='(') OR (CurrentChar=')') OR
(CurrentChar='"') OR (CurrentChar="'") OR (CurrentChar='{')
THEN
RETURN( TRUE )
ELSIF
(CurrentChar='}') OR (CurrentChar='[') OR (CurrentChar=']') OR
(CurrentChar='#') OR (CurrentChar='=') OR (CurrentChar='<')
THEN
RETURN( TRUE )
ELSIF
(CurrentChar='>') OR (CurrentChar='.') OR (CurrentChar=';') OR
(CurrentChar=':') OR (CurrentChar='^') OR (CurrentChar=',')
THEN
RETURN( TRUE )
ELSE
RETURN( FALSE )
END
END Delimiter ;
PROCEDURE DoubleDelimiter () : BOOLEAN ;
BEGIN
RETURN (
((CurrentChar='>') AND (NextChar='=')) OR
((CurrentChar='<') AND (NextChar='=')) OR
((CurrentChar='<') AND (NextChar='>')) OR
((CurrentChar=':') AND (NextChar='=')) OR
((CurrentChar='.') AND (NextChar='.'))
)
END DoubleDelimiter ;
PROCEDURE AdvanceChar ;
BEGIN
IF NOT Eof
THEN
CurrentChar := NextChar ;
CurCharIndex := NextCharIndex ;
IndexCur := IndexNext ;
CurLine := NextLine ;
IF CurrentChar=eof
THEN
Eof := TRUE
ELSIF NextCharIndex=HighNext
THEN
IndexNext := (IndexCur+1) MOD Wrap ;
HighNext := 0 ;
REPEAT
NextChar := ReadChar(f) ;
IF NOT IsNoError(f)
THEN
NextChar := eof ;
Lines[IndexNext][HighNext] := NextChar ;
INC( HighNext )
END ;
WHILE (NextChar#eof) AND (NextChar#lf) AND (NextChar#cr) AND (HighNext<MaxLine) DO
Lines[IndexNext][HighNext] := NextChar ;
INC( HighNext ) ;
NextChar := ReadChar(f) ;
IF NOT IsNoError(f)
THEN
NextChar := eof
END
END ;
IF (NextChar=eof) OR (NextChar=lf) OR (NextChar=cr)
THEN
IF InQuotes
THEN
Lines[IndexNext][HighNext] := ' ' ; (* Space for delimiter *)
Lines[IndexNext][HighNext+1] := nul ;
WriteError('missing end of quote on this source line') ; HALT
END ;
INC( NextLine )
END
UNTIL HighNext>0 ;
IF HighNext>=MaxLine THEN WriteError('Line too long') ; HALT END ;
Lines[IndexNext][HighNext] := ' ' ; (* Space for delimiter *)
Lines[IndexNext][HighNext+1] := nul ;
NextCharIndex := 0 ;
NextChar := Lines[IndexNext][NextCharIndex]
ELSE
INC(NextCharIndex) ;
NextChar := Lines[IndexNext][NextCharIndex]
END
END
END AdvanceChar ;
PROCEDURE Init ;
BEGIN
StackPtr := 0 ;
InQuotes := FALSE ;
Eof := FALSE ;
IndexCur := 1 ;
IndexNext := 0 ;
CurCharIndex := 0 ;
Lines[IndexCur][0] := nul ;
HighNext := 0 ;
NextCharIndex := 0 ;
CurLine := 1 ;
NextLine := 1 ;
CurrentChar := ' ' ;
NextChar := ' ' ;
StrCopy("", CurrentSymbol) ;
StrCopy("", LastSymbol) ;
IndexCur := IndexNext
END Init ;
BEGIN
Init
END M2Lex.
|