-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.c
293 lines (262 loc) · 7.32 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <inttypes.h>
#include "lexer.h"
#include "display.h"
#ifdef DEBUG
static const char* tokenStrings[] = {
#define ET(x) #x,
#include "tokens.h"
#undef ET
};
#endif
void lexer_print_token(Token t, uint8_t printType){
for(uint64_t i = 0;i < t.length;i++)
pmgn("%c", t.string[i]);
#ifdef DEBUG
if(printType)
pblue("(%s) \t", tokenStrings[t.type]);
#endif
}
/* The code below is for testing and debugging purposes.
* =====================================================
*/
#ifdef DEBUG
void lexer_print_tokens(TokenList list){
size_t prevLine = 0;
for(uint64_t i = 0;i < list.count;i++){
if(prevLine != list.tokens[i].line){
pmgn(ANSI_FONT_BOLD "\n<line %zd>", list.tokens[i].line);
prevLine = list.tokens[i].line;
}
lexer_print_token(list.tokens[i], 1);
}
}
#endif // DEBUG
static Token keywords[] = {
#define ET(x) TOKEN_##x
#define KEYWORD(name, length) {#name, NULL, length, 0, 0, ET(name)},
#include "keywords.h"
#undef KEYWORD
#undef ET
};
static char* source = NULL;
static size_t present = 0, length = 0, start = 0, line = 1;
static Token lastToken;
static void addToken(TokenList *list, Token token){
list->tokens = (Token *)realloc(list->tokens, sizeof(Token) * (++list->count));
token.source = list->source;
list->tokens[list->count - 1] = token;
}
#define makeToken(x) makeToken2(TOKEN_##x)
static Token makeToken2(TokenType type){
Token t;
t.source = NULL;
t.type = type;
t.line = line;
t.string = &source[start];
t.start = start;
t.length = present - start+1;
lastToken = t;
//#ifdef DEBUG
// dbg("TokenType : %s", tokenStrings[type]);
// dbg("Line : %zd", line);
// dbg("Start : %zd", start);
// dbg("End : %zd\n", present);
//#endif
return t;
}
// rtype 1 --> error
// rtype 2 --> warning
// rtype 0 --> info
static void print_source(const char *s, size_t line, size_t hfrom, size_t hto, uint8_t rtype){
if(rtype == 1)
pred( ANSI_FONT_BOLD "\n<line %zd>\t" ANSI_COLOR_RESET, line);
else if(rtype == 2)
pylw( ANSI_FONT_BOLD "\n<line %zd>\t" ANSI_COLOR_RESET, line);
else
pblue( ANSI_FONT_BOLD "\n<line %zd>\t" ANSI_COLOR_RESET, line);
if(s == NULL){
pmgn("<source not available>");
return;
}
size_t l = 1, p = 0;
while(l < line){
if(s[p] == '\n')
l++;
p++;
}
for(size_t i = p;s[i] != '\n' && s[i] != '\0';i++)
printf("%c", s[i]);
printf("\n \t");
for(size_t i = p;i < hto;i++){
if(i >= hfrom && i <= hto)
pmgn(ANSI_FONT_BOLD "~" ANSI_COLOR_RESET);
else
printf(" ");
}
printf("\n");
}
void token_print_source(Token t, uint8_t rtype){
print_source(t.source, t.line, t.start, t.start + t.length, rtype);
}
static Token makeKeyword(){
while(isalnum(source[present]))
present++;
char word[present - start + 1];
for(uint64_t i = start;i < present;i++){
word[i - start] = source[i];
};
word[present - start] = '\0';
for(uint64_t i = 0;i < sizeof(keywords)/sizeof(Token);i++){
if(keywords[i].length == (present - start)){
if(strcmp(keywords[i].string, word) == 0){
present--;
return makeToken2(keywords[i].type);
}
}
}
present--;
return makeToken(identifier);
}
static Token makeNumber(){
uint8_t decimal = 0;
uint64_t bak;
while(isdigit(source[present]) || source[present] == '.'){
if(source[present] == '.'){
if(decimal == 0)
bak = present;
decimal++;
}
present++;
}
present--;
if(((decimal == 1) && bak == present) || decimal > 1){
return makeToken(unknown);
}
if(decimal == 1)
return makeToken(number);
return makeToken(integer);
}
static Token nextToken(uint8_t atStart){
if(!atStart)
present++;
start = present;
if(present == length)
return makeToken(eof);
if(isalpha(source[present])){
return makeKeyword();
}
else if(isdigit(source[present])){
return makeNumber();
}
switch(source[present]){
case ' ':
case '\t':
case '\r':
return nextToken(0);
case '\n':
line++;
return nextToken(0);
case ',':
return makeToken(comma);
case '.':
return makeToken(dot);
case ':':
return makeToken(colon);
case '+':
return makeToken(plus);
case '-':
return makeToken(minus);
case '/':
return makeToken(backslash);
case '*':
return makeToken(star);
case '^':
return makeToken(cap);
case '{':
return makeToken(brace_open);
case '}':
return makeToken(brace_close);
case '(':
return makeToken(paranthesis_open);
case ')':
return makeToken(paranthesis_close);
case '>':
if(source[present + 1] == '='){
present++;
return makeToken(greater_equal);
}
return makeToken(greater);
case '<':
if(source[present + 1] == '='){
present++;
return makeToken(lesser_equal);
}
return makeToken(lesser);
case '=':
if(source[present + 1] == '='){
present++;
return makeToken(equal_equal);
}
return makeToken(equal);
case '!':
if(source[present + 1] == '='){
present++;
return makeToken(not_equal);
}
return makeToken(not);
case '"':
{
present++;
start = present;
while(source[present] != '"' && source[present] != '\0'){
if(source[present] == '\\' && source[present+1] == '"')
present++;
else if(source[present] == '\n')
line++;
present++;
}
present--;
Token t = makeToken(string);
present++;
//if(source[present] == '"')
// present++;
return t;
}
}
return makeToken(unknown);
}
static uint64_t hasErrors = 0;
TokenList tokens_scan(const char* line){
source = strdup(line);
length = strlen(source);
present = 0;
start = 0;
TokenList list = {source, NULL, 0, 0};
while(present < length){
Token t = nextToken(present == 0);
list.hasError += t.type == TOKEN_unknown;
addToken(&list, t);
if(t.type == TOKEN_unknown){
err("Unexpected token '");
lexer_print_token(t, 0);
printf("'");
token_print_source(list.tokens[list.count - 1], 1);
hasErrors++;
}
}
if(list.tokens[list.count - 1].type != TOKEN_eof)
addToken(&list, makeToken(eof));
return list;
}
void tokens_free(TokenList list){
free(list.tokens);
free(list.source);
}
uint64_t lexer_has_errors(){
return hasErrors;
}