-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.c
327 lines (288 loc) · 10.2 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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "memory.h"
#include "scanner.h"
#define CHAR_HT 0x09 // ASCII horizontal tab
#define CHAR_LF 0x0a // ASCII line feed
#define CHAR_CR 0x0d // ASCII carriage return
#define CHAR_RS 0x1e // ASCII record separator, used to separate input lines without triggering eval
typedef struct {
const char* start;
const char* current;
int line;
} Scanner;
static Scanner scanner;
void initScanner(const char* source) {
scanner.start = source;
scanner.current = source;
scanner.line = 1;
}
#define isAtEnd() (*scanner.current == '\0')
#define advance() scanner.current++
#define peek() (*scanner.current)
static char peekNext(void) {
if (isAtEnd())
return '\0';
return scanner.current[1];
}
static bool match(char expected) {
if (isAtEnd() || *scanner.current != expected)
return false;
scanner.current++;
return true;
}
static Token makeToken(TokenType type) {
Token token;
token.type = type;
token.start = scanner.start;
token.length = scanner.current - scanner.start;
token.line = scanner.line;
return token;
}
static Token errorToken(const char* message) {
Token token;
token.type = TOKEN_ERROR;
token.start = message;
token.length = strlen(message);
token.line = scanner.line;
return token;
}
static Token makeNumToken(bool isReal) {
Token token = makeToken(isReal ? TOKEN_REAL : TOKEN_INT);
if (token.length == 1 && !isdigit(*token.start))
return errorToken("No digits after radix.");
return token;
}
static void skipWhitespace(void) {
char c;
again:
c = peek();
if (c == CHAR_RS || c == CHAR_LF) {
scanner.line++;
goto advance;
} else if (c == ' ' || c == CHAR_CR || c == CHAR_HT) {
advance: advance();
goto again;
} else if (c == '/') {
if (peekNext() == '/') {
// A comment goes until the end of the line.
while ((c = peek()) != '\0' && c != CHAR_LF && c != CHAR_RS)
advance();
goto again;
}
}
}
// Wrapping 4 bytes Trie info into a single 32 bit argument
#ifdef WRAP_BIG_ENDIAN
#define WRAP(a,b,c,d) ((a)<<24|(b)<<16|(c)<<8|(d))
#else
#define WRAP(a,b,c,d) ((a)|(b)<<8|(c)<<16|(d)<<24)
#endif
#define AMBIGUOUS (-1)
typedef struct {
int8_t start;
int8_t length;
int8_t offset;
int8_t type;
} TrieInfo;
// and accessing Trie info components
#define TRIE(name) ((TrieInfo*)&trie)->name
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif
static Token identifier(int32_t trie) {
// Overlapping keyword postfixes in single string
const char* rest =
"andleturnassilsefynvarintupereakisue";
// 012345678901234567890123456789012345
// And ##
// Break ####
// CAse ##
// CLass ###
// Dynvar #####
// Else ###
// FAlse ###
// FOr #
// FUn #
// Handle #####
// If #
// Nil ##
// Or #
// Print ####
// Return #####
// Super ####
// THis ##
// TRue ##
// Var ##
// WHEn #
// WHIle ##
// 012345678901234567890123456789012345
int16_t id_length;
const char *src;
char c = scanner.start[0];
char c1;
TokenType tokenType = TOKEN_IDENTIFIER;
while (isalnum(peek()) || peek() == '_')
advance();
id_length = scanner.current - scanner.start;
// Disambiguate possible keywords starting with same letter.
if (trie == AMBIGUOUS) {
trie = 0;
if (id_length > 1) {
c1 = scanner.start[1];
if (c == 'c') {
if (c1 == 'a') trie = WRAP(2, 2, 14, TOKEN_CASE);
else if (c1 == 'l') trie = WRAP(2, 3, 9, TOKEN_CLASS);
} else if (c == 'f') {
if (c1 == 'a') trie = WRAP(2, 3, 13, TOKEN_FALSE);
else if (c1 == 'o') trie = WRAP(2, 1, 7, TOKEN_FOR);
else if (c1 == 'u') trie = WRAP(2, 1, 1, TOKEN_FUN);
} else if (c == 't') {
if (c1 == 'h') trie = WRAP(2, 2, 32, TOKEN_THIS);
else if (c1 == 'r') trie = WRAP(2, 2, 34, TOKEN_TRUE);
} else { // if (c == 'w') always true
if (c1 == 'h' && id_length > 2) {
c1 = scanner.start[2];
if (c1 == 'e') trie = WRAP(3, 1, 1, TOKEN_WHEN);
else if (c1 == 'i') trie = WRAP(3, 2, 3, TOKEN_WHILE);
}
}
}
}
if (trie && id_length == TRIE(start) + TRIE(length)) {
src = scanner.start + TRIE(start);
rest += TRIE(offset);
do {
if (*src++ != *rest++)
goto noKeyword;
} while (--TRIE(length));
tokenType = TRIE(type);
}
noKeyword:
return makeToken(tokenType);
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
static Token number(char start) {
bool exponentEmpty = true;
bool isReal = false;
if (start == '%')
while ((peek() | 0x01) == '1') // '0' or '1'
advance();
else if (start == '$')
while (isxdigit(peek()))
advance();
else {
while (isdigit(peek()))
advance();
// Look for fractional part
if (peek() == '.' && isdigit(peekNext())) {
// Consume the ".".
advance();
isReal = true;
while (isdigit(peek()))
advance();
}
// Look for exponential part
if ((peek() | LOWER_CASE_MASK) == 'e') { // 'E' or 'e'
advance();
isReal = true;
if (peek() == '+' || peek() == '-')
advance();
while (isdigit(peek())) {
advance();
exponentEmpty = false;
}
if (exponentEmpty)
return errorToken("Empty exponent in number.");
}
}
return makeNumToken(isReal);
}
static Token string(void) {
while (peek() != '"' && !isAtEnd()) {
if (peek() == CHAR_LF || peek() == CHAR_RS)
scanner.line++;
advance();
}
if (isAtEnd())
return errorToken("Unterminated string.");
// the closing quote
advance();
return makeToken(TOKEN_STRING);
}
Token scanToken(void) {
char c;
TokenType token;
int32_t trie = 0;
skipWhitespace();
scanner.start = scanner.current;
if (isAtEnd())
return makeToken(TOKEN_EOF);
c = *scanner.current++;
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '$': case '%':
return number(c);
// can't start keywords
case 'g': case 'j': case 'k': case 'l': case 'm':
case 'q': case 'u': case 'x': case 'y': case 'z':
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y':
case 'Z': case '_':
ident:
return identifier(trie);
// possibly starting unique keyword
case 'a': trie = WRAP(1, 2, 1, TOKEN_AND); goto ident;
case 'b': trie = WRAP(1, 4, 28, TOKEN_BREAK); goto ident;
case 'd': trie = WRAP(1, 5, 17, TOKEN_DYNVAR); goto ident;
case 'e': trie = WRAP(1, 3, 13, TOKEN_ELSE); goto ident;
case 'h': trie = WRAP(1, 5, 0, TOKEN_HANDLE); goto ident;
case 'i': trie = WRAP(1, 1, 16, TOKEN_IF); goto ident;
case 'n': trie = WRAP(1, 2, 12, TOKEN_NIL); goto ident;
case 'o': trie = WRAP(1, 1, 7, TOKEN_OR); goto ident;
case 'p': trie = WRAP(1, 4, 21, TOKEN_PRINT); goto ident;
case 'r': trie = WRAP(1, 5, 4, TOKEN_RETURN); goto ident;
case 's': trie = WRAP(1, 4, 25, TOKEN_SUPER); goto ident;
case 'v': trie = WRAP(1, 2, 20, TOKEN_VAR); goto ident;
// possibly starting several keywords
case 'c': case 'f': case 't': case 'w':
trie = AMBIGUOUS;
goto ident;
case '"':
return string();
case '(': token = TOKEN_LEFT_PAREN; break;
case ')': token = TOKEN_RIGHT_PAREN; break;
case '{': token = TOKEN_LEFT_BRACE; break;
case '}': token = TOKEN_RIGHT_BRACE; break;
case '[': token = TOKEN_LEFT_BRACKET; break;
case ']': token = TOKEN_RIGHT_BRACKET; break;
case ';': token = TOKEN_SEMICOLON; break;
case ':': token = TOKEN_COLON; break;
case ',': token = TOKEN_COMMA; break;
case '+': token = TOKEN_PLUS; break;
case '/': token = TOKEN_SLASH; break;
case '*': token = TOKEN_STAR; break;
case 92 : token = TOKEN_BACKSLASH; break; // IDE68k doesn't like '\\'
case '@': token = TOKEN_AT; break;
case '^': token = TOKEN_HAT; break;
case '?': token = TOKEN_PRINT; break;
case '!': if (match('=')) token = TOKEN_BANG_EQUAL; else token = TOKEN_BANG; break;
case '=': if (match('=')) token = TOKEN_EQUAL_EQUAL; else token = TOKEN_EQUAL; break;
case '<': if (match('=')) token = TOKEN_LESS_EQUAL; else token = TOKEN_LESS; break;
case '>': if (match('=')) token = TOKEN_GREATER_EQUAL; else token = TOKEN_GREATER; break;
case '.': if (match('.')) token = TOKEN_DOT_DOT; else token = TOKEN_DOT; break;
case '-': if (match('>')) token = TOKEN_ARROW; else token = TOKEN_MINUS; break;
default:
sprintf(buffer, "Invalid character '%c'.", c);
return errorToken(buffer);
}
return makeToken(token);
}