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
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
//! TOML conversion support for Eure format.
//!
//! This crate provides conversion from TOML documents to Eure's [`SourceDocument`],
//! preserving section ordering.
//!
//! # Example
//!
//! ```
//! use eure_toml::to_source_document;
//!
//! let toml_str = r#"
//! [server]
//! host = "localhost"
//! port = 8080
//! "#;
//!
//! let source_doc = to_source_document(toml_str).unwrap();
//! ```
mod error;
mod query;
pub use error::TomlToEureError;
pub use query::{TomlToEureDocument, TomlToEureSource};
use eure_document::document::constructor::{DocumentConstructor, Scope};
use eure_document::identifier::Identifier;
use eure_document::path::{ArrayIndexKind, PathSegment};
use eure_document::source::{
ArrayElementSource, BindSource, BindingSource, Comment, EureSource, SectionBody,
SourceDocument, SourceKey, SourcePathSegment, Trivia,
};
use eure_document::text::{Language, Text};
use eure_document::value::ObjectKey;
use eure_document::value::PrimitiveValue;
use num_bigint::BigInt;
use toml_parser::decoder::Encoding;
use toml_parser::decoder::ScalarKind;
use toml_parser::parser::EventReceiver;
use toml_parser::{ErrorSink, ParseError, Source, Span};
/// Convert a TOML string to a SourceDocument.
///
/// This preserves:
/// - Section ordering (including interleaved `[table]` and `[[array]]` sections)
/// - All TOML values
pub fn to_source_document(toml_str: &str) -> Result<SourceDocument, TomlToEureError> {
let source = Source::new(toml_str);
let tokens: Vec<_> = source.lex().collect();
let mut converter = TomlParserConverter::new(source);
let mut errors = ErrorCollector::new();
toml_parser::parser::parse_document(&tokens, &mut converter, &mut errors);
if let Some(err) = errors.first_error() {
return Err(err);
}
converter.finish()
}
/// Error collector for toml_parser
struct ErrorCollector {
errors: Vec<TomlToEureError>,
}
impl ErrorCollector {
fn new() -> Self {
Self { errors: Vec::new() }
}
fn first_error(&self) -> Option<TomlToEureError> {
self.errors.first().cloned()
}
}
impl ErrorSink for ErrorCollector {
fn report_error(&mut self, error: ParseError) {
self.errors.push(TomlToEureError::ParseError {
message: format!("{:?}", error),
});
}
}
/// State for tracking current parsing context
#[derive(Debug, Clone)]
enum ValueContext {
/// At the root document level
Root,
/// Inside a [table] section
StdTable {
/// Trivia (comments/blank lines) before this section
trivia_before: Vec<Trivia>,
/// Path segments for this section
path: Vec<SourcePathSegment>,
/// Bindings collected for this section
bindings: Vec<BindingSource>,
/// Scope for the DocumentConstructor
scope: Scope,
},
/// Inside a [[array_table]] section
ArrayTable {
/// Trivia (comments/blank lines) before this section
trivia_before: Vec<Trivia>,
/// Path segments for this section
path: Vec<SourcePathSegment>,
/// Bindings collected for this section
bindings: Vec<BindingSource>,
/// Scope for the DocumentConstructor
scope: Scope,
},
/// Inside an inline table { }
InlineTable {
/// Scope for the DocumentConstructor
scope: Scope,
/// Binding path for this inline table (if it's a value)
binding_path: Vec<SourcePathSegment>,
},
/// Inside an array [ ]
Array {
/// Scope for the DocumentConstructor
scope: Scope,
/// Current element index
element_index: usize,
/// Binding path for this array (if it's a value)
binding_path: Vec<SourcePathSegment>,
/// Per-element trivia collected during parsing
element_sources: Vec<ArrayElementSource>,
/// Pending trivia for the next element
element_pending_trivia: Vec<Trivia>,
/// Whether the array was multi-line in the original TOML
is_multiline: bool,
/// Span end of the last element (for trailing comment detection)
last_element_span_end: Option<usize>,
},
}
/// Main converter from TOML to SourceDocument
struct TomlParserConverter<'a> {
/// The source TOML string
source: Source<'a>,
/// Document constructor for building EureDocument
constructor: DocumentConstructor,
/// Arena for EureSource blocks
sources: Vec<EureSource>,
/// Stack of parsing contexts
context_stack: Vec<ValueContext>,
/// Current key path being built (for dotted keys like `a.b.c`)
current_keys: Vec<(String, Option<Encoding>)>,
/// Whether we're currently parsing a key (before `=`)
parsing_key: bool,
/// Pending trivia to attach to the next item
pending_trivia: Vec<Trivia>,
/// Flag to track blank lines (consecutive newlines)
saw_newline: bool,
/// Array nodes that should be formatted multi-line
multiline_arrays: std::collections::HashSet<eure_document::document::NodeId>,
}
impl<'a> TomlParserConverter<'a> {
fn new(source: Source<'a>) -> Self {
// Create root EureSource
let sources = vec![EureSource::default()];
Self {
source,
constructor: DocumentConstructor::new(),
sources,
context_stack: vec![ValueContext::Root],
current_keys: Vec::new(),
parsing_key: false,
pending_trivia: Vec::new(),
saw_newline: false,
multiline_arrays: std::collections::HashSet::new(),
}
}
fn finish(mut self) -> Result<SourceDocument, TomlToEureError> {
// Close any remaining sections
self.close_current_section();
// Any remaining pending trivia becomes trailing trivia of the root source
if !self.pending_trivia.is_empty() {
self.sources[0].trailing_trivia = std::mem::take(&mut self.pending_trivia);
}
let mut source_doc = SourceDocument::new(self.constructor.finish(), self.sources);
source_doc.multiline_arrays = self.multiline_arrays;
Ok(source_doc)
}
fn current_context(&self) -> &ValueContext {
self.context_stack.last().unwrap()
}
fn current_context_mut(&mut self) -> &mut ValueContext {
self.context_stack.last_mut().unwrap()
}
/// Close the current section and add it to sources
fn close_current_section(&mut self) {
if let Some(context) = self.context_stack.pop() {
match context {
ValueContext::StdTable {
trivia_before,
path,
bindings,
scope,
} => {
self.constructor.end_scope(scope).expect("scope mismatch");
// Add section to root source
self.sources[0]
.sections
.push(eure_document::source::SectionSource {
trivia_before,
path,
body: SectionBody::Items {
value: None,
bindings,
},
trailing_comment: None,
});
}
ValueContext::ArrayTable {
trivia_before,
path,
bindings,
scope,
} => {
self.constructor.end_scope(scope).expect("scope mismatch");
// Add section to root source
self.sources[0]
.sections
.push(eure_document::source::SectionSource {
trivia_before,
path,
body: SectionBody::Items {
value: None,
bindings,
},
trailing_comment: None,
});
}
ValueContext::Root => {
// Don't pop root, push it back
self.context_stack.push(ValueContext::Root);
}
_ => {}
}
}
}
/// Decode a key from span
fn decode_key(&self, span: Span, encoding: Option<Encoding>) -> String {
let raw = self.source.get(span).expect("valid span");
let raw = toml_parser::Raw::new_unchecked(raw.as_str(), encoding, span);
let mut output = String::new();
let mut errors = ErrorCollector::new();
raw.decode_key(&mut output, &mut errors);
output
}
/// Decode a scalar value from span
fn decode_scalar(&self, span: Span, encoding: Option<Encoding>) -> (ScalarKind, String) {
let raw = self.source.get(span).expect("valid span");
let raw = toml_parser::Raw::new_unchecked(raw.as_str(), encoding, span);
let mut output = String::new();
let mut errors = ErrorCollector::new();
let kind = raw.decode_scalar(&mut output, &mut errors);
(kind, output)
}
/// Parse a key string into SourceKey and PathSegment
fn parse_key(&self, key: &str) -> (SourceKey, PathSegment) {
match key.parse::<Identifier>() {
Ok(id) => (SourceKey::Ident(id.clone()), PathSegment::Ident(id)),
Err(_) => (
SourceKey::quoted(key.to_string()),
PathSegment::Value(ObjectKey::String(key.to_string())),
),
}
}
/// Create a SourcePathSegment from a SourceKey
fn source_path_segment(&self, key: SourceKey) -> SourcePathSegment {
SourcePathSegment { key, array: None }
}
/// Check if there's a newline between two byte positions in the source
fn has_newline_between(&self, start: usize, end: usize) -> bool {
if let Some(raw) = self.source.get(Span::new_unchecked(start, end)) {
raw.as_str().contains('\n')
} else {
// If we can't get the slice, assume there's a newline to be safe
true
}
}
/// Navigate to the key path and bind a value
fn bind_value(&mut self, value: PrimitiveValue) {
self.constructor
.bind_primitive(value)
.expect("binding should succeed");
}
/// Add a binding to the current context
fn add_binding(&mut self, path: Vec<SourcePathSegment>, node: eure_document::document::NodeId) {
// Don't consume trivia when in inline contexts (it should go to the outer binding)
match self.current_context() {
ValueContext::InlineTable { .. } | ValueContext::Array { .. } => {
// Inline structures don't track bindings in source
return;
}
_ => {}
}
// Attach pending trivia to this binding
let trivia_before = std::mem::take(&mut self.pending_trivia);
let binding = BindingSource {
trivia_before,
path,
bind: BindSource::Value(node),
trailing_comment: None,
};
match self.current_context_mut() {
ValueContext::Root => {
self.sources[0].bindings.push(binding);
}
ValueContext::StdTable { bindings, .. } | ValueContext::ArrayTable { bindings, .. } => {
bindings.push(binding);
}
ValueContext::InlineTable { .. } | ValueContext::Array { .. } => {
// Already handled above
}
}
}
/// Add an array binding with per-element trivia to the current context
fn add_array_binding(
&mut self,
path: Vec<SourcePathSegment>,
node: eure_document::document::NodeId,
elements: Vec<ArrayElementSource>,
) {
// Don't consume trivia when in inline contexts (it should go to the outer binding)
match self.current_context() {
ValueContext::InlineTable { .. } | ValueContext::Array { .. } => {
// Inline structures don't track bindings in source
return;
}
_ => {}
}
// Attach pending trivia to this binding
let trivia_before = std::mem::take(&mut self.pending_trivia);
let binding = BindingSource {
trivia_before,
path,
bind: BindSource::Array { node, elements },
trailing_comment: None,
};
match self.current_context_mut() {
ValueContext::Root => {
self.sources[0].bindings.push(binding);
}
ValueContext::StdTable { bindings, .. } | ValueContext::ArrayTable { bindings, .. } => {
bindings.push(binding);
}
ValueContext::InlineTable { .. } | ValueContext::Array { .. } => {
// Already handled above
}
}
}
/// Convert a scalar value to PrimitiveValue
fn scalar_to_primitive(
&self,
kind: ScalarKind,
value: &str,
encoding: Option<Encoding>,
) -> PrimitiveValue {
match kind {
ScalarKind::String => {
// Check if this is a multi-line string (TOML """ or ''')
let is_multiline = matches!(
encoding,
Some(Encoding::MlBasicString) | Some(Encoding::MlLiteralString)
);
if is_multiline {
// Use block text for multi-line strings
// Determine appropriate block level based on content
use eure_document::text::SyntaxHint;
let mut content = value.to_string();
if !content.ends_with('\n') {
content.push('\n');
}
// Find the minimum block level needed
let syntax_hint = if content.contains("``````") {
// Content has 6 backticks, can't safely delimit
// Use Block6 and hope for the best
SyntaxHint::Block6
} else if content.contains("`````") {
SyntaxHint::Block6
} else if content.contains("````") {
SyntaxHint::Block5
} else if content.contains("```") {
SyntaxHint::Block4
} else {
SyntaxHint::Block3
};
PrimitiveValue::Text(Text {
content,
language: Language::Implicit,
syntax_hint: Some(syntax_hint),
})
} else {
// Use plaintext for single-line strings
let text = Text::plaintext(value.to_string());
PrimitiveValue::Text(text)
}
}
ScalarKind::Boolean(b) => PrimitiveValue::Bool(b),
ScalarKind::Integer(_radix) => {
// Parse the integer, handling underscores
let clean: String = value.chars().filter(|c| *c != '_').collect();
let parsed = if clean.starts_with("0x") || clean.starts_with("0X") {
i64::from_str_radix(&clean[2..], 16)
} else if clean.starts_with("0o") || clean.starts_with("0O") {
i64::from_str_radix(&clean[2..], 8)
} else if clean.starts_with("0b") || clean.starts_with("0B") {
i64::from_str_radix(&clean[2..], 2)
} else {
clean.parse::<i64>()
};
match parsed {
Ok(n) => PrimitiveValue::Integer(BigInt::from(n)),
Err(_) => {
// i64 overflow: try parsing as BigInt for very large numbers
let n = clean.parse::<BigInt>().unwrap_or_else(|e| {
panic!("TOML parser validated integer '{clean}' failed to parse: {e}")
});
PrimitiveValue::Integer(n)
}
}
}
ScalarKind::Float => {
let clean: String = value.chars().filter(|c| *c != '_').collect();
if clean == "inf" || clean == "+inf" {
PrimitiveValue::F64(f64::INFINITY)
} else if clean == "-inf" {
PrimitiveValue::F64(f64::NEG_INFINITY)
} else if clean == "nan" || clean == "+nan" || clean == "-nan" {
PrimitiveValue::F64(f64::NAN)
} else {
let f = clean.parse::<f64>().unwrap_or_else(|e| {
panic!("TOML parser validated float '{clean}' failed to parse: {e}")
});
PrimitiveValue::F64(f)
}
}
ScalarKind::DateTime => {
// Determine the datetime type and create appropriate Text with language tag
let lang = if value.contains('T') || value.contains(' ') {
// Has date and time component (datetime)
"datetime"
} else if value.contains(':') {
// Time only
"time"
} else {
// Date only
"date"
};
PrimitiveValue::Text(Text::new(value.to_string(), Language::Other(lang.into())))
}
}
}
}
impl<'a> EventReceiver for TomlParserConverter<'a> {
fn std_table_open(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Reset newline tracking when we see new content
self.saw_newline = false;
// Close previous section if any
self.close_current_section();
// Reset key state
self.current_keys.clear();
self.parsing_key = true;
}
fn std_table_close(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Capture pending trivia for this section
let trivia_before = std::mem::take(&mut self.pending_trivia);
// Collect keys first to avoid borrow issues
let keys: Vec<_> = self.current_keys.drain(..).collect();
// Build the path from collected keys
let path: Vec<SourcePathSegment> = keys
.iter()
.map(|(key, _)| {
let (source_key, _) = self.parse_key(key);
self.source_path_segment(source_key)
})
.collect();
// Navigate to this path in the document
let scope = self.constructor.begin_scope();
// Navigate for each segment
for seg in &path {
let path_seg = match &seg.key {
SourceKey::Ident(id) => PathSegment::Ident(id.clone()),
SourceKey::String(s, _) => PathSegment::Value(ObjectKey::String(s.clone())),
_ => continue,
};
self.constructor
.navigate(path_seg)
.expect("navigation should succeed");
}
// Ensure it's a map
if self.constructor.current_node().content.is_hole() {
self.constructor
.bind_empty_map()
.expect("binding should succeed");
}
self.context_stack.push(ValueContext::StdTable {
trivia_before,
path,
bindings: Vec::new(),
scope,
});
self.parsing_key = false;
}
fn array_table_open(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Reset newline tracking when we see new content
self.saw_newline = false;
// Close previous section if any
self.close_current_section();
// Reset key state
self.current_keys.clear();
self.parsing_key = true;
}
fn array_table_close(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Capture pending trivia for this section
let trivia_before = std::mem::take(&mut self.pending_trivia);
// Build the path from collected keys with array marker
let keys: Vec<_> = self.current_keys.drain(..).collect();
let mut path: Vec<SourcePathSegment> = Vec::new();
for (i, (key, _)) in keys.iter().enumerate() {
let (source_key, _) = self.parse_key(key);
let mut seg = self.source_path_segment(source_key);
// Add array marker to last segment
if i == keys.len() - 1 {
seg = seg.with_array_push();
}
path.push(seg);
}
// Navigate to this path in the document
let scope = self.constructor.begin_scope();
for (i, (key, _)) in keys.iter().enumerate() {
let (_, path_seg) = self.parse_key(key);
self.constructor
.navigate(path_seg)
.expect("navigation should succeed");
if i == keys.len() - 1 {
// Last key - ensure it's an array and push new element
if self.constructor.current_node().content.is_hole() {
self.constructor
.bind_empty_array()
.expect("binding should succeed");
}
self.constructor
.navigate(PathSegment::ArrayIndex(ArrayIndexKind::Push))
.expect("array navigation should succeed");
}
}
// Ensure current position is a map
if self.constructor.current_node().content.is_hole() {
self.constructor
.bind_empty_map()
.expect("binding should succeed");
}
self.context_stack.push(ValueContext::ArrayTable {
trivia_before,
path,
bindings: Vec::new(),
scope,
});
self.parsing_key = false;
}
fn inline_table_open(&mut self, _span: Span, _error: &mut dyn ErrorSink) -> bool {
let scope = self.constructor.begin_scope();
// Build binding path before clearing keys
let binding_path: Vec<SourcePathSegment> = self
.current_keys
.iter()
.map(|(key, _)| {
let (source_key, _) = self.parse_key(key);
self.source_path_segment(source_key)
})
.collect();
// Navigate to the key path first
for (key, _) in &self.current_keys {
let (_, path_seg) = self.parse_key(key);
self.constructor
.navigate(path_seg)
.expect("navigation should succeed");
}
// Check if we're in an array context (values don't have keys)
if let Some(ValueContext::Array {
element_index,
element_pending_trivia,
element_sources,
..
}) = self.context_stack.last_mut()
{
self.constructor
.navigate(PathSegment::ArrayIndex(ArrayIndexKind::Push))
.expect("array navigation should succeed");
// Capture pending trivia for this element
let trivia = std::mem::take(element_pending_trivia);
let idx = *element_index;
element_sources.push(ArrayElementSource {
trivia_before: trivia,
index: idx,
trailing_comment: None,
});
*element_index += 1;
// Reset newline tracking - element newline shouldn't count as blank line
self.saw_newline = false;
}
self.constructor
.bind_empty_map()
.expect("binding should succeed");
self.context_stack.push(ValueContext::InlineTable {
scope,
binding_path,
});
self.current_keys.clear();
true
}
fn inline_table_close(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
if let Some(ValueContext::InlineTable {
scope,
binding_path,
}) = self.context_stack.pop()
{
let node_id = self.constructor.current_node_id();
self.constructor.end_scope(scope).expect("scope mismatch");
// Add binding if we have a path
if !binding_path.is_empty() {
self.add_binding(binding_path, node_id);
}
}
}
fn array_open(&mut self, _span: Span, _error: &mut dyn ErrorSink) -> bool {
let scope = self.constructor.begin_scope();
// Build binding path before clearing keys
let binding_path: Vec<SourcePathSegment> = self
.current_keys
.iter()
.map(|(key, _)| {
let (source_key, _) = self.parse_key(key);
self.source_path_segment(source_key)
})
.collect();
// Navigate to the key path first
for (key, _) in &self.current_keys {
let (_, path_seg) = self.parse_key(key);
self.constructor
.navigate(path_seg)
.expect("navigation should succeed");
}
// Check if we're in an array context (nested arrays)
// Handle pending trivia for this element from parent array
if let Some(ValueContext::Array {
element_index,
element_pending_trivia,
element_sources,
..
}) = self.context_stack.last_mut()
{
self.constructor
.navigate(PathSegment::ArrayIndex(ArrayIndexKind::Push))
.expect("array navigation should succeed");
let trivia = std::mem::take(element_pending_trivia);
let idx = *element_index;
*element_index += 1;
// Create element source for this nested array element
element_sources.push(ArrayElementSource {
trivia_before: trivia,
index: idx,
trailing_comment: None,
});
// Reset newline tracking - element newline shouldn't count as blank line
self.saw_newline = false;
}
self.constructor
.bind_empty_array()
.expect("binding should succeed");
self.context_stack.push(ValueContext::Array {
scope,
element_index: 0,
binding_path,
element_sources: Vec::new(),
element_pending_trivia: Vec::new(),
is_multiline: false,
last_element_span_end: None,
});
self.current_keys.clear();
true
}
fn array_close(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Reset newline tracking - newline after ] shouldn't count as blank line
self.saw_newline = false;
if let Some(ValueContext::Array {
scope,
binding_path,
element_sources,
is_multiline,
..
}) = self.context_stack.pop()
{
let node_id = self.constructor.current_node_id();
self.constructor.end_scope(scope).expect("scope mismatch");
// Track multiline arrays for formatting (even when inside inline contexts)
if is_multiline {
self.multiline_arrays.insert(node_id);
}
// Add binding if we have a path
if !binding_path.is_empty() {
// Use array binding if multiline or has element trivia to preserve formatting
let has_element_trivia = element_sources
.iter()
.any(|e| !e.trivia_before.is_empty() || e.trailing_comment.is_some());
if is_multiline || has_element_trivia {
self.add_array_binding(binding_path, node_id, element_sources);
} else {
self.add_binding(binding_path, node_id);
}
}
}
}
fn simple_key(&mut self, span: Span, kind: Option<Encoding>, _error: &mut dyn ErrorSink) {
let key = self.decode_key(span, kind);
self.current_keys.push((key, kind));
}
fn key_sep(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Dot separator between keys - keys are already being collected
}
fn key_val_sep(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Reset newline tracking when we see new content (key = value)
self.saw_newline = false;
// = separator - now we'll receive the value
self.parsing_key = false;
}
fn scalar(&mut self, span: Span, kind: Option<Encoding>, _error: &mut dyn ErrorSink) {
let (scalar_kind, value) = self.decode_scalar(span, kind);
let primitive = self.scalar_to_primitive(scalar_kind, &value, kind);
// Build path from current_keys
let path: Vec<SourcePathSegment> = self
.current_keys
.iter()
.map(|(key, _)| {
let (source_key, _) = self.parse_key(key);
self.source_path_segment(source_key)
})
.collect();
// Navigate to the path
let scope = self.constructor.begin_scope();
for (key, _) in &self.current_keys {
let (_, path_seg) = self.parse_key(key);
self.constructor
.navigate(path_seg)
.expect("navigation should succeed");
}
// Check if we're in an array context
if let Some(ValueContext::Array {
element_index,
element_pending_trivia,
element_sources,
..
}) = self.context_stack.last_mut()
{
// Navigate to array index
self.constructor
.navigate(PathSegment::ArrayIndex(ArrayIndexKind::Push))
.expect("array navigation should succeed");
// Capture pending trivia for this element
let trivia = std::mem::take(element_pending_trivia);
let idx = *element_index;
element_sources.push(ArrayElementSource {
trivia_before: trivia,
index: idx,
trailing_comment: None,
});
*element_index += 1;
// Reset newline tracking - element newline shouldn't count as blank line
self.saw_newline = false;
}
// Set span end for trailing comment detection
if let Some(ValueContext::Array {
last_element_span_end,
..
}) = self.context_stack.last_mut()
{
*last_element_span_end = Some(span.end());
}
let node_id = self.constructor.current_node_id();
self.bind_value(primitive);
self.constructor.end_scope(scope).expect("scope mismatch");
// Only add binding if we have a path (not in array context without keys)
if !path.is_empty() {
self.add_binding(path, node_id);
self.current_keys.clear();
}
}
fn value_sep(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Comma separator - clear keys for next item in inline table
if matches!(self.current_context(), ValueContext::InlineTable { .. }) {
self.current_keys.clear();
}
}
fn comment(&mut self, span: Span, _error: &mut dyn ErrorSink) {
// Decode the comment text
if let Some(raw) = self.source.get(span) {
let text = raw.as_str();
// Strip the leading # character
let content = text
.strip_prefix('#')
.map(|s| s.trim_start().to_string())
.unwrap_or_else(|| text.to_string());
let comment = Comment::Line(content);
// Check if we're in array context and if this is a trailing comment
let is_trailing_comment = if let Some(ValueContext::Array {
last_element_span_end: Some(elem_end),
element_sources,
..
}) = self.context_stack.last()
{
!element_sources.is_empty() && !self.has_newline_between(*elem_end, span.start())
} else {
false
};
if is_trailing_comment
// Set as trailing comment on the last array element
&& let Some(ValueContext::Array {
element_sources,
last_element_span_end,
..
}) = self.context_stack.last_mut()
&& let Some(last_elem) = element_sources.last_mut()
{
last_elem.trailing_comment = Some(comment);
*last_element_span_end = None; // Clear to prevent double assignment
self.saw_newline = false;
return;
}
// Route to element trivia if in array context
if let Some(ValueContext::Array {
element_pending_trivia,
..
}) = self.context_stack.last_mut()
{
element_pending_trivia.push(Trivia::Comment(comment));
} else {
self.pending_trivia.push(Trivia::Comment(comment));
}
}
self.saw_newline = false;
}
fn whitespace(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Ignore whitespace (but don't reset saw_newline)
}
fn newline(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Track blank lines (consecutive newlines)
if self.saw_newline {
let trivia = Trivia::BlankLine;
// Route to element trivia if in array context
if let Some(ValueContext::Array {
element_pending_trivia,
..
}) = self.context_stack.last_mut()
{
element_pending_trivia.push(trivia);
} else {
self.pending_trivia.push(trivia);
}
}
// Mark array as multiline when we see a newline inside it
if let Some(ValueContext::Array { is_multiline, .. }) = self.context_stack.last_mut() {
*is_multiline = true;
}
self.saw_newline = true;
}
fn error(&mut self, _span: Span, _error: &mut dyn ErrorSink) {
// Errors are collected by ErrorCollector
}
}
// Re-export formatting functions from eure-fmt
pub use eure_fmt::{build_source_doc, format_source_document};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple_key_value() {
let toml = r#"key = "value""#;
let result = to_source_document(toml);
assert!(result.is_ok());
let source = result.expect("conversion should succeed");
assert_eq!(source.root_source().bindings.len(), 1);
}
#[test]
fn test_section() {
let toml = r#"
[server]
host = "localhost"
port = 8080
"#;
let result = to_source_document(toml);
assert!(result.is_ok());
let source = result.expect("conversion should succeed");
// Should have one section
assert_eq!(source.root_source().sections.len(), 1);
}
#[test]
fn test_array_of_tables() {
let toml = r#"
[[items]]
name = "first"
[[items]]
name = "second"
"#;
let result = to_source_document(toml);
assert!(result.is_ok());
let source = result.expect("conversion should succeed");
// Should have two sections (one for each [[items]])
assert_eq!(source.root_source().sections.len(), 2);
}
#[test]
fn test_interleaved_sections() {
// With toml_parser, we should preserve the source order!
let toml = r#"
[[example]]
name = "first"
[metadata.first]
description = "First example"
[[example]]
name = "second"
[metadata.second]
description = "Second example"
"#;
let result = to_source_document(toml);
assert!(result.is_ok());
let source = result.expect("conversion should succeed");
// toml_parser preserves order: [[example]], [metadata.first], [[example]], [metadata.second]
assert_eq!(source.root_source().sections.len(), 4);
}
#[test]
fn test_quoted_string_key() {
// Keys that are not valid identifiers should be converted to quoted strings
let toml = r#""invalid key with spaces" = "value""#;
let result = to_source_document(toml);
assert!(result.is_ok());
// Verify the source document uses a quoted string key
let source_doc = result.unwrap();
let formatted = format_source_document(&source_doc);
assert!(
formatted.contains(r#""invalid key with spaces""#),
"Expected quoted key in output: {}",
formatted
);
}
#[test]
fn test_numeric_key() {
// Keys starting with numbers should be converted to quoted strings
let toml = r#"[features]
2d = ["value"]"#;
let result = to_source_document(toml);
assert!(result.is_ok());
let source_doc = result.unwrap();
let formatted = format_source_document(&source_doc);
assert!(
formatted.contains(r#""2d""#),
"Expected quoted key in output: {}",
formatted
);
}
}