oxirs-core 0.2.2

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
//! Turtle Grammar Recognizer
//!
//! Implements the W3C Turtle grammar specification as a rule recognizer
//! that builds RDF triples from N3/Turtle token streams.

#![allow(dead_code)]

use super::error::{ParseResult, RdfParseError, RdfSyntaxError, TextPosition};
use super::n3_lexer::N3Token;
use super::toolkit::{Parser, RuleRecognizer};
use crate::model::{BlankNode, Literal, NamedNode, Object, Predicate, Subject, Triple};
use std::collections::HashMap;

/// AST node types for Turtle grammar
#[derive(Debug, Clone, PartialEq)]
pub enum TurtleNode {
    Triple(Triple),
    PrefixDeclaration { prefix: String, iri: String },
    BaseDeclaration { iri: String },
    Comment(String),
}

/// Turtle parser context for prefix management and base IRI resolution
#[derive(Debug, Clone)]
pub struct TurtleContext {
    /// Current base IRI for relative IRI resolution
    pub base_iri: Option<String>,
    /// Prefix declarations mapping prefix -> IRI
    pub prefixes: HashMap<String, String>,
    /// Auto-generated blank node counter
    pub blank_node_counter: u64,
    /// Current position for error reporting
    pub position: TextPosition,
}

impl Default for TurtleContext {
    fn default() -> Self {
        let mut prefixes = HashMap::new();
        // Add standard prefixes
        prefixes.insert(
            "rdf".to_string(),
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#".to_string(),
        );
        prefixes.insert(
            "rdfs".to_string(),
            "http://www.w3.org/2000/01/rdf-schema#".to_string(),
        );
        prefixes.insert(
            "xsd".to_string(),
            "http://www.w3.org/2001/XMLSchema#".to_string(),
        );
        prefixes.insert(
            "owl".to_string(),
            "http://www.w3.org/2002/07/owl#".to_string(),
        );

        Self {
            base_iri: None,
            prefixes,
            blank_node_counter: 0,
            position: TextPosition::start(),
        }
    }
}

impl TurtleContext {
    pub fn new() -> Self {
        Self::default()
    }

    /// Resolve a prefixed name to a full IRI
    pub fn resolve_prefixed_name(&self, prefix: Option<&str>, local: &str) -> ParseResult<String> {
        match prefix {
            Some(prefix) => match self.prefixes.get(prefix) {
                Some(base_iri) => Ok(format!("{base_iri}{local}")),
                None => Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                    format!("Undefined prefix: {prefix}"),
                    self.position,
                ))),
            },
            None => {
                // Default prefix (empty prefix)
                match self.prefixes.get("") {
                    Some(base_iri) => Ok(format!("{base_iri}{local}")),
                    None => Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                        "No default prefix defined".to_string(),
                        self.position,
                    ))),
                }
            }
        }
    }

    /// Resolve a relative IRI against the base IRI
    pub fn resolve_iri(&self, iri: &str) -> ParseResult<String> {
        if self.is_absolute_iri(iri) {
            Ok(iri.to_string())
        } else {
            match &self.base_iri {
                Some(base) => Ok(self.resolve_relative_iri(base, iri)),
                None => Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                    format!("Relative IRI without base: {iri}"),
                    self.position,
                ))),
            }
        }
    }

    /// Generate a new anonymous blank node
    pub fn generate_blank_node(&mut self) -> BlankNode {
        self.blank_node_counter += 1;
        BlankNode::new(format!("_:gen{}", self.blank_node_counter))
            .expect("generated blank node format is always valid")
    }

    /// Check if an IRI is absolute (has scheme)
    fn is_absolute_iri(&self, iri: &str) -> bool {
        iri.contains(':') && !iri.starts_with(':')
    }

    /// Resolve relative IRI against base IRI
    fn resolve_relative_iri(&self, base: &str, relative: &str) -> String {
        if relative.is_empty() {
            return base.to_string();
        }

        // Simple implementation - in production would use proper URI resolution
        if base.ends_with('/') || base.ends_with('#') {
            format!("{base}{relative}")
        } else {
            format!("{base}/{relative}")
        }
    }
}

/// Turtle grammar recognizer state machine
#[derive(Debug, Clone, PartialEq)]
pub enum TurtleGrammarState {
    /// Expecting statement (triple, directive, or comment)
    ExpectingStatement,
    /// Processing prefix declaration
    PrefixDeclaration { prefix: Option<String> },
    /// Processing base declaration
    BaseDeclaration,
    /// Processing triple with subject
    TripleWithSubject { subject: Subject },
    /// Processing predicate-object list
    PredicateObjectList {
        subject: Subject,
        predicates: Vec<(Predicate, Vec<Object>)>,
    },
    /// Processing object list for current predicate
    ObjectList {
        subject: Subject,
        predicate: Predicate,
        objects: Vec<Object>,
    },
    /// Processing blank node property list
    BlankNodePropertyList {
        properties: Vec<(Predicate, Vec<Object>)>,
    },
    /// Processing collection (RDF list)
    Collection { items: Vec<Object> },
    /// Error recovery state
    ErrorRecovery,
}

/// Turtle grammar recognizer implementation
#[derive(Debug, Clone)]
pub struct TurtleGrammarRecognizer {
    state: TurtleGrammarState,
}

impl Default for TurtleGrammarRecognizer {
    fn default() -> Self {
        Self {
            state: TurtleGrammarState::ExpectingStatement,
        }
    }
}

impl TurtleGrammarRecognizer {
    pub fn new() -> Self {
        Self::default()
    }

    /// Parse a term (subject, predicate, or object) from a token
    fn parse_term(&self, token: &N3Token, context: &mut TurtleContext) -> ParseResult<Object> {
        match token {
            N3Token::Iri(iri) => {
                let resolved_iri = context.resolve_iri(iri)?;
                Ok(Object::NamedNode(
                    NamedNode::new(resolved_iri)
                        .map_err(|e| RdfParseError::internal(e.to_string()))?,
                ))
            }
            N3Token::PrefixedName { prefix, local } => {
                let iri = context.resolve_prefixed_name(prefix.as_deref(), local)?;
                Ok(Object::NamedNode(
                    NamedNode::new(iri).map_err(|e| RdfParseError::internal(e.to_string()))?,
                ))
            }
            N3Token::BlankNode(label) => Ok(Object::BlankNode(
                BlankNode::new(label.clone())
                    .map_err(|e| RdfParseError::internal(e.to_string()))?,
            )),
            N3Token::Literal {
                value,
                datatype,
                language,
            } => {
                let literal: Literal = match (datatype, language) {
                    (Some(dt), None) => {
                        let dt_iri = context.resolve_iri(dt)?;
                        Literal::new_typed_literal(
                            value,
                            NamedNode::new(dt_iri)
                                .map_err(|e| RdfParseError::internal(e.to_string()))?,
                        )
                    }
                    (None, Some(lang)) => Literal::new_language_tagged_literal(value, lang)
                        .map_err(|e| RdfParseError::InvalidLanguageTag(e.to_string()))?,
                    (None, None) => Literal::new_simple_literal(value),
                    (Some(_), Some(_)) => {
                        return Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                            "Literal cannot have both datatype and language tag".to_string(),
                            context.position,
                        )));
                    }
                };
                Ok(Object::Literal(literal))
            }
            N3Token::Integer(i) => {
                let xsd_integer = NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                    .map_err(|e| RdfParseError::internal(e.to_string()))?;
                Ok(Object::Literal(Literal::new_typed_literal(
                    i.to_string(),
                    xsd_integer,
                )))
            }
            N3Token::Decimal(d) => {
                let xsd_decimal = NamedNode::new("http://www.w3.org/2001/XMLSchema#decimal")
                    .map_err(|e| RdfParseError::internal(e.to_string()))?;
                Ok(Object::Literal(Literal::new_typed_literal(
                    d.to_string(),
                    xsd_decimal,
                )))
            }
            N3Token::Double(d) => {
                let xsd_double = NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .map_err(|e| RdfParseError::internal(e.to_string()))?;
                Ok(Object::Literal(Literal::new_typed_literal(
                    d.to_string(),
                    xsd_double,
                )))
            }
            N3Token::True => {
                let xsd_boolean = NamedNode::new("http://www.w3.org/2001/XMLSchema#boolean")
                    .map_err(|e| RdfParseError::internal(e.to_string()))?;
                Ok(Object::Literal(Literal::new_typed_literal(
                    "true",
                    xsd_boolean,
                )))
            }
            N3Token::False => {
                let xsd_boolean = NamedNode::new("http://www.w3.org/2001/XMLSchema#boolean")
                    .map_err(|e| RdfParseError::internal(e.to_string()))?;
                Ok(Object::Literal(Literal::new_typed_literal(
                    "false",
                    xsd_boolean,
                )))
            }
            N3Token::A => {
                // 'a' is shorthand for rdf:type
                let rdf_type = NamedNode::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
                    .map_err(|e| RdfParseError::internal(e.to_string()))?;
                Ok(Object::NamedNode(rdf_type))
            }
            _ => Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                format!("Unexpected token in term position: {token:?}"),
                context.position,
            ))),
        }
    }

    /// Parse a subject from a token
    fn parse_subject(&self, token: &N3Token, context: &mut TurtleContext) -> ParseResult<Subject> {
        match self.parse_term(token, context)? {
            Object::NamedNode(n) => Ok(Subject::NamedNode(n)),
            Object::BlankNode(b) => Ok(Subject::BlankNode(b)),
            _ => Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                "Invalid subject: must be IRI or blank node".to_string(),
                context.position,
            ))),
        }
    }

    /// Parse a predicate from a token
    fn parse_predicate(
        &self,
        token: &N3Token,
        context: &mut TurtleContext,
    ) -> ParseResult<Predicate> {
        match self.parse_term(token, context)? {
            Object::NamedNode(n) => Ok(Predicate::NamedNode(n)),
            _ => Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                "Invalid predicate: must be IRI".to_string(),
                context.position,
            ))),
        }
    }
}

impl RuleRecognizer<TurtleNode> for TurtleGrammarRecognizer {
    fn recognize_next_node<Token>(
        &mut self,
        _parser: &mut Parser<Token>,
    ) -> ParseResult<Option<TurtleNode>> {
        // This is a simplified implementation - the full implementation would be much larger
        // and handle all Turtle grammar rules according to the W3C specification

        // For now, return None to indicate no node recognized
        // In the complete implementation, this would process tokens according to the current state
        // and return TurtleNode::Triple, TurtleNode::PrefixDeclaration, etc.

        Ok(None)
    }
}

/// High-level Turtle parser combining lexer and grammar recognizer
pub struct TurtleParser {
    context: TurtleContext,
}

impl TurtleParser {
    pub fn new() -> Self {
        Self {
            context: TurtleContext::new(),
        }
    }

    /// Parse Turtle from a string into triples
    pub fn parse_str(&mut self, _input: &str) -> ParseResult<Vec<Triple>> {
        let results = Vec::new();

        // For now, return empty results - the full implementation would:
        // 1. Create a lexer with N3Lexer
        // 2. Create a parser with TurtleGrammarRecognizer
        // 3. Process all tokens through the grammar recognizer
        // 4. Convert TurtleNode::Triple results to Triple objects

        Ok(results)
    }

    /// Parse Turtle from a reader into triples
    pub fn parse_reader<R: std::io::Read>(&mut self, _reader: R) -> ParseResult<Vec<Triple>> {
        // Full implementation would use ReaderBuffer from toolkit
        Ok(Vec::new())
    }

    /// Set base IRI for relative IRI resolution
    pub fn set_base_iri(&mut self, base_iri: String) {
        self.context.base_iri = Some(base_iri);
    }

    /// Add a prefix declaration
    pub fn add_prefix(&mut self, prefix: String, iri: String) {
        self.context.prefixes.insert(prefix, iri);
    }

    /// Get the current context (for inspection)
    pub fn context(&self) -> &TurtleContext {
        &self.context
    }
}

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

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

    #[test]
    fn test_turtle_context_prefix_resolution() {
        let context = TurtleContext::new();

        // Test standard prefix resolution
        let resolved = context
            .resolve_prefixed_name(Some("rdf"), "type")
            .expect("prefix resolution should succeed");
        assert_eq!(resolved, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");

        // Test undefined prefix
        assert!(context
            .resolve_prefixed_name(Some("undefined"), "test")
            .is_err());
    }

    #[test]
    fn test_turtle_context_iri_resolution() {
        let mut context = TurtleContext::new();
        context.base_iri = Some("http://example.org/".to_string());

        // Test absolute IRI (should remain unchanged)
        let resolved = context
            .resolve_iri("http://other.org/test")
            .expect("operation should succeed");
        assert_eq!(resolved, "http://other.org/test");

        // Test relative IRI resolution
        let resolved = context
            .resolve_iri("relative")
            .expect("operation should succeed");
        assert_eq!(resolved, "http://example.org/relative");

        // Test relative IRI without base (should error)
        context.base_iri = None;
        assert!(context.resolve_iri("relative").is_err());
    }

    #[test]
    fn test_blank_node_generation() {
        let mut context = TurtleContext::new();

        let bn1 = context.generate_blank_node();
        let bn2 = context.generate_blank_node();

        assert_ne!(bn1, bn2);
        assert!(bn1.to_string().starts_with("_:gen"));
        assert!(bn2.to_string().starts_with("_:gen"));
    }

    #[test]
    fn test_turtle_parser_creation() {
        let parser = TurtleParser::new();
        assert!(parser.context.prefixes.contains_key("rdf"));
        assert!(parser.context.prefixes.contains_key("xsd"));
    }
}