-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscanner.c
381 lines (341 loc) · 7.73 KB
/
scanner.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
/*
* arbre
*
* (c) 2011-2012, Alexis Sellier
*
* scanner.c
*
* raw source file scanner.
*
* + scanner
* - scanner_free
*
* -> scan
*
* usage:
*
* struct source *src = source("fnord.arb");
* struct scanner *s = scanner(src);
* Token *t;
*
* while ((t = scan(s)).tok != T_EOF) {
* ... do something with token ...
* }
*
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "arbre.h"
#include "scanner.h"
static void next(struct scanner *);
static void pushlvl(struct scanner *, int);
static int poplvl(struct scanner *);
/*
* Scanner allocator
*/
struct scanner *scanner(struct source *source)
{
struct scanner *s = malloc(sizeof(*s));
s->src = source->data;
s->source = source;
s->len = source->size;
s->pos = s->rpos = -1;
s->ch = T_ILLEGAL;
s->lvl = 0;
s->dedent = 0;
s->lf = 0;
s->line = 0;
s->linepos= 0;
s->lvls.size = 0;
s->lvls.stack = NULL;
s->lvls.sp = NULL;
source_addline(s->source, 0);
pushlvl(s, 0);
next(s);
return s;
}
/*
* Scanner de-allocator
*/
void scanner_free(struct scanner *s)
{
free(s->lvls.stack);
free(s);
}
/*
* Read the next character into s->ch.
* Set s->ch to -1 if we've reached the EOF.
*/
static void next(struct scanner *s)
{
s->rpos ++;
if (s->rpos <= s->len) {
char c = s->src[s->rpos];
if (c == '\0') {
/* TODO: Handle error */
/* TODO: Handle unicode */
} else {
s->ch = c;
}
} else {
s->ch = -1; /* EOF */
}
}
/*
* Consume whitespace. '\n' is treated differently,
* due to the indentation-sensitive grammar.
*/
static void whitespace(struct scanner *s)
{
while (s->ch != '\n' && isspace(s->ch)) {
next(s);
}
}
/*
* Consume an identifier, such as: fnord
*/
static void scan_ident(struct scanner *s)
{
if (isalnum(s->ch) || s->ch == '_')
next(s);
while (isalnum(s->ch) || s->ch == '_' || s->ch == '\'') {
next(s);
}
}
/*
* Consume an atom, such as: 'fnord
*/
static void scan_atom(struct scanner *s)
{
if (s->ch != '\'')
return;
next(s);
while (isalnum(s->ch) || s->ch == '_' || s->ch == '.') {
next(s);
}
}
/*
* Consume a number, such as: 42
*/
static void scan_number(struct scanner *s)
{
while (isdigit(s->ch)) {
next(s);
}
}
/*
* Consume a string, such as: "fnord, fnord, fnord"
*/
static void scan_string(struct scanner *s)
{
while (s->ch != '"') {
if (s->ch == '\n' || s->ch < 0) {
/* TODO: Handle error */
}
next(s);
}
next(s); /* Consume closing '"' */
}
/*
* Consume a comment, such as: -- fnord comment
*/
static void scan_comment(struct scanner *s)
{
while (s->ch != '\n') {
next(s);
}
}
/*
* Consume a character, such as: `z`
*/
static void scan_char(struct scanner *s)
{
if (s->ch == '`') {
next(s);
} else if (isascii(s->ch)) {
next(s);
if (s->ch == '`') {
next(s);
} else {
/* TODO: Handle error */
}
} else {
/* TODO: Handle error */
}
}
/*
* Consume indentation. Return `true` if there's an indent.
*/
static int scan_indent(struct scanner *s)
{
size_t pos, indent;
/* calculate current indentation */
pos = s->rpos, whitespace(s), indent = s->rpos - pos;
/* indent, dedent or don't change indentation */
if (indent > s->lvl && s->ch != '\n') {
pushlvl(s, (s->lvl = indent));
return true;
} else {
/* Only dedent if the current indentation
* is smaller than the previous indentation. */
while ((s->lvl > 0) && (indent <= *(s->lvls.sp - 1))) {
poplvl(s);
s->lvl = *(s->lvls.sp);
s->dedent ++;
}
}
return false;
}
/*
* Push the current indentation level to the stack
*/
static void pushlvl(struct scanner *s, int lvl)
{
s->lvls.size ++;
s->lvls.stack = realloc(s->lvls.stack, s->lvls.size * sizeof(lvl));
s->lvls.sp = s->lvls.stack + s->lvls.size - 1;
*(s->lvls.sp) = lvl;
}
/*
* Pop the current indentation from the stack
*/
static int poplvl(struct scanner *s)
{
int lvl = *(s->lvls.sp);
s->lvls.size --;
s->lvls.stack = realloc(s->lvls.stack, s->lvls.size * sizeof(lvl));
s->lvls.sp = s->lvls.stack + s->lvls.size - 1;
return lvl;
}
static void newline(struct scanner *s)
{
s->lf = false;
s->line ++;
s->linepos = s->rpos;
source_addline(s->source, s->linepos);
s->source->col = 0;
s->source->line ++;
}
/*
* Scan the source for the next token, and return it.
* We treat '\n' specially, as it could precede a change
* in indentation.
* Because it is possible for the level of indentation to
* fall from N to 0 after a single T_LF token, and we want
* to return a T_DEDENT for *each* of these levels,
* we have to keep track of dedents which haven't been returned
* by the scanner yet, and return them on next call to `scan` in [1].
* This is done via the `dedent` property of the scanner.
*/
Token *scan(struct scanner *s)
{
TOKEN tok = T_ILLEGAL;
size_t pos = s->pos = s->rpos;
char c;
/* if the last token a T_LF, scan for indentation changes */
if (s->lf) {
newline(s);
if (scan_indent(s)) {
return token(T_INDENT, s->source, pos, NULL);
}
}
/* 1. We have pending dedents, return a T_DEDENT, and decrement
* the dedent counter. */
if (s->dedent) {
s->dedent --;
return token(T_DEDENT, s->source, pos, NULL);
}
whitespace(s);
c = s->ch;
pos = s->rpos;
if (c == -1) {
tok = T_EOF;
} else if (islower(c)) {
scan_ident(s);
tok = T_IDENT;
} else if (c == '\'') {
scan_atom(s);
tok = T_ATOM;
} else if (isdigit(c)) {
scan_number(s);
tok = T_INT;
} else {
next(s); /* We advance here, but keep matching on `c` */
switch (c) {
case '@' : tok = T_AT; break;
case '_' : tok = T_UNDER; break;
case '?' : tok = T_QUESTION; break;
case '&' : tok = T_AND; break;
case '|' : tok = T_PIPE; break;
case '(' : tok = T_LPAREN; break;
case ')' : tok = T_RPAREN; break;
case '[' : tok = T_LBRACK; break;
case ']' : tok = T_RBRACK; break;
case '{' : tok = T_LBRACE; break;
case '}' : tok = T_RBRACE; break;
case ';' : tok = T_SEMICOLON; break;
case ',' : tok = T_COMMA; break;
case '"' : tok = T_STRING; scan_string(s); break;
case '`' : tok = T_CHAR; scan_char(s); break;
case '\n' : tok = T_LF; s->lf = true; break;
case ' ' : tok = T_SPACE; break;
case '+' : tok = T_PLUS; break;
case '/' : tok = T_SLASH; break;
case '\\' : tok = T_BSLASH; break;
case '.' :
if (s->ch == '.') {
tok = T_ELLIPSIS;
next(s);
} else {
tok = T_PERIOD;
}
break;
case ':' :
switch (s->ch) {
case '=': tok = T_DEFINE; next(s); break;
default : tok = T_COLON;
}
break;
case '<':
switch (s->ch) {
case '-': tok = T_LARROW; next(s); break;
case '=': tok = T_LDARROW; next(s); break;
default : tok = T_LT;
}
break;
case '>':
if (s->ch == '>') {
next(s);
tok = T_RDARROW;
} else {
tok = T_GT;
}
break;
case '-':
if (isdigit(s->ch)) {
tok = T_INT;
next(s);
scan_number(s);
} else {
switch (s->ch) {
case '>': tok = T_RARROW; next(s); break;
case '-': tok = T_COMMENT; scan_comment(s); break;
default : tok = T_MINUS;
}
}
break;
case '=':
switch (s->ch) {
case '>': tok = T_REQARROW; next(s); break;
default : tok = T_EQ;
}
break;
}
}
s->source->col = pos - s->linepos;
s->pos = pos;
return token(tok, s->source, pos, strndup(s->src + pos, s->rpos - pos));
}