From 257cd08846ff07f0e13800bc43dfdf63c3be742e Mon Sep 17 00:00:00 2001 From: Tpt Date: Tue, 26 Jun 2018 18:05:05 +0200 Subject: [PATCH] Forbids variables in property path Allows BGP to contain TriplePattern --- src/sparql/algebra.rs | 218 +++++++++++++++++------------- src/sparql/parser.rs | 31 ++++- src/sparql/sparql_grammar.rustpeg | 67 ++++----- 3 files changed, 183 insertions(+), 133 deletions(-) diff --git a/src/sparql/algebra.rs b/src/sparql/algebra.rs index 719c60a8..d22991a2 100644 --- a/src/sparql/algebra.rs +++ b/src/sparql/algebra.rs @@ -7,7 +7,7 @@ use utils::Escaper; #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] pub enum PropertyPath { - PredicatePath(NamedNodeOrVariable), //TODO: restrict to NamedNode + PredicatePath(NamedNode), InversePath(Box), SequencePath(Box, Box), AlternativePath(Box, Box), @@ -20,7 +20,7 @@ pub enum PropertyPath { impl fmt::Display for PropertyPath { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - PropertyPath::PredicatePath(p) => write!(f, "{}", p), + PropertyPath::PredicatePath(p) => write!(f, "link({})", p), PropertyPath::InversePath(p) => write!(f, "inv({})", p), PropertyPath::AlternativePath(a, b) => write!(f, "alt({}, {})", a, b), PropertyPath::SequencePath(a, b) => write!(f, "seq({}, {})", a, b), @@ -73,38 +73,26 @@ impl<'a> fmt::Display for SparqlPropertyPath<'a> { } } -impl From for PropertyPath { - fn from(p: NamedNodeOrVariable) -> Self { - PropertyPath::PredicatePath(p) - } -} - impl From for PropertyPath { fn from(p: NamedNode) -> Self { PropertyPath::PredicatePath(p.into()) } } -impl From for PropertyPath { - fn from(p: Variable) -> Self { - PropertyPath::PredicatePath(p.into()) - } -} - #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] -pub struct PropertyPathPattern { +pub struct PathPattern { pub subject: TermOrVariable, pub path: PropertyPath, pub object: TermOrVariable, } -impl fmt::Display for PropertyPathPattern { +impl fmt::Display for PathPattern { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{} {} {}", self.subject, self.path, self.object) + write!(f, "Path({} {} {})", self.subject, self.path, self.object) } } -impl PropertyPathPattern { +impl PathPattern { pub fn new( subject: impl Into, path: impl Into, @@ -118,19 +106,9 @@ impl PropertyPathPattern { } } -impl From for PropertyPathPattern { - fn from(p: TriplePattern) -> Self { - Self { - subject: p.subject, - path: p.predicate.into(), - object: p.object, - } - } -} - -struct SparqlPropertyPathPattern<'a>(&'a PropertyPathPattern); +struct SparqlPathPattern<'a>(&'a PathPattern); -impl<'a> fmt::Display for SparqlPropertyPathPattern<'a> { +impl<'a> fmt::Display for SparqlPathPattern<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, @@ -142,6 +120,44 @@ impl<'a> fmt::Display for SparqlPropertyPathPattern<'a> { } } +#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] +pub enum TripleOrPathPattern { + Triple(TriplePattern), + Path(PathPattern), +} + +impl<'a> fmt::Display for TripleOrPathPattern { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + TripleOrPathPattern::Triple(tp) => write!(f, "{}", tp), + TripleOrPathPattern::Path(ppp) => write!(f, "{}", ppp), + } + } +} + +impl From for TripleOrPathPattern { + fn from(tp: TriplePattern) -> Self { + TripleOrPathPattern::Triple(tp) + } +} + +impl From for TripleOrPathPattern { + fn from(ppp: PathPattern) -> Self { + TripleOrPathPattern::Path(ppp) + } +} + +struct SparqlTripleOrPathPattern<'a>(&'a TripleOrPathPattern); + +impl<'a> fmt::Display for SparqlTripleOrPathPattern<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.0 { + TripleOrPathPattern::Triple(tp) => write!(f, "{}", tp), + TripleOrPathPattern::Path(ppp) => write!(f, "{}", SparqlPathPattern(&ppp)), + } + } +} + #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] pub enum Expression { ConstantExpression(TermOrVariable), @@ -750,7 +766,7 @@ impl<'a> fmt::Display for SparqlExpression<'a> { #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] pub enum MultiSetPattern { - BGP(Vec), + BGP(Vec), Join(Box, Box), LeftJoin(Box, Box, Expression), Filter(Expression, Box), @@ -792,8 +808,8 @@ impl Default for MultiSetPattern { } } -impl From for MultiSetPattern { - fn from(p: PropertyPathPattern) -> Self { +impl From for MultiSetPattern { + fn from(p: TripleOrPathPattern) -> Self { MultiSetPattern::BGP(vec![p]) } } @@ -818,12 +834,26 @@ impl MultiSetPattern { match self { MultiSetPattern::BGP(p) => { for pattern in p { - if let TermOrVariable::Variable(ref s) = pattern.subject { - vars.insert(s); - } - //TODO: pred - if let TermOrVariable::Variable(ref o) = pattern.object { - vars.insert(o); + match pattern { + TripleOrPathPattern::Triple(tp) => { + if let TermOrVariable::Variable(ref s) = tp.subject { + vars.insert(s); + } + if let NamedNodeOrVariable::Variable(ref p) = tp.predicate { + vars.insert(p); + } + if let TermOrVariable::Variable(ref o) = tp.object { + vars.insert(o); + } + } + TripleOrPathPattern::Path(ppp) => { + if let TermOrVariable::Variable(ref s) = ppp.subject { + vars.insert(s); + } + if let TermOrVariable::Variable(ref o) = ppp.object { + vars.insert(o); + } + } } } } @@ -865,7 +895,7 @@ impl<'a> fmt::Display for SparqlMultiSetPattern<'a> { f, "{}", p.iter() - .map(|v| SparqlPropertyPathPattern(v).to_string()) + .map(|v| SparqlTripleOrPathPattern(v).to_string()) .collect::>() .join(" . ") ) @@ -1318,12 +1348,12 @@ impl fmt::Display for Query { #[test] fn test_sparql_algebra_examples() { assert_eq!( - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p"), Variable::new("o") )).try_into(), - Ok(GraphPattern::BGP(vec![PropertyPathPattern::new( + Ok(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p"), Variable::new("o"), @@ -1332,43 +1362,43 @@ fn test_sparql_algebra_examples() { assert_eq!( ast::GraphPattern::GroupPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )), - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), )), ]).try_into(), Ok(GraphPattern::BGP(vec![ - PropertyPathPattern::new(Variable::new("s"), Variable::new("p1"), Variable::new("v1")), - PropertyPathPattern::new(Variable::new("s"), Variable::new("p2"), Variable::new("v2")), + PathPattern::new(Variable::new("s"), Variable::new("p1"), Variable::new("v1")), + PathPattern::new(Variable::new("s"), Variable::new("p2"), Variable::new("v2")), ])) ); assert_eq!( ast::GraphPattern::UnionPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )), - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), )), ]).try_into(), Ok(GraphPattern::Union( - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )])), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), @@ -1378,17 +1408,17 @@ fn test_sparql_algebra_examples() { assert_eq!( ast::GraphPattern::UnionPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )), - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), )), - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p3"), Variable::new("v3"), @@ -1396,18 +1426,18 @@ fn test_sparql_algebra_examples() { ]).try_into(), Ok(GraphPattern::Union( Box::new(GraphPattern::Union( - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )])), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), )])), )), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p3"), Variable::new("v3"), @@ -1417,13 +1447,13 @@ fn test_sparql_algebra_examples() { assert_eq!( ast::GraphPattern::GroupPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )), - ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PropertyPathPattern( - ast::PropertyPathPattern::new( + ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PathPattern( + ast::PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), @@ -1431,12 +1461,12 @@ fn test_sparql_algebra_examples() { ))), ]).try_into(), Ok(GraphPattern::LeftJoin( - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )])), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), @@ -1447,20 +1477,20 @@ fn test_sparql_algebra_examples() { assert_eq!( ast::GraphPattern::GroupPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )), - ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PropertyPathPattern( - ast::PropertyPathPattern::new( + ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PathPattern( + ast::PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), ), ))), - ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PropertyPathPattern( - ast::PropertyPathPattern::new( + ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PathPattern( + ast::PathPattern::new( Variable::new("s"), Variable::new("p3"), Variable::new("v3"), @@ -1469,19 +1499,19 @@ fn test_sparql_algebra_examples() { ]).try_into(), Ok(GraphPattern::LeftJoin( Box::new(GraphPattern::LeftJoin( - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )])), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), )])), ast::Expression::ConstantExpression(Literal::from(true).into()), )), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p3"), Variable::new("v3"), @@ -1492,13 +1522,13 @@ fn test_sparql_algebra_examples() { assert_eq!( ast::GraphPattern::GroupPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )), ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::GroupPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), @@ -1512,12 +1542,12 @@ fn test_sparql_algebra_examples() { ]))), ]).try_into(), Ok(GraphPattern::LeftJoin( - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )])), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), @@ -1534,19 +1564,19 @@ fn test_sparql_algebra_examples() { assert_eq!( ast::GraphPattern::GroupPattern(vec![ ast::GraphPattern::UnionPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )), - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), )), ]), - ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PropertyPathPattern( - ast::PropertyPathPattern::new( + ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PathPattern( + ast::PathPattern::new( Variable::new("s"), Variable::new("p3"), Variable::new("v3"), @@ -1555,18 +1585,18 @@ fn test_sparql_algebra_examples() { ]).try_into(), Ok(GraphPattern::LeftJoin( Box::new(GraphPattern::Union( - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )])), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), )])), )), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p3"), Variable::new("v3"), @@ -1577,7 +1607,7 @@ fn test_sparql_algebra_examples() { assert_eq!( ast::GraphPattern::GroupPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), @@ -1588,8 +1618,8 @@ fn test_sparql_algebra_examples() { )), Box::new(ast::Expression::ConstantExpression(Literal::from(3).into())), )), - ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PropertyPathPattern( - ast::PropertyPathPattern::new( + ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PathPattern( + ast::PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), @@ -1604,12 +1634,12 @@ fn test_sparql_algebra_examples() { Box::new(ast::Expression::ConstantExpression(Literal::from(3).into())), ), Box::new(GraphPattern::LeftJoin( - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v1"), )])), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p2"), Variable::new("v2"), @@ -1621,7 +1651,7 @@ fn test_sparql_algebra_examples() { assert_eq!( ast::GraphPattern::GroupPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p"), Variable::new("v"), @@ -1635,7 +1665,7 @@ fn test_sparql_algebra_examples() { ), Variable::new("v2"), ), - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v2"), @@ -1643,7 +1673,7 @@ fn test_sparql_algebra_examples() { ]).try_into(), Ok(GraphPattern::Join( Box::new(GraphPattern::Extend( - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p"), Variable::new("v"), @@ -1656,7 +1686,7 @@ fn test_sparql_algebra_examples() { )), ), )), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v2"), @@ -1668,13 +1698,13 @@ fn test_sparql_algebra_examples() { assert_eq!( ast::GraphPattern::GroupPattern(vec![ - ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new( + ast::GraphPattern::PathPattern(ast::PathPattern::new( Variable::new("s"), Variable::new("p"), Variable::new("v"), )), - ast::GraphPattern::MinusPattern(Box::new(ast::GraphPattern::PropertyPathPattern( - ast::PropertyPathPattern::new( + ast::GraphPattern::MinusPattern(Box::new(ast::GraphPattern::PathPattern( + ast::PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v2"), @@ -1682,12 +1712,12 @@ fn test_sparql_algebra_examples() { ))), ]).try_into(), Ok(GraphPattern::Minus( - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p"), Variable::new("v"), )])), - Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new( + Box::new(GraphPattern::BGP(vec![PathPattern::new( Variable::new("s"), Variable::new("p1"), Variable::new("v2"), diff --git a/src/sparql/parser.rs b/src/sparql/parser.rs index c4d1cb64..effb52f1 100644 --- a/src/sparql/parser.rs +++ b/src/sparql/parser.rs @@ -49,12 +49,29 @@ mod grammar { } } - struct FocusedPropertyPathPattern { + #[derive(Clone)] + enum VariableOrPropertyPath { + Variable(Variable), + PropertyPath(PropertyPath), + } + + fn to_triple_or_path_pattern( + s: TermOrVariable, + p: VariableOrPropertyPath, + o: TermOrVariable, + ) -> TripleOrPathPattern { + match p { + VariableOrPropertyPath::Variable(p) => TriplePattern::new(s, p, o).into(), + VariableOrPropertyPath::PropertyPath(p) => PathPattern::new(s, p, o).into(), + } + } + + struct FocusedTripleOrPathPattern { focus: F, - patterns: Vec, + patterns: Vec, } - impl FocusedPropertyPathPattern { + impl FocusedTripleOrPathPattern { fn new(focus: F) -> Self { Self { focus, @@ -63,7 +80,7 @@ mod grammar { } } - impl Default for FocusedPropertyPathPattern { + impl Default for FocusedTripleOrPathPattern { fn default() -> Self { Self { focus: F::default(), @@ -72,8 +89,8 @@ mod grammar { } } - impl From> for FocusedPropertyPathPattern> { - fn from(input: FocusedPropertyPathPattern) -> Self { + impl From> for FocusedTripleOrPathPattern> { + fn from(input: FocusedTripleOrPathPattern) -> Self { Self { focus: vec![input.focus], patterns: input.patterns, @@ -81,7 +98,7 @@ mod grammar { } } - impl> From> for FocusedPropertyPathPattern { + impl> From> for FocusedTripleOrPathPattern { fn from(input: FocusedTriplePattern) -> Self { Self { focus: input.focus.into(), diff --git a/src/sparql/sparql_grammar.rustpeg b/src/sparql/sparql_grammar.rustpeg index 6d0c0cd6..f708af2c 100644 --- a/src/sparql/sparql_grammar.rustpeg +++ b/src/sparql/sparql_grammar.rustpeg @@ -87,7 +87,7 @@ ConstructQuery -> Query = algebra: build_select(Selection { option: SelectionOption::Reduced, variables: None - }, MultiSetPattern::BGP(c.into_iter().map(|p| PropertyPathPattern::from(p)).collect()).into(), + }, MultiSetPattern::BGP(c.into_iter().map(|p| TripleOrPathPattern::from(p)).collect()).into(), h, o, l, v) } } @@ -253,14 +253,14 @@ GroupGraphPatternSub_item -> Vec = a:GraphPatternNotTriples } //[55] -TriplesBlock -> Vec = h:TriplesSameSubjectPath _ t:TriplesBlock_tail? { +TriplesBlock -> Vec = h:TriplesSameSubjectPath _ t:TriplesBlock_tail? { let mut triples = h; if let Some(l) = t { triples.extend_from_slice(&l) } triples } -TriplesBlock_tail -> Vec = '.' _ t:TriplesBlock? _ { +TriplesBlock_tail -> Vec = '.' _ t:TriplesBlock? _ { t.unwrap_or_else(|| Vec::default()) } @@ -433,12 +433,12 @@ ObjectList_item -> FocusedTriplePattern = o:Object _ { o } Object -> FocusedTriplePattern = GraphNode //[81] -TriplesSameSubjectPath -> Vec = +TriplesSameSubjectPath -> Vec = s:VarOrTerm _ po:PropertyListPathNotEmpty { let mut patterns = po.patterns; for (p, os) in po.focus { for o in os { - patterns.push(PropertyPathPattern::new(s.clone(), p.clone(), o)) + patterns.push(to_triple_or_path_pattern(s.clone(), p.clone(), o)) } } patterns @@ -448,20 +448,20 @@ TriplesSameSubjectPath -> Vec = patterns.extend_from_slice(&po.patterns); for (p, os) in po.focus { for o in os { - patterns.push(PropertyPathPattern::new(s.focus.clone(), p.clone(), o)) + patterns.push(to_triple_or_path_pattern(s.focus.clone(), p.clone(), o)) } } patterns } //[82] -PropertyListPath -> FocusedPropertyPathPattern)>> = +PropertyListPath -> FocusedTripleOrPathPattern)>> = PropertyListPathNotEmpty / - { FocusedPropertyPathPattern::default() } + { FocusedTripleOrPathPattern::default() } //[83] -PropertyListPathNotEmpty -> FocusedPropertyPathPattern)>> = hp:(VerbPath / VerbSimple) _ ho:ObjectListPath _ t:PropertyListPathNotEmpty_item* { - t.into_iter().fold(FocusedPropertyPathPattern { +PropertyListPathNotEmpty -> FocusedTripleOrPathPattern)>> = hp:(VerbPath / VerbSimple) _ ho:ObjectListPath _ t:PropertyListPathNotEmpty_item* { + t.into_iter().fold(FocusedTripleOrPathPattern { focus: vec![(hp, ho.focus)], patterns: ho.patterns }, |mut a, b| { @@ -470,7 +470,7 @@ PropertyListPathNotEmpty -> FocusedPropertyPathPattern FocusedTriplePattern<(PropertyPath,Vec)> = ';' _ p:(VerbPath / VerbSimple) _ o:ObjectList _ { //TODO: make values after ';' optional +PropertyListPathNotEmpty_item -> FocusedTriplePattern<(VariableOrPropertyPath,Vec)> = ';' _ p:(VerbPath / VerbSimple) _ o:ObjectList _ { //TODO: make values after ';' optional FocusedTriplePattern { focus: (p, o.focus), patterns: o.patterns @@ -478,25 +478,27 @@ PropertyListPathNotEmpty_item -> FocusedTriplePattern<(PropertyPath,Vec PropertyPath = Path +VerbPath -> VariableOrPropertyPath = p:Path { + VariableOrPropertyPath::PropertyPath(p) +} //[85] -VerbSimple -> PropertyPath = v:Var { - v.into() +VerbSimple -> VariableOrPropertyPath = v:Var { + VariableOrPropertyPath::Variable(v) } //[86] -ObjectListPath -> FocusedPropertyPathPattern> = o:ObjectPath_item **<1,> (',' _) { - o.into_iter().fold(FocusedPropertyPathPattern::>::default(), |mut a, b| { +ObjectListPath -> FocusedTripleOrPathPattern> = o:ObjectPath_item **<1,> (',' _) { + o.into_iter().fold(FocusedTripleOrPathPattern::>::default(), |mut a, b| { a.focus.push(b.focus); a.patterns.extend_from_slice(&b.patterns); a }) } -ObjectPath_item -> FocusedPropertyPathPattern = o:ObjectPath _ { o } +ObjectPath_item -> FocusedTripleOrPathPattern = o:ObjectPath _ { o } //[87] -ObjectPath -> FocusedPropertyPathPattern = GraphNodePath +ObjectPath -> FocusedTripleOrPathPattern = GraphNodePath //[88] Path -> PropertyPath = PathAlternative @@ -531,7 +533,8 @@ PathEltOrInverse -> PropertyPath = //[94] PathPrimary -> PropertyPath = - v:Verb { v.into() } / + 'a' { rdf::TYPE.clone().into() } / + v:iri { v.into() } / '!' _ p:PathNegatedPropertySet { p } / '(' _ p:Path _ ')' { p } @@ -591,18 +594,18 @@ BlankNodePropertyList -> FocusedTriplePattern = '[' _ po:Propert } //[100] -TriplesNodePath -> FocusedPropertyPathPattern = CollectionPath / BlankNodePropertyListPath +TriplesNodePath -> FocusedTripleOrPathPattern = CollectionPath / BlankNodePropertyListPath //[101] -BlankNodePropertyListPath -> FocusedPropertyPathPattern = '[' _ po:PropertyListPathNotEmpty _ ']' { - let mut patterns: Vec = Vec::default(); +BlankNodePropertyListPath -> FocusedTripleOrPathPattern = '[' _ po:PropertyListPathNotEmpty _ ']' { + let mut patterns: Vec = Vec::default(); let mut bnode = TermOrVariable::from(BlankNode::default()); for (p, os) in po.focus { for o in os { - patterns.push(PropertyPathPattern::new(bnode.clone(), p.clone(), o)); + patterns.push(to_triple_or_path_pattern(bnode.clone(), p.clone(), o)); } } - FocusedPropertyPathPattern { + FocusedTripleOrPathPattern { focus: bnode, patterns } @@ -627,22 +630,22 @@ Collection -> FocusedTriplePattern = '(' _ o:Collection_item+ ') Collection_item -> FocusedTriplePattern = o:GraphNode _ { o } //[103] -CollectionPath -> FocusedPropertyPathPattern = '(' _ o:CollectionPath_item+ _ ')' { - let mut patterns: Vec = Vec::default(); +CollectionPath -> FocusedTripleOrPathPattern = '(' _ o:CollectionPath_item+ _ ')' { + let mut patterns: Vec = Vec::default(); let mut current_list_node = TermOrVariable::from(rdf::NIL.clone()); for objWithPatterns in o.into_iter().rev() { let new_blank_node = TermOrVariable::from(BlankNode::default()); - patterns.push(PropertyPathPattern::new(new_blank_node.clone(), rdf::FIRST.clone(), objWithPatterns.focus.clone())); - patterns.push(PropertyPathPattern::new(new_blank_node.clone(), rdf::REST.clone(), current_list_node)); + patterns.push(TriplePattern::new(new_blank_node.clone(), rdf::FIRST.clone(), objWithPatterns.focus.clone()).into()); + patterns.push(TriplePattern::new(new_blank_node.clone(), rdf::REST.clone(), current_list_node).into()); current_list_node = new_blank_node; patterns.extend_from_slice(&objWithPatterns.patterns); } - FocusedPropertyPathPattern { + FocusedTripleOrPathPattern { focus: current_list_node, patterns } } -CollectionPath_item -> FocusedPropertyPathPattern = p:GraphNodePath _ { p } +CollectionPath_item -> FocusedTripleOrPathPattern = p:GraphNodePath _ { p } //[104] GraphNode -> FocusedTriplePattern = @@ -650,8 +653,8 @@ GraphNode -> FocusedTriplePattern = TriplesNode //[105] -GraphNodePath -> FocusedPropertyPathPattern = - t:VarOrTerm { FocusedPropertyPathPattern::new(t.into()) } / +GraphNodePath -> FocusedTripleOrPathPattern = + t:VarOrTerm { FocusedTripleOrPathPattern::new(t.into()) } / TriplesNodePath //[106]