oxirs-ttl 0.2.4

Turtle-family RDF parser and serializer for OxiRS - ported from Oxigraph
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
//! N-Triples format parser and serializer
//!
//! N-Triples is the simplest RDF serialization format, consisting of one triple per line.
//! This implementation provides both streaming parsing and serialization capabilities.

use crate::error::{TextPosition, TurtleParseError, TurtleResult, TurtleSyntaxError};
use crate::toolkit::{FormattedWriter, Parser, SerializationConfig, Serializer};
use oxirs_core::model::{BlankNode, Literal, NamedNode, Object, Predicate, Subject, Triple};
use std::io::{BufRead, BufReader, Read, Write};

/// N-Triples parser
#[derive(Debug, Clone)]
pub struct NTriplesParser {
    /// Whether to continue parsing after errors
    pub lenient: bool,
}

impl Default for NTriplesParser {
    fn default() -> Self {
        Self::new()
    }
}

impl NTriplesParser {
    /// Create a new N-Triples parser
    pub fn new() -> Self {
        Self { lenient: false }
    }

    /// Create a new lenient N-Triples parser (continues after errors)
    pub fn new_lenient() -> Self {
        Self { lenient: true }
    }

    /// Strip inline comments from a line (# after data, not inside quotes or IRIs)
    fn strip_inline_comment<'a>(&self, line: &'a str) -> &'a str {
        let mut in_string = false;
        let mut in_iri = false;
        let mut escaped = false;

        for (i, ch) in line.char_indices() {
            if escaped {
                escaped = false;
                continue;
            }

            match ch {
                '\\' if in_string => escaped = true,
                '"' => in_string = !in_string,
                '<' if !in_string => in_iri = true,
                '>' if !in_string => in_iri = false,
                '#' if !in_string && !in_iri => return line[..i].trim_end(),
                _ => {}
            }
        }

        line
    }

    /// Parse a single N-Triples line
    pub fn parse_line(&self, line: &str, line_number: usize) -> TurtleResult<Option<Triple>> {
        let line = line.trim();

        // Skip empty lines and comments
        if line.is_empty() || line.starts_with('#') {
            return Ok(None);
        }

        // Strip inline comments (# after the statement, not inside quotes)
        let line = self.strip_inline_comment(line);

        // Must end with a dot
        if !line.ends_with('.') {
            return Err(TurtleParseError::syntax(TurtleSyntaxError::Generic {
                message: "N-Triples line must end with '.'".to_string(),
                position: TextPosition::new(line_number, line.len(), 0),
            }));
        }

        let line = &line[..line.len() - 1].trim(); // Remove dot and trim

        // Split into tokens (simplified tokenization)
        let tokens = self.tokenize_line(line, line_number)?;

        if tokens.len() != 3 {
            return Err(TurtleParseError::syntax(TurtleSyntaxError::Generic {
                message: format!(
                    "Expected 3 tokens (subject predicate object), found {}",
                    tokens.len()
                ),
                position: TextPosition::new(line_number, 1, 0),
            }));
        }

        let subject = self.parse_subject(&tokens[0], line_number)?;
        let predicate = self.parse_predicate(&tokens[1], line_number)?;
        let object = self.parse_object(&tokens[2], line_number)?;

        Ok(Some(Triple::new(subject, predicate, object)))
    }

    /// Tokenize a line (simplified - proper implementation would handle quoted strings properly)
    fn tokenize_line(&self, line: &str, line_number: usize) -> TurtleResult<Vec<String>> {
        let mut tokens = Vec::new();
        let mut current_token = String::new();
        let mut in_quotes = false;
        let mut escaped = false;

        for (_i, ch) in line.char_indices() {
            if escaped {
                current_token.push(ch);
                escaped = false;
                continue;
            }

            match ch {
                '\\' if in_quotes => {
                    escaped = true;
                    current_token.push(ch);
                }
                '"' => {
                    in_quotes = !in_quotes;
                    current_token.push(ch);
                }
                ' ' | '\t' if !in_quotes => {
                    if !current_token.is_empty() {
                        tokens.push(current_token.clone());
                        current_token.clear();
                    }
                }
                _ => {
                    current_token.push(ch);
                }
            }
        }

        if in_quotes {
            return Err(TurtleParseError::syntax(TurtleSyntaxError::Generic {
                message: "Unterminated string literal".to_string(),
                position: TextPosition::new(line_number, line.len(), 0),
            }));
        }

        if !current_token.is_empty() {
            tokens.push(current_token);
        }

        Ok(tokens)
    }

    /// Parse a subject (IRI or blank node)
    fn parse_subject(&self, token: &str, line_number: usize) -> TurtleResult<Subject> {
        if token.starts_with('<') && token.ends_with('>') {
            let iri = &token[1..token.len() - 1];
            let named_node = NamedNode::new(iri).map_err(TurtleParseError::model)?;
            Ok(Subject::NamedNode(named_node))
        } else if let Some(id) = token.strip_prefix("_:") {
            let blank_node = BlankNode::new(id).map_err(TurtleParseError::model)?;
            Ok(Subject::BlankNode(blank_node))
        } else {
            Err(TurtleParseError::syntax(TurtleSyntaxError::Generic {
                message: format!("Invalid subject: {token}"),
                position: TextPosition::new(line_number, 1, 0),
            }))
        }
    }

    /// Parse a predicate (must be an IRI)
    fn parse_predicate(&self, token: &str, line_number: usize) -> TurtleResult<Predicate> {
        if token.starts_with('<') && token.ends_with('>') {
            let iri = &token[1..token.len() - 1];
            let named_node = NamedNode::new(iri).map_err(TurtleParseError::model)?;
            Ok(Predicate::NamedNode(named_node))
        } else {
            Err(TurtleParseError::syntax(TurtleSyntaxError::Generic {
                message: format!("Invalid predicate: {token}"),
                position: TextPosition::new(line_number, 1, 0),
            }))
        }
    }

    /// Parse an object (IRI, blank node, or literal)
    fn parse_object(&self, token: &str, line_number: usize) -> TurtleResult<Object> {
        if token.starts_with('<') && token.ends_with('>') {
            let iri = &token[1..token.len() - 1];
            let named_node = NamedNode::new(iri).map_err(TurtleParseError::model)?;
            Ok(Object::NamedNode(named_node))
        } else if let Some(id) = token.strip_prefix("_:") {
            let blank_node = BlankNode::new(id).map_err(TurtleParseError::model)?;
            Ok(Object::BlankNode(blank_node))
        } else if token.starts_with('"') {
            self.parse_literal(token, line_number)
        } else {
            Err(TurtleParseError::syntax(TurtleSyntaxError::Generic {
                message: format!("Invalid object: {token}"),
                position: TextPosition::new(line_number, 1, 0),
            }))
        }
    }

    /// Parse a literal object
    fn parse_literal(&self, token: &str, line_number: usize) -> TurtleResult<Object> {
        if !token.starts_with('"') {
            return Err(TurtleParseError::syntax(TurtleSyntaxError::Generic {
                message: "Literal must start with quote".to_string(),
                position: TextPosition::new(line_number, 1, 0),
            }));
        }

        // Find the end quote (handling escapes) - use byte indices for proper UTF-8 handling
        let mut end_quote_byte_idx = None;
        let mut escaped = false;
        let char_indices = token.char_indices().skip(1); // Skip opening quote

        for (byte_idx, ch) in char_indices {
            if escaped {
                escaped = false;
                continue;
            }

            if ch == '\\' {
                escaped = true;
            } else if ch == '"' {
                end_quote_byte_idx = Some(byte_idx);
                break;
            }
        }

        let end_quote_byte_idx = end_quote_byte_idx.ok_or_else(|| {
            TurtleParseError::syntax(TurtleSyntaxError::Generic {
                message: "Unterminated string literal".to_string(),
                position: TextPosition::new(line_number, token.len(), 0),
            })
        })?;

        // Use byte indices for slicing to properly handle UTF-8 multi-byte characters
        let value = &token[1..end_quote_byte_idx];
        let remainder = &token[end_quote_byte_idx + 1..];

        // Unescape the string value
        let unescaped_value = self.unescape_string(value)?;

        // Check for language tag or datatype
        if remainder.is_empty() {
            // Simple string literal
            let literal = Literal::new_simple_literal(&unescaped_value);
            Ok(Object::Literal(literal))
        } else if let Some(language) = remainder.strip_prefix('@') {
            // Language-tagged literal
            let literal = Literal::new_language_tagged_literal(&unescaped_value, language)
                .map_err(|e| TurtleParseError::model(e.into()))?;
            Ok(Object::Literal(literal))
        } else if remainder.starts_with("^^<") && remainder.ends_with('>') {
            // Typed literal
            let datatype_iri = &remainder[3..remainder.len() - 1];
            let datatype = NamedNode::new(datatype_iri).map_err(TurtleParseError::model)?;
            let literal = Literal::new_typed_literal(&unescaped_value, datatype);
            Ok(Object::Literal(literal))
        } else {
            Err(TurtleParseError::syntax(TurtleSyntaxError::Generic {
                message: format!("Invalid literal suffix: {remainder}"),
                position: TextPosition::new(line_number, end_quote_byte_idx + 1, 0),
            }))
        }
    }

    /// Unescape a string literal
    fn unescape_string(&self, s: &str) -> TurtleResult<String> {
        let mut result = String::with_capacity(s.len());
        let mut chars = s.chars().peekable();

        while let Some(ch) = chars.next() {
            if ch == '\\' {
                match chars.next() {
                    Some('t') => result.push('\t'),
                    Some('n') => result.push('\n'),
                    Some('r') => result.push('\r'),
                    Some('\\') => result.push('\\'),
                    Some('"') => result.push('"'),
                    Some('u') => {
                        // Unicode escape \uXXXX
                        let mut code = String::new();
                        for _ in 0..4 {
                            if let Some(hex_char) = chars.next() {
                                code.push(hex_char);
                            } else {
                                return Err(TurtleParseError::syntax(
                                    TurtleSyntaxError::InvalidEscape {
                                        sequence: format!("u{code}"),
                                        position: TextPosition::start(),
                                    },
                                ));
                            }
                        }
                        let code_point = u32::from_str_radix(&code, 16).map_err(|_| {
                            TurtleParseError::syntax(TurtleSyntaxError::InvalidEscape {
                                sequence: format!("u{code}"),
                                position: TextPosition::start(),
                            })
                        })?;
                        let unicode_char = char::from_u32(code_point).ok_or_else(|| {
                            TurtleParseError::syntax(TurtleSyntaxError::InvalidUnicode {
                                codepoint: code_point,
                                position: TextPosition::start(),
                            })
                        })?;
                        result.push(unicode_char);
                    }
                    Some('U') => {
                        // Unicode escape \UXXXXXXXX
                        let mut code = String::new();
                        for _ in 0..8 {
                            if let Some(hex_char) = chars.next() {
                                code.push(hex_char);
                            } else {
                                return Err(TurtleParseError::syntax(
                                    TurtleSyntaxError::InvalidEscape {
                                        sequence: format!("U{code}"),
                                        position: TextPosition::start(),
                                    },
                                ));
                            }
                        }
                        let code_point = u32::from_str_radix(&code, 16).map_err(|_| {
                            TurtleParseError::syntax(TurtleSyntaxError::InvalidEscape {
                                sequence: format!("U{code}"),
                                position: TextPosition::start(),
                            })
                        })?;
                        let unicode_char = char::from_u32(code_point).ok_or_else(|| {
                            TurtleParseError::syntax(TurtleSyntaxError::InvalidUnicode {
                                codepoint: code_point,
                                position: TextPosition::start(),
                            })
                        })?;
                        result.push(unicode_char);
                    }
                    Some(other) => {
                        return Err(TurtleParseError::syntax(TurtleSyntaxError::InvalidEscape {
                            sequence: other.to_string(),
                            position: TextPosition::start(),
                        }));
                    }
                    None => {
                        return Err(TurtleParseError::syntax(TurtleSyntaxError::UnexpectedEof {
                            position: TextPosition::start(),
                        }));
                    }
                }
            } else {
                result.push(ch);
            }
        }

        Ok(result)
    }
}

impl Parser<Triple> for NTriplesParser {
    fn parse<R: Read>(&self, reader: R) -> TurtleResult<Vec<Triple>> {
        let mut triples = Vec::new();
        let mut errors = Vec::new();

        for (line_number, line_result) in BufReader::new(reader).lines().enumerate() {
            let line = line_result.map_err(TurtleParseError::io)?;

            match self.parse_line(&line, line_number + 1) {
                Ok(Some(triple)) => triples.push(triple),
                Ok(None) => {} // Empty line or comment
                Err(e) => {
                    if self.lenient {
                        errors.push(e);
                    } else {
                        return Err(e);
                    }
                }
            }
        }

        if !errors.is_empty() && self.lenient {
            eprintln!(
                "Warning: {} errors encountered during parsing",
                errors.len()
            );
        }

        Ok(triples)
    }

    fn for_reader<R: BufRead + 'static>(
        &self,
        reader: R,
    ) -> Box<dyn Iterator<Item = TurtleResult<Triple>>> {
        Box::new(NTriplesIterator {
            lines: reader.lines().enumerate(),
            parser: self.clone(),
        })
    }
}

/// Iterator for streaming N-Triples parsing
pub struct NTriplesIterator<L> {
    lines: L,
    parser: NTriplesParser,
}

impl<L> Iterator for NTriplesIterator<L>
where
    L: Iterator<Item = (usize, std::io::Result<String>)>,
{
    type Item = TurtleResult<Triple>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.lines.next() {
                None => return None,
                Some((line_number, Ok(line))) => {
                    match self.parser.parse_line(&line, line_number + 1) {
                        Ok(Some(triple)) => return Some(Ok(triple)),
                        Ok(None) => continue, // Empty line or comment
                        Err(e) => {
                            if self.parser.lenient {
                                continue; // Skip errors in lenient mode
                            } else {
                                return Some(Err(e));
                            }
                        }
                    }
                }
                Some((_, Err(io_err))) => {
                    return Some(Err(TurtleParseError::io(io_err)));
                }
            }
        }
    }
}

/// N-Triples serializer
#[derive(Debug, Clone)]
pub struct NTriplesSerializer {
    config: SerializationConfig,
}

impl Default for NTriplesSerializer {
    fn default() -> Self {
        Self::new()
    }
}

impl NTriplesSerializer {
    /// Create a new N-Triples serializer
    pub fn new() -> Self {
        Self {
            config: SerializationConfig {
                pretty: false,       // N-Triples doesn't support pretty printing
                use_prefixes: false, // N-Triples doesn't support prefixes
                ..SerializationConfig::default()
            },
        }
    }

    /// Serialize a subject
    fn serialize_subject<W: Write>(
        &self,
        subject: &Subject,
        writer: &mut FormattedWriter<W>,
    ) -> TurtleResult<()> {
        match subject {
            Subject::NamedNode(nn) => {
                writer
                    .write_str(&format!("<{}>", nn.as_str()))
                    .map_err(TurtleParseError::io)?;
            }
            Subject::BlankNode(bn) => {
                writer
                    .write_str(&format!("_:{}", bn.as_str()))
                    .map_err(TurtleParseError::io)?;
            }
            Subject::Variable(var) => {
                writer
                    .write_str(&format!("?{}", var.as_str()))
                    .map_err(TurtleParseError::io)?;
            }
            Subject::QuotedTriple(_) => {
                return Err(TurtleParseError::syntax(TurtleSyntaxError::Generic {
                    message: "QuotedTriple subjects not supported in N-Triples".to_string(),
                    position: TextPosition::default(),
                }));
            }
        }
        Ok(())
    }

    /// Serialize a predicate
    fn serialize_predicate<W: Write>(
        &self,
        predicate: &Predicate,
        writer: &mut FormattedWriter<W>,
    ) -> TurtleResult<()> {
        match predicate {
            Predicate::NamedNode(nn) => {
                writer
                    .write_str(&format!("<{}>", nn.as_str()))
                    .map_err(TurtleParseError::io)?;
            }
            Predicate::Variable(var) => {
                writer
                    .write_str(&format!("?{}", var.as_str()))
                    .map_err(TurtleParseError::io)?;
            }
        }
        Ok(())
    }

    /// Serialize an object
    fn serialize_object<W: Write>(
        &self,
        object: &Object,
        writer: &mut FormattedWriter<W>,
    ) -> TurtleResult<()> {
        match object {
            Object::NamedNode(nn) => {
                writer
                    .write_str(&format!("<{}>", nn.as_str()))
                    .map_err(TurtleParseError::io)?;
            }
            Object::BlankNode(bn) => {
                writer
                    .write_str(&format!("_:{}", bn.as_str()))
                    .map_err(TurtleParseError::io)?;
            }
            Object::Literal(literal) => {
                let escaped = writer.escape_string(literal.value());
                writer.write_str(&escaped).map_err(TurtleParseError::io)?;

                if let Some(language) = literal.language() {
                    writer
                        .write_str(&format!("@{language}"))
                        .map_err(TurtleParseError::io)?;
                } else if literal.datatype().as_str() != "http://www.w3.org/2001/XMLSchema#string" {
                    writer
                        .write_str(&format!("^^<{}>", literal.datatype().as_str()))
                        .map_err(TurtleParseError::io)?;
                }
            }
            Object::Variable(var) => {
                writer
                    .write_str(&format!("?{}", var.as_str()))
                    .map_err(TurtleParseError::io)?;
            }
            Object::QuotedTriple(_) => {
                return Err(TurtleParseError::syntax(TurtleSyntaxError::Generic {
                    message: "QuotedTriple objects not supported in N-Triples".to_string(),
                    position: TextPosition::default(),
                }));
            }
        }
        Ok(())
    }
}

impl Serializer<Triple> for NTriplesSerializer {
    fn serialize<W: Write>(&self, triples: &[Triple], writer: W) -> TurtleResult<()> {
        let mut formatted_writer = FormattedWriter::new(writer, self.config.clone());

        for triple in triples {
            self.serialize_item(triple, &mut formatted_writer)?;
            formatted_writer
                .write_str(" .\n")
                .map_err(TurtleParseError::io)?;
        }

        Ok(())
    }

    fn serialize_item<W: Write>(&self, triple: &Triple, writer: W) -> TurtleResult<()> {
        let mut formatted_writer = FormattedWriter::new(writer, self.config.clone());
        self.serialize_item_formatted(triple, &mut formatted_writer)
    }
}

impl NTriplesSerializer {
    /// Serialize a single triple to a formatted writer
    fn serialize_item_formatted<W: Write>(
        &self,
        triple: &Triple,
        writer: &mut FormattedWriter<W>,
    ) -> TurtleResult<()> {
        self.serialize_subject(triple.subject(), writer)?;
        writer.write_str(" ").map_err(TurtleParseError::io)?;
        self.serialize_predicate(triple.predicate(), writer)?;
        writer.write_str(" ").map_err(TurtleParseError::io)?;
        self.serialize_object(triple.object(), writer)?;
        Ok(())
    }
}

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

    #[test]
    fn test_parse_simple_triple() {
        let parser = NTriplesParser::new();
        let input = r#"<http://example.org/subject> <http://example.org/predicate> "object" ."#;

        let triple = parser
            .parse_line(input, 1)
            .expect("parsing should succeed")
            .expect("parsing should succeed");

        if let Subject::NamedNode(subject) = triple.subject() {
            assert_eq!(subject.as_str(), "http://example.org/subject");
        } else {
            panic!("Expected named node subject");
        }
    }

    #[test]
    fn test_parse_multiple_triples() {
        let parser = NTriplesParser::new();
        let input = r#"<http://example.org/s1> <http://example.org/p1> "o1" .
<http://example.org/s2> <http://example.org/p2> "o2" .
"#;

        let triples = parser
            .parse(Cursor::new(input))
            .expect("parsing should succeed");
        assert_eq!(triples.len(), 2);
    }

    #[test]
    fn test_serialize_triple() {
        let serializer = NTriplesSerializer::new();
        let subject =
            Subject::NamedNode(NamedNode::new("http://example.org/subject").expect("valid IRI"));
        let predicate = Predicate::NamedNode(
            NamedNode::new("http://example.org/predicate").expect("valid IRI"),
        );
        let object = Object::Literal(Literal::new_simple_literal("object"));
        let triple = Triple::new(subject, predicate, object);

        let mut output = Vec::new();
        serializer
            .serialize(&[triple], &mut output)
            .expect("serialization should succeed");

        let output_str = String::from_utf8(output).expect("valid UTF-8");
        assert!(output_str.contains("<http://example.org/subject>"));
        assert!(output_str.contains("\"object\""));
    }
}