-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtoml.jou
More file actions
991 lines (853 loc) · 34 KB
/
Copy pathtoml.jou
File metadata and controls
991 lines (853 loc) · 34 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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# TOML 1.1 parser
#
# See doc/toml.md for documentation.
import "stdlib/ascii.jou"
import "stdlib/assert.jou"
import "stdlib/intnative.jou"
import "stdlib/io.jou"
import "stdlib/limits.jou"
import "stdlib/list.jou"
import "stdlib/math.jou"
import "stdlib/mem.jou"
import "stdlib/str.jou"
import "stdlib/utf8.jou"
@public
enum TOMLType:
Error # the parser relies on this being first, and uses this as "not initialized" value
Table
Array
String
Boolean
Integer
Floating
global TOML_TYPE_NAMES: byte*[enum_count(TOMLType)] = [
"Error", "Table", "Array", "String", "Boolean", "Integer", "Floating"
]
# This is used to prevent adding more stuff to an existing table/array when
# that is not allowed by TOML's rules.
enum CreatedBy:
Unset # must be the first member so that this is the default
Inline # e.g. {x = "foo", y = "bar"} or ["foo", "bar"]
Section # foo was created with [foo] or [[foo]] on a line of its own
DottedKey # table foo was created only to access foo.bar
@public
class TOML:
type: TOMLType
union:
error_message: byte[300] # only for TOMLType.Error
table: List[TOMLKeyVal] # only for TOMLType.Table
array: List[TOML] # only for TOMLType.Array
string: byte* # only for TOMLType.String
boolean: bool # only for TOMLType.Boolean
integer: int64 # only for TOMLType.Integer
floating: double # only for TOMLType.Floating
# For TOMLType.String, this is the string length with included zero bytes.
# For other types this is always zero.
string_len: intnative
lineno: int # Line number, available on all TOML objects
depth: int # 0 for return value of parse_toml(), 1 for its contents etc
# Internal detail. Not meant to be accessed outside this file.
created_by: CreatedBy
@public
def free(self) -> None:
match self.type:
case TOMLType.Table:
for kv = self.table.ptr; kv < self.table.end(); kv++:
free(kv.key)
kv.value.free()
free(self.table.ptr)
case TOMLType.Array:
for p = self.array.ptr; p < self.array.end(); p++:
p.free()
free(self.array.ptr)
case TOMLType.String:
free(self.string)
case TOMLType.Boolean | TOMLType.Integer | TOMLType.Floating | TOMLType.Error:
pass
# In case someone frees a part of a big TOML document for whatever reason
*self = TOML{}
# For debugging
@public
def print(self) -> None:
printf("%s on line %d:", TOML_TYPE_NAMES[self.type as int], self.lineno)
match self.type:
case TOMLType.Error:
printf(" %s\n", self.error_message)
case TOMLType.Table:
printf("\n")
for kv = self.table.ptr; kv < self.table.end(); kv++:
printf("%*s%s = ", 2*kv.value.depth, "", kv.key)
kv.value.print()
case TOMLType.Array:
printf("\n")
for item = self.array.ptr; item < self.array.end(); item++:
printf("%*s", 2*item.depth, "")
item.print()
case TOMLType.String:
printf(" \"")
for p = self.string; p < &self.string[self.string_len]; p++:
match *p:
case '\n':
printf("\\n")
case '\t':
printf("\\t")
case '\r':
printf("\\r")
case '"':
printf("\\\"")
case '\\':
printf("\\\\") # lol
case _:
if *p < 128 and not is_ascii_printable(*p):
# unprintable ASCII character
printf("\\x%02x", *p)
else:
# This prints all non-ASCII characters as is
putchar(*p)
printf("\"\n")
case TOMLType.Boolean:
printf(" %s\n", "true" if self.boolean else "false")
case TOMLType.Integer:
printf(" %lld\n", self.integer)
case TOMLType.Floating:
if isnan(self.floating):
printf(" nan\n")
elif isinf(self.floating):
printf(" %sinf\n", "+" if self.floating > 0 else "-")
else:
# Same logic as in stdlib/json.jou
buf: byte[64] = ""
snprintf(buf, sizeof(buf), "%.16g", self.floating)
if atof(buf) == self.floating:
printf(" %.16g\n", self.floating)
else:
printf(" %.17g\n", self.floating)
@public
class TOMLKeyVal:
key: byte*
value: TOML
@public
def toml_get(table: TOML*, key: byte*) -> TOML*:
if table != NULL and key != NULL and table.type == TOMLType.Table:
for kv = table.table.ptr; kv < table.table.end(); kv++:
if strcmp(kv.key, key) == 0:
return &kv.value
return NULL
# Very deeply nested TOML is banned, similarly to JSON. Creating such data
# structures would be fine, but we're in trouble when it's time to free the
# memory: we would need to recurse through the data structure to free each
# member, and with deep nesting, that could overflow the stack.
const MAX_DEPTH: int = 64
def hexdigit_value(b: byte) -> int:
if '0' <= b and b <= '9':
return b - '0'
if 'A' <= b and b <= 'F':
return 10 + (b - 'A')
if 'a' <= b and b <= 'f':
return 10 + (b - 'a')
return -1
def looks_like_end_of_line(s: byte*) -> bool:
while *s == ' ' or *s == '\t':
s++
return *s == '\n' or *s == '\0' or *s == '#' # allow comments at end of line
def looks_like_date(s: byte*) -> bool:
return (
is_ascii_digit(*s++)
and is_ascii_digit(*s++)
and is_ascii_digit(*s++)
and is_ascii_digit(*s++)
and *s++ == '-'
and is_ascii_digit(*s)
)
def looks_like_time(s: byte*) -> bool:
return (
is_ascii_digit(*s++)
and is_ascii_digit(*s++)
and *s++ == ':'
and is_ascii_digit(*s)
)
class TOMLParser:
ptr: byte*
root_table: TOML
current_table: TOML*
lineno: int
error: byte[300]
def parse_the_whole_thing(self) -> bool:
while *self.ptr != '\0':
match *self.ptr:
case '\n':
self.ptr++
self.lineno++
case ' ' | '\t':
self.ptr++
case '#':
if not self.skip_comment():
return False
case '[':
if not self.parse_header():
return False
case _:
if not self.parse_key_equals_value(self.current_table):
return False
if not looks_like_end_of_line(self.ptr):
self.error = "invalid syntax" # probably won't be able to do much better than this...
return False
return True
def init_value(self, value_ptr: TOML*, type: TOMLType, depth: int) -> None:
*value_ptr = TOML{type = type, depth = depth, lineno = self.lineno}
def check_depth(self, depth: int) -> bool:
assert depth <= MAX_DEPTH + 1 # If this fails, some intermediate value was not checked
if depth > MAX_DEPTH:
snprintf(
self.error,
sizeof(self.error),
"nesting more than %d levels deep is not supported",
MAX_DEPTH,
)
return False
return True
def skip_comment(self) -> bool:
assert *self.ptr == '#'
while *self.ptr != '\0' and *self.ptr != '\n':
if *self.ptr == 127 or (*self.ptr < 0x20 and *self.ptr != '\t'):
self.error = "invalid character in comment"
return False
self.ptr++
return True
def parse_key_equals_value(self, relative_to: TOML*) -> bool:
if *self.ptr == '=':
self.error = "missing key"
return False
value_dest_ptr = self.parse_key(relative_to, True)
if value_dest_ptr == NULL:
return False
if value_dest_ptr.type != TOMLType.Error:
self.error = "duplicate key"
return False
while *self.ptr == ' ' or *self.ptr == '\t':
self.ptr++
if *self.ptr != '=':
self.error = "invalid key-value pair"
return False
self.ptr++
while *self.ptr == ' ' or *self.ptr == '\t':
self.ptr++
if *self.ptr == '\n' or *self.ptr == '\0':
self.error = "missing value"
return False
value: TOML
if not self.parse_value(&value, value_dest_ptr.depth):
return False
*value_dest_ptr = value
return True
# Returns a pointer where the corresponding value should be stored.
# Make sure not to overwrite the .depth of the returned pointer.
def parse_key(self, relative_to: TOML*, this_is_the_key_of_a_keyval_pair: bool) -> TOML*:
assert relative_to.depth > 0 or relative_to == &self.root_table
value_ptr = self.parse_component_of_key(relative_to, this_is_the_key_of_a_keyval_pair)
if value_ptr == NULL:
return NULL
assert value_ptr.depth == relative_to.depth + 1
while *self.ptr == ' ' or *self.ptr == '\t':
self.ptr++
while *self.ptr == '.':
self.ptr++
while *self.ptr == ' ' or *self.ptr == '\t':
self.ptr++
value_ptr = self.parse_component_of_key(value_ptr, this_is_the_key_of_a_keyval_pair)
if value_ptr == NULL:
return NULL
while *self.ptr == ' ' or *self.ptr == '\t':
self.ptr++
return value_ptr
def parse_component_of_key(self, parent_value: TOML*, this_is_the_key_of_a_keyval_pair: bool) -> TOML*:
assert parent_value != NULL
key: byte*
if *self.ptr == '"' or *self.ptr == '\'':
key_len: intnative
key = self.parse_string(False, &key_len)
if key == NULL:
return NULL
if strlen(key) < key_len:
self.error = "zero bytes in keys are not supported"
free(key)
return NULL
else:
n = strspn(self.ptr, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
if n == 0 or n > INT32_MAX:
self.error = "invalid key"
return NULL
key = NULL
asprintf(&key, "%.*s", n as int, self.ptr)
assert key != NULL
self.ptr = &self.ptr[n]
# Go to a part within an already chosen key
match parent_value.type:
case TOMLType.Error:
# Accessing foo.bar creates foo if it doesn't exist yet
parent_value.type = TOMLType.Table
parent_value.lineno = self.lineno
target_table = parent_value
case TOMLType.Table:
target_table = parent_value
case TOMLType.Array:
# Refer to the last element. It must be a table.
if parent_value.array.len == 0:
self.error = "cannot refer to last element of empty array"
free(key)
return NULL
if parent_value.array.end()[-1].type != TOMLType.Table:
self.error = "last element of array is not a table"
free(key)
return NULL
target_table = &parent_value.array.end()[-1]
case TOMLType.String:
self.error = "expected a table, got a string"
free(key)
return NULL
case TOMLType.Boolean:
self.error = "expected a table, got a boolean"
free(key)
return NULL
case TOMLType.Integer | TOMLType.Floating:
self.error = "expected a table, got a number"
free(key)
return NULL
assert target_table.type == TOMLType.Table
if target_table.created_by == CreatedBy.Inline:
self.error = "inline tables cannot be extended after creating the table"
free(key)
return NULL
if not self.check_depth(target_table.depth + 1):
free(key)
return NULL
# Does this key already exists? If it exists, value will go there
existing = toml_get(target_table, key)
if existing != NULL:
free(key)
return existing
# Key not found --> create new table, unless it's the weird special
# case where that is not allowed
if target_table.created_by == CreatedBy.Section and this_is_the_key_of_a_keyval_pair:
self.error = "a dotted key of a key-value pair cannot extend a table after the content of the table has been explicitly listed above"
free(key)
return NULL
target_table.table.append(TOMLKeyVal{key = key})
result = &target_table.table.end()[-1].value
result.depth = target_table.depth + 1
if this_is_the_key_of_a_keyval_pair:
result.created_by = CreatedBy.DottedKey
return result
def parse_value(self, result: TOML*, depth: int) -> bool:
match *self.ptr:
case '"' | '\'':
self.init_value(result, TOMLType.String, depth)
result.string = self.parse_string(True, &result.string_len)
return result.string != NULL
case 't':
if starts_with(self.ptr, "true"):
self.ptr = &self.ptr[4]
self.init_value(result, TOMLType.Boolean, depth)
result.boolean = True
return True
case 'T':
if starts_with(self.ptr, "True"):
self.error = "should be true, not True"
return False
if starts_with(self.ptr, "TRUE"):
self.error = "should be true, not TRUE"
return False
case 'f':
if starts_with(self.ptr, "false"):
self.ptr = &self.ptr[5]
self.init_value(result, TOMLType.Boolean, depth)
result.boolean = False
return True
case 'F':
if starts_with(self.ptr, "False"):
self.error = "should be false, not False"
return False
if starts_with(self.ptr, "FALSE"):
self.error = "should be false, not FALSE"
return False
case (
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
| '+' | '-'
| 'i' | 'n' # inf and nan
):
if looks_like_date(self.ptr) or looks_like_time(self.ptr):
self.error = "dates and times in TOML are not supported :("
return False
return self.parse_number(result, depth)
case '[':
return self.parse_inline_array(result, depth)
case '{':
return self.parse_inline_table(result, depth)
case _:
pass
self.error = "invalid value"
return False
def parse_number(self, result: TOML*, depth: int) -> bool:
sign = 1
max_abs = INT64_MAX as uint64
if starts_with(self.ptr, "0b"):
base = 2
elif starts_with(self.ptr, "0o"):
base = 8
elif starts_with(self.ptr, "0x"):
base = 16
else:
base = 10
# Only base 10 numbers and floats can be negative in TOML!
if *self.ptr == '-':
sign = -1
max_abs++ # Support the most negative 64-bit integer
if *self.ptr == '+' or *self.ptr == '-':
self.ptr++
if base != 10:
self.ptr = &self.ptr[2]
if base == 10 and starts_with(self.ptr, "inf"):
self.ptr = &self.ptr[3]
self.init_value(result, TOMLType.Floating, depth)
result.floating = sign * INFINITY
return True
if base == 10 and starts_with(self.ptr, "nan"):
self.ptr = &self.ptr[3]
self.init_value(result, TOMLType.Floating, depth)
result.floating = sign * NAN
return True
# We validate the matched digits below, but we can't always search for
# A-F and a-f because floats can contain E and e.
len = strspn(self.ptr, "0123456789ABCDEFabcdef_" if base == 16 else "0123456789_")
if len == 0:
self.error = "invalid number"
return False
if *self.ptr == '_':
self.error = "number cannot start with underscore"
return False
if self.ptr[len-1] == '_':
self.error = "number cannot end with underscore"
return False
if base == 10 and len >= 2 and *self.ptr == '0':
self.error = "unnecessary zero at the start of a number"
return False
for i = 0; i < len; i++:
if starts_with(&self.ptr[i], "__"):
self.error = "consecutive underscores are not allowed in numbers"
return False
if base == 10 and (self.ptr[len] == 'E' or self.ptr[len] == 'e' or self.ptr[len] == '.'):
return self.parse_float(result, sign, depth)
unsigned_result: uint64 = 0
for i = 0; i < len; i++:
if self.ptr[i] != '_':
v = hexdigit_value(self.ptr[i])
assert v != -1
if v >= base:
self.error = "invalid integer"
return False
old = unsigned_result
new = (old*base + v) as uint64
if new // base != old or new > max_abs:
self.error = "integer doesn't fit to int64"
return False
unsigned_result = new
self.ptr = &self.ptr[len]
signed_result = sign * unsigned_result
self.init_value(result, TOMLType.Integer, depth)
result.integer = signed_result
return True
def skip_float_integer_part(self) -> bool:
if not is_ascii_digit(*self.ptr):
self.error = "invalid float"
return False
while is_ascii_digit(*self.ptr) or *self.ptr == '_':
self.ptr++
if starts_with(self.ptr, "__"):
self.error = "consecutive underscores are not allowed in numbers"
return False
if self.ptr[-1] == '_':
self.error = "invalid float"
return False
return True
# Assumes that the sign has already been parsed.
def parse_float(self, result: TOML*, sign: int, depth: int) -> bool:
assert sign == 1 or sign == -1
start_of_float = self.ptr
if not self.skip_float_integer_part():
return False
assert *self.ptr == '.' or *self.ptr == 'E' or *self.ptr == 'e'
if *self.ptr == '.':
self.ptr++
if not self.skip_float_integer_part():
return False
if *self.ptr == 'E' or *self.ptr == 'e':
self.ptr++
if *self.ptr == '+' or *self.ptr == '-':
self.ptr++
if not self.skip_float_integer_part():
return False
# Get rid of all underscores for atof(): "1_2.3_4" --> "12.34 "
#
# This won't modify the string given by user, because we allocate a
# copy of it for CRLF (\r\n) handling anyway.
dest = start_of_float
for p = start_of_float; p < self.ptr; p++:
if *p != '_':
*dest++ = *p
while dest < self.ptr:
*dest++ = ' '
self.init_value(result, TOMLType.Floating, depth)
result.floating = sign * atof(start_of_float)
return True
def read_unicode_escape_in_string(self, prefix_char: byte, num_hex_digits: int, out: List[byte]*) -> bool:
codepoint: uint32 = 0
for i = 0; i < num_hex_digits; i++:
v = hexdigit_value(self.ptr[i])
if v == -1:
self.error = "invalid backslash escape in string"
return False
codepoint <<= 4
codepoint |= v as uint32
if codepoint == 0:
out.append('\0')
else:
s = utf8_encode_char(codepoint)
if s[0] == '\0':
self.error = "escape in string produced an invalid Unicode character"
return False
out.extend_from_ptr(s, strlen(s))
self.ptr = &self.ptr[num_hex_digits]
return True
def parse_string(self, allow_multiline: bool, result_len: intnative*) -> byte*:
quote = *self.ptr
assert quote == '"' or quote == '\''
quote3 = [quote, quote, quote, '\0']
quote4 = [quote, quote, quote, quote, '\0']
quote6 = [quote, quote, quote, quote, quote, quote, '\0']
multiline = starts_with(self.ptr, quote3)
if multiline and not allow_multiline:
self.error = "multiline strings are not allowed here"
return NULL
self.ptr++
if multiline:
self.ptr++
self.ptr++
if *self.ptr == '\n':
self.lineno++
self.ptr++
result = List[byte]{}
while (
*self.ptr != quote
# Multiline strings are weird:
# """foo""" --> 'foo'
# """foo"""" --> 'foo"'
# """foo""""" --> 'foo""'
# """foo"""""" --> error
# """foo""""""" --> error
or (multiline and not starts_with(self.ptr, quote3))
or (multiline and starts_with(self.ptr, quote4) and not starts_with(self.ptr, quote6))
):
if *self.ptr == '\0' or (*self.ptr == '\n' and not multiline):
if multiline:
self.error = "missing quotes to end the string"
else:
self.error = "missing quote to end the string"
free(result.ptr)
return NULL
elif (
quote == '"'
and multiline
and *self.ptr == '\\'
and self.ptr[1 + strspn(&self.ptr[1], " \t")] == '\n'
):
# Line continuation backslash causes all whitespace to be
# trimmed after the backslash. TOML is weird :)
self.ptr++
while *self.ptr == ' ' or *self.ptr == '\t' or *self.ptr == '\n':
if *self.ptr++ == '\n':
self.lineno++
elif quote == '"' and *self.ptr == '\\':
self.ptr++
match *self.ptr++:
case '"':
result.append('"')
case '\\':
result.append('\\')
case 'b':
result.append(0x08) # Jou doesn't have \b
case 'e':
result.append(0x1b) # Jou doesn't have \e
case 'f':
result.append(0x0c) # Jou doesn't have \f
case 'n':
result.append('\n')
case 'r':
result.append('\r')
case 't':
result.append('\t')
case 'x':
if not self.read_unicode_escape_in_string('x', 2, &result):
free(result.ptr)
return NULL
case 'u':
if not self.read_unicode_escape_in_string('u', 4, &result):
free(result.ptr)
return NULL
case 'U':
if not self.read_unicode_escape_in_string('U', 8, &result):
free(result.ptr)
return NULL
case _:
self.error = "invalid backslash escape in string"
free(result.ptr)
return NULL
elif *self.ptr == 0x7f or (*self.ptr < 0x20 and *self.ptr != '\n' and *self.ptr != '\t'):
self.error = "invalid character in string"
free(result.ptr)
return NULL
else:
if *self.ptr == '\n':
self.lineno++
result.append(*self.ptr++)
self.ptr = &self.ptr[3 if multiline else 1]
*result_len = result.len
result.append('\0')
return result.ptr
def parse_header(self) -> bool:
# Mark the old current table as complete. This must be done quite
# early, because we may create a new table below, and that messes up
# pointers into the old table when the tables are in the same array.
self.current_table.created_by = CreatedBy.Section
self.current_table = NULL # not necessary, but causes bugs to fail more loudly
assert *self.ptr == '['
self.ptr++
is_array = (*self.ptr == '[')
if is_array:
self.ptr++
while *self.ptr == ' ' or *self.ptr == '\t':
self.ptr++
value_ptr = self.parse_key(&self.root_table, False)
if value_ptr == NULL:
if strcmp(self.error, "invalid key") == 0:
self.error = "invalid header"
return False
if value_ptr.lineno == 0:
value_ptr.lineno = self.lineno
if is_array:
match value_ptr.type:
case TOMLType.Error:
# Just created for this
#
# Unlike with tables, we set created_by immediately, not
# when we are done with populating the table. That doesn't
# really matter. In fact, we could just leave created_by
# unset here, but this feels like it makes more sense.
value_ptr.type = TOMLType.Array
value_ptr.created_by = CreatedBy.Section
case TOMLType.Array:
if value_ptr.created_by == CreatedBy.Inline:
self.error = "inline arrays cannot be extended after creating the array"
return False
case _:
self.error = "cannot redefine an existing value as an array"
return False
if not self.check_depth(value_ptr.depth + 1):
return False
item: TOML
self.init_value(&item, TOMLType.Table, value_ptr.depth + 1)
value_ptr.array.append(item)
self.current_table = &value_ptr.array.end()[-1]
else:
match value_ptr.type:
case TOMLType.Error:
# Just created for this
value_ptr.type = TOMLType.Table
case TOMLType.Table:
# Add more stuff to an existing table
if value_ptr.created_by != CreatedBy.Unset:
self.error = "table already exists"
return False
case _:
self.error = "cannot redefine an existing value as a table"
return False
self.current_table = value_ptr
while *self.ptr == ' ' or *self.ptr == '\t':
self.ptr++
suffix = "]]" if is_array else "]"
if starts_with(self.ptr, suffix) and looks_like_end_of_line(&self.ptr[strlen(suffix)]):
self.ptr = &self.ptr[strlen(suffix)]
return True
else:
self.error = "invalid header"
return False
def whitespace_comment_newline(self) -> bool:
while True:
match *self.ptr:
case ' ' | '\t':
self.ptr++
case '\n':
self.ptr++
self.lineno++
case '#':
if not self.skip_comment():
return False
case _:
return True
def parse_inline_array(self, result: TOML*, depth_of_array: int) -> bool:
assert *self.ptr == '['
self.ptr++
self.init_value(result, TOMLType.Array, depth_of_array)
if not self.whitespace_comment_newline():
return False
while *self.ptr != ']':
if *self.ptr == '\0':
self.error = "missing ']' to end the array"
result.free()
return False
if not self.check_depth(depth_of_array + 1):
return False
value: TOML
if not self.parse_value(&value, depth_of_array + 1):
result.free()
return False
result.array.append(value)
if not self.whitespace_comment_newline():
result.free()
return False
match *self.ptr:
case ']':
break
case ',':
self.ptr++
if not self.whitespace_comment_newline():
result.free()
return False
case _:
self.error = "invalid array"
result.free()
return False
assert *self.ptr == ']'
self.ptr++
result.created_by = CreatedBy.Inline
return True
def parse_inline_table(self, result: TOML*, depth_of_table: int) -> bool:
assert *self.ptr == '{'
self.ptr++
self.init_value(result, TOMLType.Table, depth_of_table)
if not self.whitespace_comment_newline():
return False
while *self.ptr != '}':
if *self.ptr == '\0':
self.error = "missing '}' to end the table"
return False
keyval_ok = self.parse_key_equals_value(result)
if not keyval_ok:
result.free()
return False
if not self.whitespace_comment_newline():
result.free()
return False
match *self.ptr:
case '}':
break
case ',':
self.ptr++
self.whitespace_comment_newline()
case _:
self.error = "invalid inline table"
result.free()
return False
assert *self.ptr == '}'
self.ptr++
result.created_by = CreatedBy.Inline
return True
# Returns -1 if valid, line number if invalid
def validate_utf8(string: byte*) -> int:
lineno = 1
while *string != '\0':
if *string == '\n':
lineno++
if utf8_decode_char(&string) == -1:
return lineno
return -1
# Replaces \r\n with \n in-place
def fix_newlines(s: byte*) -> None:
out = s
while *s != '\0':
if starts_with(s, "\r\n"):
s++
*out++ = *s++
*out = '\0'
@public
def parse_toml(string: byte*) -> TOML:
assert string != NULL
utf8_error_lineno = validate_utf8(string)
if utf8_error_lineno != -1:
return TOML{type = TOMLType.Error, error_message = "invalid UTF-8", lineno = utf8_error_lineno}
string = strdup(string)
assert string != NULL
fix_newlines(string)
parser = TOMLParser{ptr = string, root_table = TOML{type = TOMLType.Table, lineno = 1}, lineno = 1}
parser.current_table = &parser.root_table # the "default section"
ok = parser.parse_the_whole_thing()
free(string)
if ok:
return parser.root_table
else:
parser.root_table.free()
return TOML{
type = TOMLType.Error,
error_message = parser.error,
lineno = parser.lineno,
}
@public
def toml_to_int(toml: TOML*, fallback: int) -> int:
if toml == NULL:
return fallback
match toml.type:
case TOMLType.Integer:
if toml.integer < INT32_MIN or toml.integer > INT32_MAX:
return fallback
return toml.integer as int
case TOMLType.Floating:
if isfinite(toml.floating) and toml.floating == (toml.floating as int):
return toml.floating as int
return fallback
case _:
return fallback
@public
def toml_to_int64(toml: TOML*, fallback: int64) -> int64:
if toml == NULL:
return fallback
match toml.type:
case TOMLType.Integer:
return toml.integer
case TOMLType.Floating:
if isfinite(toml.floating) and toml.floating == (toml.floating as int64):
return toml.floating as int64
return fallback
case _:
return fallback
@public
def toml_to_double(toml: TOML*) -> double:
if toml == NULL:
return NAN
match toml.type:
case TOMLType.Integer:
return toml.integer as double
case TOMLType.Floating:
return toml.floating
case _:
return NAN
# Unlike with json_to_string(), you should NOT free() the result!
@public
def toml_to_string(toml: TOML*) -> byte*:
if toml == NULL or toml.type != TOMLType.String or strlen(toml.string) != toml.string_len:
return NULL
return toml.string
@public
def toml_is_true(toml: TOML*) -> bool:
return toml != NULL and toml.type == TOMLType.Boolean and toml.boolean
@public
def toml_is_false(toml: TOML*) -> bool:
return toml != NULL and toml.type == TOMLType.Boolean and not toml.boolean