forked from facebookarchive/warp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringlit.d
336 lines (293 loc) · 9.11 KB
/
stringlit.d
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* C preprocessor
* Copyright: 2013 by Digital Mars
* License: $(LINK2 http://boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Walter Bright
*/
module stringlit;
import std.array;
import std.range;
import std.stdio;
import std.traits;
import std.utf;
import lexer;
import main;
import macros;
import ranges;
// Kind of string literal
enum STR
{
s, // default
f, // #include string, i.e. no special meaning for \ character
L, // wchar_t
u8, // UTF-8
u, // wchar
U // dchar
}
/*****************************************
* Read in a string literal.
* Input:
* tc string terminating character, which is one of ', " or >
* Output:
* bytes written to s
*/
R lexStringLiteral(R, S)(R r, ref S s, char tc, STR str)
{
alias Unqual!(ElementEncodingType!R) E;
/* Read \0, \x, \u and \U digit sequences
*/
dchar readRadix(int radix, int ndigits)
{
int n = 0;
dchar d = 0;
do
{
if (r.empty)
break;
E c = cast(E)r.front;
uint i = 32;
if (c >= '0' && c <= '9')
i = c - '0';
else if (c >= 'A' && c <= 'F')
i = c + 10 - 'A';
else if (c >= 'a' && c <= 'f')
i = c + 10 - 'a';
if (i >= radix)
{
if (n == 0 || ndigits >= 4)
err_fatal("radix %s digit expected, saw '%c'", radix, c);
break;
}
d = d * radix + i;
r.popFront();
} while (++n < ndigits);
/* Don't worry about disallowed Universal characters, as they
* vary from Standard to Standard, and we want this to work with
* all of them.
*/
return d;
}
bool slash;
while (!r.empty)
{
E c = cast(E)r.front;
dchar d = c;
switch (c)
{
case '"':
case '\'':
case '>':
if (c == tc && !slash)
{
r.popFront();
return r;
}
goto default;
case ESC.space:
case ESC.brk: // token separator
r.popFront();
break;
case '\\':
if (str == STR.f) // ignore escapes in #include strings
goto default;
Lslash:
/* Escape sequences
*/
r.popFront();
if (r.empty)
goto Lerror;
c = cast(E)r.front;
switch (c)
{
case '\r':
goto Lslash;
case '\n':
r.popFront();
continue;
case '"':
case '\'':
case '\\':
case '?': d = c; break;
case 'a': d = '\a'; break;
case 'b': d = '\b'; break;
case 'f': d = '\f'; break;
case 'n': d = '\n'; break;
case 'r': d = '\r'; break;
case 't': d = '\t'; break;
case 'v': d = '\v'; break;
case '0': .. case '7': // \nnn octal
d = readRadix(8, 3);
if (d >= 0x100)
err_fatal("octal escape exceeds 0xFF");
goto Lput;
case 'x': // \xnn hex
r.popFront();
d = readRadix(16, 2);
goto Lput;
case 'u': // \unnnn hex
r.popFront();
d = readRadix(16, 4);
goto Lput;
case 'U': // \Unnnnnnnn hex
r.popFront();
d = readRadix(16, 8);
goto Lput;
default:
err_fatal("invalid escape sequence");
break;
}
goto default;
default:
r.popFront();
Lput:
/* Stuff d into the output buffer, how it is done
* depends on the kind of string literal being built.
*/
final switch (str)
{
case STR.s:
case STR.f:
s.put(cast(E)d);
break;
case STR.u8:
{
char[4] buf = void;
size_t len = std.utf.encode(buf, d);
foreach (chr; buf[0 .. len])
s.put(chr);
break;
}
case STR.L:
s.put(cast(E)d);
s.put(cast(E)(d >> 8));
version (Windows)
{
}
else
{
s.put(cast(E)(d >> 16));
s.put(cast(E)(d >> 24));
}
break;
case STR.u:
{
wchar[2] buf = void;
size_t len = std.utf.encode(buf, d);
foreach (chr; (cast(E*)buf.ptr)[0 .. len * wchar.sizeof])
s.put(chr);
break;
}
case STR.U:
s.put(cast(E)d);
s.put(cast(E)(d >> 8));
s.put(cast(E)(d >> 16));
s.put(cast(E)(d >> 24));
break;
}
slash = false;
break;
}
}
Lerror:
err_fatal("string literal is not closed with %s", tc);
return r;
}
unittest
{
StaticArrayBuffer!(char, 100) buf = void;
buf.initialize();
auto r = `abc"`.lexStringLiteral(buf, '"', STR.s);
assert(r.empty && buf[] == "abc");
buf.initialize();
r = `\\\"\'\?\a\b\f\n\r\t\v"`.lexStringLiteral(buf, '"', STR.s);
assert(r.empty && buf[] == "\\\"\'\?\a\b\f\n\r\t\v");
buf.initialize();
r = `\0x\1773"`.lexStringLiteral(buf, '"', STR.s);
assert(r.empty && buf[] == "\0x\1773");
buf.initialize();
r = `\xa\xAFc"`.lexStringLiteral(buf, '"', STR.s);
//writefln("|%s|", buf[]);
assert(r.empty && buf[] == "\x0a\xafc");
buf.initialize();
r = `\uabcdc"`.lexStringLiteral(buf, '"', STR.u);
assert(r.empty && buf.length == 4 && buf[][0] == 0xCD &&
buf[][1] == 0xAB &&
buf[][2] == 'c' &&
buf[][3] == 0x00);
buf.initialize();
r = `\U000DEF01c"`.lexStringLiteral(buf, '"', STR.U);
//writef("%d:", buf.length);
//foreach (c; 0..buf.length) writef(" %02x", buf[][c]);
//writeln();
assert(r.empty && buf.length == 8 && buf[][0] == 0x01 &&
buf[][1] == 0xEF &&
buf[][2] == 0x0D &&
buf[][3] == 0x00 &&
buf[][4] == 'c' &&
buf[][5] == 0x00 &&
buf[][6] == 0x00 &&
buf[][7] == 0x00);
}
/*******************************************
* Lex a character literal, and convert it to an integer i.
*/
R lexCharacterLiteral(R)(R r, ref ppint_t i, STR str)
{
alias Unqual!(ElementEncodingType!R) E;
StaticArrayBuffer!(E, 100) buf;
buf.initialize();
r = r.lexStringLiteral(buf, '\'', str);
E[] a = buf[];
size_t n;
final switch (str)
{
case STR.s:
case STR.u8:
n = a.length;
if (n == 1) // most common case
{
i = a[0];
return r;
}
if (n > ppint_t.sizeof)
err_fatal("too many characters in literal");
else
{
E* p = cast(E*)(&i) + n;
foreach (c; a[0 .. n])
*--p = c;
}
break;
case STR.L:
version (Windows)
{
i = *cast(ushort*)a.ptr;
}
else
{
i = *cast(uint*)a.ptr;
}
break;
case STR.u:
i = *cast(ushort*)a.ptr;
break;
case STR.U:
i = *cast(uint*)a.ptr;
break;
case STR.f:
assert(0);
}
return r;
}
unittest
{
ppint_t n;
{
auto r = `a'`.lexCharacterLiteral(n, STR.s);
assert(r.empty && n == 'a');
}
{
auto r = `ab'`.lexCharacterLiteral(n, STR.s);
assert(r.empty && n == 0x6162);
}
}