-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.c
272 lines (230 loc) · 7.14 KB
/
lexer.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
#include "lexer.h"
#define POS_ADD(n) lexer->position += n
#define COL_ADD(n) lexer->column += n
#define LINE_ADD(n) lexer->line += n; RESET_COL
#define RESET_COL lexer->column = 0
#define IS_EOF (lexer->offset >= lexer->length)
#define OFFSET_ADD(n) lexer->offset += n;
#define ADD_OFFSET_POSITION(n) OFFSET_ADD(n); POS_ADD(n)
#define PEEK(n) ((lexer->offset < lexer->length + 1) ? lexer->buffer[lexer->offset+n] : 0)
#define NEXT(n) lexer->buffer[lexer->offset+=n]; POS_ADD(n); COL_ADD(n);
// TKN macros
#define TKN_RESET lexer->token = NO_TOKEN; lexer->token.position = lexer->position; \
lexer->token.line = lexer->line; lexer->token.column = lexer->column
#define SET_TKNESCAPED(value) lexer->token.escaped = value
#define SET_TKNTYPE(t) lexer->token.type = t
static inline bool is_newline(int c){
// many more cases
return (c == '\n');
}
int token_int(struct lexer* lexer, struct token token){
// get as string first --> probably a much better way
char* value = malloc((token.end - token.start + 1)*(sizeof(char)));
strncpy(value, lexer->buffer + token.start, token.end - token.start);
value[token.end - token.start] = '\0';
int x = atoi(value); // guaranteed to be int
free(value);
return x;
}
char* token_string(struct lexer* lexer, struct token token){
char* value = malloc((token.end - token.start + 1)*(sizeof(char)));
strncpy(value, lexer->buffer + token.start, token.end - token.start);
value[token.end - token.start] = '\0';
return value;
}
static inline bool is_identifier (int c) {
return ((isalpha(c)) || (isdigit(c)) || (c == '_'));
}
static inline bool is_alpha (int c) {
if (c == '_') return true;
return isalpha(c);
}
static inline bool is_string (int c) {
return ((c == '"') || (c == '\''));
}
uint32_t utf8_charbytes (const char *s, uint32_t i) {
unsigned char c = s[i];
// req. bytes (RFC 3629)
if ((c > 0) && (c <= 127)) return 1;
if ((c >= 194) && (c <= 223)) return 2;
if ((c >= 224) && (c <= 239)) return 3;
if ((c >= 240) && (c <= 244)) return 4;
// means error
return 0;
}
int lexer_error(struct lexer* lexer, char* error){
return -1;
}
struct lexer* lexer_create(const char* file_name){
struct lexer* lexer = malloc(sizeof(struct lexer));
FILE* f = fopen(file_name, "r");
lexer->buffer = src;
lexer->offset = 0;
lexer->line = 0;
lexer->column = 0;
lexer->position = 0;
lexer->length = len;
return lexer;
}
void lexer_free(struct lexer* lexer){
free(lexer);
}
static inline int next_utf8(struct lexer* lexer) {
printf("next utf8\n");
int c = NEXT(1);
lexer->token.end++;
uint32_t len = utf8_charbytes((const char *)&c, 0);
if (len == 1) {
return c;
}
switch(len) {
case 0: lexer_error(lexer, "Unknown character inside a string literal"); return 0;
case 2: OFFSET_ADD(1) COL_ADD(1); break;
case 3: OFFSET_ADD(2); COL_ADD(1); break;
case 4: OFFSET_ADD(3); COL_ADD(1); POS_ADD(1); break;
}
return c;
}
struct token lexer_scan_string(struct lexer* lexer) {
printf("Scanning string\n");
int c, c2;
printf("set token start to offset %d\n", lexer->offset);
lexer->token.start = lexer->offset;
lexer->token.end = lexer->offset;
// no memory allocation here
c = NEXT(1); // save escaped character
TKN_RESET; // save offset
SET_TKNESCAPED(false); // set escaped flag to false
while ((c2 = (unsigned char)PEEK(0)) != c) {
if (IS_EOF) {lexer_error(lexer, "Unexpected EOF inside a string literal");}
if (is_newline(c2)) LINE_ADD(1);
// handle escaped characters
if (c2 == '\\') {
SET_TKNESCAPED(true);
lexer->offset+=2;
lexer->token.end+=2;
continue;
}
// scan next
next_utf8(lexer);
}
// skip last escape character
ADD_OFFSET_POSITION(1);
printf("string with start %d and end %d\n", lexer->token.start, lexer->token.end);
// string is from buffer->[nseek] and it is nlen length
lexer->token.type = TKN_STRING;
printf("Found string: %.*s\n", lexer->token.end - lexer->token.start,
lexer->buffer + lexer->token.start);
return lexer->token;
}
int is_operator(int c){
return ((c == '=') || (c == ',') || (c == '{') || (c == '}') ||
(c == '[') || (c == ']') || (c == '(') ||
(c == ')') || (c == '!') || (c == ':') || (c == '|'));
}
// straight from Gravity
struct token lexer_scan_number(struct lexer *lexer) {
printf("Scanning number\n");
int c;
TKN_RESET;
loop:
c = PEEK(0);
// explicitly list all accepted cases
if (IS_EOF) goto report_token;
if (isdigit(c)) goto accept_char;
if (is_whitespace(c)) goto report_token;
if (is_newline(c)) goto report_token;
if (is_operator(c)) {
goto report_token;
}
// any other case is an error
goto report_error;
accept_char:
lexer->token.end++;
ADD_OFFSET_POSITION(1);
goto loop;
report_token:
// number is from buffer->[nseek] and it is bytes length
lexer->token.type = TKN_NUMBER;
return lexer->token;
//return TKN_NUMBER;
report_error:
lexer_error(lexer, "Malformed number expression.");
}
struct token lexer_scan_operator(struct lexer* lexer){
printf("Scanning operator\n");
int c = lexer_peek(lexer, 1);
// current only handles one character oprators
printf("Found operator: %c\n", c);
switch (c) {
case '=':
lexer->token.type = TKN_ASSIGN; break;
case ',':
lexer->token.type = TKN_COMMA;break;
case '{':
lexer->token.type = TKN_OPEN_BRACE;break;
case '}':
lexer->token.type = TKN_CLOSE_BRACE;break;
case '[':
lexer->token.type = TKN_OPEN_SQUARE;break;
case ']':
lexer->token.type = TKN_CLOSE_SQUARE; break;
case '(':
lexer->token.type = TKN_OPEN_BRACKET; break;
case ')':
lexer->token.type = TKN_CLOSE_BRACKET; break;
case '!':
lexer->token.type = TKN_EXCLAMATION; break;
case ':':
lexer->token.type = TKN_COLON; break;
case '|':
lexer->token.type = TKN_DIVIDER; break;
default:
lexer->token.type = TKN_ERROR;
}
return lexer->token;
}
struct token lexer_scan_identifier(struct lexer* lexer) {
printf("scanning identifier\n");
TKN_RESET;
lexer->token.start = lexer->offset;
lexer->token.end = lexer->offset;
while (is_identifier(PEEK(0))) {
OFFSET_ADD(1);
COL_ADD(1);
lexer->token.end++;
}
SET_TKNTYPE(TKN_IDENTIFIER);
printf("Found identifier: %.*s\n", lexer->token.end - lexer->token.start,
lexer->buffer + lexer->token.start);
return lexer->token;
}
struct token lexer_next(struct lexer* lexer){
int c;
struct token token;
while (1) {
if (IS_EOF) {
token.type = TKN_EOF;
return token;
}
c = PEEK(0);
if (is_whitespace(c)){
ADD_OFFSET_POSITION(1);
} else if (is_newline(c)){
ADD_OFFSET_POSITION(1);
} else if (is_operator(c)){
lexer_scan_operator(lexer);
} else if (is_alpha(c)) {
return lexer_scan_identifier(lexer);
} else if (is_digit(c)) {
return lexer_scan_number(lexer);
} else if (is_string(c)){
return lexer_scan_string(lexer);
} else {
lexer_error(lexer, "Unrecognised token.");
}
}
return token;
}
enum token_type lexer_peek(struct lexer* lexer, int ahead){
}