@@ -362,7 +362,12 @@ pub fn (mut p Parser) root_table() ? {
362
362
t := p.find_table () ?
363
363
unsafe {
364
364
util.printdbg (@MOD + '.' + @STRUCT + '.' + @FN, 'setting "$key.str ()" = $val.to_json () in table ${ptr_str(t)} ' )
365
- t[key.str ()] = val
365
+ key_str := key.str ()
366
+ if _ := t[key_str] {
367
+ return error (@MOD + '.' + @STRUCT + '.' + @FN +
368
+ ' key "$key " is already initialized with a value. At "$p.tok.kind " "$p.tok.lit " in this (excerpt): "...${p.excerpt()} ..."' )
369
+ }
370
+ t[key_str] = val
366
371
}
367
372
}
368
373
}
@@ -549,7 +554,11 @@ pub fn (mut p Parser) array_of_tables(mut table map[string]ast.Value) ? {
549
554
}
550
555
}
551
556
p.last_aot = key_str
552
- p.last_aot_index = 0
557
+
558
+ unsafe {
559
+ arr := & (table[p.last_aot] as []ast.Value)
560
+ p.last_aot_index = arr.len - 1
561
+ }
553
562
}
554
563
555
564
// double_array_of_tables parses next tokens into an array of tables of arrays of `ast.Value`s...
@@ -570,31 +579,44 @@ pub fn (mut p Parser) double_array_of_tables(mut table map[string]ast.Value) ? {
570
579
p.check (.rsbr) ?
571
580
p.check (.rsbr) ?
572
581
582
+ p.ignore_while (parser.all_formatting)
583
+
573
584
ks := key_str.split ('.' )
574
585
575
586
if ks.len != 2 {
576
587
return error (@MOD + '.' + @STRUCT + '.' + @FN +
577
588
' nested array of tables does not support more than 2 levels. (excerpt): "...${p.excerpt()} ..."' )
578
589
}
579
590
580
- first := ks[0 ]
581
- last := ks[1 ]
591
+ first := ks[0 ] // The array that holds the entries
592
+ last := ks[1 ] // The key the parsed array data should be added to
593
+
594
+ mut t_arr := & []ast.Value (0 )
595
+ mut t_map := ast.Value (ast.Null{})
582
596
583
597
unsafe {
584
598
// NOTE this is starting to get EVEN uglier. TOML is not at all simple at this point...
585
- if p.last_aot != first {
586
- table[first] = []ast.Value{}
587
- p.last_aot = first
588
- mut t_arr := & (table[p.last_aot] as []ast.Value)
589
- t_arr << map [string ]ast.Value{}
590
- p.last_aot_index = 0
599
+ if first != p.last_aot {
600
+ // Implicit allocation
601
+ if p.last_aot == '' {
602
+ util.printdbg (@MOD + '.' + @STRUCT + '.' + @FN, 'implicit allocation of array for nested key `$key_str `.' )
603
+ table[first] = []ast.Value{}
604
+ p.last_aot = first
605
+ t_arr = & (table[p.last_aot] as []ast.Value)
606
+ t_arr << ast.Value (map [string ]ast.Value{})
607
+ p.last_aot_index = t_arr.len - 1
608
+ } else {
609
+ return error (@MOD + '.' + @STRUCT + '.' + @FN +
610
+ ' nested array of tables key "$first " does not match "$p.last_aot ". (excerpt): "...${p.excerpt()} ..."' )
611
+ }
591
612
}
592
613
593
- mut t_arr : = & (table[p.last_aot] as []ast.Value)
594
- mut t_map : = ast.Value (map [string ]ast.Value{})
595
- if t_arr.len > 0 {
614
+ t_arr = & (table[p.last_aot] as []ast.Value)
615
+ t_map = ast.Value (map [string ]ast.Value{})
616
+ if p.last_aot_index < t_arr.len {
596
617
t_map = t_arr[p.last_aot_index]
597
618
}
619
+
598
620
mut t := & (t_map as map [string ]ast.Value)
599
621
600
622
if last in t.keys () {
@@ -617,7 +639,7 @@ pub fn (mut p Parser) double_array_of_tables(mut table map[string]ast.Value) ? {
617
639
}
618
640
if t_arr.len == 0 {
619
641
t_arr << t
620
- p.last_aot_index = 0
642
+ p.last_aot_index = t_arr.len - 1
621
643
}
622
644
}
623
645
}
0 commit comments