/
SlipScan.c
executable file
·381 lines (325 loc) · 13.6 KB
/
SlipScan.c
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
/*---------------------------------------------*/
/* >>>Slip<<< */
/* Theo D'Hondt */
/* VUB Software Languages Lab */
/* (c) 2012 */
/*---------------------------------------------*/
/* version 13: completion and optimization */
/*---------------------------------------------*/
/* scanner */
/*---------------------------------------------*/
#include <stdlib.h>
#include <string.h>
#include "SlipMain.h"
#include "SlipScan.h"
/*------------------------------------- private types ----------------------------------*/
typedef enum { Apo, /* ' */
Att, /* @ */
Bkq, /* ` */
Bks, /* \ */
Com, /* , */
Dgt, /* 0 1 2 3 4 5 6 7 8 9 */
Eol, /* */
Exp, /* e E */
Fls, /* f F */
Hsh, /* # */
Ill, /* */
Opr, /* ! $ % & * / : < = > ? ^ _ ~ */
Lpr, /* ( */
Ltr, /* a A b B ... z Z */
Mns, /* - */
Per, /* . */
Pls, /* + */
Quo, /* " */
Rpr, /* ) */
Smc, /* ; */
Tru, /* t T */
Wsp } Cat_type; /* <HT> <FF> <SP> */
typedef enum { att_allowed = (1<<Att),
bks_allowed = (1<<Bks),
del_allowed = (1<<Eol)|
(1<<Lpr)|
(1<<Quo)|
(1<<Rpr)|
(1<<Smc)|
(1<<Wsp),
dig_allowed = (1<<Dgt),
end_allowed = (1<<Eol),
esc_allowed = (1<<Bks)|
(1<<Quo),
exp_allowed = (1<<Exp),
fls_allowed = (1<<Fls),
idt_allowed = (1<<Dgt)|
(1<<Exp)|
(1<<Fls)|
(1<<Ltr)|
(1<<Mns)|
(1<<Opr)|
(1<<Pls)|
(1<<Tru),
lpr_allowed = (1<<Lpr),
ltr_allowed = (1<<Ltr),
per_allowed = (1<<Per),
quo_allowed = (1<<Quo),
rwk_allowed = (1<<Per)|
(1<<Dgt)|
(1<<Opr),
sgn_allowed = (1<<Mns)|
(1<<Pls),
trm_allowed = (1<<Eol)|
(1<<Quo),
tru_allowed = (1<<Tru),
wsp_allowed = (1<<Wsp) } All_type;
/*----------------------------------- private constants --------------------------------*/
enum { Buffer_size = 1024 };
static const RWS_type Newline_char_rawstring = "\n";
static const RWS_type Newline_text_rawstring = "newline";
static const RWS_type Space_char_rawstring = " ";
static const RWS_type Space_text_rawstring = "space";
static const Cat_type ASCII_table[] =
/*NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI */
{ Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Wsp,Eol,Ill,Wsp,Eol,Ill,Ill,
/*DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB 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*/
Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,
/* */
Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,
/* */
Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill,Ill };
/*----------------------------------- private variables --------------------------------*/
static RWC_type Current_character;
static UNS_type Cursor;
/*----------------------------------- public variables ---------------------------------*/
RWC_type Scan_String[Buffer_size];
/*----------------------------------- private prototypes -------------------------------*/
static NIL_type copy_char(RWC_type);
static NIL_type next_character(NIL_type);
/*----------------------------------- private functions --------------------------------*/
static BYT_type check(All_type Allowed)
{ return (Allowed >> ASCII_table[Current_character]) & 1; }
static NIL_type copy_and_get_char(NIL_type)
{ copy_char(Current_character);
next_character(); }
static NIL_type copy_and_get_until(All_type Allowed)
{ do
copy_and_get_char();
while (!check(Allowed)); }
static NIL_type copy_and_get_while(All_type Allowed)
{ do
copy_and_get_char();
while (check(Allowed)); }
static NIL_type copy_char(RWC_type Rawcharacter)
{ if (Cursor == Buffer_size)
Scan_Error(BFO_error);
Scan_String[Cursor] = Rawcharacter;
Cursor += 1; }
static SCA_type error_and_return(BYT_type Error)
{ Scan_Error(Error);
return EOL_token; }
static NIL_type get_until(All_type Allowed)
{ do
next_character();
while (!check(Allowed)); }
static NIL_type get_while(All_type Allowed)
{ do
next_character();
while (check(Allowed)); }
static NIL_type next_character(NIL_type)
{ Current_character = Slip_Read();
return; }
static SCA_type next_character_return(SCA_type Token)
{ next_character();
return Token; }
static BYT_type reserved_name(RWS_type rawstring)
{ return (strcmp(Scan_String,
rawstring) == 0); }
static NIL_type stop_copy_text(NIL_type)
{ copy_char(0); }
static SCA_type stop_copy_text_return(SCA_type Token)
{ stop_copy_text();
return Token; }
/*--------------------------------------------------------------------------------------*/
static SCA_type character(NIL_type)
{ next_character();
copy_and_get_until(del_allowed);
stop_copy_text();
if (reserved_name(Space_text_rawstring))
strcpy(Scan_String,
Space_char_rawstring);
else if (reserved_name(Newline_text_rawstring))
strcpy(Scan_String,
Newline_char_rawstring);
else if (strlen(Scan_String) != 1)
return error_and_return(ICL_error);
return CHA_token; }
static NIL_type integer(NIL_type)
{ if (!check(dig_allowed))
Scan_Error(DRQ_error);
copy_and_get_while(dig_allowed); }
static SCA_type number(NIL_type)
{ SCA_type token;
token = NBR_token;
copy_and_get_while(dig_allowed);
if (check(per_allowed))
{ token = REA_token;
copy_and_get_char();
integer(); }
if (check(exp_allowed))
{ token = REA_token;
copy_and_get_char();
if (check(sgn_allowed))
copy_and_get_char();
integer(); }
return stop_copy_text_return(token); }
/*------------------------------------ scan functions ----------------------------------*/
static SCA_type Apo_fun(NIL_type)
{ return next_character_return(QUO_token); }
static SCA_type Hsh_fun(NIL_type)
{ next_character();
if (check(tru_allowed))
return next_character_return(TRU_token);
if (check(fls_allowed))
return next_character_return(FLS_token);
if (check(bks_allowed))
return character();
if (check(lpr_allowed))
return next_character_return(VEC_token);
if (check(ltr_allowed)) {
switch (Current_character) {
case 'K': case 'k':
next_character();
while (check(rwk_allowed))
copy_and_get_char();
stop_copy_text();
return KID_token;
}
}
return error_and_return(ILH_error); }
static SCA_type Idt_fun(NIL_type)
{ copy_and_get_while(idt_allowed);
return stop_copy_text_return(IDT_token); }
static SCA_type Ill_fun(NIL_type)
{ return error_and_return(ILC_error); }
static SCA_type Lpr_fun(NIL_type)
{ return next_character_return(LPR_token); }
static SCA_type Nbr_fun(NIL_type)
{ return number(); }
static SCA_type Eol_fun(NIL_type)
{ return EOL_token; }
static SCA_type Per_fun(NIL_type)
{ return next_character_return(PER_token); }
static SCA_type Qqu_fun(NIL_type)
{ return next_character_return(QQU_token); }
static SCA_type Rpr_fun(NIL_type)
{ return next_character_return(RPR_token); }
static SCA_type Sgn_fun(NIL_type)
{ copy_and_get_char();
if (check(dig_allowed))
return number();
while (check(idt_allowed))
copy_and_get_char();
return stop_copy_text_return(IDT_token); }
static SCA_type Smc_fun(NIL_type)
{ get_until(end_allowed);
next_character();
return Scan_Next(); }
static SCA_type Str_fun(NIL_type)
{ next_character();
while (!check(trm_allowed))
copy_and_get_char();
stop_copy_text();
if (!check(quo_allowed))
return error_and_return(QRQ_error);
return next_character_return(STR_token); }
static SCA_type Unq_fun(NIL_type)
{ next_character();
if (check(att_allowed))
return next_character_return(UQS_token);
return UNQ_token; }
static SCA_type Wsp_fun(NIL_type)
{ get_while(wsp_allowed);
return Scan_Next(); }
static SCA_type next_token(NIL_type)
{ Cat_type category;
Cursor = 0;
Scan_String[0] = 0;
category = ASCII_table[Current_character];
switch (category)
{ case Apo:
return Apo_fun();
case Att:
return Ill_fun();
case Bkq:
return Qqu_fun();
case Bks:
return Ill_fun();
case Com:
return Unq_fun();
case Dgt:
return Nbr_fun();
case Eol:
return Eol_fun();
case Exp:
return Idt_fun();
case Fls:
return Idt_fun();
case Hsh:
return Hsh_fun();
case Ill:
return Ill_fun();
case Opr:
return Idt_fun();
case Lpr:
return Lpr_fun();
case Ltr:
return Idt_fun();
case Mns:
return Sgn_fun();
case Per:
return Per_fun();
case Pls:
return Sgn_fun();
case Quo:
return Str_fun();
case Rpr:
return Rpr_fun();
case Smc:
return Smc_fun();
case Tru:
return Idt_fun();
case Wsp:
return Wsp_fun();
default:
return Eol_fun(); }}
/*----------------------------------- public functions ---------------------------------*/
SCA_type Scan_Next(NIL_type)
{ return next_token(); }
SCA_type Scan_Preset(NIL_type)
{ next_character();
return next_token(); }
/*--------------------------------------------------------------------------------------*/
NIL_type Scan_Initialize(NIL_type)
{ return; }