oxirs-star 0.3.1

RDF-star and SPARQL-star grammar support for quoted triples
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
//! Statement-level parsing for Turtle-star and N-Triples-star formats.
//!
//! This sibling module of `crate::parser` contains the line-oriented and
//! statement-oriented parsing for the Turtle-star and N-Triples-star
//! serializations, including directive handling and annotation expansion.

use std::io::{BufRead, BufReader, Read};

use anyhow::{Context, Result};
use tracing::{debug, span, Level};

use crate::model::{StarGraph, StarTerm, StarTriple};
use crate::parser::context::{ErrorSeverity, ParseContext};
use crate::parser::tokenizer;
use crate::parser::StarParser;
use crate::{StarError, StarResult};

impl StarParser {
    /// Parse Turtle-star format with enhanced error handling and multi-line statement support
    pub fn parse_turtle_star<R: Read>(&self, reader: R) -> StarResult<StarGraph> {
        let span = span!(Level::DEBUG, "parse_turtle_star");
        let _enter = span.enter();

        let mut graph = StarGraph::new();
        let mut context = self.create_parse_context();
        let buf_reader = BufReader::new(reader);

        // Accumulator for multi-line statements
        let mut accumulated_line = String::new();
        let mut error_count = 0;
        let max_errors = self.config().max_parse_errors.unwrap_or(100);

        for (line_num, line_result) in buf_reader.lines().enumerate() {
            let line = line_result.map_err(|e| StarError::parse_error(e.to_string()))?;
            // Use SIMD-accelerated trimming
            let line = self.simd_scanner().trim(&line);

            // Update position tracking
            context.update_position(line_num + 1, 0);

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

            // Strip inline comments (but respect strings)
            let line = tokenizer::strip_inline_comment(line);
            // Use SIMD-accelerated trimming
            let line = self.simd_scanner().trim(line);

            // Skip if line becomes empty after comment stripping
            if line.is_empty() {
                continue;
            }

            // Check if line looks incomplete after comment stripping (no period, not a directive, not in a special block)
            // This handles cases like "ex:charlie ex:knows  # missing object"
            // But allow lines that are part of multi-line blocks (annotation blocks or quoted triples)
            // Use SIMD-accelerated pattern detection
            let in_annotation_block = self
                .simd_scanner()
                .contains_annotation_block(&accumulated_line)
                || self.simd_scanner().contains_annotation_block(line);
            let in_quoted_triple = self
                .simd_scanner()
                .contains_quoted_triple(&accumulated_line);

            if !line.ends_with('.')
                && !line.ends_with('{')
                && !line.ends_with('}')
                && !in_annotation_block
                && !in_quoted_triple
            {
                // Check if line contains incomplete quoted triple using SIMD
                let has_incomplete_quoted_triple =
                    if self.simd_scanner().contains_quoted_triple(line) {
                        // Use SIMD-accelerated counting
                        !self.simd_scanner().is_quoted_balanced(line)
                    } else {
                        false
                    };

                // This line appears incomplete and might have been truncated by inline comment
                // Skip it in error recovery mode if:
                // 1. It's not a directive (doesn't start with '@'), AND
                // 2. It's incomplete (doesn't end with '.', '{', '}' and isn't part of annotation/quoted block)
                if !line.starts_with('@') && !context.strict_mode {
                    // This is an incomplete statement - skip it in error recovery mode
                    let reason = if has_incomplete_quoted_triple {
                        "incomplete quoted triple"
                    } else {
                        "incomplete triple (possibly truncated by comment)"
                    };
                    context.add_error(
                        format!("Skipping {}: {line}", reason),
                        line.to_string(),
                        ErrorSeverity::Warning,
                    );
                    continue;
                }
            }

            // Handle directives immediately (they're always single-line)
            if line.starts_with("@prefix") || line.starts_with("@base") {
                match self.parse_turtle_line_safe(line, &mut context, &mut graph) {
                    Ok(_) => continue,
                    Err(e) => {
                        error_count += 1;
                        let line_number = line_num + 1;
                        let error_context = format!("line {line_number}: {line}");
                        context.add_error(
                            format!("Parse error: {e}"),
                            error_context,
                            if context.strict_mode {
                                ErrorSeverity::Fatal
                            } else {
                                ErrorSeverity::Error
                            },
                        );

                        if context.strict_mode || error_count >= max_errors {
                            return Err(StarError::parse_error(format!(
                                "Parsing failed at line {} with {} errors",
                                line_num + 1,
                                context.get_errors().len()
                            )));
                        }
                        continue;
                    }
                }
            }

            // Accumulate lines for multi-line statements
            accumulated_line.push_str(line);
            accumulated_line.push(' ');

            // Check if we have a complete statement (ends with '.')
            if tokenizer::is_complete_turtle_statement(&accumulated_line) {
                match self.parse_turtle_line_safe(accumulated_line.trim(), &mut context, &mut graph)
                {
                    Ok(_) => {
                        accumulated_line.clear();
                    }
                    Err(e) => {
                        error_count += 1;
                        let line_number = line_num + 1;
                        let error_context =
                            format!("line {line_number}: {}", accumulated_line.trim());
                        context.add_error(
                            format!("Parse error: {e}"),
                            error_context,
                            if context.strict_mode {
                                ErrorSeverity::Fatal
                            } else {
                                ErrorSeverity::Error
                            },
                        );

                        if context.strict_mode || error_count >= max_errors {
                            return Err(StarError::parse_error(format!(
                                "Parsing failed at line {} with {} errors. First error: {}",
                                line_num + 1,
                                context.get_errors().len(),
                                context
                                    .get_errors()
                                    .first()
                                    .map(|e| &e.message)
                                    .unwrap_or(&"Unknown error".to_string())
                            )));
                        }

                        // Clear accumulated line and try to recover
                        accumulated_line.clear();
                    }
                }
            }
        }

        // Handle any remaining incomplete statement
        if !accumulated_line.trim().is_empty() {
            context.add_error(
                "Incomplete Turtle-star statement at end of input".to_string(),
                accumulated_line.trim().to_string(),
                ErrorSeverity::Error,
            );
            if context.strict_mode {
                return Err(StarError::parse_error(format!(
                    "Incomplete Turtle-star statement at end of input: {}",
                    accumulated_line.trim()
                )));
            }
        }

        // Report any accumulated errors
        if !context.get_errors().is_empty() {
            debug!(
                "Parsing completed with {} warnings/errors",
                context.get_errors().len()
            );
            for error in context.get_errors() {
                debug!(
                    "Line {}, Column {}: {} ({})",
                    error.line, error.column, error.message, error.context
                );
            }
        }

        debug!("Parsed {} triples in Turtle-star format", graph.len());
        Ok(graph)
    }

    /// Parse a single Turtle-star line with enhanced error handling
    pub(crate) fn parse_turtle_line_safe(
        &self,
        line: &str,
        context: &mut ParseContext,
        graph: &mut StarGraph,
    ) -> StarResult<()> {
        // Handle directives
        if line.starts_with("@prefix") {
            return self.parse_prefix_directive_safe(line, context);
        }

        if line.starts_with("@base") {
            return self.parse_base_directive_safe(line, context);
        }

        // Parse triple
        if let Some(stripped) = line.strip_suffix('.') {
            let triple_str = stripped.trim();

            // Check for annotation syntax {| ... |}
            if triple_str.contains("{|") {
                return self.parse_triple_with_annotation(triple_str, context, graph);
            }

            match self.parse_triple_pattern_safe(triple_str, context) {
                Ok(triple) => {
                    graph.insert(triple).map_err(|e| {
                        StarError::parse_error(format!("Failed to insert triple: {e}"))
                    })?;
                }
                Err(e) => {
                    let error_msg = format!("Failed to parse triple pattern '{triple_str}': {e}");
                    context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);

                    if context.strict_mode {
                        return Err(StarError::parse_error(error_msg));
                    }
                    // In non-strict mode, log error and continue
                }
            }
        } else if !line.trim().is_empty() {
            // Non-empty line that doesn't end with '.' - potential malformed statement
            let error_msg = format!("Malformed statement (missing terminating '.'): {line}");
            context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Warning);

            if context.strict_mode {
                return Err(StarError::parse_error(error_msg));
            }
        }

        Ok(())
    }

    /// Parse triple with annotation syntax: subject predicate object {| annotation properties |} .
    ///
    /// Example: :alice :age 30 {| :certainty 0.9; :source :survey |} .
    /// Expands to:
    ///   <<:alice :age 30>> :certainty 0.9 .
    ///   <<:alice :age 30>> :source :survey .
    pub(crate) fn parse_triple_with_annotation(
        &self,
        statement: &str,
        context: &mut ParseContext,
        graph: &mut StarGraph,
    ) -> StarResult<()> {
        // Find annotation block delimiters
        let annotation_start = statement.find("{|").ok_or_else(|| {
            StarError::parse_error("Missing annotation start delimiter {|".to_string())
        })?;

        let annotation_end = statement.find("|}").ok_or_else(|| {
            StarError::parse_error("Missing annotation end delimiter |}".to_string())
        })?;

        if annotation_end < annotation_start {
            return Err(StarError::parse_error(
                "Annotation delimiters in wrong order".to_string(),
            ));
        }

        // Extract base triple and annotation block
        let base_triple_str = statement[..annotation_start].trim();
        let annotation_block = statement[annotation_start + 2..annotation_end].trim();

        // Parse the base triple
        let base_triple = self.parse_triple_pattern_safe(base_triple_str, context)?;

        // If the base triple has a quoted triple as subject, insert it
        // This is a regular assertion about a quoted triple, not just metadata
        if base_triple.subject.is_quoted_triple() {
            graph.insert(base_triple.clone())?;
        }

        // Create quoted triple from base triple for annotations
        let quoted_triple = StarTerm::quoted_triple(base_triple.clone());

        // Parse annotation properties (separated by semicolons)
        let annotation_statements = annotation_block.split(';');

        for ann_stmt in annotation_statements {
            let ann_stmt = ann_stmt.trim();
            if ann_stmt.is_empty() {
                continue;
            }

            // Parse annotation as predicate-object pair
            let ann_tokens = self
                .tokenize_triple(&format!("_:dummy {ann_stmt}"))
                .map_err(|e| StarError::parse_error(e.to_string()))?;
            if ann_tokens.len() != 3 {
                let error_msg = format!(
                    "Invalid annotation property: expected predicate object, found {} terms",
                    ann_tokens.len() - 1
                );
                context.add_error(
                    error_msg.clone(),
                    ann_stmt.to_string(),
                    ErrorSeverity::Error,
                );
                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
                continue;
            }

            // Parse annotation predicate and object (skip dummy subject at index 0)
            let ann_predicate = self.parse_term_safe(&ann_tokens[1], context)?;
            let ann_object = self.parse_term_safe(&ann_tokens[2], context)?;

            // Create annotation triple: <<base triple>> annotation_predicate annotation_object
            let annotation_triple =
                StarTriple::new(quoted_triple.clone(), ann_predicate, ann_object);

            // Insert annotation triple
            graph.insert(annotation_triple)?;
        }

        Ok(())
    }

    /// Parse a single Turtle-star line (legacy method)
    #[allow(dead_code)]
    pub(crate) fn parse_turtle_line(
        &self,
        line: &str,
        context: &mut ParseContext,
        graph: &mut StarGraph,
    ) -> Result<()> {
        // Handle directives
        if line.starts_with("@prefix") {
            self.parse_prefix_directive(line, context)?;
            return Ok(());
        }

        if line.starts_with("@base") {
            self.parse_base_directive(line, context)?;
            return Ok(());
        }

        // Parse triple
        if let Some(stripped) = line.strip_suffix('.') {
            let triple_str = stripped.trim();
            let triple = self.parse_triple_pattern(triple_str, context)?;
            graph.insert(triple)?;
        }

        Ok(())
    }

    /// Parse @prefix directive with enhanced error handling
    pub(crate) fn parse_prefix_directive_safe(
        &self,
        line: &str,
        context: &mut ParseContext,
    ) -> StarResult<()> {
        // @prefix prefix: <namespace> .
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 3 {
            let prefix_name = parts[1].trim_end_matches(':');
            let namespace = parts[2].trim_matches(['<', '>', '.']);

            // Validate prefix format
            if prefix_name.is_empty() {
                let error_msg = "Empty prefix name in @prefix directive".to_string();
                context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);
                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
            } else if namespace.is_empty() {
                let error_msg = "Empty namespace in @prefix directive".to_string();
                context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);
                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
            } else {
                context
                    .prefixes
                    .insert(prefix_name.to_string(), namespace.to_string());
            }
        } else {
            let error_msg = format!("Malformed @prefix directive: {line}");
            context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);
            if context.strict_mode {
                return Err(StarError::parse_error(error_msg));
            }
        }
        Ok(())
    }

    /// Parse @base directive with enhanced error handling
    pub(crate) fn parse_base_directive_safe(
        &self,
        line: &str,
        context: &mut ParseContext,
    ) -> StarResult<()> {
        // @base <iri> .
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 2 {
            let base_iri = parts[1].trim_matches(['<', '>', '.']);
            if base_iri.is_empty() {
                let error_msg = "Empty base IRI in @base directive".to_string();
                context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);
                if context.strict_mode {
                    return Err(StarError::parse_error(error_msg));
                }
            } else {
                context.base_iri = Some(base_iri.to_string());
            }
        } else {
            let error_msg = format!("Malformed @base directive: {line}");
            context.add_error(error_msg.clone(), line.to_string(), ErrorSeverity::Error);
            if context.strict_mode {
                return Err(StarError::parse_error(error_msg));
            }
        }
        Ok(())
    }

    /// Parse @prefix directive (legacy)
    pub(crate) fn parse_prefix_directive(
        &self,
        line: &str,
        context: &mut ParseContext,
    ) -> Result<()> {
        // @prefix prefix: <namespace> .
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 3 {
            let prefix_name = parts[1].trim_end_matches(':');
            let namespace = parts[2].trim_matches(['<', '>', '.']);
            context
                .prefixes
                .insert(prefix_name.to_string(), namespace.to_string());
        }
        Ok(())
    }

    /// Parse @base directive (legacy)
    pub(crate) fn parse_base_directive(
        &self,
        line: &str,
        context: &mut ParseContext,
    ) -> Result<()> {
        // @base <iri> .
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 2 {
            let base_iri = parts[1].trim_matches(['<', '>', '.']);
            context.base_iri = Some(base_iri.to_string());
        }
        Ok(())
    }

    /// Parse N-Triples-star format
    pub fn parse_ntriples_star<R: Read>(&self, reader: R) -> StarResult<StarGraph> {
        let span = span!(Level::DEBUG, "parse_ntriples_star");
        let _enter = span.enter();

        let mut graph = StarGraph::new();
        let mut context = ParseContext::new();
        let buf_reader = BufReader::new(reader);

        for (line_num, line_result) in buf_reader.lines().enumerate() {
            let line = line_result.map_err(|e| StarError::parse_error(e.to_string()))?;
            let line = line.trim();

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

            if let Some(stripped) = line.strip_suffix('.') {
                let triple_str = &stripped.trim();
                let triple = self
                    .parse_triple_pattern(triple_str, &mut context)
                    .with_context(|| format!("Error parsing line {}: {}", line_num + 1, line))
                    .map_err(|e| StarError::parse_error(e.to_string()))?;
                graph.insert(triple)?;
            } else {
                // In N-Triples-star, all non-empty lines must be valid triples ending with '.'
                return Err(StarError::parse_error(format!(
                    "Invalid N-Triples-star line {}: {}",
                    line_num + 1,
                    line
                )));
            }
        }

        debug!("Parsed {} triples in N-Triples-star format", graph.len());
        Ok(graph)
    }
}