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
|
#include <da.h>
#include <mbstring.h>
#include <rtype.h>
#include "cerr.h"
#include "common.h"
#include "lexer.h"
#include "macros.h"
#define ISDIGIT(n) ((n) >= '0' && (n) <= '9')
#define U8MOV(sv, n) ((sv)->p += (n), (sv)->len -= (n))
#define E_BASE "integer with invalid base specifier"
#define E_EXTRA "unknown extraneous character"
#define E_IDENTCCHAR "illegal character in identifier"
#define E_IDENTLOST "local label missing identifier"
#define E_IDENTSCHAR "illegal first character in identifier"
#define E_UNTERMINATED "unterminated string literal"
#define E_UTF8 "invalid UTF-8 byte near ‘%02X’"
#define EOLS U"\n\v\f\r\x85\u2028\u2029"
#define NUMCHARS U"'0123456789abcdefABCDEF"
static void lexline(struct tokens *, struct u8view *);
static bool skipws(struct u8view *);
const char *
tokrepr(tokkind k)
{
return (const char *[]){
[T_COLON] = "colon", [T_EOL] = "end of line",
[T_IDENT] = "identifier", [T_NUMBER] = "number",
[T_STRING] = "string",
}[k];
}
struct tokens
lexfile(void)
{
const char8_t *s;
struct tokens toks;
struct u8view sv = filebuf;
if (s = u8chk(sv.p, sv.len))
die_with_off(filename, s - sv.p, E_UTF8, *s);
dainit(&toks, 256);
while (sv.len) {
size_t len = u8cbspn(sv.p, sv.len, EOLS, lengthof(EOLS) - 1);
struct u8view line = {
.p = sv.p,
.len = len,
};
lexline(&toks, &line);
/* Skip trailing EOL */
if (sv.len > len)
len += u8rlen(sv.p + len);
U8MOV(&sv, len);
}
return toks;
}
#define DIE_AT_POS_WITH_CODE2(HL, ...) \
DIE_AT_POS_WITH_CODE((HL), sv->p - w, __VA_ARGS__)
void
lexline(struct tokens *toks, struct u8view *sv)
{
struct token tok;
for (;;) {
int w;
rune ch;
if (!skipws(sv))
goto end;
tok.sv.p = sv->p;
tok.sv.len = w = u8next(&ch, &sv->p, &sv->len);
if (ISDIGIT(ch)) {
size_t off, m = 10;
tok.kind = T_NUMBER;
tok.base = 10;
if (ch == '0') {
w = u8next(&ch, &sv->p, &sv->len);
if (!w || rprop_is_pat_ws(ch)) {
U8MOV(sv, -w);
goto out;
}
tok.sv.len++;
switch (ch) {
case 'b':
tok.base = m = 2;
break;
case 'o':
tok.base = m = 8;
break;
case 'd':
/* Implicitly base-10 already */
break;
case 'x':
/* m = 22 because A–F can be both upper- or lowercase */
tok.base = 16;
m = 22;
break;
default:
if (!ISDIGIT(ch))
DIE_AT_POS_WITH_CODE2(tok.sv, E_BASE);
}
}
out:
/* +1 to support the digit separator */
tok.sv.len += off = u8bspn(sv->p, sv->len, NUMCHARS, m + 1);
U8MOV(sv, off);
} else if (ch == '.' || ch == '_' || rprop_is_xids(ch)) {
tok.kind = T_IDENT;
if (ch == '.') {
tok.sv.len += w = u8next(&ch, &sv->p, &sv->len);
if (!w || rprop_is_pat_ws(ch))
DIE_AT_POS_WITH_CODE2(tok.sv, E_IDENTLOST);
if (ch != '_' && !rprop_is_xids(ch)) {
U8MOV(&tok.sv, 1);
DIE_AT_POS_WITH_CODE2(tok.sv, E_IDENTSCHAR);
}
}
while (w = u8next(&ch, &sv->p, &sv->len)) {
if (ch == ':' || rprop_is_pat_ws(ch)) {
U8MOV(sv, -w);
break;
}
if (!rprop_is_xidc(ch)) {
struct u8view hl = {
.p = sv->p - w,
.len = w,
};
DIE_AT_POS_WITH_CODE2(hl, E_IDENTCCHAR);
}
tok.sv.len += w;
}
} else if (ch == '"') {
tok.kind = T_STRING;
while (w = u8next(&ch, &sv->p, &sv->len)) {
tok.sv.len += w;
if (ch == '"')
goto found;
}
DIE_AT_POS_WITH_CODE2(tok.sv, E_UNTERMINATED);
found:
} else if (ch == ':') {
tok.kind = T_COLON;
} else if (ch == ';') {
goto end;
} else {
struct u8view hl = {.p = sv->p - w, .len = w};
DIE_AT_POS_WITH_CODE2(hl, E_EXTRA);
}
/* The colon is the only token that isn’t whitespace separated */
if (ch != ':' && sv->len) {
w = u8next(&ch, &sv->p, &sv->len);
if (!w || !rprop_is_pat_ws(ch)) {
struct u8view hl = {.p = sv->p - w, .len = w};
DIE_AT_POS_WITH_CODE2(hl, E_EXTRA);
}
}
dapush(toks, tok);
}
end:
tok = (struct token){
.kind = T_EOL,
.sv.p = sv->p,
.sv.len = 0,
};
dapush(toks, tok);
}
#undef DIE_AT_POS_WITH_CODE2
bool
skipws(struct u8view *sv)
{
rune ch;
if (!sv->len)
return false;
for (int w = u8tor_uc(&ch, sv->p); rprop_is_pat_ws(ch);
w = u8tor_uc(&ch, sv->p))
{
U8MOV(sv, w);
if (!sv->len)
return false;
}
return true;
}
|