summaryrefslogtreecommitdiff
path: root/tokenizer.c
blob: f841d90037e754edb9e88e1da6bde286996b250d (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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
  Copyright (C) 2019, 2020 Leo Tenenbaum.
  This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever.
  You should have received a copy of the GNU General Public License along with toc. If not, see <https://www.gnu.org/licenses/>.
*/
static const char *keywords[KW_COUNT] =
	{";", ":", ",", "(", ")", "{", "}", "[", "]", "==",
	 "+=", "-=", "*=", "/=",
	 "!=", "<=", "<", ">=", ">",
	 "+", "-", "*", "!", "&", "/", "..", ".",
	 "=",
	 "if", "elif", "else", "while", "each", "return", "fn", "as",
	 "new", "del", "struct",
	 "int", "i8", "i16", "i32", "i64",
	 "u8", "u16", "u32", "u64", "float", "f32", "f64", "Type",
	 "char", "bool", "true", "false"};

static inline const char *kw_to_str(Keyword k) { return keywords[k]; }

static const char *directives[DIRECT_COUNT] =
	{"C", "sizeof", "alignof", "export"};

/* Returns KW_COUNT if it's not a keyword */
/* OPTIM: don't use strncmp so much */
static Keyword tokenize_kw(char **s) {
	for (Keyword k = 0; k < KW_COUNT; k = k + 1) {
		size_t len = strlen(keywords[k]);
		if (strncmp(*s, keywords[k], len) == 0) {
			if (k > KW_LAST_SYMBOL) {
				/* 
				   it's not a symbol, so we need to check if it's something like "intfoo"
				 */
				if (isident((*s)[len])) {
					continue;
				}
			}
			*s += len;
			return k;
		}
	}
	return KW_COUNT;
}



/* Returns DIRECT_COUNT if it's not a directive */
static Directive tokenize_direct(char **s) {
	for (Directive d = 0; d < DIRECT_COUNT; d = d + 1) {
		size_t len = strlen(directives[d]);
		if (strncmp(*s, directives[d], len) == 0) {
			if (isident((*s)[len])) {
				continue;
			}
			*s += len;
			return d;
		}
	}
	return DIRECT_COUNT;
}

static inline bool token_is_kw(Token *t, Keyword kw) {
	return t->kind == TOKEN_KW && t->kw == kw;
}

static inline bool token_is_direct(Token *t, Directive d) {
	return t->kind == TOKEN_DIRECT && t->direct == d;
}

static const char *token_kind_to_str(TokenKind t) {
	switch (t) {
	case TOKEN_KW: return "keyword";
	case TOKEN_IDENT: return "identifier";
	case TOKEN_DIRECT: return "directive";
	case TOKEN_LITERAL_NUM: return "numerical literal";
	case TOKEN_LITERAL_CHAR: return "character literal";
	case TOKEN_LITERAL_STR: return "string literal";
	case TOKEN_EOF: return "end of file";
	}
	assert(0);
	return "";
}

static void fprint_token(FILE *out, Token *t) {
	fprintf(out, "l%lu-", (unsigned long)t->where.line);
	switch (t->kind) {
	case TOKEN_KW:
		fprintf(out, "keyword: %s", kw_to_str(t->kw));
		break;
	case TOKEN_IDENT:
		fprintf(out, "identifier: %p: ", (void*)t->ident);
		fprint_ident(out, t->ident);
		break;
	case TOKEN_LITERAL_NUM:
		fprintf(out, "number: ");
		switch (t->num.kind) {
		case NUM_LITERAL_INT:
			fprintf(out, U64_FMT, t->num.intval);
			break;
		case NUM_LITERAL_FLOAT:
			fprintf(out, "%g", (double)t->num.floatval);
			break;
		}
		break;
	case TOKEN_LITERAL_CHAR:
		fprintf(out, "char: '%c' (%d)", t->chr, t->chr);
		break;
	case TOKEN_LITERAL_STR:
		fprintf(out, "str: \"%s\"", t->str.str);
		break;
	case TOKEN_DIRECT:
		fprintf(out, "directive: #%s", directives[t->direct]);
		break;
	case TOKEN_EOF:
		fprintf(out, "eof");
		break;
	}
}

static inline void tokr_nextchar(Tokenizer *t) {
	if (*(t->s) == '\n') {
		++t->line;
	}
	++t->s;
}

static char tokr_esc_seq(Tokenizer *t) {
	/* TODO: add more of these incl. \x41, \100 */
	switch (*t->s) {
	case '\'':
		tokr_nextchar(t);
		return '\'';
	case '"':
		tokr_nextchar(t);
		return '"';
	case '\\':
		tokr_nextchar(t);
		return '\\';
	case 'n':
		tokr_nextchar(t);
		return '\n';
	default:
		return 0;
	}

}

/* to be used during tokenization */
static void tokenization_err(Tokenizer *t, const char *fmt, ...) {
	va_list args;
	Location where;
	where.line = t->line;
	where.ctx = t->err_ctx;
	where.pos = (U32)(t->s - where.ctx->str);
	va_start(args, fmt);
	err_vprint(where, fmt, args);
	va_end(args);
	char *end_of_line = strchr(t->s, '\n');
	if (end_of_line) {
		t->s = end_of_line;
		++t->s; /* move past newline */
	} else {
		t->s = strchr(t->s, '\0');
	}
	++t->line;
}

/* to be used after tokenization */
static void tokr_err_(
#if ERR_SHOW_SOURCE_LOCATION
					  const char *src_file, int src_line,
#endif
					  Tokenizer *t, const char *fmt, ...) {
	if (!t->token->where.ctx->enabled) return;
#if ERR_SHOW_SOURCE_LOCATION
	err_fprint("At line %d of %s:\n", src_line, src_file);
#endif
	va_list args;
	va_start(args, fmt);
	err_vprint(t->token->where, fmt, args);
	va_end(args);
}

#if ERR_SHOW_SOURCE_LOCATION
#define tokr_err(...) tokr_err_(__FILE__, __LINE__, __VA_ARGS__)
#else
#define tokr_err tokr_err_
#endif

static void tokr_put_location(Tokenizer *tokr, Token *t) {
	t->where.line = tokr->line;
	t->where.ctx = tokr->err_ctx;
	t->where.pos = (U32)(tokr->s - t->where.ctx->str);
}

static void tokr_get_location(Tokenizer *tokr, Token *t) {
	tokr->line = t->where.line;
	tokr->s = t->where.pos + t->where.ctx->str;
}

/* 
the allocator you pass it will be used for string literals, so it shouldn't be freed
until everything is done
*/
static void tokr_create(Tokenizer *t, Identifiers *idents, ErrCtx *err_ctx, Allocator *allocr) {
	t->tokens = NULL;
	arr_resv(&t->tokens, 256);
	t->allocr = allocr;
	t->idents = idents;
	t->err_ctx = err_ctx;
}

static inline void *tokr_malloc(Tokenizer *t, size_t bytes) {
	return allocr_malloc(t->allocr, bytes);
}

static Token *tokr_add(Tokenizer *t) {
	Token *token = arr_add(&t->tokens);
	tokr_put_location(t, token);
	return token;
}

static bool tokenize_string(Tokenizer *t, char *str) {
	int has_err = 0;
	t->s = str;
	t->err_ctx->str = str;
	t->line = 1;
	while (1) {
		if (*t->s == 0) break;
		if (isspace(*t->s)) {
			tokr_nextchar(t);
	    	continue;
		}
		
		if (*t->s == '/') {
			/* maybe it's a comment */
			int is_comment = 1;
			switch (t->s[1]) {
			case '/': /* single line comment */
				tokr_nextchar(t);
				while (*t->s && *t->s != '\n') ++t->s;
				if (*t->s) tokr_nextchar(t); /* skip newline */
				break;
			case '*': { /* multi line comment */
				tokr_nextchar(t);
				int comment_level = 1; /* allow nested multi-line comments */
			    while (*t->s) {
					if (t->s[0] == '*' && t->s[1] == '/') {
						t->s += 2;
						--comment_level;
						if (comment_level == 0) {
							break;
						}
					} else if (t->s[0] == '/' && t->s[1] == '*') {
						t->s += 2;
						++comment_level;
					} else {
						tokr_nextchar(t);
					}
				}
				if (*t->s == 0) {
					tokenization_err(t, "End of file reached inside multi-line comment.");
					abort(); /* there won't be any further errors, of course */
				}
			} break;
			default:
				is_comment = 0;
				break;
			}
			if (is_comment) continue;
		}
		
		if (*t->s == '#') {
			/* it's a directive */
			char *start_s = t->s;
			++t->s; /* move past # */
		    Directive direct = tokenize_direct(&t->s);
			if (direct != DIRECT_COUNT) {
				/* it's a directive */
				Token *token = tokr_add(t);
				tokr_put_location(t, token);
				token->where.pos = (U32)(start_s - token->where.ctx->str);
				token->kind = TOKEN_DIRECT;
				token->direct = direct;
				continue;
			}
			--t->s; /* go back to # */
			tokenization_err(t, "Unrecognized directive.");
			goto err;
		}
		
		{
			char *start_s = t->s;
			Keyword kw = tokenize_kw(&t->s);
			if (kw != KW_COUNT) {
				/* it's a keyword */
				Token *token = tokr_add(t);
				tokr_put_location(t, token);
				token->where.pos = (U32)(start_s - token->where.ctx->str);
				token->kind = TOKEN_KW;
				token->kw = kw;
				continue;
			}
		}
		
		/* check if it's a number */
		if (isdigit(*t->s)) {
			/* it's a numeric literal */
			int base = 10;
			Floating decimal_pow10 = 0;
			Token *token = tokr_add(t);
			tokr_put_location(t, token);
			NumLiteral *n = &token->num;
			n->kind = NUM_LITERAL_INT;
			n->intval = 0;
			
			if (*t->s == '0') {
				tokr_nextchar(t);
				/* octal/hexadecimal/binary (or zero) */
				char format = *t->s;
				if (isdigit(format)) /* octal */
					base = 8;
				else {
					switch (format) {
					case 'b':
						base = 2;
						tokr_nextchar(t);
						break;
					case 'x':
						base = 16;
						tokr_nextchar(t);
						break;
					default:
						/* it's 0/0.something etc.  */
						break;
					}
				}
			}
			while (1) {
				if (*t->s == '.') {
					if (t->s[1] == '.') {
						/* .. (not a decimal point; end the number here) */
						break;
					}
					if (n->kind == NUM_LITERAL_FLOAT) {
						tokenization_err(t, "Double . in number.");
						goto err;
					}
					if (base != 10) {
						tokenization_err(t, "Decimal point in non base 10 number.");
						goto err;
					}
				    n->kind = NUM_LITERAL_FLOAT;
					decimal_pow10 = 0.1;
					n->floatval = (Floating)n->intval;
					tokr_nextchar(t);
					continue;
				} else if (*t->s == 'e') {
					tokr_nextchar(t);
					if (n->kind == NUM_LITERAL_INT) {
						n->kind = NUM_LITERAL_FLOAT;
						n->floatval = (Floating)n->intval;
					}
					/* TODO: check if exceeding maximum exponent */
					int exponent = 0;
					if (*t->s == '+')
						tokr_nextchar(t); /* ignore + after e */
					
					int negative_exponent = 0;
					if (*t->s == '-') {
						tokr_nextchar(t);
						negative_exponent = 1;
					}
					for (; isdigit(*t->s); tokr_nextchar(t)) {
						exponent *= 10;
						exponent += *t->s - '0';
					}
					/* OPTIM: Slow for very large exponents (unlikely to happen) */
					for (int i = 0; i < exponent; ++i) {
						if (negative_exponent)
							n->floatval /= 10;
						else
							n->floatval *= 10;
					}
						
					break;
				}
				int digit = -1;
				if (base == 16) {
					if (*t->s >= 'a' && *t->s <= 'f')
						digit = 10 + *t->s - 'a';
					else if (*t->s >= 'A' && *t->s <= 'F')
						digit = *t->s - 'A';
				}
				if (digit == -1) {
					if (*t->s >= '0' && *t->s <= '9')
						digit = *t->s - '0';
				}
				if (digit < 0 || digit >= base) {
					if (isdigit(*t->s)) {
						/* something like 0b011012 */
						tokenization_err(t, "Digit %d cannot appear in a base %d number.", digit, base);
						goto err;
					}
					/* end of numeric literal */
					break;
				}
				switch (n->kind) {
				case NUM_LITERAL_INT:
					if (n->intval > U64_MAX / (U64)base ||
						n->intval * (U64)base > U64_MAX - (U64)digit) {
						/* too big! */
						tokenization_err(t, "Number too big to fit in a numeric literal.");
						goto err;
					}
					n->intval *= (U64)base;
					n->intval += (U64)digit;
					break;
				case NUM_LITERAL_FLOAT:
					n->floatval += decimal_pow10 * (Floating)digit;
					decimal_pow10 /= 10;
					break;
				}
				tokr_nextchar(t);
			}
		    
			token->kind = TOKEN_LITERAL_NUM;
			continue;
		}

		if (*t->s == '\'') {
			/* it's a character literal! */
			Token *token = tokr_add(t);
			tokr_put_location(t, token);
			tokr_nextchar(t);
			char c;
			if (*t->s == '\\') {
				/* escape sequence */
				tokr_nextchar(t);
				c = tokr_esc_seq(t);
				if (c == 0) {
					tokenization_err(t, "Unrecognized escape character: '\\%c'.", *t->s);
					goto err;
				}
			} else {
				c = *t->s;
				tokr_nextchar(t);
			}
			if (*t->s != '\'') {
				tokenization_err(t, "End of character literal expected.");
				goto err;
			}
			tokr_nextchar(t);
			token->kind = TOKEN_LITERAL_CHAR;
			token->chr = c;
			continue;
		}

		if (*t->s == '"') {
			/* it's a string literal! */
			Token *token = tokr_add(t);
			tokr_put_location(t, token);
			tokr_nextchar(t);
			size_t len = 0; /* counts \n as 2 chars */
			size_t backslashes = 0;
			while (*t->s != '"' || backslashes % 2 == 1) {
				if (*t->s == '\\') {
					++backslashes;
				} else if (*t->s == 0) {
					/* return t to opening " so that we go to the next line */
					tokr_get_location(t, token);
					tokenization_err(t, "No matching \" found.");
					goto err;
				} else {
					backslashes = 0;
				}
				++len;
				tokr_nextchar(t);
			}
			char *strlit = tokr_malloc(t, len + 1);
		    char *strptr = strlit;
			tokr_get_location(t, token);
			tokr_nextchar(t); /* past opening " */
			while (*t->s != '"') {
				assert(*t->s);
				if (*t->s == '\\') {
					tokr_nextchar(t);
					char c = tokr_esc_seq(t);
					if (c == 0) {
						tokenization_err(t, "Unrecognized escape character: '\\%c'.", *t->s);
						goto err;
					}
					*strptr++ = c;
				} else {
					*strptr++ = *t->s;
					tokr_nextchar(t);
				}
			}
			*strptr = 0;
			token->kind = TOKEN_LITERAL_STR;
			token->str.len = (size_t)(strptr - strlit);
			token->str.str = strlit;
			tokr_nextchar(t); /* move past closing " */
			continue;
		}

		if (isident(*t->s)) {
			/* it's an identifier */
			Token *token = tokr_add(t);
			tokr_put_location(t, token);
			Identifier ident = ident_insert(t->idents, &t->s);
			token->kind = TOKEN_IDENT;
			token->ident = ident;
			continue;
		}		
		tokenization_err(t, "Token not recognized");
	err:
		has_err = 1;
	}
	Token *token = tokr_add(t);
	token->kind = TOKEN_EOF;
	
	t->token = t->tokens;
	return !has_err;
}

/* 
   skip to one token past the next semicolon not in braces (or the end of the file).
*/
static void tokr_skip_semicolon(Tokenizer *t) {
	int brace_level = 0;
	while (t->token->kind != TOKEN_EOF) {
		if (t->token->kind == TOKEN_KW) switch (t->token->kw) {
			case KW_LBRACE: ++brace_level; break;
			case KW_RBRACE: --brace_level; break;
			case KW_SEMICOLON:
				if (brace_level == 0) {
					++t->token;
					return;
				}
				break;
			default: break;
			}
		++t->token;
	}
}

/* only frees tokens, not string literals (because those are on the allocator). */
static void tokr_free(Tokenizer *t) {
    arr_clear(&t->tokens);
}