This repository has been archived by the owner on May 31, 2018. It is now read-only.
/
lexer.c
360 lines (313 loc) · 7.5 KB
/
lexer.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
//
// lexer.c
//
// Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
//
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "lexer.h"
/*
* Next char in the array.
*/
#ifdef EBUG_LEXER
#define next \
(self->stash = self->source[self->offset++]\
, fprintf(stderr, "'%c'\n", self->stash)\
, self->stash)
#else
#define next (self->stash = self->source[self->offset++])
#endif
/*
* Undo the previous char.
*/
#define undo (self->source[--self->offset] = self->stash)
/*
* Assign token `t`.
*/
#define token(t) (self->tok.type = LUNA_TOKEN_##t)
/*
* Accept char `c` or undo and return 0.
*/
#define accept(c) (c == next ? c : (undo, 0))
/*
* Set error `msg` and assign ILLEGAL token.
*/
#define error(msg) (self->error = msg, token(ILLEGAL))
/*
* Initialize lexer with the given `source` and `filename`.
*/
void
luna_lexer_init(luna_lexer_t *self, char *source, const char *filename) {
self->error = NULL;
self->source = source;
self->filename = filename;
self->indent_stack[0] = 0;
self->indents = 0;
self->outdents = 0;
self->lineno = 1;
self->offset = 0;
}
/*
* Convert hex digit `c` to a base 10 int,
* returning -1 on failure.
*/
static int
hex(const char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
/*
* Perform an outdent.
*/
static int
outdent(luna_lexer_t *self) {
--self->outdents;
return token(OUTDENT);
}
/*
* Scan newline, and determine if we have
* an indentation.
*/
static int
scan_newline(luna_lexer_t *self) {
int curr = 0;
int prev = self->indent_stack[self->indents];
++self->lineno;
while (accept(' ')) ++curr;
if (curr > prev) {
token(INDENT);
self->indent_stack[++self->indents] = curr;
} else if (curr < prev) {
while (self->indent_stack[self->indents] > curr) {
++self->outdents;
--self->indents;
}
outdent(self);
} else {
token(NEWLINE);
}
return 1;
}
/*
* Scan identifier.
*/
static int
scan_ident(luna_lexer_t *self, int c) {
int len = 0;
char buf[128]; // TODO: ditch these buffers
token(ID);
do {
buf[len++] = c;
} while (isalpha(c = next) || isdigit(c) || '_' == c);
undo;
buf[len++] = 0;
// TODO: refactor this lameness with length checks etc
if (0 == strcmp("if", buf)) return token(IF);
else if (0 == strcmp("else", buf)) return token(ELSE);
else if (0 == strcmp("unless", buf)) return token(UNLESS);
else if (0 == strcmp("while", buf)) return token(WHILE);
else if (0 == strcmp("until", buf)) return token(UNTIL);
else if (0 == strcmp("for", buf)) return token(FOR);
else if (0 == strcmp("return", buf)) return token(RETURN);
else if (0 == strcmp("not", buf)) return token(OP_LNOT);
self->tok.value.as_string = strdup(buf); // TODO: remove
return 1;
}
/*
* Scan string hex literal, returning -1 on invalid digits.
*/
static int
hex_literal(luna_lexer_t *self) {
int a = hex(next)
, b = hex(next);
if (a > -1 && b > -1) return a << 4 | b;
error("string hex literal \\x contains invalid digits");
return -1;
}
/*
* Scan string.
*/
static int
scan_string(luna_lexer_t *self, int quote) {
int c, len = 0;
char buf[128];
token(STRING);
while (quote != (c = next)) {
switch (c) {
case '\n': ++self->lineno; break;
case '\\':
switch (c = next) {
case 'a': c = '\a'; break;
case 'b': c = '\b'; break;
case 'e': c = '\e'; break;
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'v': c = '\v'; break;
case 'x':
if (-1 == (c = hex_literal(self)))
return 0;
}
break;
}
buf[len++] = c;
}
buf[len++] = 0;
self->tok.value.as_string = strdup(buf); // TODO: remove
return 1;
}
/*
* Scan number.
*/
static int
scan_number(luna_lexer_t *self, int c) {
// TODO: exponential notation
int n = 0, type = 0;
token(INT);
switch (c) {
case '0': goto scan_hex;
default: goto scan_int;
}
scan_hex:
switch (c = next) {
case 'x':
if (!isxdigit(c = next)) {
error("hex literal expects one or more digits");
return 0;
} else {
do n = n << 4 | hex(c);
while (isxdigit(c = next));
}
self->tok.value.as_int = n;
undo;
return 1;
default:
undo;
c = '0';
goto scan_int;
}
// [0-9_]+
scan_int:
do {
if ('_' == c) continue;
else if ('.' == c) goto scan_float;
n = n * 10 + c - '0';
} while (isdigit(c = next) || '_' == c || '.' == c);
undo;
self->tok.value.as_int = n;
// [0-9_]+
scan_float: {
int e = 1;
type = 1;
token(FLOAT);
while (isdigit(c = next) || '_' == c) {
if ('_' == c) continue;
n = n * 10 + c - '0';
e *= 10;
}
undo;
self->tok.value.as_float = (float) n / e;
}
return 1;
}
/*
* Scan the next token in the stream, returns 0
* on EOS, ILLEGAL token, or a syntax error.
*/
int
luna_scan(luna_lexer_t *self) {
int c;
token(ILLEGAL);
// deferred outdents
if (self->outdents) return outdent(self);
// scan
scan:
switch (c = next) {
case ' ':
case '\t': goto scan;
case '(': return token(LPAREN);
case ')': return token(RPAREN);
case '{': return token(LBRACE);
case '}': return token(RBRACE);
case '[': return token(LBRACK);
case ']': return token(RBRACK);
case ',': return token(COMMA);
case '.': return token(OP_DOT);
case '%': return token(OP_MOD);
case '^': return token(OP_BIT_XOR);
case '~': return token(OP_BIT_NOT);
case '?': return token(QMARK);
case ':': return token(COLON);
case '#':
self->tok.value.as_int = next;
return token(INT);
case '+':
switch (next) {
case '+': return token(OP_INCR);
case '=': return token(OP_PLUS_ASSIGN);
default: return undo, token(OP_PLUS);
}
case '-':
switch (next) {
case '-': return token(OP_DECR);
case '=': return token(OP_MINUS_ASSIGN);
default: return undo, token(OP_MINUS);
}
case '*':
return '*' == next
? token(OP_POW)
: (undo, token(OP_MULT));
case '!':
return '=' == next
? token(OP_NEQ)
: (undo, token(OP_NOT));
case '=':
return '=' == next
? token(OP_EQ)
: (undo, token(OP_ASSIGN));
case '&':
return '&' == next
? token(OP_AND)
: (undo, token(OP_BIT_AND));
case '|':
return '|' == next
? token(OP_OR)
: (undo, token(OP_BIT_OR));
case '<':
if ('=' == next) return token(OP_LTE); undo;
if ('<' == next) return token(OP_BIT_SHL); undo;
return token(OP_LT);
case '>':
if ('=' == next) return token(OP_GTE); undo;
if ('>' == next) return token(OP_BIT_SHR); undo;
return token(OP_GT);
case '/':
if ('/' == next) {
while ((c = next) != '\n' && c) ; undo;
goto scan;
}
return token(OP_DIV);
case '\n':
return scan_newline(self);
case '"':
case '\'':
return scan_string(self, c);
case 0:
if (self->indents) {
--self->indents;
return token(OUTDENT);
}
token(EOS);
return 0;
default:
if (isalpha(c) || '_' == c) return scan_ident(self, c);
if (isdigit(c) || '.' == c) return scan_number(self, c);
error("illegal character");
return 0;
}
}