From 66ba1cea5e6e657989a0c7857687b00d11a355d0 Mon Sep 17 00:00:00 2001 From: Tpt Date: Mon, 21 May 2018 11:56:46 +0200 Subject: [PATCH] Adds some constant NamedNodes for basic vocabularies --- src/model/mod.rs | 1 + src/model/vocab.rs | 86 +++++++++++++++++++++++++++ src/rio/turtle/turtle_grammar.rustpeg | 19 +++--- 3 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 src/model/vocab.rs diff --git a/src/model/mod.rs b/src/model/mod.rs index 7a345e4c..c87dbf9b 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -1 +1,2 @@ pub mod data; +pub mod vocab; diff --git a/src/model/vocab.rs b/src/model/vocab.rs new file mode 100644 index 00000000..79c6898d --- /dev/null +++ b/src/model/vocab.rs @@ -0,0 +1,86 @@ +///! Provides ready to use NamedNode for basic RDF vocabularies + +pub mod rdf { + use model::data::NamedNode; + use std::str::FromStr; + + lazy_static! { + pub static ref FIRST: NamedNode = + NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#first").unwrap(); + pub static ref HTML: NamedNode = + NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML").unwrap(); + pub static ref LANG_STRING: NamedNode = + NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString").unwrap(); + pub static ref NIL: NamedNode = + NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil").unwrap(); + pub static ref REST: NamedNode = + NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest").unwrap(); + pub static ref TYPE: NamedNode = + NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").unwrap(); + pub static ref XML_LITERAL: NamedNode = + NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral").unwrap(); + } +} + +pub mod xsd { + ///! NamedNodes for [RDF compatible XSD datatypes](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-compatible-xsd-types) + use model::data::NamedNode; + use std::str::FromStr; + + lazy_static! { + /// true, false + pub static ref BOOLEAN: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#boolean").unwrap(); + /// Dates (yyyy-mm-dd) with or without timezone + pub static ref DATE: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#date").unwrap(); + /// Duration of time (days, hours, minutes, seconds only) + pub static ref DAY_TIME_DURATION: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#dayTimeDuration").unwrap(); + /// Date and time with or without timezone + pub static ref DATE_TIME: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#dateTime").unwrap(); + /// Date and time with required timezone + pub static ref DATE_TIME_STAMP: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#dateTimeStamp").unwrap(); + /// Arbitrary-precision decimal numbers + pub static ref DECIMAL: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#decimal").unwrap(); + /// 64-bit floating point numbers incl. ±Inf, ±0, NaN + pub static ref DOUBLE: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#double").unwrap(); + /// Duration of time + pub static ref DURATION: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#duration").unwrap(); + /// 32-bit floating point numbers incl. ±Inf, ±0, NaN + pub static ref FLOAT: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#float").unwrap(); + /// Gregorian calendar day of the month + pub static ref G_DAY: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#gDay").unwrap(); + /// Gregorian calendar month + pub static ref G_MONTH: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#gMonth").unwrap(); + /// Gregorian calendar month and day + pub static ref G_MONTH_DAY: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#gMonthDay").unwrap(); + /// Gregorian calendar year + pub static ref G_YEAR: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#gYear").unwrap(); + /// Gregorian calendar year and month + pub static ref G_YEAR_MONTH: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#gYearMonth").unwrap(); + /// Arbitrary-size integer numbers + pub static ref INTEGER: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#integer").unwrap(); + /// Times (hh:mm:ss.sss…) with or without timezone + pub static ref TIME: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#time").unwrap(); + /// Character strings (but not all Unicode character strings) + pub static ref STRING: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#string").unwrap(); + /// Duration of time (months and years only) + pub static ref YEAR_MONTH_DURATION: NamedNode = + NamedNode::from_str("http://www.w3.org/2001/XMLSchema#yearMonthDuration").unwrap(); + } +} diff --git a/src/rio/turtle/turtle_grammar.rustpeg b/src/rio/turtle/turtle_grammar.rustpeg index a3b3900b..915b31f0 100644 --- a/src/rio/turtle/turtle_grammar.rustpeg +++ b/src/rio/turtle/turtle_grammar.rustpeg @@ -3,7 +3,8 @@ use std::char; use url::Url; use model::data::*; -use std::str::FromStr; +use model::vocab::rdf; +use model::vocab::xsd; use std::iter; use rio::turtle::ParserState; @@ -71,7 +72,7 @@ objectList -> () = object _ ("," _ object _)* //[9] verb -> NamedNode = predicate / - "a" { NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").unwrap() } + "a" { rdf::TYPE.clone() } // [10] subject -> NamedOrBlankNode = @@ -116,13 +117,11 @@ blankNodePropertyList_open -> () = "[" { //[15] collection -> NamedOrBlankNode = '(' _ o:(collection_value*) ')' { - let first = NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#first").unwrap(); - let rest = NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest").unwrap(); - let mut current_list_node = NamedOrBlankNode::from(NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil").unwrap()); + let mut current_list_node = NamedOrBlankNode::from(rdf::NIL.clone()); for obj in o.into_iter().rev() { let new_blank_node = NamedOrBlankNode::from(data_factory.new_blank_node()); - buffer.push(Triple::new(new_blank_node.clone(), first.clone(), obj)); - buffer.push(Triple::new(new_blank_node.clone(), rest.clone(), current_list_node)); + buffer.push(Triple::new(new_blank_node.clone(), rdf::FIRST.clone(), obj)); + buffer.push(Triple::new(new_blank_node.clone(), rdf::REST.clone(), current_list_node)); current_list_node = new_blank_node; } current_list_node @@ -131,9 +130,9 @@ collection_value -> Term = o:object_value _ { o } //[16] NumericLiteral -> Literal = - d:$(DOUBLE) { data_factory.typed_literal(d, NamedNode::from_str("http://www.w3.org/2001/XMLSchema#double").unwrap()) } / - d:$(DECIMAL) { data_factory.typed_literal(d, NamedNode::from_str("http://www.w3.org/2001/XMLSchema#decimal").unwrap()) } / - i:$(INTEGER) { data_factory.typed_literal(i, NamedNode::from_str("http://www.w3.org/2001/XMLSchema#integer").unwrap()) } + d:$(DOUBLE) { data_factory.typed_literal(d, xsd::DOUBLE.clone()) } / + d:$(DECIMAL) { data_factory.typed_literal(d, xsd::DECIMAL.clone()) } / + i:$(INTEGER) { data_factory.typed_literal(i, xsd::INTEGER.clone()) } //[128s] RDFLiteral -> Literal =