-
Notifications
You must be signed in to change notification settings - Fork 11
/
FastLexer.HC
214 lines (200 loc) · 4.51 KB
/
FastLexer.HC
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
#ifndef FAST_LEXER_HC
#define FAST_LEXER_HC
// Hand-rolled lexer written specifically for Mal. Should be much faster
// than the general lexer in Lexer.HC that uses greedy matching of regexes
// in a dumb way.
// Token indices
#define WHITESPACE 0
#define SPECIAL_TWO 1
#define SPECIAL_ONE 2
#define QUOTE_STRING 3
#define COMMENT 4
#define OTHER 5
#include "PArray.HC"
#include "String.HC"
#include "Lexer.HC"
Token *_matchWhitespace(String *s, INT *cur)
{
/* CHAR c = StringGet(s, *cur); */
// suppress warning about 64-bit register
INT c = StringGet(s, *cur);
String *_s;
switch (c) {
case ' ':
case ',':
case '\t':
case '\n':
case 31: // shift+space dot thing
_s = StringEmpty;
StringAppend(_s, c);
// careful with the syntax below.
// ++*cur and *cur++ don't appear to work in HolyC as they do in C
*cur = *cur + 1;
return TokenMk(WHITESPACE, _s);
default:
return NULL;
}
}
Token *_matchSpecialTwo(String *s, INT *cur)
{
String *sub = StringSubstr(s, *cur, 2);
if (StringEqC(sub, "~@")) {
*cur = *cur + 2;
return TokenMk(SPECIAL_TWO, StringMk("~@"));
}
else
return NULL;
}
Token *_matchSpecialOne(String *s, INT *cur)
{
/* CHAR c = StringGet(s, *cur); */
// suppress warning about 64-bit register
INT c = StringGet(s, *cur);
String *_s;
switch (c) {
case '[': case ']': case '{': case '}': case '(':
case ')': case '\'': case '`': case '~': case '^':
_s = StringEmpty;
StringAppend(_s, c);
*cur = *cur + 1;
return TokenMk(SPECIAL_ONE, _s);
default:
return NULL;
}
}
Token *_matchQuote(String *s, INT *cur)
{
INT local_cur = *cur;
/* CHAR c = StringGet(s, local_cur++); */
// suppress warning about 64-bit register
INT c = StringGet(s, local_cur++);
String *acc = StringMk("\"");
if (c == '"') {
while (local_cur < StringLen(s)) {
c = StringGet(s, local_cur++);
if (c == '\\') {
c = StringGet(s, local_cur++);
if (c == '"') {
StringAppend(acc, '"');
}
else {
StringAppend(acc, '\\');
StringAppend(acc, c);
}
}
else {
if (c == '"') {
*cur = local_cur;
StringAppend(acc, '"');
return TokenMk(QUOTE_STRING, acc);
}
else {
StringAppend(acc, c);
}
}
}
return NULL;
}
else
return NULL;
}
BOOL isOtherChar(CHAR c)
{
switch (c) {
case 'a'...'z': case 'A'...'Z': case '0'...'9':
case '=': case '+': case '!': case '#': case '$$':
case '%': case '&': case '*': case '_': case '-':
case '<': case '>': case '.': case '?': case '/':
case '"': case '\'': case ':': case '@':
return TRUE;
default:
return FALSE;
}
}
Token *_matchComment(String *s, INT *cur)
{
INT local_cur = *cur;
/* CHAR c = StringGet(s, local_cur++); */
// suppress warning about 64-bit register
INT c = StringGet(s, local_cur++);
String *acc = StringEmpty;
if (c == ';') {
while (local_cur < StringLen(s)) {
c = StringGet(s, local_cur++);
if (c == '\n') break;
else StringAppend(acc, c);
}
*cur = local_cur;
return TokenMk(COMMENT, acc);
}
else {
return NULL;
}
}
Token *_matchOther(String *s, INT *cur)
{
INT local_cur = *cur;
/* CHAR c = StringGet(s, local_cur++); */
// suppress warning about 64-bit register
INT c = StringGet(s, local_cur++);
String *acc = StringEmpty;
if (isOtherChar(c)) {
StringAppend(acc, c);
while (local_cur < StringLen(s)) {
c = StringGet(s, local_cur++);
if (isOtherChar(c)) {
StringAppend(acc, c);
}
else break;
}
*cur = local_cur - 1;
return TokenMk(OTHER, acc);
}
else {
return NULL;
}
}
// Tokenize an input string.
PArray *FastTokenize(String *s)
{
INT cur = 0;
PArray *tokens = PArrayEmpty;
Token *t;
while (cur < StringLen(s)) {
t = _matchWhitespace(s, &cur);
if (t) {
PArrayPush(tokens, t);
goto FAST_TOKENIZE_LOOP_END;
}
t = _matchSpecialTwo(s, &cur);
if (t) {
PArrayPush(tokens, t);
goto FAST_TOKENIZE_LOOP_END;
}
t = _matchSpecialOne(s, &cur);
if (t) {
PArrayPush(tokens, t);
goto FAST_TOKENIZE_LOOP_END;
}
t = _matchQuote(s, &cur);
if (t) {
PArrayPush(tokens, t);
goto FAST_TOKENIZE_LOOP_END;
}
t = _matchComment(s, &cur);
if (t) {
PArrayPush(tokens, t);
goto FAST_TOKENIZE_LOOP_END;
}
t = _matchOther(s, &cur);
if (t) {
PArrayPush(tokens, t);
goto FAST_TOKENIZE_LOOP_END;
}
/* Fail if nothing matches */
return NULL;
FAST_TOKENIZE_LOOP_END:;
}
return tokens;
}
#endif