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
use std::collections::HashMap;

use if_chain::if_chain;

use crate::core::*;
use crate::parsing::base::{
    Parsed,
    RDFParser,
    ParserError
};

#[derive(Clone, Debug, PartialEq)]
enum Keyword {
    Prefix,
    Base
}

#[derive(Clone, PartialEq)]
enum Token {
    Keyword(Keyword),
    Whitespace,
    TripleSep,
    PredicateSep,
    ObjectSep,
    Word(String),
    PropertyListOpen,
    PropertyListClose,
    CollectionOpen,
    CollectionClose,
}

impl std::fmt::Debug for Token {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self {
            Token::Keyword(kw) => write!(f, "{:?}", kw),
            Token::Whitespace => write!(f, " "),
            Token::TripleSep => write!(f, "."),
            Token::PredicateSep => write!(f, ";"),
            Token::ObjectSep => write!(f, ","),
            Token::Word(word) => write!(f, "{}", word),
            Token::PropertyListOpen => write!(f, "["),
            Token::PropertyListClose => write!(f, "]"),
            Token::CollectionOpen => write!(f, "("),
            Token::CollectionClose => write!(f, ")"),
        }
    }
}

pub struct TurtleParser;
impl TurtleParser {
    // A very simple lexer to tokenize rdf input for later parsing
    fn tokenize(s: &str) -> Vec<Token> {
        let chars: Vec<char> = s.chars().collect();

        let mut tokens: Vec<Token> = Vec::new();
        let mut current: String = String::new();
        
        let mut quoted = false;
        for c in chars {
            match c {
                c if (c.is_whitespace() || c == ',') && !quoted => {
                    if !current.is_empty() {
                        match &current as &str {
                            "@prefix" | "PREFIX" => {
                                tokens.push(Token::Keyword(Keyword::Prefix));
                            },
                            "@base" | "BASE" => {
                                tokens.push(Token::Keyword(Keyword::Base));
                            },
                            _ => { 
                                tokens.push(Token::Word(current));
                            }
                        }

                        current = String::new();
                    }
                    
                    if c == ',' {
                        tokens.push(Token::ObjectSep);
                    } else {
                        tokens.push(Token::Whitespace);
                    }
                },
                '.' if current.is_empty() && !quoted => {
                    tokens.push(Token::TripleSep);
                },
                ';' if current.is_empty() && !quoted => {
                    tokens.push(Token::PredicateSep);
                },
                '[' if current.is_empty() && !quoted => {
                    tokens.push(Token::PropertyListOpen);
                },
                ']' if current.is_empty() && !quoted => {
                    tokens.push(Token::PropertyListClose);
                },
                '(' if current.is_empty() && !quoted => {
                    tokens.push(Token::CollectionOpen);
                },
                ')' if current.is_empty() && !quoted => {
                    tokens.push(Token::CollectionClose);
                },
                _ => { 
                    if c == '"' { quoted = !quoted; }
                    current += &c.to_string();
                }
            }
        }

        tokens
    }

    // Expands an rdf collection into its corresponding blank property list format
    // (https://w3.org/TR/turtle/ Examples 20 and 21)
    fn expand_collection_tokens_naive(mut tokens: Vec<Token>) -> Parsed<Vec<Token>> {
        let mut expanded: Vec<Token> = vec![Token::PropertyListOpen];

        let last: Token = tokens.pop().unwrap();
        
        // Will fail if the collection contains anything but Token::Word(..)s 
        // TODO: more thorough collection unwrapping
        for word in &tokens {
            if let Token::Word(word) = word {
                expanded.append(&mut vec![
                    Token::Word("rdf:first".into()),
                    Token::Word(word.to_string()),
                    Token::PredicateSep,
                    Token::Word("rdf:rest".into()),
                    Token::PropertyListOpen
                ])
            } else {
                return Err(ParserError(format!("Collections can only contain Words; {:?}", word)));
            }
        }
        
        expanded.append(&mut vec![
            Token::Word("rdf:first".into()),
            last,
            Token::PredicateSep,
            Token::Word("rdf:rest".into()),
            Token::Word("rdf:nil".into()),
            Token::PropertyListClose,
            Token::PropertyListClose
        ]);

        Ok(expanded)
    }
    
    fn parse_triple_recursive(mut tokens: Vec<Token>, mut triples: Vec<Triple>, blank_node_num: &mut usize) -> Parsed<Vec<Triple>> {
        // Expand collections into blank property lists.
        if tokens.contains(&Token::CollectionOpen) {
            // Get the index of the first open paren
            let first_open_index = tokens.iter().position(|t| t == &Token::CollectionOpen).unwrap();

            // Get the index of the associated closing paren
            let mut depth = 0;
            let mut opened = false;
            let collection_close_index = tokens.iter().position(|t| {
                if let Token::CollectionOpen = t {
                    opened = true;
                    depth += 1;
                } else if let Token::CollectionClose = t {
                    depth -= 1;
                }

                depth == 0 && opened
            }).unwrap();

            // Get the tokens just within the parens
            let collection_tokens = tokens[first_open_index + 1..collection_close_index].to_vec();
            // and expand them into nested blank property lists.
            let collection_tokens = Self::expand_collection_tokens_naive(collection_tokens)?;

            // Replace the collection in the original token list with the expanded version
            tokens.splice(first_open_index..=collection_close_index, collection_tokens);
            // Parse the new expanded version of the tokens.
            return Ok(Self::parse_triple_recursive(tokens, triples, blank_node_num)?);
        }

        // If the first token is a word (the subject)...
        if let Token::Word(subject) = &tokens[0] {
            // ...And the second token is a word (the predicate)...
            if let Token::Word(predicate) = &tokens[1] {
                // ...And the third token is a word (the object)...
                if let Token::Word(object) = &tokens[2] {
                    // Then add this triple to the list.
                    triples.push(
                       (Self::resource(&subject)?, Self::relationship(&predicate)?, Self::object(&object)?).into()
                    );
                    
                    // If this is the end of the triple,
                    if let Token::TripleSep = &tokens[3] {
                        // return the triples.
                        Ok(triples)
                    // If the triple continues with a list of predicates,
                    } else if let Token::PredicateSep = &tokens[3] {
                        // Remove the object and predicate pair that was just parsed
                        tokens.drain(1..=3);
                        // and continue parsing.
                        Ok(Self::parse_triple_recursive(tokens, triples, blank_node_num)?)
                    // If the triple continues with a list of objects,
                    } else if let Token::ObjectSep = &tokens[3] {
                        // Remove the object that was just parsed
                        tokens.drain(2..=3);
                        // and continue parsing
                        Ok(Self::parse_triple_recursive(tokens, triples, blank_node_num)?)
                    } else {
                        Err(ParserError(format!("Triple must end with ' .' or continue with ',' or ' ;'. Found: {:?}", &tokens[3])))
                    }
                // ...And the object is a blank property list...
                } else if let Token::PropertyListOpen = &tokens[2] {
                    // First, increment the node_num for variable naming
                    *blank_node_num += 1;

                    // Second, get just the inner portion of the list
                    let object_tokens = &tokens[2..].to_vec();
                    let mut depth: i8 = 0;
                    let mut i = 0;
                    let token_parts = object_tokens.splitn(2, |t| {
                        i += 1;
                        if let Token::PropertyListOpen = t {
                            depth += 1;
                        } else if let Token::PropertyListClose = t {
                            depth -= 1;
                        }

                        depth == 0 && i > 2
                    }).collect::<Vec<&[Token]>>();

                    let mut inner = token_parts[0].to_vec();

                    // Replace the opening brace with a blank subject token
                    let object = format!("_:blank{}", blank_node_num);
                    inner[0] = Token::Word(object.clone());
                    // And append with a triple terminator
                    inner.push(Token::TripleSep);

                    // Get the list of triples from within the blank prop list
                    let inner_triples = Self::parse_triple_recursive(inner, triples.clone(), blank_node_num)?;

                    // Insert the subject of the prop list as the object of the current triple
                    let mut tokens = tokens[..2].to_vec();
                    tokens.push(Token::Word(object));
                    tokens.append(&mut token_parts[1].to_vec());
                    
                    // Rerun with new tokens and triples
                    Ok(Self::parse_triple_recursive(tokens, inner_triples, blank_node_num)?)
                } else {
                    Err(ParserError(format!("Object must be a resource, literal, or a property list. Found: {:?}", &tokens[2])))
                }
            } else {
                Err(ParserError(format!("Predicate must be a valid URI. Found: {:?}", &tokens[1])))
            }
        // If the subject is a blank property list...
        } else if let Token::PropertyListOpen = &tokens[0] {
            // First, increment the node_num for variable naming
            *blank_node_num += 1;

            // Second, get just the inner portion of the list
            let mut depth: i8 = 0;
            let token_parts = tokens.splitn(2, |t| {
                if let Token::PropertyListOpen = t {
                    depth += 1;
                } else if let Token::PropertyListClose = t {
                    depth -= 1;
                }

                depth == 0
            }).collect::<Vec<&[Token]>>();

            let mut inner = token_parts[0].to_vec();

            // Replace the opening brace with a blank subject token
            inner[0] = Token::Word(format!("_:blank{}", blank_node_num));
            // and append with a Triple terminator
            inner.push(Token::TripleSep);

            // Get the list of triples from the inner section of the blank prop list
            let inner_triples = Self::parse_triple_recursive(inner, triples.clone(), blank_node_num)?;
            
            // Insert the subject of the prop list as the subject of the current triple
            let mut tokens = token_parts[1].to_vec();
            tokens.insert(0, Token::Word(inner_triples[0].subject.to_string()));

            // Rerun with new triples and tokens
            Ok(Self::parse_triple_recursive(tokens, inner_triples, blank_node_num)?)
        } else {
            Err(ParserError(format!("Subject must be a valid URI or a blank property list. Found: {:?}", &tokens[0])))
        }
    }
}

impl RDFParser for TurtleParser {
    /// Parses a [`Uri`] from a string
    ///
    /// # Errors
    ///
    /// Returns a [`ParserError`] if the string is not a valid URI
    ///
    /// # Examples
    ///
    /// ```
    /// # use rdf_rs::parsing::{ TurtleParser, RDFParser, ParserError };
    /// # fn main() -> Result<(), ParserError> {
    /// let uri = TurtleParser::uri("<http://example.com/rdf/Person>")?;
    /// let uri = TurtleParser::uri("<#Person>")?;
    /// let uri = TurtleParser::uri("ex:Person")?;
    /// let uri = TurtleParser::uri(":Person")?;
    /// # Ok(())
    /// # }
    /// ```
    fn uri(u: &str) -> Parsed<Uri> {
        use crate::core::uri::{ UriType, matches };

        let full_url = &matches::FULL_URL;
        let relative_url = &matches::RELATIVE_URL;
        let prefixed = &matches::PREFIXED;
        let empty_prefix = &matches::EMPTY_PREFIX;

        // Trim the leading and trailing whitespace.
        let u = u.trim();

        // If u is in the form <http://[valid url]/foo> or <http://[valid url]#foo>
        let (prefix, name, uri_type) = if full_url.is_match(u) {
            let caps = full_url.captures(u).unwrap();

            (caps[1].to_string(), caps[2].to_string(), UriType::Full)
        // If u is in the form <#foo>
        } else if relative_url.is_match(u) {
            let caps = relative_url.captures(u).unwrap();

            ("".into(), caps[1].to_string(), UriType::Relative)
        // If u is in the form prefix:foo
        } else if prefixed.is_match(u) {
            let caps = prefixed.captures(u).unwrap();

            (caps[1].to_string(), caps[2].to_string(), UriType::Prefixed)
        // If u is in the form :foo
        } else if empty_prefix.is_match(u) {
            let caps = empty_prefix.captures(u).unwrap();

            ("".into(), caps[1].to_string(), UriType::PrefixedWithBase)
        // If u is the identity relationship
        } else if u == "a" {
            ("http://www.w3.org/1999/02/22-rdf-syntax-ns/".into(), "type".into(), UriType::Full)
        } else {
            return Err(ParserError(format!("Invalid URI: {}", u)));
        };

        Ok(Uri::new(&prefix, &name, uri_type))
    }

    /// Parses a [`Resource`] from a string. A wrapper around [`RDFParser::uri()`] specifically 
    /// for RDF resources.
    ///
    /// # Errors
    ///
    /// Returns a [`ParserError`] if the string is not a valid Resource
    ///
    /// # Examples
    ///
    /// ```
    /// # use rdf_rs::parsing::{ TurtleParser, RDFParser, ParserError };
    /// # fn main() -> Result<(), ParserError> {
    /// let res = TurtleParser::resource("<http://example.com/rdf/Person>")?;
    /// let res = TurtleParser::resource("<#Person>")?;
    /// let res = TurtleParser::resource("ex:Person")?;
    /// let res = TurtleParser::resource(":Person")?;
    /// # Ok(())
    /// # }
    /// ```
    fn resource(r: &str) -> Parsed<Resource> {
        let uri = Self::uri(r)?;
        Ok(Resource(uri))
    }

    /// Parses a [`Relationship`] from a string. A wrapper around [`RDFParser::uri()`] specifically 
    /// for RDF relationships.
    ///
    /// # Errors
    ///
    /// Returns a [`ParserError`] if the string is not a valid Relationship
    ///
    /// # Examples
    ///
    /// ```
    /// # use rdf_rs::parsing::{ TurtleParser, RDFParser, ParserError };
    /// # fn main() -> Result<(), ParserError> {
    /// let rel = TurtleParser::relationship("<http://example.com/foaf#knows>")?;
    /// let rel = TurtleParser::relationship("<#knows>")?;
    /// let rel = TurtleParser::relationship("foaf:knows")?;
    /// let rel = TurtleParser::relationship(":knows")?;
    /// # Ok(())
    /// # }
    /// ```
    fn relationship(r: &str) -> Parsed<Relationship> {
        let uri = Self::uri(r)?;
        Ok(Relationship(uri))
    }

    /// Parses an [`Object`] from a string.
    ///
    /// # Errors
    ///
    /// Returns a [`ParserError`] if the string is not a valid Object
    ///
    /// # Examples
    ///
    /// ```
    /// # use rdf_rs::parsing::{ TurtleParser, RDFParser, ParserError };
    /// # fn main() -> Result<(), ParserError> {
    /// let obj = TurtleParser::object(r#""john@example.com""#)?;
    /// let obj = TurtleParser::object(r#""すし"@jp"#)?;
    /// let obj = TurtleParser::object("foaf:Person")?;
    /// # Ok(())
    /// # }
    /// ```
    fn object(o: &str) -> Parsed<Object> {
        use crate::core::object::matches;
        let with_datatype = &matches::WITH_DATATYPE;
        let with_lang = &matches::WITH_LANG;

        // If o is in the form "literal"^^datatype:uri
        if with_datatype.is_match(o) {
            let caps = with_datatype.captures(o).unwrap();
            Ok(Object::Literal(Literal{
                value: caps[1].to_string(),
                datatype: Self::uri(&caps[2])?,
                language: None
            }))
        // If o is in the form "some string"@lang
        } else if with_lang.is_match(o) {
            let caps = with_lang.captures(o).unwrap();
            Ok(Object::Literal(Literal{
                value: caps[1].to_string(),
                datatype: Self::uri("xsd:string")?,
                language: Some(caps[2].to_string())
            }))
        // If o is a valid URI
        } else if let Ok(uri) = Self::uri(o) {
            Ok(Object::Resource(uri))
        // If o is a boolean
        } else if o == "true" || o == "false" {
            Ok(Object::Literal(Literal{
                value: o.to_string(),
                datatype: Self::uri("xsd:boolean")?,
                language: None
            }))
        // Else, o is a string literal
        } else {
            Ok(Object::Literal(Literal {
                value: o.to_string(),
                datatype: Self::uri("xsd:string")?,
                language: None
            }))
        }
    }

    /// Parses a [`Vec<Triple>`] from a string.
    ///
    /// # Errors
    ///
    /// Returns a [`ParserError`] if the string is not a valid Triple or 
    /// collection of triples.
    ///
    /// # Examples
    ///
    /// ```
    /// # use rdf_rs::parsing::{ TurtleParser, RDFParser, ParserError };
    /// # fn main() -> Result<(), ParserError> {
    /// let triple = TurtleParser::triple(r#"ex:John foaf:mbox "john@example.com" ."#)?;
    /// let triple = TurtleParser::triple(r#"[ foaf:name "Alice" ] foaf:knows [ foaf:name "Bob" ] ."#)?;
    /// # Ok(())
    /// # }
    /// ```
    fn triple(t: &str) -> Parsed<Vec<Triple>> {
        // Strip all of the whitespace tokens
        let tokens: Vec<Token> = Self::tokenize(t).into_iter().filter(|t| t != &Token::Whitespace).collect();

        Ok(Self::parse_triple_recursive(tokens, Vec::new(), &mut 0)?)
    }
    
    /// Parses a [`Graph`] from a string (typically a file).
    ///
    /// # Errors
    ///
    /// Returns a [`ParserError`] if the string is not a valid Graph.
    ///
    /// # Examples
    ///
    /// ```
    /// # use rdf_rs::parsing::{ TurtleParser, RDFParser, ParserError };
    /// # fn main() -> Result<(), ParserError> {
    /// let triple = TurtleParser::graph(r#"
    ///     @base <http://example.com/> .
    ///     @prefix foaf: <http://xmlns.com/foaf/0.1/> .
    ///     @prefix owl: <http://www.w3.org/2002/07/owl#> .
    ///
    ///     :John foaf:mbox "john@example.com" .
    ///     [ foaf:mbox "john@example.com" ] owl:sameAs :John .
    /// "#)?;
    /// # Ok(())
    /// # }
    /// ```
    fn graph(g: &str) -> Parsed<Graph> {
        let tokens: Vec<Token> = Self::tokenize(g);
        let mut base_prefix: Option<String> = None;

        // Set the base of the graph if it exists
        if tokens.contains(&Token::Keyword(Keyword::Base)) {
            tokens.iter()
                .filter(|&t| t != &Token::Whitespace)
                .collect::<Vec<&Token>>()
                .split_inclusive(|&t| t == &Token::TripleSep)
                .for_each(|w| {
                    if_chain! {
                        if let Token::Keyword(Keyword::Base) = &w[0];
                        if let Token::Word(prefix) = &w[1];
                        if let Token::TripleSep = &w[2];
                        then {
                            base_prefix = Some(prefix.replace(|c| { "<>".contains(c) }, ""));
                        }
                    }
                });
        }

        let mut prefixes: HashMap<String, String> = HashMap::new();

        // default prefixes
        prefixes.insert("rdf:".into(), "http://www.w3.org/1999/02/22-rdf-syntax-ns#".into());
        prefixes.insert("xsd:".into(), "http://www.w3.org/2001/XMLSchema#".into());

        // If the graph contains prefixes, parse them
        if tokens.contains(&Token::Keyword(Keyword::Prefix)) {
            tokens.iter()
                .filter(|&t| t != &Token::Whitespace)
                .collect::<Vec<&Token>>()
                .split_inclusive(|&t| t == &Token::TripleSep)
                .for_each(|w| {
                    if_chain! {
                        if let Token::Keyword(Keyword::Prefix) = &w[0];
                        if let Token::Word(prefix) = &w[1];
                        if let Token::Word(expanded) = &w[2];
                        if let Token::TripleSep = &w[3];
                        then {
                            prefixes.insert(prefix.to_string(), expanded.replace(|c| { "<>".contains(c) }, ""));
                        }
                    }
                });
        }

        // Split the input string on 'decimal then newline' 
        // and filter on the lines that don't start with '@' (metadata)
        let full_triples: Vec<&str> = g.split_inclusive(".\n").filter(|t| !t.starts_with('@')).collect();
        let triples: Vec<Triple> = full_triples.into_iter().flat_map(|t| {
            // Trim leading and trainling whitespace
            Self::triple(t.trim()).unwrap()
        }).collect();

        Ok(Graph {
            base_prefix,
            prefixes,
            triples
        })
    }
}