babbel_yaml 0.1.2

Fast, modular YAML 1.2 parser and emitter with anchors, aliases, and tags
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
//! Inline Token Parsing Helpers
//!
//! Provides parsing logic and helpers for handling inline YAML collections (sequences and mappings),
//! including special cases like double-colon scalars. All error construction uses centralized helpers.
//!
//! Copyright (c) 2026 YAML Library Developers

// DRY NOTE: All error construction in this file must use centralized helpers from error_builder.rs (e.g., syntax_error, structure_error, mapping_key_error_yaml, etc.).
// Do not return raw error strings or construct errors directly.
/// DRY ENTRY POINT: Parses a value or key in inline collections, handling special cases like double-colon scalars.
///
/// Used by both sequence and mapping parsers. All inline value parsing must use this function.
fn parse_inline_value(
    stream: &mut TokenStream,
    directives: &DirectiveContext,
    depth: usize,
) -> crate::parser::ParseResult<Node> {
    // Special-case a leading double-colon inside a flow sequence
    if matches!(stream.current(), Some(Token::Colon)) {
        if let Some(Token::Colon) = stream.peek()? {
            stream.next()?; // first ':'
            stream.next()?; // second ':'
            skip_inline_trivia(stream)?;
            match stream.consume_scalar() {
                Ok((s, _)) => Ok(Node::Str(
                    format!("::{}", s),
                    QuoteType::Unquoted,
                    BlockStyle::None,
                )),
                Err(e) => {
                    // Centralize error: treat as empty scalar, but log error for debugging
                    log::debug!("Failed to parse scalar after double colon: {}", e);
                    Ok(Node::Str(
                        "::".to_string(),
                        QuoteType::Unquoted,
                        BlockStyle::None,
                    ))
                }
            }
        } else {
            parse_value_with_tokens(stream, directives, depth + 1)
        }
    } else {
        parse_value_with_tokens(stream, directives, depth + 1)
    }
}
/// DRY ENTRY POINT: Constructs a Node::Array from a vector of items.
///
/// All array node construction in inline parsing must use this helper.
fn make_array_node(items: Vec<Node>) -> Node {
    Node::Array(items)
}

/// DRY ENTRY POINT: Constructs a Node::Mapping from a vector of key-value pairs.
///
/// All mapping node construction in inline parsing must use this helper.
fn make_mapping_node(pairs: Vec<(Node, Node)>) -> Node {
    Node::Mapping(pairs)
}
/// DRY ENTRY POINT: Skips whitespace and comments in the token stream.
///
/// All trivia (whitespace/comment) skipping in inline parsing must use this helper before parsing any token.
fn skip_inline_trivia(stream: &mut TokenStream) -> crate::parser::ParseResult<()> {
    stream.skip_trivia()
}
/// Token-based flow collection parsers
///
/// DRY: All inline YAML collection parsing (sequences and mappings) uses token-based helpers for clarity and error handling.
use crate::nodes::node::Node;
use crate::nodes::node_utils::make_set_node;
use crate::parser::directives::DirectiveContext;
use crate::parser::lexer::Token;
use crate::parser::token_stream::TokenStream;
use crate::parser::tokens::value::parse_value_with_tokens;
use crate::{BlockStyle, QuoteType};

#[cfg(feature = "debug-trace")]
#[inline]
fn inline_log(msg: String) {
    #[cfg(feature = "std")]
    {
        if let Ok(v) = std::env::var("YAML_TRACE_INLINE") {
            if v.eq_ignore_ascii_case("1")
                || v.eq_ignore_ascii_case("true")
                || v.eq_ignore_ascii_case("on")
            {
                log::debug!("{}", msg);
                return;
            }
        }
    }
    log::trace!("{}", msg);
}

/// DRY ENTRY POINT: Parses a flow (inline) sequence using tokens.
///
/// Example: `[1, 2, 3]` or `[a, b, c]`
///
/// Handles:
/// - Empty sequences: `[]`
/// - Trailing commas: `[1, 2, ]`
/// - Nested collections: `[[1, 2], [3, 4]]`
/// - Mixed types: `[1, "str", true]`
/// - Implicit mappings and double-colon scalars
///
/// All inline sequence parsing must use this function.
pub fn parse_inline_sequence_with_tokens(
    stream: &mut TokenStream,
    directives: &DirectiveContext,
    depth: usize,
    outer_block_indent: Option<usize>,
) -> crate::parser::ParseResult<Node> {
    #[cfg(feature = "debug-trace")]
    log::debug!(
        "inline_tokens: start flow sequence at token = {:?}",
        stream.current()
    );
    // Always skip trivia before starting parsing
    skip_inline_trivia(stream)?;
    // Expect opening bracket
    stream.expect(Token::FlowSequenceStart)?;

    use crate::utils::optimization::{CapacityHints, NodeBuilder};
    // Use a small capacity profile for typical inline sequences
    let node_builder = NodeBuilder::with_hints(CapacityHints::small());
    let mut items = Vec::with_capacity(node_builder.hints().sequence_items);
    let mut expect_item = true; // After [ or comma, we expect an item

    loop {
        // Skip whitespace/comments
        skip_inline_trivia(stream)?;

        match stream.current() {
            Some(Token::FlowSequenceEnd) => {
                // Closing bracket - done
                let _ = stream.consume_flow_sequence_end()?;
                // If at top-level (depth == 0), check for extra closing bracket (4H7K)
                if depth == 0 {
                    skip_inline_trivia(stream)?;
                    if matches!(stream.current(), Some(Token::FlowSequenceEnd)) {
                        return Err(crate::parser::document::flow_punctuation::unexpected_extra_closing_bracket_in_flow_sequence(stream));
                    }
                }
                break;
            }
            Some(Token::Comma) => {
                if expect_item {
                    // Comma found when expecting an item: leading or double comma
                    return Err(crate::parser::document::flow_punctuation::leading_or_double_comma_in_flow_sequence(stream));
                }
                // Allow trailing comma: set to expect next item, but do not error
                // If immediately followed by ']', the loop will close cleanly
                let _ = stream.consume_if(Token::Comma)?;
                expect_item = true;
            }
            None | Some(Token::Eof) => {
                return Err(
                    crate::parser::document::flow_punctuation::unexpected_eof_in_flow_sequence(
                        stream,
                    ),
                );
            }
            _ => {
                if !expect_item {
                    // Found value without comma separator
                    // DRY: use centralized helper to emit identical error
                    if let Err(e) =
                        crate::parser::document::flow_punctuation::ensure_separator_or_end(
                            stream,
                            crate::parser::document::flow_punctuation::FlowContext::Sequence,
                            Token::FlowSequenceEnd,
                        )
                    {
                        return Err(e);
                    }
                }

                // 9C9N: Reject flow sequence content that appears on a new line at
                // the outer block context's indentation level (column 0 when the mapping
                // key is at column 0).  Same rule as VJP3/00 for flow mappings.
                if outer_block_indent.is_some() && stream.is_preceded_by_linebreak() {
                    return Err(crate::parser::utils::error_builder::indentation_error(
                        stream.source_mut(),
                        "Flow collection content must be indented more than enclosing block context",
                    ));
                }

                // Parse what might be a value or the key of an implicit mapping.
                // Special-case a leading double-colon inside a flow sequence
                // (e.g., `::vector` in DBG4). The lexer tokenizes this as
                // `Colon, Colon, Plain("vector")`, which would normally be
                // misinterpreted as an implicit mapping and raise
                // "Expected comma or ] in flow sequence". Instead, treat it
                // as a single plain scalar "::vector" when it appears at the
                // start of a sequence item.
                //
                // DK4H: record the start-of-scan source line for the current
                // token (the potential key) BEFORE calling parse_inline_value.
                // In flow context scan_plain_scalar folds newlines internally
                // and increments source_line while scanning, so by the time
                // Token::Plain("key") is visible as current, source_line has
                // ALREADY been incremented past the fold.  current_token_start_line()
                // captures where that scan BEGAN (before any fold), letting us
                // compare it against source_line() after the key is consumed to
                // detect a crossed-line separator even in suppressed-newline context.
                let key_start_line = stream.current_token_start_line();
                let value_or_key = parse_inline_value(stream, directives, depth)?;

                // Skip inline trivia to check if this is actually a key
                // (followed by a colon) for an implicit mapping. Per YAML
                // 1.2, the `key: value` pair in a flow sequence must keep
                // the colon on the same logical line as the key. If a
                // newline/indent appears before the colon (as in ZXT5:
                // `[ "key"\n  :value ]`), this must *not* be treated as an
                // implicit mapping and should instead trigger a sequence
                // syntax error when the stray ':' is encountered.
                let mut saw_line_break = false;
                loop {
                    match stream.current() {
                        Some(Token::Newline) | Some(Token::Indent(_)) => {
                            saw_line_break = true;
                            stream.next()?;
                        }
                        Some(Token::Comment(_)) => {
                            stream.next()?;
                        }
                        _ => break,
                    }
                }

                // DK4H: also detect multiline key via source_line counter.
                // In flow context Token::Newline is suppressed, so
                // saw_line_break is never set. But source_line() still
                // increments for every real newline, so if it advanced
                // between key_start_line and now the key spanned a line break.
                let crossed_line = saw_line_break || stream.source_line() > key_start_line;

                // Check if this is an implicit mapping (key: value in a flow sequence),
                // but only when the colon is on the same line (no intervening newline/indent).
                if !crossed_line && matches!(stream.current(), Some(Token::Colon)) {
                    // This is actually a key, not a standalone value
                    // Parse as a single-pair mapping
                    let _ = stream.consume_if(Token::Colon)?; // consume colon
                    skip_inline_trivia(stream)?;

                    // Parse the value (or use None if followed by comma/bracket)
                    let val = if matches!(
                        stream.current(),
                        Some(Token::Comma) | Some(Token::FlowSequenceEnd)
                    ) {
                        Node::None
                    } else {
                        parse_value_with_tokens(stream, directives, depth + 1)?
                    };

                    // Create a single-pair mapping
                    let mapping = make_mapping_node(vec![(value_or_key, val)]);
                    #[cfg(feature = "debug-trace")]
                    log::debug!(
                        "inline_tokens: seq item (implicit mapping) -> {:?}",
                        mapping
                    );
                    items.push(mapping);
                } else if crossed_line && matches!(stream.current(), Some(Token::Colon)) {
                    // DK4H: the key and its ':' separator are on different source
                    // lines — implicit keys in flow sequences must be single-line
                    // (YAML spec §7.4.1 / §8.1.1).
                    return Err(crate::parser::utils::error_builder::syntax_error(
                        stream.source_mut(),
                        "Implicit key in flow sequence spans multiple lines \
                         (key and ':' separator must be on the same line)",
                    ));
                } else {
                    // It's a regular value
                    #[cfg(feature = "debug-trace")]
                    log::debug!("inline_tokens: seq item -> {:?}", value_or_key);
                    items.push(value_or_key);
                }
                expect_item = false;
            }
        }
    }
    #[cfg(feature = "debug-trace")]
    log::debug!(
        "inline_tokens: end flow sequence with {} item(s)",
        items.len()
    );
    // Special-case invalid flow sequence entries that are bare '-' scalars.
    // Extend rule to reject any flow sequence where all entries are the bare
    // string "-" (including single-entry sequences like `[-]`).
    if !items.is_empty() {
        let all_bare_dashes = items.iter().all(|n| match n {
            Node::Str(s, ..) => s == "-",
            _ => false,
        });
        if all_bare_dashes {
            return Err(
                crate::parser::document::flow_punctuation::invalid_bare_dash_entries_in_flow_sequence(
                    stream,
                ),
            );
        }
    }
    Ok(make_array_node(items))
}

/// DRY ENTRY POINT: Parse a flow (inline) mapping using tokens
///
/// Example: `{a: 1, b: 2}` or `{key: value}`
///
/// Handles:
/// - Empty mappings: `{}`
/// - Trailing commas: `{a: 1, b: 2, }`
/// - Nested collections: `{a: {b: c}}`
/// - Quoted keys: `{"key": value}`
///
/// All inline mapping parsing must use this function.
pub fn parse_inline_mapping_with_tokens(
    stream: &mut TokenStream,
    directives: &DirectiveContext,
    depth: usize,
    is_set: bool,
    outer_block_indent: Option<usize>,
) -> crate::parser::ParseResult<Node> {
    #[cfg(feature = "debug-trace")]
    inline_log(format!(
        "ENTER parse_inline_mapping_with_tokens, current token: {:?}",
        stream.current()
    ));
    // Always skip trivia before starting parsing
    skip_inline_trivia(stream)?;
    // Expect opening brace
    stream.expect(Token::FlowMappingStart)?;

    use crate::utils::optimization::{CapacityHints, NodeBuilder};
    // Use a small capacity profile for typical inline mappings
    let node_builder = NodeBuilder::with_hints(CapacityHints::small());
    let mut pairs = Vec::with_capacity(node_builder.hints().mapping_pairs);
    let mut expect_entry = true; // After { or comma, we expect a key

    let mut iteration = 0;
    loop {
        iteration += 1;
        if iteration > 1000 {
            #[cfg(feature = "debug-trace")]
            inline_log(
                "Exceeded 1000 iterations in parse_inline_mapping_with_tokens, possible infinite loop"
                    .to_string(),
            );
            return Err(crate::parser::utils::error_builder::limit_error(
                "flow mapping parser",
                1000,
                "loop iterations",
            ));
        }
        // Skip whitespace/comments
        skip_inline_trivia(stream)?;

        #[cfg(feature = "debug-trace")]
        inline_log(format!(
            "Iteration {}, current token: {:?}",
            iteration,
            stream.current()
        ));
        match stream.current() {
            Some(Token::FlowMappingEnd) => {
                // Closing brace - done
                let _ = stream.consume_flow_mapping_end()?;
                break;
            }
            Some(Token::Comma) => {
                // Allow trailing comma: set to expect next entry, but do not error
                // If immediately followed by '}', the loop will close cleanly
                let _ = stream.consume_if(Token::Comma)?;
                expect_entry = true;
            }
            None | Some(Token::Eof) => {
                // Regression fix: produce a syntax error with 'Syntax error' in the message for unclosed flow mapping
                return Err(crate::parser::document::flow_punctuation::unexpected_eof_in_flow_mapping_unclosed(stream));
            }
            _ => {
                if !expect_entry {
                    // After a key-value pair, allow either a comma separator or closing brace.
                    // DRY: use centralized helper to validate next token is separator or end
                    match crate::parser::document::flow_punctuation::ensure_separator_or_end(
                        stream,
                        crate::parser::document::flow_punctuation::FlowContext::Mapping,
                        Token::FlowMappingEnd,
                    ) {
                        Err(e) => return Err(e),
                        Ok(()) => {
                            if matches!(stream.current(), Some(Token::FlowMappingEnd)) {
                                let _ = stream.consume_flow_mapping_end()?;
                                break;
                            }
                            if matches!(stream.current(), Some(Token::Comma)) {
                                let _ = stream.consume_if(Token::Comma)?;
                                expect_entry = true;
                                continue;
                            }
                        }
                    }
                }

                // VJP3/00: Reject flow collection content that appears on a new line at
                // the outer block context's indentation level.  The YAML spec requires flow
                // content on continuation lines to be indented MORE than the enclosing block
                // context (parameter n).  Detection: if `last_was_linebreak` is still true
                // at this point, no Indent token was emitted since the last newline, which
                // means the content sits at column 0 on a fresh line — violating the rule
                // when there is an enclosing block mapping context (outer_block_indent is Some).
                if outer_block_indent.is_some() && stream.is_preceded_by_linebreak() {
                    return Err(crate::parser::utils::error_builder::indentation_error(
                        stream.source_mut(),
                        "Flow collection content must be indented more than enclosing block context",
                    ));
                }

                // Parse the mapping key. Special-case an empty key in flow
                // context where the entry starts with ':' (e.g. `{ : value }`).
                // In that case, the key is the empty string and the ':'
                // remains for the separator logic below.
                let key = if matches!(stream.current(), Some(Token::Colon)) {
                    #[cfg(feature = "debug-trace")]
                    inline_log("Empty key in flow mapping (starting with ':')".to_string());
                    Node::Str(String::new(), QuoteType::Unquoted, BlockStyle::None)
                } else {
                    let before_key = stream.stream_position();
                    #[cfg(feature = "debug-trace")]
                    inline_log(format!("before_key position = {}", before_key));
                    let key = parse_inline_value(stream, directives, depth)?;
                    let after_key = stream.stream_position();
                    #[cfg(feature = "debug-trace")]
                    inline_log(format!("after_key position = {}", after_key));
                    ensure_progress(stream, before_key, after_key, "key in flow mapping")?;
                    key
                };

                // Skip whitespace
                skip_inline_trivia(stream)?;

                // Debug: print current token before colon check
                #[cfg(feature = "debug-trace")]
                inline_log(format!(
                    "Before colon check, current token: {:?}",
                    stream.current()
                ));
                // Ensure all comments and newlines are skipped before colon check
                skip_inline_trivia(stream)?;
                // Expect colon for key-value pair
                if matches!(stream.current(), Some(Token::Colon)) {
                    // DRY: consume single colon with compliance validation (no behavior change)
                    let _ = stream.consume_single_colon()?;
                    // No need to call skip_trivia twice; one call above suffices

                    // Progress check: record position before parsing value
                    let before_value = stream.stream_position();
                    #[cfg(feature = "debug-trace")]
                    inline_log(format!("before_value position = {}", before_value));

                    // Special case: a second ':' immediately after the key-value separator
                    // should be treated as part of the plain value (e.g., {"key"::value} => ":value").
                    if matches!(stream.current(), Some(Token::Colon)) {
                        let _ = stream.consume_if(Token::Colon)?; // consume leading ':' of value
                        skip_inline_trivia(stream)?;
                        // Consume the following scalar and prepend ':'
                        match stream.consume_scalar() {
                            Ok((s, _)) => {
                                let combined = format!(":{}", s);
                                pairs.push((
                                    key,
                                    Node::Str(combined, QuoteType::Unquoted, BlockStyle::None),
                                ));
                                expect_entry = false;
                                continue;
                            }
                            Err(_) => {
                                // No scalar follows; treat value as just ':'
                                pairs.push((
                                    key,
                                    Node::Str(
                                        ":".to_string(),
                                        QuoteType::Unquoted,
                                        BlockStyle::None,
                                    ),
                                ));
                                expect_entry = false;
                                continue;
                            }
                        }
                    }

                    // Check for empty value (key: followed by , or })
                    let value = if matches!(
                        stream.current(),
                        Some(Token::Comma) | Some(Token::FlowMappingEnd)
                    ) {
                        // Empty value - use None (null)
                        Node::None
                    } else {
                        let val = parse_value_with_tokens(stream, directives, depth + 1)?;
                        let after_value = stream.stream_position();
                        #[cfg(feature = "debug-trace")]
                        inline_log(format!("after_value position = {}", after_value));
                        ensure_progress(
                            stream,
                            before_value,
                            after_value,
                            "value in flow mapping",
                        )?;
                        val
                    };
                    #[cfg(feature = "debug-trace")]
                    log::debug!("inline_tokens: map entry -> ({:?}, {:?})", key, value);

                    pairs.push((key, value));
                } else {
                    // In flow mappings, a key without a colon has an implicit null value
                    // This is valid in YAML 1.2: {key} is equivalent to {key: null}
                    #[cfg(feature = "debug-trace")]
                    log::debug!(
                        "inline_tokens: map entry with implicit null -> ({:?}, None)",
                        key
                    );
                    pairs.push((key, Node::None));
                }
                expect_entry = false;
            }
        }
    }
    #[cfg(feature = "debug-trace")]
    log::debug!(
        "inline_tokens: end flow mapping with {} pair(s)",
        pairs.len()
    );
    if is_set {
        // For !!set, convert mapping pairs with Node::None values to Node::Set
        if let Some(set_items) =
            crate::parser::utils::node_utils::pairs_to_set_items_if_all_none(&pairs)
        {
            Ok(make_set_node(set_items))
        } else {
            // Fallback to mapping for compatibility when any value isn't None
            Ok(make_mapping_node(pairs))
        }
    } else {
        Ok(make_mapping_node(pairs))
    }
}

/// DRY ENTRY POINT: Ensure that the token stream progressed between two checkpoints, else raise a syntax error.
///
/// All progress checking in inline parsing must use this helper.
fn ensure_progress(
    stream: &mut TokenStream,
    before: usize,
    after: usize,
    context: &str,
) -> crate::parser::ParseResult<()> {
    if before == after {
        // Regression fix: if at EOF, produce a syntax error for compatibility with error handling tests
        if matches!(
            stream.current(),
            None | Some(crate::parser::lexer::Token::Eof)
        ) {
            return Err(
                crate::parser::errors::token_errors::parser_did_not_advance_syntax(
                    stream.source_mut(),
                    context,
                ),
            );
        } else {
            return Err(
                crate::parser::errors::token_errors::parser_did_not_advance_structure(
                    stream.source_mut(),
                    context,
                ),
            );
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::io::sources::buffer::Buffer;
    use crate::parser::directives::DirectiveContext;

    #[test]
    fn test_empty_flow_sequence() {
        let yaml = b"[]";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();

        let result = parse_inline_sequence_with_tokens(&mut stream, &directives, 0, None).unwrap();

        if let Node::Array(items) = result {
            assert_eq!(items.len(), 0);
        } else {
            panic!("Expected Array node");
        }
    }

    #[test]
    fn test_simple_flow_sequence() {
        let yaml = b"[1, 2, 3]";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();

        let result = parse_inline_sequence_with_tokens(&mut stream, &directives, 0, None).unwrap();

        if let Node::Array(items) = result {
            assert_eq!(items.len(), 3);
        } else {
            panic!("Expected Array node");
        }
    }

    #[test]
    fn test_flow_sequence_trailing_comma() {
        let yaml = b"[1, 2, ]";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();

        let result = parse_inline_sequence_with_tokens(&mut stream, &directives, 0, None).unwrap();

        if let Node::Array(items) = result {
            assert_eq!(items.len(), 2);
        } else {
            panic!("Expected Array node");
        }
    }

    #[test]
    fn test_empty_flow_mapping() {
        let yaml = b"{}";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();

        let result =
            parse_inline_mapping_with_tokens(&mut stream, &directives, 0, false, None).unwrap();

        if let Node::Mapping(pairs) = result {
            assert_eq!(pairs.len(), 0);
        } else {
            panic!("Expected Mapping node");
        }
    }

    #[test]
    fn test_simple_flow_mapping() {
        let yaml = b"{a: 1, b: 2}";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();

        let result =
            parse_inline_mapping_with_tokens(&mut stream, &directives, 0, false, None).unwrap();

        if let Node::Mapping(pairs) = result {
            assert_eq!(pairs.len(), 2);
        } else {
            panic!("Expected Mapping node");
        }
    }

    #[test]
    fn test_flow_mapping_trailing_comma() {
        let yaml = b"{a: 1, b: 2, }";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();

        let result =
            parse_inline_mapping_with_tokens(&mut stream, &directives, 0, false, None).unwrap();

        if let Node::Mapping(pairs) = result {
            assert_eq!(pairs.len(), 2);
        } else {
            panic!("Expected Mapping node");
        }
    }

    #[test]
    fn test_nested_flow_collections() {
        let yaml = b"[[1, 2], [3, 4]]";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();

        let result = parse_inline_sequence_with_tokens(&mut stream, &directives, 0, None).unwrap();

        if let Node::Array(items) = result {
            assert_eq!(items.len(), 2);
            assert!(matches!(items[0], Node::Array(_)));
            assert!(matches!(items[1], Node::Array(_)));
        } else {
            panic!("Expected Array node");
        }
    }

    #[test]
    fn test_flow_sequence_double_colon_scalar() {
        // Ensure a leading double-colon inside a flow sequence (DBG4 pattern
        // "[ ::vector, ... ]") is parsed as a single plain scalar "::vector"
        // and does not trigger the "Expected comma or ] in flow sequence" error.
        let yaml = b"[ ::vector ]";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();

        let result = parse_inline_sequence_with_tokens(&mut stream, &directives, 0, None).unwrap();

        if let Node::Array(items) = result {
            assert_eq!(items.len(), 1);
            assert!(matches!(
                &items[0],
                Node::Str(s, _, _) if s == "::vector"
            ));
        } else {
            panic!("Expected Array node");
        }
    }
    #[test]
    fn test_flow_sequence_invalid_token() {
        let yaml = b"[1, 2, @]";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();
        let result = parse_inline_sequence_with_tokens(&mut stream, &directives, 0, None).unwrap();
        if let Node::Array(items) = result {
            assert_eq!(items.len(), 3);
            assert!(matches!(&items[2], Node::Str(s, _, _) if s == "@"));
        } else {
            panic!("Expected Array node");
        }
    }

    #[test]
    fn test_flow_mapping_unexpected_end() {
        let yaml = b"{a: 1, b:";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();
        let result = parse_inline_mapping_with_tokens(&mut stream, &directives, 0, false, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_flow_sequence_mixed_types() {
        let yaml = b"[1, {a: 2}, [3, 4]]";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();
        let result = parse_inline_sequence_with_tokens(&mut stream, &directives, 0, None);
        match result {
            Ok(Node::Array(items)) => {
                assert_eq!(items.len(), 3);
                assert!(matches!(&items[0], Node::Number(_)));
                assert!(matches!(&items[1], Node::Mapping(_)));
                assert!(matches!(&items[2], Node::Array(_)));
            }
            Err(e) => {
                // If parser does not support mixed types, print error for diagnosis
                println!("Parser error: {}", e);
                assert!(false, "Parser failed to handle mixed types: {}", e);
            }
            _ => {
                assert!(false, "Unexpected node type returned");
            }
        }
    }

    #[test]
    fn test_double_colon_scalar_error_handling() {
        let yaml = b"[::]";
        let mut source = Buffer::new(yaml);
        let directives = DirectiveContext::new();
        let mut stream = TokenStream::new(&mut source, &directives, false).unwrap();
        let result = parse_inline_sequence_with_tokens(&mut stream, &directives, 0, None).unwrap();
        if let Node::Array(items) = result {
            assert_eq!(items.len(), 1);
            assert!(matches!(&items[0], Node::Str(s, _, _) if s == "::"));
        } else {
            panic!("Expected Array node");
        }
    }

    #[test]
    fn test_dk4h_multiline_implicit_key_in_flow_seq_should_error() {
        // DK4H: in a flow sequence, an implicit key is separated from its
        // ':' by a newline — this is invalid (YAML spec §7.4.1 implicit keys
        // must be single-line). The lexer suppresses Token::Newline in flow
        // context, so we detect this via the token_start_source_line counter.
        let yaml = "---\n[ key\n  : value ]\n";
        let config = crate::parser::config::ParserConfig::strict();
        let result = crate::parse_with_config(yaml, config);
        assert!(
            result.is_err(),
            "DK4H should fail: implicit key in flow sequence with colon on next line, got: {:?}",
            result
        );
    }

    #[test]
    fn test_same_line_implicit_key_in_flow_seq_should_succeed() {
        // Verify that a same-line implicit key in a flow sequence still works
        // after the DK4H fix (avoid regression).
        let yaml = "[key: value]";
        let config = crate::parser::config::ParserConfig::strict();
        let result = crate::parse_with_config(yaml, config);
        assert!(
            result.is_ok(),
            "Same-line implicit key in flow sequence should succeed, got: {:?}",
            result.err()
        );
    }
}