-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtokenizer.c
227 lines (224 loc) · 8 KB
/
tokenizer.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
#include "compilium.h"
static struct Node *CreateNextToken(const char *p, const char *src, int *line) {
assert(line);
if (!*p) return NULL;
if (*p == ' ') {
return AllocToken(src, *line, p, 1, kTokenDelimiter);
}
if (*p == '\n') {
(*line)++;
return AllocToken(src, *line, p, 1, kTokenDelimiter);
}
if (p[0] == '\\' && p[1] == '\n') {
(*line)++;
return AllocToken(src, *line, p, 2, kTokenZeroWidthNoBreakSpace);
}
if ('1' <= *p && *p <= '9') {
int length = 0;
while ('0' <= p[length] && p[length] <= '9') {
length++;
}
return AllocToken(src, *line, p, length, kTokenIntegerConstant);
} else if ('0' == *p) {
int length = 0;
if (p[1] == 'x') {
// Hexadecimal
length += 2;
while (('0' <= p[length] && p[length] <= '9') ||
('A' <= p[length] && p[length] <= 'F') ||
('a' <= p[length] && p[length] <= 'f')) {
length++;
}
} else {
// Octal
while ('0' <= p[length] && p[length] <= '7') {
length++;
}
}
return AllocToken(src, *line, p, length, kTokenIntegerConstant);
} else if (('A' <= *p && *p <= 'Z') || ('a' <= *p && *p <= 'z') ||
*p == '_') {
int length = 0;
while (('A' <= p[length] && p[length] <= 'Z') ||
('a' <= p[length] && p[length] <= 'z') || p[length] == '_' ||
('0' <= p[length] && p[length] <= '9')) {
length++;
}
struct Node *t = AllocToken(src, *line, p, length, kTokenIdent);
if (IsEqualTokenWithCStr(t, "break")) t->token_type = kTokenKwBreak;
if (IsEqualTokenWithCStr(t, "char")) t->token_type = kTokenKwChar;
if (IsEqualTokenWithCStr(t, "const")) t->token_type = kTokenKwConst;
if (IsEqualTokenWithCStr(t, "continue")) t->token_type = kTokenKwContinue;
if (IsEqualTokenWithCStr(t, "else")) t->token_type = kTokenKwElse;
if (IsEqualTokenWithCStr(t, "extern")) t->token_type = kTokenKwExtern;
if (IsEqualTokenWithCStr(t, "for")) t->token_type = kTokenKwFor;
if (IsEqualTokenWithCStr(t, "if")) t->token_type = kTokenKwIf;
if (IsEqualTokenWithCStr(t, "int")) t->token_type = kTokenKwInt;
if (IsEqualTokenWithCStr(t, "long")) t->token_type = kTokenKwLong;
if (IsEqualTokenWithCStr(t, "return")) t->token_type = kTokenKwReturn;
if (IsEqualTokenWithCStr(t, "sizeof")) t->token_type = kTokenKwSizeof;
if (IsEqualTokenWithCStr(t, "static")) t->token_type = kTokenKwStatic;
if (IsEqualTokenWithCStr(t, "struct")) t->token_type = kTokenKwStruct;
if (IsEqualTokenWithCStr(t, "typedef")) t->token_type = kTokenKwTypedef;
if (IsEqualTokenWithCStr(t, "unsigned")) t->token_type = kTokenKwUnsigned;
if (IsEqualTokenWithCStr(t, "void")) t->token_type = kTokenKwVoid;
if (IsEqualTokenWithCStr(t, "while")) t->token_type = kTokenKwWhile;
return t;
} else if ('\'' == *p) {
int length = 1;
while (p[length] && p[length] != '\'') {
if (p[length] == '\\' && p[length + 1]) {
length++;
}
length++;
}
if (p[length] != '\'') {
Error("Expected end of char literal (')");
}
length++;
return AllocToken(src, *line, p, length, kTokenCharLiteral);
} else if ('"' == *p) {
int length = 1;
while (p[length] && p[length] != '"') {
if (p[length] == '\\' && p[length + 1]) {
length++;
}
length++;
}
if (p[length] != '"') {
Error("Expected end of string literal (\")");
}
length++;
return AllocToken(src, *line, p, length, kTokenStringLiteral);
} else if ('#' == *p) {
if (p[1] == '#') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('&' == *p) {
if (p[1] == '&') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('|' == *p) {
if (p[1] == '|') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('<' == *p) {
if (p[1] == '<') {
if (p[2] == '=') {
return AllocToken(src, *line, p, 3, kTokenPunctuator);
}
return AllocToken(src, *line, p, 2, kTokenPunctuator);
} else if (p[1] == '=') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('>' == *p) {
if (p[1] == '>') {
if (p[2] == '=') {
return AllocToken(src, *line, p, 3, kTokenPunctuator);
}
return AllocToken(src, *line, p, 2, kTokenPunctuator);
} else if (p[1] == '=') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('=' == *p) {
if (p[1] == '=') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('!' == *p) {
if (p[1] == '=') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('^' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('+' == *p) {
if (p[1] == '+') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
if (p[1] == '=') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('-' == *p) {
if (p[1] == '-' || p[1] == '=' || p[1] == '>') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('*' == *p) {
if (p[1] == '/') {
return AllocToken(src, *line, p, 2, kTokenBlockCommentEnd);
}
if (p[1] == '=') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('/' == *p) {
if (p[1] == '/') {
return AllocToken(src, *line, p, 2, kTokenLineComment);
}
if (p[1] == '*') {
return AllocToken(src, *line, p, 2, kTokenBlockCommentBegin);
}
if (p[1] == '=') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('%' == *p) {
if (p[1] == '=') {
return AllocToken(src, *line, p, 2, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('~' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('?' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if (':' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if (',' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if (';' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('{' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('}' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('(' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if (')' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('.' == *p) {
if (p[1] == '.' && p[2] == '.') {
return AllocToken(src, *line, p, 3, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if ('[' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
} else if (']' == *p) {
return AllocToken(src, *line, p, 1, kTokenPunctuator);
}
return AllocToken(src, *line, p, 1, kTokenUnknownChar);
}
struct Node *CreateToken(const char *input) {
int line = 1;
return CreateNextToken(input, input, &line);
}
struct Node *Tokenize(const char *input) {
// returns head of tokens.
struct Node *token_head = NULL;
struct Node **last_next_token = &token_head;
const char *p = input;
struct Node *t;
int line = 1;
while ((t = CreateNextToken(p, input, &line))) {
*last_next_token = t;
last_next_token = &t->next_token;
p = t->begin + t->length;
}
return token_head;
}