aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/token.c
blob: 197fbd0d9588f7f76c429cb86f85b0c6675921d4 (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

/*
 * Tokenization routines for the bootstrap compiler.
 *
 * Copyright © 2025 Samuel Lidén Borell <samuel@kodafritt.se>
 *
 * SPDX-License-Identifier: EUPL-1.2+
 */
#include <string.h>
#include "compiler.h"
#include "token.h"

#define DONE_1CH(token) do { t = (token); goto done_1ch; } while (0)
#define DONE_2CH(token) do { t = (token); goto done_2ch; } while (0)

static FILE *f;
static char line[SOURCELINE_MAX];
static const char *s;
static const char *last_token_start;

static enum Token tok_alphanum(struct LexemeInfo *li_out);
static enum Token tok_number(struct LexemeInfo *li_out);
static enum Token tok_string(struct LexemeInfo *li_out);

void tokenizer_init(FILE *file)
{
    f = file;
    s = NULL;
}

bool tokenizer_next_line(void)
{
    if (s && *s) {
        error("Expected end of line");
    }
    s = line;
    /* Don't strip comments here, because '#' might appear inside strings,
       which read_source_line() doesn't know about. */
    return read_source_line(f, line, NULL, KEEP_COMMENTS);
}

bool tokenizer_line_is_indented(void)
{
    return line[0] == ' ' || line[0] == '\t';
}


enum Token tokenize(struct LexemeInfo *li_out)
{
    enum Token t;
    size_t len;
    char c;

    memset(li_out, 99, sizeof(*li_out));
    while (*s == ' ' || *s == '\t') s++;

    last_token_start = s;
    c = *s;
    if (c == '\0' || c == '#') {
        /* read_source_line() strips \n and \r */
        t = T_EOL;
        s = "";
        len = 0;
        goto done;
    } else if (c <= 31 || c >= 127) {
        error("Non-ASCII (or control) character outside string/comment");
    } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_') {
        return tok_alphanum(li_out);
    } else if (c >= '0' && c <= '9') {
        return tok_number(li_out);
    } else if (c == '"') {
        return tok_string(li_out);
    } else {
        s++;
        switch (c) {
        case '=':
            if (*s == '=') DONE_2CH(T_SYM_DoubleEqual);
            DONE_1CH(T_SYM_SingleEqual);
        case '<':
            if (*s == '-') DONE_2CH(T_SYM_LArrow);
            if (*s == '>') DONE_2CH(T_SYM_NotEqual);
            if (*s == '=') DONE_2CH(T_SYM_LessEqual);
            DONE_1CH(T_SYM_Less);
        case '>':
            if (*s == '=') DONE_2CH(T_SYM_GreaterEqual);
            DONE_1CH(T_SYM_Greater);
        case '.':
            /* TODO "..." for imcomplete stuff? */
            DONE_1CH(T_SYM_Dot);
        case '+':
            DONE_1CH(T_SYM_Plus);
        case '-':
            if (*s == '>') DONE_2CH(T_SYM_RArrow);
            DONE_1CH(T_SYM_Minus);
        case '*':
            DONE_1CH(T_SYM_Asterisk);
        case '/':
            DONE_1CH(T_SYM_Slash);
        case '(':
            DONE_1CH(T_SYM_LParen);
        case ')':
            DONE_1CH(T_SYM_RParen);
        case '[':
            DONE_1CH(T_SYM_LBracket);
        case ']':
            DONE_1CH(T_SYM_RBracket);
        default:
            s--;
            error("Special character outside string/comment");
        }
    }
    unreachable();
  done_1ch:
    len = 1;
    goto done;
  done_2ch:
    len = 2;
    s++;
    goto done;
  done:
    li_out->len = len;
    return t;
}


#define CMP_KW(length, keyword)                     \
    if (memcmp(start, #keyword, (length)) == 0) {   \
        t = T_KW_##keyword;                         \
        goto is_keyword;                            \
    }

static enum Token tok_alphanum(struct LexemeInfo *li_out)
{
    enum Token t;
    size_t len = 0;
    const char *start = s;
    char c;

    do {
        c = *(++s);
        len++;
    } while ((c >= 'a' && c <= 'z') ||
             (c >= 'A' && c <= 'Z') ||
             (c >= '0' && c <= '9') || c == '_');

    switch (len) {
    case 2:
        CMP_KW(2, if)
        CMP_KW(2, in)
        CMP_KW(2, io)
        CMP_KW(2, of)
        CMP_KW(2, or)
        CMP_KW(2, to)
        break;
    case 3:
        CMP_KW(3, and)
        CMP_KW(3, end)
        CMP_KW(3, for)
        CMP_KW(3, int)
        CMP_KW(3, mod)
        CMP_KW(3, not)
        /* XXX should there be `and`/`or`, or `all_of`/`any_of`/`none_of`?
               maybe there should be a T... safe-vararg, like in Java. */
        CMP_KW(3, var)
        break;
    case 4:
        CMP_KW(4, bool)
        CMP_KW(4, byte)
        CMP_KW(4, case)
        CMP_KW(4, code)
        CMP_KW(4, elif)
        CMP_KW(4, else)
        CMP_KW(4, enum)
        CMP_KW(4, from)
        CMP_KW(4, func)
        CMP_KW(4, long)
        CMP_KW(4, none)
        CMP_KW(4, sets)
        CMP_KW(4, true)
        break;
    case 5:
        CMP_KW(5, break)
        CMP_KW(5, class)
        CMP_KW(5, false)
        CMP_KW(5, local)
        CMP_KW(5, reads)
        CMP_KW(5, trait)
        CMP_KW(5, while)
        break;
    case 6:
        CMP_KW(6, assert)
        CMP_KW(6, export)
        CMP_KW(6, record)
        CMP_KW(6, return)
        CMP_KW(6, signed)
        CMP_KW(6, switch)
        break;
    case 7:
        CMP_KW(7, aliased)
        CMP_KW(7, default)
        CMP_KW(7, loopend)
        CMP_KW(7, returns)
        CMP_KW(7, section)
        break;
    case 8:
        CMP_KW(8, continue)
        CMP_KW(8, modifies)
        CMP_KW(8, unsigned)
        CMP_KW(8, volatile)
        CMP_KW(8, wrapping)
        break;
    case 9:
        CMP_KW(9, loopempty)
        CMP_KW(9, templates)
        break;
    case 10:
        CMP_KW(10, calledfrom)
        break;
    }
    /* No keyword matches. It's an identifier */
    li_out->len = len;
    li_out->string = start;
    return *start > 'Z' ? T_LowerIdent : T_UpperIdent;
  is_keyword:
    return t;
}

static enum Token tok_number(struct LexemeInfo *li_out)
{
    uint64_t num = 0, limit;
    unsigned base;
    const char *num_start;

    base = 10;
    limit = 0x1999999999999999; /* 2^64 / 10 */
    if (*s == '0') {
        switch (s[1]) {
        case 'b':
            base = 2;
            limit = 0x8000000000000000; /* 2^64 / 2 */
            goto skip_base;
        case 'x':
            base = 16;
            limit = 0x1000000000000000; /* 2^64 / 16 */
          skip_base:
            s += 2;
            if (*s == '_') {
                error("Leading underscore in number");
            }
            break;
        case 'B':
        case 'X':
            error("Base (`x` or `b`) must be lowercase");
        }
    }

    num_start = s;
    for (;; s++) {
        char c = *s;
        unsigned digit;
        if (c >= '0' && c <= '9') {
            digit = (unsigned)c - '0';
          have_digit:
            if (digit >= base) {
                if (base == 10 && digit == 14 /* "e" */) {
                    goto float_num;
                }
                error("Invalid digit in number");
            }
            if (num > limit) {
                error("Number too large (too many digits)");
            }
            num *= base;
            if (num > 0xFFFFFFFFFFFFFFFF - digit) {
                error("Number too large");
            }
            num += digit;
        } else if (c >= 'A' && c <= 'Z') {
            digit = 10 + (unsigned)(c - 'A');
            goto have_digit;
        } else if (c >= 'a' && c <= 'z') {
            digit = 10 + (unsigned)(c - 'a');
            goto have_digit;
        } else if (c == '.') {
          float_num:
            error("Sorry, floating point is not implemented in the bootstrap compiler");
        } else if (c == '_') {
            int next = s[1];
            if (next == '_') {
                error("Repeated underscores in number");
            } else if ((next < '0' || next > '9') &&
                       (next < 'a' || next > 'z') &&
                       (next < 'A' || next > 'Z')) {
                error("Trailing `_` in number");
            }
        } else {
            break;
        }
    }
    if (s == num_start) {
        error("No digits after base prefix");
    }
    li_out->num = num;
    return T_Integer;
}

static enum Token tok_string(struct LexemeInfo *li_out)
{
    const char *start = ++s;

    for (;;) {
        char c = *(s++);
        if (c == '\0') {
            error("Missing terminating \" of string");
        } else if (c == '"') {
            /* End of string */
            break;
        } else if (c == '\\') {
            /* Escape sequence - Handled in parser */
            if (*(s++) == '\0') {
                error("Unexpected end of string in escape sequence");
            }
        } else {
            /* Regular character */
        }
    }

    li_out->string = start;
    li_out->len = (size_t)(s - start - 1);
    return T_String;
}

void unread_token(void)
{
    s = last_token_start;
}

void expect(struct LexemeInfo *li_out, enum Token expected,
            const char *errmsg)
{
    enum Token t = tokenize(li_out);
    if (t != expected) {
        error(errmsg);
    }
}

void expect_next_line(void)
{
    struct LexemeInfo li;
    expect(&li, T_EOL, "Expected line break");
    if (!tokenizer_next_line()) {
        error("Unexpected end of file");
    }
}