forked from nick-jn/uni-project-20465
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.c
executable file
·233 lines (199 loc) · 6.85 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
/*The lexer module.*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "bool.h"
#include "token.h"
#include "clist.h"
#include "filedata.h"
#include "lexer.h"
extern unsigned int ERRORS;
static token *get_next_token(token *prev_token, file_data *filedat);
static char *skip_wspace(char *str);
static void print_errlex(int index, file_data *filedat, char *message);
/*Driver for the lexer module. Tokenizes the entire line. Upon lexer
failure (i.e., get_next_token return NULL), the function returns NULL.
Otherwise, filedat->last_token is populated with the acquired tokens.*/
void tokenize_line(file_data *filedat) {
token *tok = NULL;
while (!0) {
tok = get_next_token(tok, filedat);
if (tok == NULL) { /*lexer error*/
destroy_clist(&filedat->last_token, &destroy_clist_token);
return;
}
add_clist(&filedat->last_token, tok);
if (tok->toktype == toktype_EOL) {
break;
}
}
}
/*Token extractor. Returns NULL in case of severe lexing error (see
the code). Upon reaching end of line, a special token with the type
toktype_EOL is returned.*/
static token *get_next_token(token *prev_token, file_data *filedat) {
int i, starting_index; /*the starting index of the new token*/
int length = 0; /*the length of the new token*/
char *input = filedat->current_line;
bool got_string = false; /*flag for when we get a string literal*/
token *next_token = NULL;
/*get the starting index for the new token*/
if (prev_token != NULL) {
i = prev_token->starting_index + prev_token->length;
} else {
i = 0; /*start of the line*/
}
/*skip initial whitespace*/
while (input[i] == ' ' || input[i] == '\t') {
i++; /*we need the i here, so no skip_wspace*/
}
starting_index = i;
/*essentially, it's a state machine*/
while (input[i] != '\n' && input[i] != '\0') {
/*dot*/
if (input[i] == '.') {
if (length > 0) {
break; /*write token*/
}
length++;
if (input[i+1] == ' ' || input[i+1] == '\t') {
print_errlex(i, filedat, "Error, whitespace after '.'.");
}
break; /*write token*/
/*whitespace*/
} else if (input[i] == ' ' || input[i] == '\t') {
/*look ahead for dot or colon*/
if (*skip_wspace(&input[i]) == '.') {
print_errlex(i, filedat,
"Error, '.' is preceded by whitespace.");
}
if (*skip_wspace(&input[i]) == ':') {
print_errlex(i, filedat,
"Error, ':' is preceded by whitespace.");
}
break; /*write token*/
/*hash, colon*/
} else if (input[i] == '#' || input[i] == ':') {
if (length > 0) {
break; /*write token*/
}
length++;
if (input[i] != ':' &&
(input[i+1] == ' ' || input[i+1] == '\t')) {
print_errlex(i, filedat,
"Error, erroneous whitespace.");
}
break; /*write token*/
/*comma*/
} else if (input[i] == ',') {
if (length > 0) {
break; /*write token*/
}
length++;
break;
/*string literal*/
} else if (input[i] == '"') {
if (length > 0) {
print_errlex(i, filedat,
"Error, string literal has to be "
"delimited by whitespace.");
return NULL;
}
i++; length++;
while (input[i] != '"') {
if (input[i] == '\n' || input[i] == '\0') {
print_errlex(i, filedat,
"Error, missing terminating '\"' character.");
return NULL;
}
i++; length++;
}
length++;
got_string = true;
break; /*write token*/
/*plus and minus signs*/
} else if (input[i] == '+' || input[i] == '-') {
if (length > 0) {
print_errlex(i, filedat,
"Error, number literal has to be "
"delimited by whitespace.");
return NULL;
}
if (input[i+1] == ' ' || input[i+1] == '\t') {
print_errlex(i, filedat,
"Error, erroneous whitespace "
"between sign and number.");
return NULL;
} else if (!isdigit(input[i+1])) {
print_errlex(i, filedat,
"Error, invalid number literal.");
return NULL;
}
/*semicolon*/
} else if (input[i] == ';') {
print_errlex(i, filedat,
"Error, ';' must be placed at the end of the line.");
return NULL;
}
length++; i++;
}
/*signifies end of line*/
if (length == 0) {
next_token = create_token(starting_index+1, 0, "\\n");
next_token->toktype = toktype_EOL;
return next_token;
}
/*create token*/
next_token = create_token(starting_index, length, input);
if (got_string) {
next_token->toktype = toktype_string;
} else {
next_token->toktype = get_toktype(next_token);
}
#ifdef DEBUG_LEXER
printf("TOKEN: %s\t%s\n",
new_tokstr, toktype_strings[next_token->toktype]);
#endif /*DEBUG*/
return next_token;
}
/*Skips the whitespace chars in str and returns the pointer to the
first non-whitespace char.*/
static char *skip_wspace(char *str) {
while (*str == ' ' || *str == '\t') {
str++;
}
return str;
}
/*Error printing for lexer.*/
static void print_errlex(int index, file_data *filedat, char *message) {
int i = 0;
char *line = filedat->current_line;
filedat->error = true;
ERRORS++;
while (*line == ' ' || *line == '\t') {
line++; i++;
}
fprintf(stderr, "\nLexer error.\nLine %d: %s\n",
filedat->linenum, message);
while (*line != '\n' && *line != '\0') {
if (*line == '\t') {
fprintf(stderr, " ");
} else {
fprintf(stderr, "%c", *line);
}
line++;
}
fprintf(stderr, "\n");
/*fancy line*/
for (; i < index; i++) {
fprintf(stderr, " ");
}
fprintf(stderr, "^");
i++;
while (filedat->current_line[i] == ' ' ||
filedat->current_line[i] == '\t') {
fprintf(stderr, "~");
i++;
}
fprintf(stderr, "\n");
}