-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.c3
381 lines (329 loc) · 7.67 KB
/
string.c3
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
module toml::parser;
import std::io;
import std::collections::object;
const ESCAPE = '\\';
fn bool escape_seq_char(char c) @inline
{
switch(c)
{
case 0x22: // \
case 0x5C: // "
case 0x62: // b
case 0x66: // f
case 0x6E: // n
case 0x72: // r
case 0x74: // t
case 0x75: // u 4HEXDI>
case 0x55: // U 8HEXDIG
return true;
default:
return false;
}
}
fn bool basic_unescaped(char c) @inline
{
return (is_ws(c) || c == 0x21 || (c >= 0x23 && c <= 0x5B) || (c >= 0x5D && c <= 0x7E));
}
fn bool literal_char(char c) @inline
{
return (c == 0x09 || (c >= 0x20 && c <= 0x26) || (c >= 0x28 && c <= 0x7E));
}
// string = ml-basic-string / basic-string / ml-literal-string / literal-string
fn Object*! Parser.string(&self)
{
self.start = self.current;
// first quote
char quote = self.advance()!;
// second quote
if (self.must(quote)) {
// if no third quote is found, then return empty string.
if (!self.must(quote)) return object::new_string("", self.allocator);
if (quote == '"')
{
return self.ml_basic_string()!;
}
else if (quote == '\'')
{
return self.ml_literal_string()!;
}
else
{
self.error("invalid quote; only use \" and '");
return ParserError.INVALID_QUOTE?;
}
}
else
{
// handle single-quoted string "abc"
// first quote was already consumed.
// basic-string / basic-literal
if (quote == '"')
{
self.basic_string()!;
}
else if (quote == '\'')
{
self.literal_string()!;
}
else
{
self.error("invalid quote; only use \" and '");
return ParserError.INVALID_QUOTE?;
}
return object::new_string(self.text()[1..^2], self.allocator);
}
}
// ml-basic-string = ml-basic-string-delim [ newline ] ml-basic-body
// ml-basic-string-delim
// ml-basic-string-delim = 3quotation-mark
// ml-basic-body = *mlb-content *( mlb-quotes 1*mlb-content ) [ mlb-quotes ]
// mlb-quotes = 1*2quotation-mark
fn Object*! Parser.ml_basic_string(&self)
{
// ml-basic-string-delim has been read at this point.
DString d;
d.new_init(16, self.allocator);
defer d.free();
// [ newline ]
if (is_newline(self.peek())) self.advance()!;
// ml-basic-body
while LOOP: (1)
{
self.mlb_content(&d)!;
// maximum much for quotes, but cannot have more than 5
// terminal quotes
int i = 0;
while (!self.at_end() && self.must('"'))
{
i++;
d.append_char('"');
}
switch
{
case i < 3:
continue;
case i >= 3 && i < 6:
break LOOP;
case i >= 6:
self.error("terminal quote sequence is invalid; there are too many..");
return ParserError.INVALID_TERMINAL_QUOTES?;
}
}
// must have at least 6 chars (""""""") in smallest ml string
if (d.len() <= 3)
{
self.error("multi-line string is not long enough; did add three quotes on both sides?");
return ParserError.MULTILINE_STRING_TOO_SHORT?;
}
return object::new_string(d.str_view().strip_end(`"""`), self.allocator);
}
// mlb-content = mlb-char / newline / mlb-escaped-nl
// mlb-char = mlb-unescaped / escaped
// mlb-unescaped = wschar / 0x21 / 0x23 - 0x5B / 0x5D - 0x7E / non-ascii
// mlb-escaped-nl = escape ws newline *( wschar / newline )
// TODO: non-ascii
fn void! Parser.mlb_content(&self, DString *d)
{
// mlb-content = mlb-char / newline / mlb-escaped-nl
char c;
while LOOP: (1)
{
c = self.peek();
// mlb-char = escaped
if (is_ws(c) || c == 0x21 || (c >= 0x23 && c <= 0x5B) || (c >= 0x5D && c <= 0x7E)) {
d.append_char(self.advance()!);
continue;
}
// newline
if (is_newline(c)) {
d.append_char(self.advance()!);
continue;
}
// mlb-char = escaped
// mlb-escaped-nl = escape ws newline *( wschar / newline )
if (self.must(ESCAPE))
{
if (escape_seq_char(self.peek()))
{
char u;
d.append_char(ESCAPE);
d.append_char(u = self.advance()!);
int n = 0;
switch (u)
{
case 0x75:
n = 4;
case 0x55:
n = 8;
}
for (int i = 0; i < n; i++)
{
if (self.peek().is_xdigit())
{
d.append_char(self.advance()!);
}
else
{
self.error("invalid escaped unicode sequence");
return ParserError.INVALID_ESCAPE_UNICODE?;
}
}
continue;
}
else
{
self.skip_ws()!;
while (is_newline(self.peek())) self.advance()!;
while (is_ws(self.peek()) || is_newline(self.peek())) self.advance()!;
}
}
break LOOP;
}
}
// basic-string = quotation-mark *basic-char quotation-mark
// basic-char = basic-unescaped / escaped
// basic-unescaped = wschar / 0x21 / 0x23 - 0x5B / 0x5D - 0x-7E / non-ascii
// escaped = escape escape-seq-char
// escape = 0x5C ; \
// escape-seq-char = 0x22 / 0x5C / 0x62 / 0x66 / 0x6E / 0x72 / 0x72 / unicode??
// TODO: non-ascii
fn void! Parser.basic_string(&self)
{
// beginning quote is already consumed.
while LOOP: (1)
{
// basic-unescaped
if (basic_unescaped(self.peek()))
{
self.advance()!;
continue;
}
// escaped
if (self.must(ESCAPE)) {
if (escape_seq_char(self.peek()))
{
char u;
u = self.advance()!;
int n = 0;
switch (u)
{
case 0x75:
n = 4;
case 0x55:
n = 8;
}
for (int i = 0; i < n; i++)
{
if (self.peek().is_xdigit())
{
self.advance()!;
}
else
{
self.error("invalid escaped unicode sequence");
return ParserError.INVALID_ESCAPE_UNICODE?;
}
}
continue;
}
else
{
self.error("invalid escape sequence");
return ParserError.INVALID_ESCAPE_SEQUENCE?;
}
}
if (!self.must('"'))
{
self.error("missing closing quote \"");
return ParserError.UNCLOSED_QUOTE?;
}
break LOOP;
}
}
// ml-literal-string = ml-literal-string delim [ newline ] ml-literal-body
// ml-literal-string-delim
// ml-literal-string-delim = 3apostrophe
// ml-literal-body = "mll-content *( mll-quotes 1*mll-content ) [ mll-quotes ]
fn Object*! Parser.ml_literal_string(&self)
{
// ml-literal-string-delim has been read at this point.
DString d;
d.new_init(16, self.allocator);
defer d.free();
// [ newline ]
if (is_newline(self.peek())) self.advance()!;
// ml-literal-body
while LOOP: (1)
{
self.mll_content(&d)!;
// maximum much for quotes, but cannot have more than 5
// terminal quotes
int i = 0;
while (!self.at_end() && self.must('\''))
{
i++;
d.append_char('\'');
}
switch
{
case i < 3:
continue;
case i >= 3 && i < 6:
break LOOP;
case i >= 6:
self.error("terminal apostrophe sequence is invalid; there are too many..");
return ParserError.INVALID_TERMINAL_QUOTES?;
}
}
// must have at least 3 chars (''') in smallest ml string
if (d.len() <= 3)
{
self.error("multi-line string is not long enough; did add three apostrophes on both sides?");
return ParserError.MULTILINE_STRING_TOO_SHORT?;
}
return object::new_string(d.str_view().strip_end(`'''`), self.allocator);
}
// mll-content = mll-char / newline
// mll-char = 0x09 / 0x20 - 0x26 / 0x28 - 0x7E / non-ascci
// mll-quotes = 1*2apstrophe
// TODO: non-ascii
fn void! Parser.mll_content(&self, DString *d)
{
// mll-content = mll-char / newline
char c;
while LOOP: (1)
{
c = self.peek();
// mll-char
if (c == 0x09 || (c >= 0x20 && c <= 0x26) || (c >= 0x28 && c <= 0x7E)) {
d.append_char(self.advance()!);
continue;
}
// newline
if (is_newline(c)) {
d.append_char(self.advance()!);
continue;
}
break LOOP;
}
}
// literal-string = apostrophe *literal-char apostrophe
// literal-char = 0x09 / 0x20 - 0x26 / 0x28 - 0x7E / non-ascii
// TODO: non-ascii
fn void! Parser.literal_string(&self)
{
// beginning apostrophe is already consumed.
while (1)
{
if (!literal_char(self.peek()))
{
if (!self.must('\''))
{
self.error("missing closing apostrophe '");
return ParserError.UNCLOSED_QUOTE?;
}
return;
}
self.advance()!;
}
}