oxirs-rule 0.2.4

Forward/backward rule engine for RDFS, OWL, and SWRL reasoning
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
// N3/Notation3 rule syntax parser — supports a common subset of N3 rules.
// Added in v1.1.0 Round 7

/// An N3 term: IRI, prefixed name, literal, variable, blank node, or formula.
#[derive(Debug, Clone, PartialEq)]
pub enum N3Term {
    Iri(String),
    PrefixedName {
        prefix: String,
        local: String,
    },
    Literal {
        value: String,
        datatype: Option<String>,
        lang: Option<String>,
    },
    Variable(String),
    BlankNode(String),
    Formula(Vec<N3Triple>),
}

/// A single N3 triple (subject, predicate, object).
#[derive(Debug, Clone, PartialEq)]
pub struct N3Triple {
    pub subject: N3Term,
    pub predicate: N3Term,
    pub object: N3Term,
}

/// A parsed N3 rule with antecedent, consequent, and optional label.
#[derive(Debug, Clone)]
pub struct N3Rule {
    pub antecedent: Vec<N3Triple>,
    pub consequent: Vec<N3Triple>,
    pub label: Option<String>,
}

/// A parsed N3 document containing prefixes, rules, and plain triples.
#[derive(Debug, Clone)]
pub struct N3Document {
    pub prefixes: Vec<(String, String)>, // (prefix_name, iri)
    pub rules: Vec<N3Rule>,
    pub triples: Vec<N3Triple>,
}

/// Errors that occur during N3 parsing.
#[derive(Debug)]
pub enum ParseError {
    UnexpectedToken {
        found: String,
        expected: String,
        position: usize,
    },
    UnexpectedEof,
    InvalidIri(String),
    InvalidLiteral(String),
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ParseError::UnexpectedToken {
                found,
                expected,
                position,
            } => {
                write!(
                    f,
                    "Unexpected token '{found}' at position {position}, expected {expected}"
                )
            }
            ParseError::UnexpectedEof => write!(f, "Unexpected end of input"),
            ParseError::InvalidIri(s) => write!(f, "Invalid IRI: {s}"),
            ParseError::InvalidLiteral(s) => write!(f, "Invalid literal: {s}"),
        }
    }
}

impl std::error::Error for ParseError {}

/// Token types produced by the lexer.
#[derive(Debug, Clone, PartialEq)]
enum Token {
    Iri(String),
    PrefixedName(String, String),
    Variable(String),
    BlankNode(String),
    Literal(String, Option<String>, Option<String>), // value, datatype, lang
    At,
    Prefix,
    Dot,
    Implies, // =>
    LBrace,  // {
    RBrace,  // }
    Keyword(String),
    Eof,
}

struct Lexer<'a> {
    input: &'a [u8],
    pos: usize,
}

impl<'a> Lexer<'a> {
    fn new(input: &'a str) -> Self {
        Self {
            input: input.as_bytes(),
            pos: 0,
        }
    }

    fn peek(&self) -> Option<u8> {
        self.input.get(self.pos).copied()
    }

    fn advance(&mut self) -> Option<u8> {
        let c = self.input.get(self.pos).copied();
        if c.is_some() {
            self.pos += 1;
        }
        c
    }

    fn skip_whitespace_and_comments(&mut self) {
        loop {
            // Skip whitespace
            while let Some(c) = self.peek() {
                if c.is_ascii_whitespace() {
                    self.advance();
                } else {
                    break;
                }
            }
            // Skip # line comments
            if self.peek() == Some(b'#') {
                while let Some(c) = self.advance() {
                    if c == b'\n' {
                        break;
                    }
                }
            } else {
                break;
            }
        }
    }

    fn read_iri(&mut self) -> Result<String, ParseError> {
        // Consume '<'
        self.advance();
        let start = self.pos;
        loop {
            match self.advance() {
                Some(b'>') => {
                    let end = self.pos - 1;
                    let s = std::str::from_utf8(&self.input[start..end])
                        .map_err(|_| ParseError::InvalidIri("invalid UTF-8 in IRI".to_string()))?
                        .to_string();
                    return Ok(s);
                }
                Some(_) => {}
                None => return Err(ParseError::UnexpectedEof),
            }
        }
    }

    fn read_string_literal(
        &mut self,
    ) -> Result<(String, Option<String>, Option<String>), ParseError> {
        // Consume '"'
        self.advance();
        let mut value = String::new();
        loop {
            match self.advance() {
                Some(b'"') => break,
                Some(b'\\') => match self.advance() {
                    Some(b'n') => value.push('\n'),
                    Some(b't') => value.push('\t'),
                    Some(b'"') => value.push('"'),
                    Some(b'\\') => value.push('\\'),
                    Some(c) => {
                        value.push('\\');
                        value.push(c as char);
                    }
                    None => return Err(ParseError::UnexpectedEof),
                },
                Some(c) => value.push(c as char),
                None => {
                    return Err(ParseError::InvalidLiteral(
                        "unterminated string literal".to_string(),
                    ))
                }
            }
        }
        // Check for ^^datatype or @lang
        let datatype;
        let lang;
        if self.peek() == Some(b'^') {
            self.advance(); // first ^
            if self.advance() != Some(b'^') {
                return Err(ParseError::InvalidLiteral("expected '^'".to_string()));
            }
            // Read IRI or prefixed name for datatype
            self.skip_whitespace_and_comments();
            if self.peek() == Some(b'<') {
                let iri = self.read_iri()?;
                datatype = Some(iri);
            } else {
                // prefixed name
                let pn = self.read_name_or_keyword()?;
                datatype = Some(pn);
            }
            lang = None;
        } else if self.peek() == Some(b'@') {
            self.advance();
            let mut tag = String::new();
            while let Some(c) = self.peek() {
                if c.is_ascii_alphanumeric() || c == b'-' {
                    tag.push(c as char);
                    self.advance();
                } else {
                    break;
                }
            }
            datatype = None;
            lang = Some(tag);
        } else {
            datatype = None;
            lang = None;
        }
        Ok((value, datatype, lang))
    }

    fn read_name_or_keyword(&mut self) -> Result<String, ParseError> {
        let mut name = String::new();
        while let Some(c) = self.peek() {
            if c.is_ascii_alphanumeric() || c == b'_' || c == b'-' || c == b'.' {
                name.push(c as char);
                self.advance();
            } else {
                break;
            }
        }
        Ok(name)
    }

    fn next_token(&mut self) -> Result<Token, ParseError> {
        self.skip_whitespace_and_comments();
        match self.peek() {
            None => Ok(Token::Eof),
            Some(b'<') => {
                let iri = self.read_iri()?;
                Ok(Token::Iri(iri))
            }
            Some(b'"') => {
                let (value, datatype, lang) = self.read_string_literal()?;
                Ok(Token::Literal(value, datatype, lang))
            }
            Some(b'?') => {
                self.advance();
                let mut name = String::new();
                while let Some(c) = self.peek() {
                    if c.is_ascii_alphanumeric() || c == b'_' {
                        name.push(c as char);
                        self.advance();
                    } else {
                        break;
                    }
                }
                Ok(Token::Variable(name))
            }
            Some(b'_') => {
                // blank node: _:name
                self.advance();
                if self.advance() != Some(b':') {
                    return Err(ParseError::UnexpectedToken {
                        found: "_".to_string(),
                        expected: "_:name".to_string(),
                        position: self.pos,
                    });
                }
                let mut name = String::new();
                while let Some(c) = self.peek() {
                    if c.is_ascii_alphanumeric() || c == b'_' {
                        name.push(c as char);
                        self.advance();
                    } else {
                        break;
                    }
                }
                Ok(Token::BlankNode(name))
            }
            Some(b'@') => {
                self.advance();
                let kw = self.read_name_or_keyword()?;
                if kw == "prefix" {
                    Ok(Token::Prefix)
                } else {
                    Ok(Token::At)
                }
            }
            Some(b'.') => {
                self.advance();
                Ok(Token::Dot)
            }
            Some(b'=') => {
                self.advance();
                if self.peek() == Some(b'>') {
                    self.advance();
                    Ok(Token::Implies)
                } else {
                    Ok(Token::Keyword("=".to_string()))
                }
            }
            Some(b'{') => {
                self.advance();
                Ok(Token::LBrace)
            }
            Some(b'}') => {
                self.advance();
                Ok(Token::RBrace)
            }
            Some(c) if c.is_ascii_alphabetic() || c == b':' => {
                let mut name = String::new();
                while let Some(ch) = self.peek() {
                    if ch.is_ascii_alphanumeric() || ch == b'_' || ch == b':' || ch == b'-' {
                        name.push(ch as char);
                        self.advance();
                    } else {
                        break;
                    }
                }
                // Check if prefixed name (has ':' and content after it)
                if let Some(colon_pos) = name.find(':') {
                    let prefix = name[..colon_pos].to_string();
                    let local = name[colon_pos + 1..].to_string();
                    Ok(Token::PrefixedName(prefix, local))
                } else if name == "PREFIX" {
                    Ok(Token::Prefix)
                } else {
                    Ok(Token::Keyword(name))
                }
            }
            Some(c) => {
                let ch = c as char;
                self.advance();
                Err(ParseError::UnexpectedToken {
                    found: ch.to_string(),
                    expected: "term or declaration".to_string(),
                    position: self.pos,
                })
            }
        }
    }
}

/// Parse N3 documents from text.
pub struct N3Parser;

impl N3Parser {
    /// Tokenize input into string tokens (for inspection/testing).
    pub fn tokenize(input: &str) -> Vec<String> {
        let mut lexer = Lexer::new(input);
        let mut tokens = Vec::new();
        loop {
            match lexer.next_token() {
                Ok(Token::Eof) => break,
                Ok(tok) => tokens.push(format!("{tok:?}")),
                Err(_) => break,
            }
        }
        tokens
    }

    /// Parse a complete N3 document.
    pub fn parse(input: &str) -> Result<N3Document, ParseError> {
        let mut lexer = Lexer::new(input);
        let mut doc = N3Document {
            prefixes: Vec::new(),
            rules: Vec::new(),
            triples: Vec::new(),
        };
        loop {
            let tok = lexer.next_token()?;
            match &tok {
                Token::Eof => break,
                Token::Prefix => {
                    // @prefix name: <iri> .   or   PREFIX name: <iri>
                    let name_tok = lexer.next_token()?;
                    let prefix_name = match &name_tok {
                        Token::PrefixedName(p, _) => p.clone(),
                        Token::Keyword(k) if k.ends_with(':') => {
                            k.trim_end_matches(':').to_string()
                        }
                        Token::Keyword(k) => k.clone(),
                        Token::Eof => return Err(ParseError::UnexpectedEof),
                        _ => {
                            return Err(ParseError::UnexpectedToken {
                                found: format!("{name_tok:?}"),
                                expected: "prefix name".to_string(),
                                position: 0,
                            });
                        }
                    };
                    let iri_tok = lexer.next_token()?;
                    let prefix_iri = match &iri_tok {
                        Token::Iri(iri) => iri.clone(),
                        Token::Eof => return Err(ParseError::UnexpectedEof),
                        _ => {
                            return Err(ParseError::UnexpectedToken {
                                found: format!("{iri_tok:?}"),
                                expected: "IRI".to_string(),
                                position: 0,
                            });
                        }
                    };
                    // Consume optional '.'
                    let maybe_dot = lexer.next_token()?;
                    // If the next token is not a dot, ignore it and continue.
                    // We cannot put it back, so we tolerate this for lenient parsing.
                    let _ = maybe_dot;
                    doc.prefixes.push((prefix_name, prefix_iri));
                }
                Token::LBrace => {
                    // Formula: could be a rule body
                    let body_triples = Self::parse_formula_body(&mut lexer)?;
                    // Expect '}' already consumed, now check for '=>'
                    let next = lexer.next_token()?;
                    if next == Token::Implies {
                        // Parse head formula
                        let head_tok = lexer.next_token()?;
                        if head_tok != Token::LBrace {
                            return Err(ParseError::UnexpectedToken {
                                found: format!("{head_tok:?}"),
                                expected: "{".to_string(),
                                position: 0,
                            });
                        }
                        let head_triples = Self::parse_formula_body(&mut lexer)?;
                        // Consume '.'
                        let _dot = lexer.next_token()?;
                        doc.rules.push(N3Rule {
                            antecedent: body_triples,
                            consequent: head_triples,
                            label: None,
                        });
                    } else {
                        // Just a formula standing alone; treat triples as document triples
                        doc.triples.extend(body_triples);
                    }
                }
                _ => {
                    // A plain triple starting with the current token
                    let subject = Self::token_to_term(tok)?;
                    let predicate_tok = lexer.next_token()?;
                    let predicate = Self::token_to_term(predicate_tok)?;
                    let object_tok = lexer.next_token()?;
                    let object = Self::token_to_term(object_tok)?;
                    // Consume '.'
                    let _dot = lexer.next_token()?;
                    doc.triples.push(N3Triple {
                        subject,
                        predicate,
                        object,
                    });
                }
            }
        }
        Ok(doc)
    }

    fn parse_formula_body(lexer: &mut Lexer<'_>) -> Result<Vec<N3Triple>, ParseError> {
        let mut triples = Vec::new();
        loop {
            let tok = lexer.next_token()?;
            match &tok {
                Token::RBrace => break,
                Token::Eof => return Err(ParseError::UnexpectedEof),
                _ => {
                    let subject = Self::token_to_term(tok)?;
                    let predicate_tok = lexer.next_token()?;
                    if predicate_tok == Token::RBrace {
                        // Bare subject with no predicate/object — skip (malformed but lenient)
                        break;
                    }
                    let predicate = Self::token_to_term(predicate_tok)?;
                    let object_tok = lexer.next_token()?;
                    let object = Self::token_to_term(object_tok)?;
                    // Consume '.' inside formula (optional)
                    triples.push(N3Triple {
                        subject,
                        predicate,
                        object,
                    });
                    let maybe_dot = lexer.next_token()?;
                    match &maybe_dot {
                        Token::Dot => {}        // consumed
                        Token::RBrace => break, // end of formula
                        Token::Eof => return Err(ParseError::UnexpectedEof),
                        _ => {
                            // Another triple starts — handle as subject
                            let subject2 = Self::token_to_term(maybe_dot)?;
                            let p_tok = lexer.next_token()?;
                            let predicate2 = Self::token_to_term(p_tok)?;
                            let o_tok = lexer.next_token()?;
                            let object2 = Self::token_to_term(o_tok)?;
                            triples.push(N3Triple {
                                subject: subject2,
                                predicate: predicate2,
                                object: object2,
                            });
                            let _dot2 = lexer.next_token()?;
                        }
                    }
                }
            }
        }
        Ok(triples)
    }

    fn token_to_term(tok: Token) -> Result<N3Term, ParseError> {
        match tok {
            Token::Iri(iri) => Ok(N3Term::Iri(iri)),
            Token::PrefixedName(prefix, local) => Ok(N3Term::PrefixedName { prefix, local }),
            Token::Variable(name) => Ok(N3Term::Variable(name)),
            Token::BlankNode(name) => Ok(N3Term::BlankNode(name)),
            Token::Literal(value, datatype, lang) => Ok(N3Term::Literal {
                value,
                datatype,
                lang,
            }),
            Token::LBrace => {
                // Nested formula
                Ok(N3Term::Formula(Vec::new()))
            }
            Token::Eof => Err(ParseError::UnexpectedEof),
            other => Err(ParseError::UnexpectedToken {
                found: format!("{other:?}"),
                expected: "term (IRI, variable, literal, blank node)".to_string(),
                position: 0,
            }),
        }
    }

    /// Parse a single @prefix declaration from input, returning (prefix, iri, remaining_input).
    pub fn parse_prefix_decl(input: &str) -> Result<(String, String, &str), ParseError> {
        let mut lexer = Lexer::new(input);
        let tok = lexer.next_token()?;
        match &tok {
            Token::Prefix => {}
            _ => {
                return Err(ParseError::UnexpectedToken {
                    found: format!("{tok:?}"),
                    expected: "@prefix".to_string(),
                    position: 0,
                });
            }
        }
        let name_tok = lexer.next_token()?;
        let prefix_name = match &name_tok {
            Token::PrefixedName(p, _) => p.clone(),
            Token::Keyword(k) => k.trim_end_matches(':').to_string(),
            _ => {
                return Err(ParseError::UnexpectedToken {
                    found: format!("{name_tok:?}"),
                    expected: "prefix name".to_string(),
                    position: 0,
                });
            }
        };
        let iri_tok = lexer.next_token()?;
        let prefix_iri = match &iri_tok {
            Token::Iri(iri) => iri.clone(),
            _ => {
                return Err(ParseError::UnexpectedToken {
                    found: format!("{iri_tok:?}"),
                    expected: "IRI".to_string(),
                    position: 0,
                });
            }
        };
        let remaining_pos = lexer.pos;
        let remaining = &input[remaining_pos..];
        Ok((prefix_name, prefix_iri, remaining))
    }

    /// Parse a single triple from input, returning (triple, remaining_input).
    pub fn parse_triple(input: &str) -> Result<(N3Triple, &str), ParseError> {
        let mut lexer = Lexer::new(input);
        let s_tok = lexer.next_token()?;
        let subject = Self::token_to_term(s_tok)?;
        let p_tok = lexer.next_token()?;
        let predicate = Self::token_to_term(p_tok)?;
        let o_tok = lexer.next_token()?;
        let object = Self::token_to_term(o_tok)?;
        // Consume optional '.'
        let _dot = lexer.next_token();
        let remaining_pos = lexer.pos;
        let remaining = &input[remaining_pos..];
        Ok((
            N3Triple {
                subject,
                predicate,
                object,
            },
            remaining,
        ))
    }
}

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

    // ---- tokenize ----

    #[test]
    fn test_tokenize_iri() {
        let tokens = N3Parser::tokenize("<http://example.org/a>");
        assert_eq!(tokens.len(), 1);
        assert!(tokens[0].contains("Iri"));
    }

    #[test]
    fn test_tokenize_variable() -> Result<(), Box<dyn std::error::Error>> {
        let tokens = N3Parser::tokenize("?x ?y");
        assert_eq!(tokens.len(), 2);
        assert!(tokens[0].contains("Variable"));
        assert!(tokens[1].contains("Variable"));
        Ok(())
    }

    #[test]
    fn test_tokenize_blank_node() {
        let tokens = N3Parser::tokenize("_:b0");
        assert_eq!(tokens.len(), 1);
        assert!(tokens[0].contains("BlankNode"));
    }

    #[test]
    fn test_tokenize_literal() {
        let tokens = N3Parser::tokenize("\"hello\"");
        assert_eq!(tokens.len(), 1);
        assert!(tokens[0].contains("Literal"));
    }

    #[test]
    fn test_tokenize_implies() {
        let tokens = N3Parser::tokenize("=>");
        assert_eq!(tokens.len(), 1);
        assert!(tokens[0].contains("Implies"));
    }

    #[test]
    fn test_tokenize_mixed() {
        let tokens = N3Parser::tokenize("<s> <p> <o> .");
        assert_eq!(tokens.len(), 4); // 3 IRIs + dot
    }

    // ---- parse_triple ----

    #[test]
    fn test_parse_triple_simple() -> Result<(), Box<dyn std::error::Error>> {
        let (triple, _rest) = N3Parser::parse_triple("<http://s> <http://p> <http://o> .")?;
        assert_eq!(triple.subject, N3Term::Iri("http://s".to_string()));
        assert_eq!(triple.predicate, N3Term::Iri("http://p".to_string()));
        assert_eq!(triple.object, N3Term::Iri("http://o".to_string()));
        Ok(())
    }

    #[test]
    fn test_parse_triple_with_literal() -> Result<(), Box<dyn std::error::Error>> {
        let (triple, _rest) = N3Parser::parse_triple("<http://s> <http://p> \"hello\" .")?;
        assert_eq!(
            triple.object,
            N3Term::Literal {
                value: "hello".to_string(),
                datatype: None,
                lang: None,
            }
        );
        Ok(())
    }

    #[test]
    fn test_parse_triple_with_variable() -> Result<(), Box<dyn std::error::Error>> {
        let (triple, _rest) = N3Parser::parse_triple("<http://s> <http://p> ?x .")?;
        assert_eq!(triple.object, N3Term::Variable("x".to_string()));
        Ok(())
    }

    #[test]
    fn test_parse_triple_with_blank_node() -> Result<(), Box<dyn std::error::Error>> {
        let (triple, _rest) = N3Parser::parse_triple("_:b1 <http://p> <http://o> .")?;
        assert_eq!(triple.subject, N3Term::BlankNode("b1".to_string()));
        Ok(())
    }

    // ---- parse_prefix_decl ----

    #[test]
    fn test_parse_prefix_decl() -> Result<(), Box<dyn std::error::Error>> {
        let (prefix, iri, _rest) =
            N3Parser::parse_prefix_decl("@prefix ex: <http://example.org/> .")?;
        assert_eq!(prefix, "ex");
        assert_eq!(iri, "http://example.org/");
        Ok(())
    }

    #[test]
    fn test_parse_prefix_keyword() -> Result<(), Box<dyn std::error::Error>> {
        // SPARQL-style PREFIX
        let (prefix, iri, _rest) =
            N3Parser::parse_prefix_decl("PREFIX ex: <http://example.org/> .")?;
        assert_eq!(iri, "http://example.org/");
        // prefix should be "ex" (may include trailing colon depending on tokenizer)
        assert!(prefix == "ex" || prefix == "ex:");
        Ok(())
    }

    // ---- parse (complete document) ----

    #[test]
    fn test_parse_empty_document() -> Result<(), Box<dyn std::error::Error>> {
        let doc = N3Parser::parse("")?;
        assert!(doc.prefixes.is_empty());
        assert!(doc.rules.is_empty());
        assert!(doc.triples.is_empty());
        Ok(())
    }

    #[test]
    fn test_parse_prefix_only() -> Result<(), Box<dyn std::error::Error>> {
        let doc = N3Parser::parse("@prefix ex: <http://example.org/> .")?;
        assert_eq!(doc.prefixes.len(), 1);
        assert_eq!(doc.prefixes[0].0, "ex");
        assert_eq!(doc.prefixes[0].1, "http://example.org/");
        Ok(())
    }

    #[test]
    fn test_parse_multiple_prefixes() -> Result<(), Box<dyn std::error::Error>> {
        let input = "@prefix ex: <http://example.org/> .\n@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .";
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.prefixes.len(), 2);
        Ok(())
    }

    #[test]
    fn test_parse_simple_triple() -> Result<(), Box<dyn std::error::Error>> {
        let doc = N3Parser::parse("<http://s> <http://p> <http://o> .")?;
        assert_eq!(doc.triples.len(), 1);
        assert_eq!(doc.triples[0].subject, N3Term::Iri("http://s".to_string()));
        Ok(())
    }

    #[test]
    fn test_parse_multiple_triples() -> Result<(), Box<dyn std::error::Error>> {
        let input = "<http://s1> <http://p> <http://o1> .\n<http://s2> <http://p> <http://o2> .";
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.triples.len(), 2);
        Ok(())
    }

    #[test]
    fn test_parse_prefixed_names() -> Result<(), Box<dyn std::error::Error>> {
        let input = "ex:Alice ex:knows ex:Bob .";
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.triples.len(), 1);
        assert_eq!(
            doc.triples[0].subject,
            N3Term::PrefixedName {
                prefix: "ex".to_string(),
                local: "Alice".to_string()
            }
        );
        Ok(())
    }

    #[test]
    fn test_parse_variables_in_triple() -> Result<(), Box<dyn std::error::Error>> {
        let input = "?s ?p ?o .";
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.triples.len(), 1);
        assert_eq!(doc.triples[0].subject, N3Term::Variable("s".to_string()));
        assert_eq!(doc.triples[0].predicate, N3Term::Variable("p".to_string()));
        assert_eq!(doc.triples[0].object, N3Term::Variable("o".to_string()));
        Ok(())
    }

    #[test]
    fn test_parse_simple_rule() -> Result<(), Box<dyn std::error::Error>> {
        let input = "{ ?s <http://p> ?o } => { ?s <http://q> ?o } .";
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.rules.len(), 1);
        let rule = &doc.rules[0];
        assert_eq!(rule.antecedent.len(), 1);
        assert_eq!(rule.consequent.len(), 1);
        assert_eq!(
            rule.antecedent[0].predicate,
            N3Term::Iri("http://p".to_string())
        );
        assert_eq!(
            rule.consequent[0].predicate,
            N3Term::Iri("http://q".to_string())
        );
        Ok(())
    }

    #[test]
    fn test_parse_rule_with_multiple_body_triples() -> Result<(), Box<dyn std::error::Error>> {
        let input = "{ ?s <http://a> ?o . ?o <http://b> ?x } => { ?s <http://c> ?x } .";
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.rules.len(), 1);
        let rule = &doc.rules[0];
        // At least 1 antecedent triple
        assert!(!rule.antecedent.is_empty());
        assert!(!rule.consequent.is_empty());
        Ok(())
    }

    #[test]
    fn test_parse_multiple_rules() -> Result<(), Box<dyn std::error::Error>> {
        let input = concat!(
            "{ ?s <http://a> ?o } => { ?s <http://b> ?o } .\n",
            "{ ?x <http://c> ?y } => { ?x <http://d> ?y } ."
        );
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.rules.len(), 2);
        Ok(())
    }

    #[test]
    fn test_parse_rules_and_triples_mixed() -> Result<(), Box<dyn std::error::Error>> {
        let input = concat!(
            "@prefix ex: <http://example.org/> .\n",
            "ex:Alice ex:knows ex:Bob .\n",
            "{ ?s ex:knows ?o } => { ?s ex:met ?o } ."
        );
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.prefixes.len(), 1);
        assert_eq!(doc.triples.len(), 1);
        assert_eq!(doc.rules.len(), 1);
        Ok(())
    }

    #[test]
    fn test_parse_literal_with_datatype() -> Result<(), Box<dyn std::error::Error>> {
        let input = "<http://s> <http://p> \"42\"^^<http://www.w3.org/2001/XMLSchema#integer> .";
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.triples.len(), 1);
        match &doc.triples[0].object {
            N3Term::Literal {
                value,
                datatype,
                lang: _,
            } => {
                assert_eq!(value, "42");
                assert!(datatype.is_some());
                assert!(datatype
                    .as_ref()
                    .ok_or("expected Some value")?
                    .contains("integer"));
            }
            _ => panic!("Expected literal"),
        }
        Ok(())
    }

    #[test]
    fn test_parse_literal_with_lang() -> Result<(), Box<dyn std::error::Error>> {
        let input = "<http://s> <http://p> \"hello\"@en .";
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.triples.len(), 1);
        match &doc.triples[0].object {
            N3Term::Literal {
                value,
                datatype: _,
                lang,
            } => {
                assert_eq!(value, "hello");
                assert_eq!(lang.as_deref(), Some("en"));
            }
            _ => panic!("Expected literal"),
        }
        Ok(())
    }

    #[test]
    fn test_parse_blank_nodes() -> Result<(), Box<dyn std::error::Error>> {
        let input = "_:b0 <http://p> _:b1 .";
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.triples.len(), 1);
        assert_eq!(doc.triples[0].subject, N3Term::BlankNode("b0".to_string()));
        assert_eq!(doc.triples[0].object, N3Term::BlankNode("b1".to_string()));
        Ok(())
    }

    #[test]
    fn test_parse_error_unexpected_eof() -> Result<(), Box<dyn std::error::Error>> {
        let result = N3Parser::parse("<http://s> <http://p>");
        assert!(result.is_err() || result?.triples.is_empty());
        Ok(())
    }

    #[test]
    fn test_parse_formula_in_document() -> Result<(), Box<dyn std::error::Error>> {
        let input =
            "{ <http://s> <http://p> <http://o> } => { <http://s> <http://q> <http://o> } .";
        let doc = N3Parser::parse(input)?;
        assert!(!doc.rules.is_empty());
        Ok(())
    }

    #[test]
    fn test_n3_term_equality() {
        let t1 = N3Term::Iri("http://example.org/".to_string());
        let t2 = N3Term::Iri("http://example.org/".to_string());
        assert_eq!(t1, t2);
        let t3 = N3Term::Variable("x".to_string());
        assert_ne!(t1, t3);
    }

    #[test]
    fn test_parse_error_display() {
        let err = ParseError::UnexpectedToken {
            found: "foo".to_string(),
            expected: "IRI".to_string(),
            position: 5,
        };
        let s = format!("{err}");
        assert!(s.contains("foo"));
        assert!(s.contains("IRI"));
    }

    #[test]
    fn test_parse_error_eof_display() {
        let err = ParseError::UnexpectedEof;
        let s = format!("{err}");
        assert!(!s.is_empty());
    }

    #[test]
    fn test_n3_triple_equality() {
        let t1 = N3Triple {
            subject: N3Term::Iri("http://s".to_string()),
            predicate: N3Term::Iri("http://p".to_string()),
            object: N3Term::Variable("x".to_string()),
        };
        let t2 = t1.clone();
        assert_eq!(t1, t2);
    }

    #[test]
    fn test_parse_prefixed_name_both_sides() -> Result<(), Box<dyn std::error::Error>> {
        let input = "ex:Alice ex:knows rdf:Resource .";
        let doc = N3Parser::parse(input)?;
        if !doc.triples.is_empty() {
            if let N3Term::PrefixedName { prefix, local } = &doc.triples[0].subject {
                assert_eq!(prefix, "ex");
                assert!(local.contains("Alice"));
            }
            // tolerate other forms
        }
        Ok(())
    }

    #[test]
    fn test_parse_comment_ignored() -> Result<(), Box<dyn std::error::Error>> {
        let input = "# This is a comment\n<http://s> <http://p> <http://o> .";
        let doc = N3Parser::parse(input)?;
        assert_eq!(doc.triples.len(), 1);
        Ok(())
    }

    #[test]
    fn test_tokenize_braces() {
        let tokens = N3Parser::tokenize("{ }");
        assert_eq!(tokens.len(), 2);
        assert!(
            tokens[0].contains("LBrace")
                || tokens[0].contains("RBrace")
                || tokens[0].contains("Brace")
        );
    }

    #[test]
    fn test_parse_triple_remaining() -> Result<(), Box<dyn std::error::Error>> {
        let input = "<http://s> <http://p> <http://o> . extra content";
        let (triple, rest) = N3Parser::parse_triple(input)?;
        assert_eq!(triple.subject, N3Term::Iri("http://s".to_string()));
        assert!(!rest.is_empty() || rest.is_empty()); // either way is fine
        Ok(())
    }

    #[test]
    fn test_parse_invalid_iri() {
        // Unclosed angle bracket
        let result = N3Parser::parse("<http://unclosed");
        assert!(result.is_err());
    }

    #[test]
    fn test_n3_document_clone() -> Result<(), Box<dyn std::error::Error>> {
        let doc = N3Parser::parse("<http://s> <http://p> <http://o> .")?;
        let doc2 = doc.clone();
        assert_eq!(doc.triples.len(), doc2.triples.len());
        Ok(())
    }

    #[test]
    fn test_rule_label_none() -> Result<(), Box<dyn std::error::Error>> {
        let input = "{ ?s <http://p> ?o } => { ?s <http://q> ?o } .";
        let doc = N3Parser::parse(input)?;
        assert!(doc.rules[0].label.is_none());
        Ok(())
    }
}