rustixml 0.3.1

Native iXML (Invisible XML) parser with left-recursion support - 76.9% spec conformance, works in Rust and WebAssembly
Documentation
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
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
//! Native iXML interpreter - direct implementation of iXML specification
//!
//! This module implements a recursive descent parser that directly interprets
//! iXML grammar ASTs without translation to an intermediate parser representation.
//! It handles insertion and suppression semantics natively.

use crate::ast::{Alternatives, BaseFactor, Factor, IxmlGrammar, Mark, Repetition, Rule, Sequence};
use crate::charclass::charclass_to_rangeset;
use crate::grammar_analysis::GrammarAnalysis;
use crate::input_stream::InputStream;
use crate::parse_context::{ParseContext, ParseError, ParseResult};
use crate::xml_node::XmlNode;
use std::collections::HashMap;

/// Native iXML parser that interprets grammar ASTs directly
pub struct NativeParser {
    grammar: IxmlGrammar,
    rules: HashMap<String, Rule>,
    analysis: GrammarAnalysis,
}

impl NativeParser {
    /// Create a new native parser from an iXML grammar
    pub fn new(grammar: IxmlGrammar) -> Self {
        // Analyze grammar using iterative algorithms (no stack overflow)
        let analysis = GrammarAnalysis::analyze(&grammar);
        let report = analysis.report();
        if !report.contains("No issues") {
            eprintln!("[rustixml] Grammar analysis:");
            eprintln!("{}", report);
        }

        // Build rule lookup table for O(1) access
        let rules: HashMap<String, Rule> = grammar
            .rules
            .iter()
            .map(|rule| (rule.name.clone(), rule.clone()))
            .collect();

        NativeParser {
            grammar,
            rules,
            analysis,
        }
    }

    /// Get the number of rules in the grammar
    pub fn rule_count(&self) -> usize {
        self.rules.len()
    }

    /// Parse input text according to the grammar
    ///
    /// Returns XML string on success, or error message on failure
    pub fn parse(&self, input: &str) -> Result<String, String> {
        let mut stream = InputStream::new(input);
        let mut ctx = ParseContext::new();

        // Start with the first rule in the grammar
        let start_rule = self
            .grammar
            .rules
            .first()
            .ok_or_else(|| "Grammar has no rules".to_string())?;

        match self.parse_rule(&mut stream, start_rule, &mut ctx) {
            Ok(result) => {
                // Check if all input was consumed
                if !stream.is_eof() {
                    let remaining = stream.remaining();
                    return Err(format!(
                        "Parse succeeded but input remains: {:?}",
                        remaining.chars().take(20).collect::<String>()
                    ));
                }

                // Convert node to XML string
                if let Some(mut node) = result.node {
                    // If grammar is potentially ambiguous, add ixml:state="ambiguous" to root element
                    if self.analysis.is_potentially_ambiguous {
                        node = self.add_ambiguity_marker(node);
                    }
                    Ok(node.to_xml())
                } else {
                    Err("Parse succeeded but produced no output (fully suppressed)".to_string())
                }
            }
            Err(e) => Err(e.format_with_context(input)),
        }
    }

    /// Parse a complete rule
    fn parse_rule(
        &self,
        stream: &mut InputStream,
        rule: &Rule,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        let start_pos = stream.position();
        let memo_key = (rule.name.clone(), start_pos);

        // Check memoization cache first
        if let Some(cached_result) = ctx.memo_cache.get(&memo_key) {
            // Clone the result and restore stream position
            let result = cached_result.clone();
            if let Ok(ref parse_result) = result {
                stream.set_position(start_pos + parse_result.consumed);
            }
            return result;
        }

        // Check for left recursion at this position
        let is_left_recursive = !ctx.enter_rule(&rule.name, start_pos);

        if is_left_recursive {
            // Left-recursion detected! Use seed-growing algorithm
            return self.parse_with_seed_growing(stream, rule, ctx, start_pos, memo_key);
        }

        // Normal (non-left-recursive) parsing
        let result = self.parse_alternatives(stream, &rule.alternatives, ctx);

        ctx.exit_rule(&rule.name, start_pos);

        // Apply rule-level mark to result
        let final_result = result.map(|res| self.apply_rule_mark(res, rule));

        // Store in memoization cache (clone before storing)
        ctx.memo_cache.insert(memo_key, final_result.clone());

        final_result
    }

    /// Parse with seed-growing for left-recursive rules (Warth et al., 2008)
    fn parse_with_seed_growing(
        &self,
        stream: &mut InputStream,
        rule: &Rule,
        ctx: &mut ParseContext,
        start_pos: usize,
        memo_key: (String, usize),
    ) -> Result<ParseResult, ParseError> {
        // Seed with failure (base case for recursion)
        let mut seed: Result<ParseResult, ParseError> = Err(ParseError::LeftRecursion {
            rule: rule.name.clone(),
            position: start_pos,
        });

        // Store failure seed in cache
        ctx.memo_cache.insert(memo_key.clone(), seed.clone());

        // Grow the seed iteratively until fixed point
        const MAX_ITERATIONS: usize = 100; // Safety limit to prevent infinite loops
        let mut iteration = 0;

        loop {
            iteration += 1;
            if iteration > MAX_ITERATIONS {
                // Safety limit reached - return current seed
                break;
            }

            // Reset stream position for this iteration
            stream.set_position(start_pos);

            // Temporarily remove from recursion stack to allow re-entry
            ctx.exit_rule(&rule.name, start_pos);

            // Try to parse (will use cached seed for recursive calls)
            let result = self.parse_alternatives(stream, &rule.alternatives, ctx);

            // Re-add to recursion stack
            let re_entered = ctx.enter_rule(&rule.name, start_pos);
            debug_assert!(
                !re_entered,
                "Should not be able to re-enter during seed-growing"
            );

            // Apply rule-level mark to result
            let final_result = result.map(|res| self.apply_rule_mark(res, rule));

            // Check if we grew the parse
            let grew = match (&seed, &final_result) {
                // Grew from failure to success
                (Err(_), Ok(_new_result)) => {
                    seed = final_result.clone();
                    ctx.memo_cache.insert(memo_key.clone(), seed.clone());
                    true
                }
                // Grew from shorter to longer parse
                (Ok(old_result), Ok(new_result)) if new_result.consumed > old_result.consumed => {
                    seed = final_result.clone();
                    ctx.memo_cache.insert(memo_key.clone(), seed.clone());
                    true
                }
                // No growth - fixed point reached
                _ => false,
            };

            if !grew {
                // No growth, we've reached fixed point
                break;
            }
        }

        // Cleanup: remove from recursion stack
        ctx.exit_rule(&rule.name, start_pos);

        // Restore stream position based on final result
        stream.set_position(start_pos);
        if let Ok(ref parse_result) = seed {
            stream.set_position(start_pos + parse_result.consumed);
        }

        seed
    }

    /// Apply rule-level mark to parse result
    fn apply_rule_mark(&self, mut result: ParseResult, rule: &Rule) -> ParseResult {
        match rule.mark {
            Mark::Hidden => {
                // Don't wrap in element - pass through content as-is
                // This is different from factor-level hiding which suppresses output
                // Rule-level hiding just means "don't create wrapper element"
                // Content is already in result.node, so just return it
            }
            Mark::Attribute => {
                // Convert to attribute
                let text = result.node.map(|n| n.text_content()).unwrap_or_default();
                result.node = Some(XmlNode::Attribute {
                    name: rule.name.clone(),
                    value: text,
                });
            }
            Mark::Promoted => {
                // Keep node as-is (promoted)
                // Node is already unwrapped
            }
            Mark::None => {
                // Wrap in element
                // If the node is a _sequence wrapper, unwrap it and use its children
                let mut children = match result.node {
                    Some(XmlNode::Element { name, children, .. }) if name == "_sequence" => {
                        // Unwrap sequence and use its children directly
                        children
                    }
                    Some(node) => vec![node],
                    None => vec![], // Empty element
                };

                // Recursively flatten any nested _sequence elements
                children = Self::flatten_sequences(children);

                // Extract attributes from children
                let (attributes, non_attrs): (Vec<_>, Vec<_>) = children
                    .into_iter()
                    .partition(|node| matches!(node, XmlNode::Attribute { .. }));

                // Convert attribute nodes to (name, value) tuples
                let attrs: Vec<(String, String)> = attributes
                    .into_iter()
                    .filter_map(|node| {
                        if let XmlNode::Attribute { name, value } = node {
                            Some((name, value))
                        } else {
                            None
                        }
                    })
                    .collect();

                children = non_attrs;

                result.node = Some(XmlNode::Element {
                    name: rule.name.clone(),
                    attributes: attrs,
                    children,
                });
            }
        }

        result
    }

    /// Parse alternatives (choice)
    fn parse_alternatives(
        &self,
        stream: &mut InputStream,
        alts: &Alternatives,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        let start_pos = stream.position();
        let mut best_result: Option<(ParseResult, usize)> = None; // (result, end_position)
        let mut attempts = 0;

        // Try each alternative and keep the longest match
        for alt in alts.alts.iter() {
            stream.set_position(start_pos); // Reset for each alternative
            attempts += 1;

            match self.parse_sequence(stream, alt, ctx) {
                Ok(result) => {
                    let end_pos = stream.position();

                    // Keep this result if it's the longest match so far
                    match &best_result {
                        None => {
                            best_result = Some((result, end_pos));
                        }
                        Some((_, best_end)) => {
                            if end_pos > *best_end {
                                best_result = Some((result, end_pos));
                            }
                        }
                    }
                }
                Err(_) => {
                    continue; // Try next alternative
                }
            }
        }

        // Return the longest match, or error if all failed
        match best_result {
            Some((result, end_pos)) => {
                stream.set_position(end_pos); // Commit to longest match
                Ok(result)
            }
            None => Err(ParseError::NoAlternativeMatched {
                position: start_pos,
                rule: ctx.rule_name.clone(),
                attempts,
            }),
        }
    }

    /// Parse a sequence (concatenation)
    fn parse_sequence(
        &self,
        stream: &mut InputStream,
        seq: &Sequence,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        let start_pos = stream.position();
        let mut children = Vec::new();
        let mut total_consumed = 0;

        // Parse each factor in sequence
        for factor in &seq.factors {
            match self.parse_factor(stream, factor, ctx) {
                Ok(result) => {
                    // Collect non-suppressed nodes
                    if let Some(node) = result.node {
                        children.push(node);
                    }
                    total_consumed += result.consumed;
                }
                Err(e) => {
                    // Sequence failed - backtrack
                    stream.set_position(start_pos);
                    return Err(e);
                }
            }
        }

        // Return sequence as children nodes
        let node = if children.is_empty() {
            None // All suppressed
        } else if children.len() == 1 {
            Some(children.into_iter().next().unwrap())
        } else {
            // Multiple children - wrap in a container element
            Some(XmlNode::Element {
                name: "_sequence".to_string(),
                attributes: vec![],
                children,
            })
        };

        Ok(ParseResult::new(node, total_consumed))
    }

    /// Parse a factor (base + repetition)
    fn parse_factor(
        &self,
        stream: &mut InputStream,
        factor: &Factor,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        match &factor.repetition {
            Repetition::None => self.parse_base_factor(stream, &factor.base, ctx),
            Repetition::ZeroOrMore => self.parse_zero_or_more(stream, &factor.base, ctx),
            Repetition::OneOrMore => self.parse_one_or_more(stream, &factor.base, ctx),
            Repetition::Optional => self.parse_optional(stream, &factor.base, ctx),
            Repetition::SeparatedZeroOrMore(sep) => {
                self.parse_separated_zero_or_more(stream, &factor.base, sep, ctx)
            }
            Repetition::SeparatedOneOrMore(sep) => {
                self.parse_separated_one_or_more(stream, &factor.base, sep, ctx)
            }
        }
    }

    /// Parse a base factor (terminal, nonterminal, charclass, group)
    fn parse_base_factor(
        &self,
        stream: &mut InputStream,
        base: &BaseFactor,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        match base {
            BaseFactor::Literal {
                value,
                insertion,
                mark,
            } => self.parse_terminal(stream, value, *mark, *insertion),
            BaseFactor::Nonterminal { name, mark } => {
                self.parse_nonterminal(stream, name, *mark, ctx)
            }
            BaseFactor::CharClass {
                content,
                negated,
                mark,
            } => self.parse_charclass(stream, content, *negated, *mark),
            BaseFactor::Group { alternatives } => {
                self.parse_alternatives(stream, alternatives, ctx)
            }
        }
    }

    /// Parse a terminal literal
    fn parse_terminal(
        &self,
        stream: &mut InputStream,
        value: &str,
        mark: Mark,
        insertion: bool,
    ) -> Result<ParseResult, ParseError> {
        let start_pos = stream.position();

        // Handle insertion: always succeeds, consumes no input
        if insertion {
            let node = match mark {
                Mark::Hidden => None,
                _ => Some(XmlNode::Text(value.to_string())),
            };
            return Ok(ParseResult::new(node, 0));
        }

        // Match literal string character by character
        let value_chars: Vec<char> = value.chars().collect();
        for expected_ch in &value_chars {
            match stream.current() {
                Some(actual_ch) if actual_ch == *expected_ch => {
                    stream.advance();
                }
                Some(actual_ch) => {
                    // Mismatch - restore position and fail
                    stream.set_position(start_pos);
                    return Err(ParseError::TerminalMismatch {
                        expected: value.to_string(),
                        actual: actual_ch.to_string(),
                        position: start_pos,
                    });
                }
                None => {
                    // Unexpected EOF
                    stream.set_position(start_pos);
                    return Err(ParseError::UnexpectedEof {
                        expected: value.to_string(),
                        position: start_pos,
                    });
                }
            }
        }

        // Success - create node based on mark
        let consumed = value_chars.len();
        let node = match mark {
            Mark::Hidden => None,
            _ => Some(XmlNode::Text(value.to_string())),
        };

        Ok(ParseResult::new(node, consumed))
    }

    /// Parse a character class
    fn parse_charclass(
        &self,
        stream: &mut InputStream,
        content: &str,
        negated: bool,
        mark: Mark,
    ) -> Result<ParseResult, ParseError> {
        let start_pos = stream.position();

        // Get current character
        let ch = match stream.current() {
            Some(c) => c,
            None => {
                return Err(ParseError::UnexpectedEof {
                    expected: format!(
                        "character matching class [{}{}]",
                        if negated { "^" } else { "" },
                        content
                    ),
                    position: start_pos,
                });
            }
        };

        // Convert character class to RangeSet and check if character matches
        let rangeset = charclass_to_rangeset(content);
        let matches = rangeset.contains(ch);
        let actual_match = if negated { !matches } else { matches };

        if !actual_match {
            return Err(ParseError::CharClassMismatch {
                charclass: content.to_string(),
                negated,
                actual: ch,
                position: start_pos,
            });
        }

        // Success - consume character and create node
        stream.advance();
        let node = match mark {
            Mark::Hidden => None,
            _ => Some(XmlNode::Text(ch.to_string())),
        };

        Ok(ParseResult::new(node, 1))
    }

    /// Parse a nonterminal (rule reference)
    fn parse_nonterminal(
        &self,
        stream: &mut InputStream,
        name: &str,
        mark: Mark,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        let start_pos = stream.position();

        // Look up the rule
        let rule = self.rules.get(name).ok_or_else(|| ParseError::Custom {
            message: format!("Undefined rule: {}", name),
            position: start_pos,
        })?;

        // Parse the rule
        let result = self.parse_rule(stream, rule, ctx)?;

        // Apply factor-level mark to the result
        let node = result.node.and_then(|n| match mark {
            Mark::Hidden => {
                // Factor-level hiding: unwrap element and pass through children + attributes
                // If the result is an Element, extract its children and attributes
                match n {
                    XmlNode::Element {
                        children,
                        attributes,
                        ..
                    } => {
                        // Pass through both children and attributes
                        // Convert attributes back to Attribute nodes
                        let mut all_nodes = Vec::new();

                        // Add attributes as Attribute nodes
                        for (name, value) in attributes {
                            all_nodes.push(XmlNode::Attribute { name, value });
                        }

                        // Add children
                        all_nodes.extend(children);

                        if all_nodes.is_empty() {
                            None
                        } else if all_nodes.len() == 1 {
                            Some(all_nodes.into_iter().next().unwrap())
                        } else {
                            // Multiple items - wrap in _sequence for now
                            Some(XmlNode::Element {
                                name: "_sequence".to_string(),
                                attributes: vec![],
                                children: all_nodes,
                            })
                        }
                    }
                    // For non-Element nodes (Text, Attribute), keep them
                    other => Some(other),
                }
            }
            Mark::Attribute => {
                // Convert to attribute
                Some(XmlNode::Attribute {
                    name: name.to_string(),
                    value: n.text_content(),
                })
            }
            Mark::Promoted => {
                // Promote content: Override any rule-level mark and wrap in element
                // If the result is NOT already wrapped in its rule name, wrap it
                match n {
                    XmlNode::Element { ref name, .. } if name == &rule.name => {
                        // Already wrapped in rule element, keep as-is
                        Some(n)
                    }
                    _ => {
                        // Not wrapped or wrapped in different element - wrap it
                        // First unwrap if it's a _sequence
                        let children = match n {
                            XmlNode::Element { name, children, .. } if name == "_sequence" => {
                                children
                            }
                            other => vec![other],
                        };

                        // Wrap in rule element
                        Some(XmlNode::Element {
                            name: rule.name.clone(),
                            attributes: vec![],
                            children,
                        })
                    }
                }
            }
            Mark::None => {
                // Keep as-is (already wrapped by rule-level mark)
                Some(n)
            }
        });

        Ok(ParseResult::new(node, result.consumed))
    }

    /// Recursively flatten nested _sequence elements
    fn flatten_sequences(children: Vec<XmlNode>) -> Vec<XmlNode> {
        let mut flattened = Vec::new();

        for node in children {
            match node {
                XmlNode::Element { name, children, .. } if name == "_sequence" => {
                    // Recursively flatten and add children
                    flattened.extend(Self::flatten_sequences(children));
                }
                other => {
                    flattened.push(other);
                }
            }
        }

        flattened
    }

    /// Merge consecutive Text nodes and return an appropriate node
    fn merge_nodes(&self, children: Vec<XmlNode>) -> Option<XmlNode> {
        if children.is_empty() {
            return None;
        }

        // Merge consecutive Text nodes
        let mut merged = Vec::new();
        let mut text_buffer = String::new();

        for node in children {
            match node {
                XmlNode::Text(s) => {
                    text_buffer.push_str(&s);
                }
                other => {
                    // Flush text buffer if not empty
                    if !text_buffer.is_empty() {
                        merged.push(XmlNode::Text(text_buffer.clone()));
                        text_buffer.clear();
                    }
                    merged.push(other);
                }
            }
        }

        // Flush remaining text
        if !text_buffer.is_empty() {
            merged.push(XmlNode::Text(text_buffer));
        }

        // Return result
        if merged.is_empty() {
            None
        } else if merged.len() == 1 {
            Some(merged.into_iter().next().unwrap())
        } else {
            // Multiple non-text nodes - wrap in sequence
            Some(XmlNode::Element {
                name: "_sequence".to_string(),
                attributes: vec![],
                children: merged,
            })
        }
    }

    /// Parse zero or more repetitions (*)
    fn parse_zero_or_more(
        &self,
        stream: &mut InputStream,
        base: &BaseFactor,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        let _start_pos = stream.position();
        let mut children = Vec::new();
        let mut total_consumed = 0;

        // Keep matching until we fail
        loop {
            let loop_start = stream.position();

            // Try to match the base factor
            match self.parse_base_factor(stream, base, ctx) {
                Ok(result) => {
                    // Epsilon-match detection: prevent infinite loops
                    if result.consumed == 0 {
                        // If we matched but consumed nothing, we'd loop forever
                        // Break here (but keep the match if it produced a node)
                        if let Some(node) = result.node {
                            children.push(node);
                        }
                        break;
                    }

                    // Collect non-suppressed nodes
                    if let Some(node) = result.node {
                        children.push(node);
                    }
                    total_consumed += result.consumed;
                }
                Err(_) => {
                    // Failed to match - that's OK for zero-or-more
                    stream.set_position(loop_start); // Backtrack this attempt
                    break;
                }
            }
        }

        // Return collected nodes (merged if they're all text)
        Ok(ParseResult::new(self.merge_nodes(children), total_consumed))
    }

    /// Parse one or more repetitions (+)
    fn parse_one_or_more(
        &self,
        stream: &mut InputStream,
        base: &BaseFactor,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        let _start_pos = stream.position();

        // Must match at least once
        let first_result = self.parse_base_factor(stream, base, ctx)?;
        let mut children = Vec::new();
        let mut total_consumed = first_result.consumed;

        if let Some(node) = first_result.node {
            children.push(node);
        }

        // Epsilon-match check: if first match consumed nothing, don't loop
        if first_result.consumed == 0 {
            let node = if children.is_empty() {
                None
            } else {
                Some(children.into_iter().next().unwrap())
            };
            return Ok(ParseResult::new(node, total_consumed));
        }

        // Try to match more
        loop {
            let loop_start = stream.position();

            match self.parse_base_factor(stream, base, ctx) {
                Ok(result) => {
                    // Epsilon-match detection
                    if result.consumed == 0 {
                        if let Some(node) = result.node {
                            children.push(node);
                        }
                        break;
                    }

                    if let Some(node) = result.node {
                        children.push(node);
                    }
                    total_consumed += result.consumed;
                }
                Err(_) => {
                    stream.set_position(loop_start);
                    break;
                }
            }
        }

        // Return collected nodes (merged if they're all text)
        Ok(ParseResult::new(self.merge_nodes(children), total_consumed))
    }

    /// Parse optional (?)
    fn parse_optional(
        &self,
        stream: &mut InputStream,
        base: &BaseFactor,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        let start_pos = stream.position();

        // Try to match once
        match self.parse_base_factor(stream, base, ctx) {
            Ok(result) => Ok(result),
            Err(_) => {
                // Failed - that's OK for optional
                stream.set_position(start_pos);
                Ok(ParseResult::new(None, 0))
            }
        }
    }

    /// Parse zero or more with separator (**)
    fn parse_separated_zero_or_more(
        &self,
        stream: &mut InputStream,
        base: &BaseFactor,
        separator: &Sequence,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        let _start_pos = stream.position();
        let mut children = Vec::new();
        let mut total_consumed = 0;

        // Try to match first element
        let first_pos = stream.position();
        match self.parse_base_factor(stream, base, ctx) {
            Ok(result) => {
                if let Some(node) = result.node {
                    children.push(node);
                }
                total_consumed += result.consumed;

                // Epsilon-match check
                if result.consumed == 0 {
                    return Ok(ParseResult::new(
                        if children.is_empty() {
                            None
                        } else {
                            Some(children.into_iter().next().unwrap())
                        },
                        total_consumed,
                    ));
                }
            }
            Err(_) => {
                // No elements - that's OK for zero-or-more
                stream.set_position(first_pos);
                return Ok(ParseResult::new(None, 0));
            }
        }

        // Try to match more: (separator element)*
        loop {
            let loop_start = stream.position();

            // Try to match separator
            match self.parse_sequence(stream, separator, ctx) {
                Ok(sep_result) => {
                    // Collect separator node (may be attribute)
                    if let Some(node) = sep_result.node {
                        children.push(node);
                    }

                    // Separator matched, now try element
                    match self.parse_base_factor(stream, base, ctx) {
                        Ok(elem_result) => {
                            // Both matched - keep going
                            if let Some(node) = elem_result.node {
                                children.push(node);
                            }
                            total_consumed += sep_result.consumed + elem_result.consumed;

                            // Epsilon-match check
                            if elem_result.consumed == 0 {
                                break;
                            }
                        }
                        Err(_) => {
                            // Element failed after separator - backtrack separator too
                            stream.set_position(loop_start);
                            break;
                        }
                    }
                }
                Err(_) => {
                    // Separator failed - we're done
                    stream.set_position(loop_start);
                    break;
                }
            }
        }

        // Return collected nodes (merged if they're all text)
        Ok(ParseResult::new(self.merge_nodes(children), total_consumed))
    }

    /// Parse one or more with separator (++)
    fn parse_separated_one_or_more(
        &self,
        stream: &mut InputStream,
        base: &BaseFactor,
        separator: &Sequence,
        ctx: &mut ParseContext,
    ) -> Result<ParseResult, ParseError> {
        let _start_pos = stream.position();

        // Must match at least one element
        let first_result = self.parse_base_factor(stream, base, ctx)?;
        let mut children = Vec::new();
        let mut total_consumed = first_result.consumed;

        if let Some(node) = first_result.node {
            children.push(node);
        }

        // Epsilon-match check
        if first_result.consumed == 0 {
            return Ok(ParseResult::new(
                if children.is_empty() {
                    None
                } else {
                    Some(children.into_iter().next().unwrap())
                },
                total_consumed,
            ));
        }

        // Try to match more: (separator element)*
        loop {
            let loop_start = stream.position();

            // Try to match separator
            match self.parse_sequence(stream, separator, ctx) {
                Ok(sep_result) => {
                    // Collect separator node (may be attribute)
                    if let Some(node) = sep_result.node {
                        children.push(node);
                    }

                    // Separator matched, now try element
                    match self.parse_base_factor(stream, base, ctx) {
                        Ok(elem_result) => {
                            // Both matched
                            if let Some(node) = elem_result.node {
                                children.push(node);
                            }
                            total_consumed += sep_result.consumed + elem_result.consumed;

                            // Epsilon-match check
                            if elem_result.consumed == 0 {
                                break;
                            }
                        }
                        Err(_) => {
                            // Element failed after separator - backtrack
                            stream.set_position(loop_start);
                            break;
                        }
                    }
                }
                Err(_) => {
                    // Separator failed - we're done
                    stream.set_position(loop_start);
                    break;
                }
            }
        }

        // Return collected nodes (merged if they're all text)
        Ok(ParseResult::new(self.merge_nodes(children), total_consumed))
    }

    /// Add ixml:state="ambiguous" attribute to root element for ambiguous grammars
    fn add_ambiguity_marker(&self, node: XmlNode) -> XmlNode {
        match node {
            XmlNode::Element {
                name,
                mut attributes,
                children,
            } => {
                // Add ixml:state attribute first (order matters for test comparison)
                attributes.push(("ixml:state".to_string(), "ambiguous".to_string()));

                // Add xmlns:ixml namespace declaration if not already present
                if !attributes.iter().any(|(k, _)| k == "xmlns:ixml") {
                    attributes.push((
                        "xmlns:ixml".to_string(),
                        "http://invisiblexml.org/NS".to_string(),
                    ));
                }

                XmlNode::Element {
                    name,
                    attributes,
                    children,
                }
            }
            // If not an element (shouldn't happen for root), return as-is
            other => other,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parser_creation() {
        use crate::grammar_ast::parse_ixml_grammar;

        let grammar_text = "test: 'hello'.";
        let grammar = parse_ixml_grammar(grammar_text).expect("Grammar should parse");
        let parser = NativeParser::new(grammar);

        assert_eq!(parser.rules.len(), 1);
        assert!(parser.rules.contains_key("test"));
    }

    #[test]
    fn test_empty_grammar() {
        let grammar = IxmlGrammar::new(vec![]);
        let parser = NativeParser::new(grammar);

        let result = parser.parse("anything");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("no rules"));
    }

    #[test]
    fn test_simple_terminal() {
        use crate::grammar_ast::parse_ixml_grammar;

        let grammar_text = "test: 'hello'.";
        let grammar = parse_ixml_grammar(grammar_text).expect("Grammar should parse");
        let parser = NativeParser::new(grammar);

        // Should match "hello"
        let result = parser.parse("hello");
        assert!(result.is_ok(), "Parse should succeed: {:?}", result);
        let xml = result.unwrap();
        println!("XML output: {}", xml);
        assert!(xml.contains("<test>"));
        assert!(xml.contains("hello"));
    }

    #[test]
    fn test_terminal_mismatch() {
        use crate::grammar_ast::parse_ixml_grammar;

        let grammar_text = "test: 'hello'.";
        let grammar = parse_ixml_grammar(grammar_text).expect("Grammar should parse");
        let parser = NativeParser::new(grammar);

        // Should fail on "world"
        let result = parser.parse("world");
        assert!(result.is_err());
        let err = result.unwrap_err();
        println!("Error: {}", err);
        assert!(
            err.contains("No alternative matched")
                || err.contains("expected")
                || err.contains("hello")
        );
    }

    #[test]
    fn test_simple_charclass() {
        use crate::grammar_ast::parse_ixml_grammar;

        let grammar_text = "digit: ['0'-'9'].";
        let grammar = parse_ixml_grammar(grammar_text).expect("Grammar should parse");
        let parser = NativeParser::new(grammar);

        // Should match any digit
        for digit in '0'..='9' {
            let input = digit.to_string();
            let result = parser.parse(&input);
            assert!(result.is_ok(), "Should match digit {}: {:?}", digit, result);
            let xml = result.unwrap();
            assert!(xml.contains(&digit.to_string()));
        }

        // Should fail on non-digit
        let result = parser.parse("a");
        assert!(result.is_err());
    }

    #[test]
    fn test_nonterminal_reference() {
        use crate::grammar_ast::parse_ixml_grammar;

        let grammar_text = r#"
            test: greeting.
            greeting: 'hello'.
        "#;
        let grammar = parse_ixml_grammar(grammar_text).expect("Grammar should parse");
        let parser = NativeParser::new(grammar);

        let result = parser.parse("hello");
        assert!(result.is_ok(), "Parse should succeed: {:?}", result);
        let xml = result.unwrap();
        println!("XML output: {}", xml);
        // Remove whitespace for simpler matching
        let normalized = xml.split_whitespace().collect::<Vec<_>>().join("");
        assert!(normalized.contains("<test>"));
        assert!(normalized.contains("<greeting>"));
        assert!(normalized.contains("hello"));
    }
}