-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize.c
347 lines (292 loc) · 8.19 KB
/
tokenize.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <float.h>
#include "tokenize.h"
#include "util.h"
// Internal functions
static void append_token(dynarray2_t* token_list, token_type_t token_type, size_t pos_start);
static void read_string(char** str_ptr, char** read_str_ptr);
static void read_number(char** str_ptr, void** read_number_ptr, token_type_t* type_ptr);
static int* read_bool(char** str_ptr);
static int read_null(char** str_ptr);
// TODO:
// - \C escaped symbols
// - 1e25 number notation
// - should the tokenizer know about delimiters (",}]") after values? (falsenull or "string""string" and other things)
// - testcase: fill memory with garbage and check what tokenize returns
dynarray2_t* tokenize(const char* str)
{
dynarray2_t* token_list = dynarray2_create(sizeof(token_t));
token_t* current_token = NULL;
char* cur = (char*) str;
ptrdiff_t tmp_pos;
char err_buf[1024] = {0};
while (*cur) {
int* bool_value_ptr;
int null_result;
switch (*cur) {
case '{':
append_token(token_list, TokenCurly, cur - str);
break;
case '}':
append_token(token_list, TokenUncurly, cur - str);
break;
case '[':
append_token(token_list, TokenSquare, cur - str);
break;
case ']':
append_token(token_list, TokenUnsquare, cur - str);
break;
case ':':
append_token(token_list, TokenColon, cur - str);
break;
case ',':
append_token(token_list, TokenComma, cur - str);
break;
case '\"':
append_token(token_list, TokenString, cur - str);
current_token = dynarray2_get_top(token_list);
read_string(&cur, (char**) ¤t_token->value_ptr);
current_token->_pos_end = cur - str;
current_token = NULL;
break;
case '.':
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
append_token(token_list, TokenLong, cur - str);
current_token = dynarray2_get_top(token_list);
read_number(&cur, ¤t_token->value_ptr, ¤t_token->type);
current_token->_pos_end = cur - str;
current_token = NULL;
break;
// TODO fix falsenull and other insanities
case 't':
case 'f':
tmp_pos = cur - str;
bool_value_ptr = read_bool(&cur);
if (NULL == bool_value_ptr) {
die("tokenize: expected true or false literal after \"%c\" in input", *cur);
}
append_token(token_list, TokenBool, tmp_pos);
current_token = dynarray2_get_top(token_list);
current_token->value_ptr = bool_value_ptr;
current_token->_pos_end = cur - str;
current_token = NULL;
break;
case 'n':
tmp_pos = (ptrdiff_t) cur;
null_result = read_null(&cur);
if (-1 == null_result) {
die("tokenize: expected null literal after \"%c\" in input in position %d", *cur, cur - str);
}
append_token(token_list, TokenNull, tmp_pos);
current_token = dynarray2_get_top(token_list);
current_token->_pos_end = cur - str;
break;
case ' ':
case '\n':
case '\r':
case '\t':
break;
default:
strncpy(err_buf, (str + (cur - str - 20)), 41);
die("tokenize: unexpected char \"%c\" in input in position %d\n%s", *cur, cur - str, err_buf);
}
cur++;
}
return token_list;
}
// Internal functions
static void append_token(dynarray2_t* token_list, token_type_t token_type, size_t pos_start)
{
token_t value = {
.type = token_type,
.value_ptr = NULL,
._pos_start = pos_start
};
dynarray2_append(token_list, &value);
}
static const char ESCAPE_SYMBOLS[] = {'"', '\\', '/', 'b', 'f', 'n', 'r', 't', 'u'};
// TODO use isxdigit
// static const char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'};
// TODO add \uXXXX hex validation
static inline bool _is_valid_escape_char(char c)
{
for (size_t i = 0; i < sizeof(ESCAPE_SYMBOLS); i++) {
if (c == ESCAPE_SYMBOLS[i]) {
return true;
}
}
return false;
}
// Returned null terminated
static void read_string(char** str_ptr, char** read_str_ptr)
{
// strings start with \"
char* start = *str_ptr + 1;
char* end = NULL;
bool is_escape = false;
while (*(*str_ptr)++) {
if (is_escape) {
if (_is_valid_escape_char(**str_ptr)) {
is_escape = false;
continue;
}
die("tokenize/read_string: unexpected escape sequence \\%c", **str_ptr);
}
if (**str_ptr == '\\') {
is_escape = true;
continue;
}
if (**str_ptr == '\"') {
end = *str_ptr;
*read_str_ptr = malloc(((end - start) + 1) * sizeof(char));
**read_str_ptr = '\0';
strncat(*read_str_ptr, start, end - start);
return;
}
}
die("tokenize/read_string: found an unterminated string");
}
static void read_number(char** str_ptr, void** read_number_ptr, token_type_t* type_ptr)
{
char* start = *str_ptr;
char* end = NULL;
bool seen_minus = 0;
bool lead_digit = 0;
bool seen_period = 0;
do {
switch (**str_ptr) {
// TODO:
// - scientific notation
// - big ints
case '-':
if (*str_ptr != start) {
die("read_number: found extra \"-\" in a number literal");
}
seen_minus = true;
break;
case '.':
if (lead_digit == false) {
die("read_number: expected a digit in front of a \".\"");
}
if (seen_period == true) {
die("read_number: found extra \".\" in a number literal");
}
seen_period = true;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (start == *str_ptr || (seen_minus == 1 && start + 1 == *str_ptr)) {
lead_digit = true;
}
break;
default:
end = *str_ptr;
// wind back one char
*str_ptr -= 1;
*read_number_ptr = malloc(seen_period ? sizeof(double) : sizeof(long));
if (seen_period) {
*(double*)*read_number_ptr = strtod(start, NULL);
*type_ptr = TokenDouble;
} else {
*(long*)*read_number_ptr = strtol(start, NULL, 10);
*type_ptr = TokenLong;
}
return;
}
} while (*(*str_ptr)++);
}
// Returns
// NULL if not a bool
// a pointer to allocated int 0/1 otherwise
static int* read_bool(char **str_ptr)
{
if (0 == strncmp("true", *str_ptr, 4)) {
*str_ptr += 3;
int* value = malloc(sizeof(int));
*value = 1;
return value;
}
if (0 == strncmp("false", *str_ptr, 5)) {
*str_ptr += 4;
int* value = malloc(sizeof(int));
*value = 0;
return value;
}
return NULL;
}
// Returns
// 0 if null
// -1 otherwise
static int read_null(char **str_ptr)
{
if (0 == strncmp("null", *str_ptr, 4)) {
// TODO: will this potentially cause a buffer overflow?
*str_ptr += 3;
return 0;
}
return -1;
}
void DEBUG_print_token(token_t* token)
{
const int PRINT_BUFFER_SIZE = 100;
char buffer[PRINT_BUFFER_SIZE];
switch (token->type)
{
case TokenCurly:
printf("<CURLY> ");
break;
case TokenUncurly:
printf("<UNCURLY> ");
break;
case TokenSquare:
printf("<SQUARE> ");
break;
case TokenUnsquare:
printf("<UNSQUARE> ");
break;
case TokenColon:
printf("<COLON> ");
break;
case TokenComma:
printf("<COMMA> ");
break;
case TokenString:
strncpy(buffer, (char*) token->value_ptr, PRINT_BUFFER_SIZE - 1);
printf("<STRING \"%s%s\"> ", buffer, strlen((char*) token->value_ptr) > PRINT_BUFFER_SIZE - 1 ? "..." : "");
break;
case TokenDouble:
printf("<DOUBLE %g> ", *((double*) token->value_ptr));
break;
case TokenLong:
printf("<LONG %d> ", *((int*) token->value_ptr));
break;
case TokenBool:
printf("<BOOL %d> ", *((int*) token->value_ptr));
break;
case TokenNull:
printf("<NULL> ");
break;
}
}