summaryrefslogtreecommitdiff
path: root/system/base/1.7.5/src/scanner
blob: 35a632cb314ff89469de5217c57ae79cc90782b4 (plain)
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
(* ------------------- VERSION 4     14.05.86 ------------------- *)
PACKET scanner DEFINES                 (* Autor: J.Liedtke        *)
 
       scan , 
       continue scan ,
       next symbol :
 
 
LET tag     = 1 ,
    bold    = 2 , 
    number  = 3 ,
    text    = 4 ,
    operator= 5 ,
    delimiter = 6 , 
    end of file = 7 , 
    within comment = 8 ,
    within text    = 9 ;

LET digit 0       = 48 ,
    digit 9       = 57 ,
    upper case a  = 65 ,
    upper case z  = 90 ,
    lower case a  = 97 ,
    lower case z  = 122;


TEXT VAR line := "" , 
         char := "" ,
         chars:= "" ;
 
INT VAR  position  := 0 ,
         comment depth ;
BOOL VAR continue text ;
 
 
PROC scan (TEXT CONST scan text) : 
 
  comment depth := 0 ;
  continue text := FALSE ;
  continue scan (scan text)
 
ENDPROC scan ;
 
PROC continue scan (TEXT CONST scan text) :
 
  line := scan text ;
  position  := 0 ;
  nextchar 
 
ENDPROC continue scan ;

PROC next symbol (TEXT VAR symbol) : 
 
  INT VAR type ;
  next symbol (symbol, type) 
 
ENDPROC next symbol ;
 
PROC next symbol (TEXT VAR symbol, INT VAR type) : 
 
  skip blanks ;
  IF   is begin comment          THEN process comment
  ELIF comment depth > 0         THEN comment depth DECR 1 ;
                                      process comment
  ELIF is quote OR continue text THEN process text 
  ELIF is lower case letter      THEN process tag 
  ELIF is upper case letter      THEN process bold 
  ELIF is digit                  THEN process number
  ELIF is delimiter              THEN process delimiter
  ELIF is niltext                THEN eof
  ELSE process operator 
  FI . 
 
 
process comment :
  read comment ;
  IF comment depth = 0
    THEN next symbol (symbol, type)
    ELSE type := within comment ;
         symbol := ""
  FI .
 
process tag : 
  type := tag ;
  assemble chars (lower case a, lower case z) ;
  symbol := chars ;
  REP 
    skip blanks ;
    IF is lower case letter
      THEN assemble chars (lower case a, lower case z)
    ELIF is digit
      THEN assemble chars (digit 0, digit 9)
    ELSE   LEAVE process tag
    FI ;
    symbol CAT chars
  PER ;
  nextchar .
 
process bold : 
  type := bold ;
  assemble chars (upper case a, upper case z) ;
  symbol := chars .
 
process number :
  type := number ;
  assemble chars (digit 0, digit 9) ;
  symbol := chars ;
  IF char = "." AND ahead char is digit
    THEN process fraction ;
         IF char = "e"
           THEN process exponent
         FI
  FI .

ahead char is digit :
  digit 0 <= code (ahead char) AND code (ahead char) <= digit 9 .

process fraction :
  symbol CAT char ;
  nextchar ;
  assemble chars (digit 0, digit 9) ;
  symbol CAT chars .

process exponent :
  symbol CAT char ;
  nextchar ;
  IF char = "+" OR char = "-"
    THEN symbol CAT char ;
         nextchar
  FI ;
  assemble chars (digit 0, digit 9) ;
  symbol CAT chars .
 
process text : 
  type := text ;
  symbol := "" ;
  IF continue text
    THEN continue text := FALSE
    ELSE next char
  FI ;
  WHILE not end of text REP 
    assemble chars (35, 254) ;
    symbol CAT chars ;
    IF NOT is quote
      THEN symbol CAT char ;
           nextchar
    FI
  ENDREP . 
 
not end of text : 
  IF is niltext 
    THEN continue text := TRUE ; type := within text ; FALSE
  ELIF is quote 
    THEN end of text or exception
  ELSE TRUE 
  FI . 
 
end of text or exception :
  next char ;
  IF is quote
    THEN get quote ;        TRUE
  ELIF is digit
    THEN get special char ; TRUE
  ELSE   FALSE
  FI .
 
get quote :
  symbol CAT char ;
  nextchar .

get special char :
  assemble chars (digit 0, digit 9) ;
  symbol CAT code (int (chars) ) ;
  nextchar .
 
process delimiter : 
  type := delimiter ;
  symbol := char ;
  nextchar . 
 
process operator : 
  type := operator ; 
  symbol := char ;
  nextchar ;
  IF symbol = ":" 
    THEN IF char = "=" OR char = ":" 
           THEN symbol := ":=" ;
                nextchar 
           ELSE type := delimiter 
         FI 
  ELIF is relational double char 
    THEN symbol CAT char ;
         nextchar 
  ELIF symbol = "*" AND char = "*" 
    THEN symbol := "**" ; 
         next char 
  FI .
 
eof : 
  type := end of file ;
  symbol := "" .
 
is lower case letter : 
     lower case a <= code (char) AND code (char) <= lower case z . 
 
is upper case letter : 
     upper case a <= code (char) AND code (char) <= upper case z .
 
is digit :
     digit 0 <= code (char) AND code (char) <= digit 9 . 
 
is delimiter : pos ( "()[].,;" , char ) > 0 . 
 
is relational double char : 
  TEXT VAR double := symbol + char ; 
  double = "<>" OR double = "<=" OR double = ">=" . 
 
is quote : char = """" .
 
is niltext : char = "" . 
 
is begin comment : char = "{" OR char = "(" AND ahead char = "*" .
 
ENDPROC next symbol ;
 
PROC next char : 
 
  position INCR 1 ;
  char := line SUB position
 
ENDPROC next char ;
 
PROC skip blanks :
 
  position := pos (line, ""33"", ""254"", position) ;
  IF position = 0
    THEN position := LENGTH line + 1
  FI ;
  char := line SUB position .

ENDPROC skip blanks ;
 
TEXT PROC ahead char : 
 
  line SUB position+1 
 
ENDPROC ahead char ; 

PROC assemble chars (INT CONST low, high) :

  INT CONST begin := position ;
  position behind valid text ;
  chars := subtext (line, begin, position-1) ;
  char := line SUB position .

position behind valid text :
  position := pos (line, ""32"", code (low-1), begin) ;
  IF position = 0
    THEN position := LENGTH line + 1
  FI ;
  INT CONST higher pos := pos (line, code (high+1), ""254"", begin) ;
  IF higher pos <> 0 AND higher pos < position
    THEN position := higher pos
  FI .

ENDPROC assemble chars ;

 
PROC read comment : 
 
  TEXT VAR last char ; 
  comment depth INCR 1 ;
  REP 
    last char := char ; 
    nextchar ; 
    IF is begin comment
      THEN read comment
    FI ;
    IF char = ""
      THEN LEAVE read comment
    FI
  UNTIL is end comment PER ; 
  comment depth DECR 1 ;
  next char ;
  skip blanks .
 
is end comment :
  char = "}" OR char = ")" AND last char = "*" .
 
is begin comment :
  char = "{" OR char = "(" AND ahead char = "*" .
 
ENDPROC read comment ;
 

PROC scan (FILE VAR f) :

  getline (f, line) ;
  scan (line)

ENDPROC scan ; 

PROC next symbol (FILE VAR f, TEXT VAR symbol) :

  INT VAR type ;
  next symbol (f, symbol, type)

ENDPROC next symbol ;

TEXT VAR scanned ;

PROC next symbol (FILE VAR f, TEXT VAR symbol, INT VAR type) :

  next symbol (symbol, type) ;
  WHILE type >= 7 AND NOT eof (f) REP
    getline (f, line) ;
    continue scan (line) ;
    next symbol (scanned, type) ;
    symbol CAT scanned
  PER .

ENDPROC next symbol ;

ENDPACKET scanner ;