-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtoken.c
256 lines (225 loc) · 6.67 KB
/
token.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
#include "compilium.h"
bool IsToken(struct Node *n) { return n && n->type == kNodeToken; }
bool IsTokenWithType(struct Node *n, enum TokenType token_type) {
return IsToken(n) && n->token_type == token_type;
}
struct Node *AllocToken(const char *src_str, int line, const char *begin,
int length, enum TokenType type) {
struct Node *t = AllocNode(kNodeToken);
t->begin = begin;
t->length = length;
t->token_type = type;
t->src_str = src_str;
t->line = line;
return t;
}
struct Node *DuplicateToken(struct Node *base_token) {
assert(IsToken(base_token));
struct Node *t = AllocNode(kNodeToken);
memcpy(t, base_token, sizeof(*t));
t->next_token = NULL;
return t;
}
struct Node *DuplicateTokenSequence(struct Node *base_head) {
struct Node *dup_head = NULL;
struct Node **dup_head_holder = &dup_head;
while (base_head) {
*dup_head_holder = DuplicateToken(base_head);
dup_head_holder = &(*dup_head_holder)->next_token;
base_head = base_head->next_token;
}
return dup_head;
}
char *CreateTokenStr(struct Node *t) {
assert(IsToken(t));
return strndup(t->begin, t->length);
}
int IsEqualTokenWithCStr(struct Node *t, const char *s) {
return IsToken(t) && strlen(s) == (unsigned)t->length &&
strncmp(t->begin, s, t->length) == 0;
}
void PrintTokenSequence(struct Node *t) {
if (!t) return;
assert(IsToken(t));
for (; t; t = t->next_token) {
if (t->token_type == kTokenZeroWidthNoBreakSpace) {
continue;
}
fprintf(stderr, "%.*s", t->length, t->begin);
}
}
void OutputTokenSequenceAsCSource(struct Node *t) {
if (!t) return;
assert(IsToken(t));
for (; t; t = t->next_token) {
if (t->token_type == kTokenZeroWidthNoBreakSpace) {
continue;
}
fprintf(stdout, "%.*s", t->length, t->begin);
}
}
void PrintToken(struct Node *t) {
fprintf(stderr, "(Token %.*s type=%d)", t->length, t->begin, t->token_type);
}
void PrintTokenBrief(struct Node *t) {
assert(t);
if (t->token_type == kTokenStringLiteral ||
t->token_type == kTokenCharLiteral) {
fprintf(stderr, "%.*s", t->length, t->begin);
return;
}
fprintf(stderr, "<%.*s>", t->length, t->begin);
}
void PrintTokenStrToFile(struct Node *t, FILE *fp) {
fprintf(fp, "%.*s", t->length, t->begin);
}
static bool ShouldRemoveToken(struct Node *t) {
return t->token_type == kTokenDelimiter ||
t->token_type == kTokenZeroWidthNoBreakSpace;
}
// Token stream
static struct Node **next_token_holder;
void InitTokenStream(struct Node **head_token_holder) {
assert(head_token_holder);
next_token_holder = head_token_holder;
}
static void AdvanceTokenStream(void) {
if (!*next_token_holder) return;
next_token_holder = &(*next_token_holder)->next_token;
}
struct Node *PeekToken(void) {
assert(next_token_holder);
return *next_token_holder;
}
struct Node *ReadToken(enum TokenType type) {
struct Node *t = *next_token_holder;
if (!t || !IsTokenWithType(t, type)) return NULL;
return t;
}
struct Node *ConsumeToken(enum TokenType type) {
struct Node *t = *next_token_holder;
if (!t || !IsTokenWithType(t, type)) return NULL;
AdvanceTokenStream();
return t;
}
struct Node *ConsumeTokenStr(const char *s) {
struct Node *t = *next_token_holder;
if (!t || !IsEqualTokenWithCStr(t, s)) return NULL;
AdvanceTokenStream();
return t;
}
struct Node *ExpectTokenStr(const char *s) {
struct Node *t = *next_token_holder;
if (!t) Error("Expect token %s but got EOF", s);
if (!ConsumeTokenStr(s)) ErrorWithToken(t, "Expected token %s here", s);
return t;
}
struct Node *ConsumePunctuator(const char *s) {
struct Node *t = *next_token_holder;
if (!t || !IsTokenWithType(t, kTokenPunctuator) ||
!IsEqualTokenWithCStr(t, s))
return NULL;
AdvanceTokenStream();
return t;
}
struct Node *ExpectPunctuator(const char *s) {
struct Node *t = *next_token_holder;
if (!t) Error("Expect token %s but got EOF", s);
if (!ConsumePunctuator(s)) ErrorWithToken(t, "Expected token %s here", s);
return t;
}
struct Node *NextToken(void) {
struct Node *t = *next_token_holder;
AdvanceTokenStream();
return t;
}
void RemoveCurrentToken(void) {
if (!*next_token_holder) return;
*next_token_holder = (*next_token_holder)->next_token;
}
void RemoveTokensTo(struct Node *end) {
while (*next_token_holder && *next_token_holder != end) {
RemoveCurrentToken();
}
}
void InsertTokens(struct Node *seq_first) {
// Insert token sequece (seq) at current cursor pos.
if (!IsToken(seq_first)) return;
struct Node *seq_last = seq_first;
while (seq_last->next_token) seq_last = seq_last->next_token;
seq_last->next_token = PeekToken();
*next_token_holder = seq_first;
}
static struct Node *CreateStringLiteralOfTokens(struct Node *head) {
assert(IsToken(head));
int len = 0;
for (struct Node *t = head; t; t = t->next_token) {
len += t->length;
}
char *s = malloc(len + 1 + 2);
assert(s);
char *p = s;
*p = '"';
p++;
for (struct Node *t = head; t; t = t->next_token) {
for (int i = 0; i < t->length; i++) {
*p = t->begin[i];
p++;
}
}
*p = '"';
p++;
*p = 0;
return AllocToken(s, 0, s, len + 2, kTokenStringLiteral);
}
void InsertTokensWithIdentReplace(struct Node *seq, struct Node *rep_list) {
// Insert token sequece (seq) at current cursor pos.
// if seq contains token in rep_list, replace it with tokens rep_list[token];
// elements of seq will be inserted directly.
if (!IsToken(seq)) return;
struct Node **next_holder = next_token_holder;
while (seq) {
struct Node *e;
if (IsEqualTokenWithCStr(seq, "#") && seq->next_token &&
(e = GetNodeByTokenKey(rep_list, seq->next_token))) {
struct Node *st = CreateStringLiteralOfTokens(e->value);
seq = seq->next_token->next_token;
//
st->next_token = *next_holder;
*next_holder = st;
next_holder = &st->next_token;
continue;
}
if (!(e = GetNodeByTokenKey(rep_list, seq))) {
// no replace
struct Node *n = seq;
seq = seq->next_token;
//
n->next_token = *next_holder;
*next_holder = n;
next_holder = &(*next_holder)->next_token;
continue;
}
struct Node *n = DuplicateTokenSequence(e->value);
struct Node *n_last = n;
while (n_last->next_token) n_last = n_last->next_token;
seq = seq->next_token;
//
n_last->next_token = *next_holder;
*next_holder = n;
next_holder = &n_last->next_token;
}
}
struct Node **RemoveDelimiterTokens(struct Node **head_holder) {
InitTokenStream(head_holder);
for (;;) {
struct Node *t = PeekToken();
if (!t) break;
if (ShouldRemoveToken(t)) {
RemoveCurrentToken();
continue;
}
AdvanceTokenStream();
}
return head_holder;
}