-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c3
More file actions
697 lines (616 loc) · 17.5 KB
/
Copy pathparser.c3
File metadata and controls
697 lines (616 loc) · 17.5 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
module xml;
import std::io;
import std::ascii;
bitstruct ParsingOptions : char
{
bool parse_comments;
}
struct Parser
{
Allocator allocator;
String path;
DString buffer;
InStream stream;
Token active_token;
usz num_lines;
usz offset;
usz line_offset;
ParsingOptions options;
bitstruct : char {
bool reached_end;
bool is_open;
}
}
struct Marker
{
String file;
usz index;
usz line;
usz col;
usz offset;
usz line_offset;
}
enum XmlTokenType
{
// Special tokens
INVALID,
EOF,
// Content tokens
IDENTIFIER,
LITERAL,
CHAR_DATA,
STRING,
BODY_TEXT,
// Quotation marks
DOUBLE_QUOTE, // "
SINGLE_QUOTE, // '
// Common punctuation
COLON, // :
EQUALS, // =
HASH, // #
DASH, // -
// XML-specific tokens
TAG_OPEN, // <
TAG_CLOSE, // >
TAG_SELF_CLOSE, // />
EXCLAMATION_MARK, // !
QUESTION_MARK, // ?
FORWARD_SLASH, // /
// CDATA and processing instruction delimiters
CDATA_START, // <![CDATA[
CDATA_END, // ]]>
PI_START, // <?
PI_END, // ?>
// XmlAttribute value delimiters
ATTR_VALUE_START, // = followed by " or '
ATTR_VALUE_END, // closing " or '
// Comment delimiters
COMMENT_START, // <!--
COMMENT_END, // -->
// DTD-related tokens
DTD_START, // <!DOCTYPE
SQUARE_BRACKET_OPEN, // [
SQUARE_BRACKET_CLOSE, // ]
}
struct Token
{
XmlTokenType type;
String text;
Marker mark;
}
faultdef
DOC_TYPE_MUST_PRECEDE_ELEMENTS,
ELEMENT_NOT_FOUND,
FILE_ERROR,
GENERAL_ERROR,
INVALID_CHARACTER_IN_STRING,
INVALID_ENTITY,
INVALID_ESCAPE_SEQUENCE,
INVALID_TOKEN,
MISMATCHED_CLOSING_TAG,
MISSING_ATTRIBUTE,
UNCLOSED_COMMENT,
UNEXPECTED_CHARACTER,
UNEXPECTED_EOF,
UNEXPECTED_TOKEN,
UNHANDLED_BANG;
fn void? Parser.init(&self, InStream stream, Allocator allocator = allocator::heap())
{
self.stream = stream;
self.allocator = allocator;
self.buffer = dstring::new_with_capacity(allocator::heap(), 64);
Char32 first = self.peek_char()!;
self.num_lines = first != '\0' ? 1 : 0;
}
fn void Parser.free(&self)
{
self.buffer.free();
}
fn Char32? Parser.read_next(&self) @private
{
if (self.reached_end) return '\0';
char? ch = self.stream.read_byte();
self.offset++;
Marker mark = self.current_mark();
if (catch err = ch)
{
switch (err)
{
case io::EOF:
self.reached_end = true;
return '\0';
default:
return @error_mark(mark, self, err,
"[Parser.read_next]: Failed to read next byte from the stream")!;
}
}
switch (ch)
{
case '\n':
self.num_lines++;
self.line_offset = self.offset;
default:
}
// Quick return for ASCII
if (ch.is_ascii())
{
if (ch == 0) self.reached_end = true;
return (Char32)ch;
}
// Determine sequence length
usz total_bytes = 0;
switch
{
case (ch & 0xE0) == 0xC0:
total_bytes = 2;
case (ch & 0xF0) == 0xE0:
total_bytes = 3;
case (ch & 0xF8) == 0xF0:
total_bytes = 4;
default:
@error_mark(mark, self, string::INVALID_UTF8,
"[Parser.read_next]: Failed to determine Unicode sequence length, 0x10FFFF and above is not defined")!;
}
// Buffer to store the complete UTF-8 sequence
char[4] buf;
buf[0] = ch;
// Read continuation bytes
for (int i = 1; i < total_bytes; i++)
{
char next = self.stream.read_byte()!!;
self.offset++;
buf[i] = next;
}
// Decode the sequence
usz bytes_read = total_bytes;
Char32? result = conv::utf8_to_char32(&buf, &total_bytes);
if (catch err = result)
{
@error_mark(self.current_mark(), self, err,
"[Parser.read_next]: Invalid UTF-8 character: %c", ch)!;
}
// Verify we read the expected number of bytes
if (bytes_read != total_bytes)
{
@error_mark(self.current_mark(), self, string::INVALID_UTF8,
"[Parser.read_next]: Invalid UTF-8 character: %c", ch)!;
}
return result;
}
fn Token? Parser.advance(&self)
{
Char32 c;
// Skip whitespace
while WS: (c = self.read_next()!)
{
switch (c)
{
case '\n':
case ' ':
case '\t':
case '\r':
case '\v':
continue;
default:
break WS;
}
}
String text;
XmlTokenType type;
Marker mark = self.current_mark();
// Handle identifier characters (including Unicode)
// XML 1.0 spec allows many Unicode characters in names
if (is_valid_identifier_start(c) && self.is_open)
{
// We found an identifier (element name, attribute key)
self.buffer.clear();
self.buffer.append_char32(c);
bool namespaced;
while (is_valid_identifier(self.peek_char()!))
{
Char32 next = self.read_next()!!; // consume the peeked char
self.buffer.append_char32(next);
if (next == ':')
{
if (namespaced) break;
namespaced = true;
}
}
text = self.buffer.str_view();
type = IDENTIFIER;
}
else if (ascii::is_print_m(c) && c != '<' && !self.is_open)
{
// We are about to read a body text (value)
type = BODY_TEXT;
if (c.is_ascii())
{
self.stream.seek(-1, Seek.CURSOR)!!;
}
else
{
// Need to seek back by the number of UTF-8 bytes for this character
char[4] temp_buf;
usz bytes = conv::char32_to_utf8(c, &temp_buf)!!;
self.stream.seek(-bytes, Seek.CURSOR)!!;
}
}
else
{
switch (c)
{
case '\0': type = EOF;
case '<':
type = TAG_OPEN;
self.is_open = true;
case '>':
type = TAG_CLOSE;
self.is_open = false;
case '!': type = EXCLAMATION_MARK;
case '?': type = QUESTION_MARK;
case '=': type = EQUALS;
case '#': type = HASH;
case '/': type = FORWARD_SLASH;
case '-': type = DASH;
case ':': type = COLON;
case '"':
case '\'':
text = self.parse_string(close: c)!!;
type = STRING;
default:
if (c >= 0x80)
{
return @error_mark(mark, self, UNEXPECTED_CHARACTER,
"[Parser.advance]: Unexpected Unicode character: %c", c)!!;
}
}
}
Token token = { .type = type, .text = text, .mark = mark };
self.active_token = token;
return token;
}
fn Marker Parser.current_mark(&self) @inline =>
{
.file = self.path,
.index = self.offset,
.line_offset = self.line_offset,
.line = self.num_lines,
.col = self.offset - self.line_offset,
};
fn Token? Parser.peek_token(&self)
{
usz current_offset = self.stream.seek(0, Seek.CURSOR)!!;
Parser old = *self;
Token token = self.advance()!!;
*self = old;
self.stream.seek(current_offset, Seek.SET)!!; // Set stream back to start pos
return token;
}
fn Char32? Parser.peek_char(&self, usz skip = 0)
{
usz num_lines = self.num_lines;
usz line_offset = self.line_offset;
usz offset = self.offset;
usz current_offset = self.stream.seek(0, Seek.CURSOR)!!;
self.stream.seek(skip, Seek.CURSOR)!!;
Char32 ch = self.read_next()!;
self.stream.seek(current_offset, Seek.SET)!!;
self.num_lines = num_lines;
self.line_offset = line_offset;
self.offset = offset;
return ch;
}
fn Token? Parser.expect_next(&self, XmlTokenType type)
{
Marker mark = self.current_mark();
Token token = self.advance()!!;
if (token.type == type) return token;
return @error_mark(mark, self, UNEXPECTED_TOKEN,
"[Parser.expect_next]: Expected \"%s\", got \"%s\".", type, token.type)!!;
}
fn void? Parser.parse_attributes(&self, XmlAttribute* attributes)
{
XmlAttributeType type = self.active_token.type == XmlTokenType.DOUBLE_QUOTE
? XmlAttributeType.DOUBLE_QUOTE : XmlAttributeType.DOUBLE_QUOTE;
attributes.set(DOCUMENT, type);
while (Token t = self.peek_token()!, t.type == IDENTIFIER)
{
@pool()
{
self.expect_next(IDENTIFIER)!!;
String key_text = self.buffer.tcopy_str();
self.expect_next(EQUALS)!!;
self.expect_next(STRING)!!;
String value_text = self.buffer.tcopy_str();
attributes.add(key_text, value_text);
};
}
}
fn void? Parser.skip_comment(&self)
{
while (Char32 c = self.read_next()!, c != '\0' || c != 0)
{
if (self.peek_char(1)! == '-' && self.peek_char(2)! == '-')
{
self.stream.seek(3, Seek.CURSOR)!!;
self.expect_next(TAG_CLOSE)!!;
return;
}
}
return UNCLOSED_COMMENT?;
}
fn String? Parser.parse_string(&self, Char32 close = '<', bool consume_close = true)
{
self.buffer.clear();
char[4] utf8_buf;
while LOOP: (true)
{
Char32 c = self.read_next()!!;
switch (c)
{
case '\0':
return UNEXPECTED_EOF?;
case close:
if (!consume_close)
{
self.offset--;
}
break LOOP;
case '<':
// Invalid character in an attribute
if (close == '\'' || close == '"')
{
return INVALID_CHARACTER_IN_STRING?;
}
nextcase;
case '&':
// Handle XML entities
Char32 entity = self.parse_entity_string()!!;
self.buffer.append_char32(entity);
continue;
default:
self.buffer.append_char32(c);
}
}
String trimmed = self.buffer.str_view();
if (trimmed != "")
{
usz end = trimmed.len - 1;
foreach_r EAT: (ch : trimmed)
{
switch (ch)
{
case ' ':
case '\t':
case '\r':
case '\n':
end--;
continue;
default:
break EAT;
}
}
trimmed = trimmed[..end];
}
if (!consume_close)
{
self.stream.seek(-1, Seek.CURSOR)!!;
}
return trimmed;
}
fn void? Parser.parse_prologue(&self, XmlDocument* doc)
{
Marker mark = self.current_mark();
self.parse_attributes(&doc.attributes)!!;
if (try String version = doc.attributes.get("version"))
{
switch (version)
{
case "1.0":
case "1.1":
break;
default:
@error_mark(mark, self, GENERAL_ERROR,
"[Parser.parse_prologue] Warning: Unhandled XML version: %s", version)!;
}
}
if (try String encoding = doc.attributes.get("encoding"))
{
@pool()
{
String temp = encoding.to_lower_tcopy();
switch (temp)
{
case "utf-8":
case "utf8":
doc.encoding = UTF_8;
case "latin-1":
case "latin1":
doc.encoding = LATIN_1;
case "iso-8859-1":
doc.encoding = ISO_8859_1;
default:
@error_mark(mark, self, GENERAL_ERROR,
"[Parser.parse_prologue] Warning: Unrecognized encoding: %s", encoding)!;
}
};
}
self.expect_next(QUESTION_MARK)!!;
self.expect_next(TAG_CLOSE)!!;
}
fn Char32? Parser.parse_entity_string(&self)
{
@pool()
{
DString dentity = dstring::temp_with_capacity(10);
Marker marker = self.current_mark();
while NEXT: (Char32 c = self.read_next()!!)
{
switch (c)
{
case ';':
break NEXT;
case '\0':
return UNEXPECTED_EOF?;
default:
dentity.append(c);
}
}
String entity = dentity.str_view();
if (entity.len == 0)
{
@error_mark(marker, self, INVALID_ENTITY,
"[Parser.parse_entity_string]: Invalid entity: %s")!!;
}
switch (entity[0])
{
case '#':
int base = 10;
int val;
entity = entity[1..];
if (entity[0] == 'x' || entity[0] == 'X')
{
base = 16;
entity = entity[1..];
}
while (entity.len)
{
char ch = entity[0];
switch (ch)
{
case '0'..'9':
val *= base;
val += (int)(ch - '0');
case 'a'..'f':
if (base == 10) { return 0; }
val *= base;
val += (int)(ch - 'a' + 10);
case 'A'..'F':
if (base == 10) { return 0; }
val *= base;
val += (int)(ch - 'A' + 10);
default:
return 0;
}
if (val > 0x10ffff) return 0;
entity = entity[1..];
}
return (Char32)val;
default:
return named_xml_entity_to_char32(entity)!;
}
};
}
fn void? Parser.skip_element(&self)
{
uint close = 1;
while LOOP: (true)
{
Token tok = self.advance()!!;
switch (tok.type)
{
case EOF:
@error_mark(self.current_mark(), self, UNEXPECTED_EOF,
"[Parser.skip_element]: Unexpected EOF")!!;
case TAG_OPEN:
close++;
case TAG_CLOSE:
close--;
if (close == 0)
{
break LOOP;
}
default:
}
}
}
fn XmlDoctype? Parser.parse_doctype(&self)
{
XmlDoctype doctype;
Token tok = self.expect_next(IDENTIFIER)!;
doctype.name = tok.text.copy(self.allocator);
usz from_offset = self.stream.seek(0, Seek.CURSOR)!!;
self.skip_element()!!;
usz to_offset = self.stream.seek(0, Seek.CURSOR)!!;
@pool()
{
char[] buf = mem::temp_array(char, (to_offset - from_offset) - 1);
self.stream.seek(from_offset, Seek.SET)!!;
self.stream.read(buf[..])!!;
doctype.body = ((String)buf).copy(self.allocator);
};
self.is_open = true;
self.expect_next(TAG_CLOSE)!!;
return doctype;
}
fn bool char.is_ascii(c) @inline => c <= 0x7F;
fn bool Char32.is_ascii(c) @inline => c <= 0x7F;
fn bool is_valid_identifier_start(Char32 c)
{
// Basic ASCII characters
if (c.is_ascii())
{
switch (c)
{
case '_': // Underscore
case ':': // Colon
case 'A'..'Z': // Uppercase ASCII letters
case 'a'..'z': // Lowercase ASCII letters
return true;
}
}
else
{
// XML 1.0 Letter ranges
switch
{
case c >= 0x4E00 && c <= 0x9FFF: // CJK Unified Ideographs
case c >= 0x3040 && c <= 0x309F: // Hiragana
case c >= 0x30A0 && c <= 0x30FF: // Katakana
case c >= 0xAC00 && c <= 0xD7AF: // Hangul
case c >= 0x0370 && c <= 0x037D: // Greek
case c >= 0x002E2 && c <= 0x002E9: // CJK Radicals
case c >= 0x00386 && c <= 0x00481: // Greek & Coptic
case c >= 0x0048A && c <= 0x00559: // Cyrillic
return true;
}
}
return false;
}
fn bool is_valid_identifier(Char32 c)
{
// Basic ASCII characters
if (c.is_ascii())
{
switch (c)
{
case '_': // Underscore
case ':': // Colon
case '-': // Hyphen
case '.': // Period
case 'A'..'Z': // Uppercase ASCII letters
case 'a'..'z': // Lowercase ASCII letters
case '0'..'9': // ASCII digits
return true;
}
}
else
{
// XML 1.0 NameChar ranges
switch
{
case c >= 0x4E00 && c <= 0x9FFF: // CJK Unified Ideographs
case c >= 0x3040 && c <= 0x309F: // Hiragana
case c >= 0x30A0 && c <= 0x30FF: // Katakana
case c >= 0xAC00 && c <= 0xD7AF: // Hangul
case c >= 0x0300 && c <= 0x036F: // Combining marks
case c >= 0x203F && c <= 0x2040: // Extenders
case c >= 0x0370 && c <= 0x037D: // Greek
case c >= 0x002E2 && c <= 0x002E9: // CJK Radicals
case c >= 0x00386 && c <= 0x00481: // Greek & Coptic
case c >= 0x0048A && c <= 0x00559: // Cyrillic
return true;
}
}
return false;
}