SPARQL-star parser: align with SPARQL 1.2 draft

Relevant PR: https://github.com/w3c/sparql-query/pull/78
pull/507/head
Tpt 2 years ago committed by Thomas Tanon
parent 9a6233b511
commit 8bec2e2ff9
  1. 123
      lib/spargebra/src/parser.rs

@ -1473,7 +1473,7 @@ parser! {
rule InlineDataFull_value() -> Option<GroundTerm> = v:DataBlockValue() _ { v } rule InlineDataFull_value() -> Option<GroundTerm> = v:DataBlockValue() _ { v }
rule DataBlockValue() -> Option<GroundTerm> = rule DataBlockValue() -> Option<GroundTerm> =
t:EmbTriple() {? t:QuotedTripleData() {?
#[cfg(feature = "rdf-star")]{Ok(Some(t.into()))} #[cfg(feature = "rdf-star")]{Ok(Some(t.into()))}
#[cfg(not(feature = "rdf-star"))]{Err("Embedded triples are only available in SPARQL-star")} #[cfg(not(feature = "rdf-star"))]{Err("Embedded triples are only available in SPARQL-star")}
} / } /
@ -1522,7 +1522,7 @@ parser! {
rule ConstructTriples_item() -> Vec<TriplePattern> = t:TriplesSameSubject() _ { t } rule ConstructTriples_item() -> Vec<TriplePattern> = t:TriplesSameSubject() _ { t }
rule TriplesSameSubject() -> Vec<TriplePattern> = rule TriplesSameSubject() -> Vec<TriplePattern> =
s:VarOrTermOrEmbTP() _ po:PropertyListNotEmpty() {? s:VarOrTerm() _ po:PropertyListNotEmpty() {?
let mut patterns = po.patterns; let mut patterns = po.patterns;
for (p, os) in po.focus { for (p, os) in po.focus {
for o in os { for o in os {
@ -1571,7 +1571,7 @@ parser! {
} }
rule ObjectList_item() -> FocusedTriplePattern<AnnotatedTerm> = o:Object() _ { o } rule ObjectList_item() -> FocusedTriplePattern<AnnotatedTerm> = o:Object() _ { o }
rule Object() -> FocusedTriplePattern<AnnotatedTerm> = g:GraphNode() _ a:AnnotationPattern()? { rule Object() -> FocusedTriplePattern<AnnotatedTerm> = g:GraphNode() _ a:Annotation()? {
if let Some(a) = a { if let Some(a) = a {
let mut patterns = g.patterns; let mut patterns = g.patterns;
patterns.extend(a.patterns); patterns.extend(a.patterns);
@ -1594,7 +1594,7 @@ parser! {
} }
rule TriplesSameSubjectPath() -> Vec<TripleOrPathPattern> = rule TriplesSameSubjectPath() -> Vec<TripleOrPathPattern> =
s:VarOrTermOrEmbTP() _ po:PropertyListPathNotEmpty() {? s:VarOrTerm() _ po:PropertyListPathNotEmpty() {?
let mut patterns = po.patterns; let mut patterns = po.patterns;
for (p, os) in po.focus { for (p, os) in po.focus {
for o in os { for o in os {
@ -1655,7 +1655,7 @@ parser! {
} }
rule ObjectListPath_item() -> FocusedTripleOrPathPattern<AnnotatedTermPath> = o:ObjectPath() _ { o } rule ObjectListPath_item() -> FocusedTripleOrPathPattern<AnnotatedTermPath> = o:ObjectPath() _ { o }
rule ObjectPath() -> FocusedTripleOrPathPattern<AnnotatedTermPath> = g:GraphNodePath() _ a:AnnotationPatternPath()? { rule ObjectPath() -> FocusedTripleOrPathPattern<AnnotatedTermPath> = g:GraphNodePath() _ a:AnnotationPath()? {
if let Some(a) = a { if let Some(a) = a {
let mut patterns = g.patterns; let mut patterns = g.patterns;
patterns.extend(a.patterns); patterns.extend(a.patterns);
@ -1818,18 +1818,53 @@ parser! {
} }
rule CollectionPath_item() -> FocusedTripleOrPathPattern<TermPattern> = p:GraphNodePath() _ { p } rule CollectionPath_item() -> FocusedTripleOrPathPattern<TermPattern> = p:GraphNodePath() _ { p }
rule Annotation() -> FocusedTriplePattern<Vec<(NamedNodePattern,Vec<AnnotatedTerm>)>> = "{|" _ a:PropertyListNotEmpty() _ "|}" { a }
rule AnnotationPath() -> FocusedTripleOrPathPattern<Vec<(VariableOrPropertyPath,Vec<AnnotatedTermPath>)>> = "{|" _ a: PropertyListPathNotEmpty() _ "|}" { a }
rule GraphNode() -> FocusedTriplePattern<TermPattern> = rule GraphNode() -> FocusedTriplePattern<TermPattern> =
t:VarOrTermOrEmbTP() { FocusedTriplePattern::new(t) } / t:VarOrTerm() { FocusedTriplePattern::new(t) } /
TriplesNode() TriplesNode()
rule GraphNodePath() -> FocusedTripleOrPathPattern<TermPattern> = rule GraphNodePath() -> FocusedTripleOrPathPattern<TermPattern> =
t:VarOrTermOrEmbTP() { FocusedTripleOrPathPattern::new(t) } / t:VarOrTerm() { FocusedTripleOrPathPattern::new(t) } /
TriplesNodePath() TriplesNodePath()
rule VarOrTerm() -> TermPattern = rule VarOrTerm() -> TermPattern =
v:Var() { v.into() } / v:Var() { v.into() } /
t:QuotedTriple() {?
#[cfg(feature = "rdf-star")]{Ok(t.into())}
#[cfg(not(feature = "rdf-star"))]{Err("Embedded triples are only available in SPARQL-star")}
} /
t:GraphTerm() { t.into() } t:GraphTerm() { t.into() }
rule QuotedTriple() -> TriplePattern = "<<" _ s:VarOrTerm() _ p:Verb() _ o:VarOrTerm() _ ">>" {?
Ok(TriplePattern {
subject: s,
predicate: p,
object: o
})
}
rule QuotedTripleData() -> GroundTriple = "<<" _ s:DataValueTerm() _ p:QuotedTripleData_p() _ o:DataValueTerm() _ ">>" {?
Ok(GroundTriple {
subject: s.try_into().map_err(|_| "Literals are not allowed in subject position of nested patterns")?,
predicate: p,
object: o
})
}
rule QuotedTripleData_p() -> NamedNode = i: iri() { i } / "a" { rdf::TYPE.into() }
rule DataValueTerm() -> GroundTerm = i:iri() { i.into() } /
l:RDFLiteral() { l.into() } /
l:NumericLiteral() { l.into() } /
l:BooleanLiteral() { l.into() } /
t:QuotedTripleData() {?
#[cfg(feature = "rdf-star")]{Ok(t.into())}
#[cfg(not(feature = "rdf-star"))]{Err("Embedded triples are only available in SPARQL-star")}
}
rule VarOrIri() -> NamedNodePattern = rule VarOrIri() -> NamedNodePattern =
v:Var() { v.into() } / v:Var() { v.into() } /
i:iri() { i.into() } i:iri() { i.into() }
@ -1907,7 +1942,7 @@ parser! {
rule PrimaryExpression() -> Expression = rule PrimaryExpression() -> Expression =
BrackettedExpression() / BrackettedExpression() /
ExprEmbTP() / ExprQuotedTriple() /
iriOrFunction() / iriOrFunction() /
v:Var() { v.into() } / v:Var() { v.into() } /
l:RDFLiteral() { l.into() } / l:RDFLiteral() { l.into() } /
@ -1915,6 +1950,19 @@ parser! {
l:BooleanLiteral() { l.into() } / l:BooleanLiteral() { l.into() } /
BuiltInCall() BuiltInCall()
rule ExprVarOrTerm() -> Expression =
ExprQuotedTriple() /
i:iri() { i.into() } /
l:RDFLiteral() { l.into() } /
l:NumericLiteral() { l.into() } /
l:BooleanLiteral() { l.into() } /
v:Var() { v.into() }
rule ExprQuotedTriple() -> Expression = "<<" _ s:ExprVarOrTerm() _ p:Verb() _ o:ExprVarOrTerm() _ ">>" {?
#[cfg(feature = "rdf-star")]{Ok(Expression::FunctionCall(Function::Triple, vec![s, p.into(), o]))}
#[cfg(not(feature = "rdf-star"))]{Err("Embedded triples are only available in SPARQL-star")}
}
rule BrackettedExpression() -> Expression = "(" _ e:Expression() _ ")" { e } rule BrackettedExpression() -> Expression = "(" _ e:Expression() _ ")" { e }
rule BuiltInCall() -> Expression = rule BuiltInCall() -> Expression =
@ -2195,65 +2243,6 @@ parser! {
rule PN_LOCAL_ESC() = ['\\'] ['_' | '~' | '.' | '-' | '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | ';' | '=' | '/' | '?' | '#' | '@' | '%'] //TODO: added '/' to make tests pass but is it valid? rule PN_LOCAL_ESC() = ['\\'] ['_' | '~' | '.' | '-' | '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | ';' | '=' | '/' | '?' | '#' | '@' | '%'] //TODO: added '/' to make tests pass but is it valid?
rule EmbTP() -> TriplePattern = "<<" _ s:EmbSubjectOrObject() _ p:Verb() _ o:EmbSubjectOrObject() _ ">>" {
TriplePattern { subject: s, predicate: p, object: o }
}
rule EmbTriple() -> GroundTriple = "<<" _ s:DataValueTerm() _ p:EmbTriple_p() _ o:DataValueTerm() _ ">>" {?
Ok(GroundTriple {
subject: s.try_into().map_err(|_| "Literals are not allowed in subject position of nested patterns")?,
predicate: p,
object: o
})
}
rule EmbTriple_p() -> NamedNode = i: iri() { i } / "a" { rdf::TYPE.into() }
rule EmbSubjectOrObject() -> TermPattern =
t:EmbTP() {?
#[cfg(feature = "rdf-star")]{Ok(t.into())}
#[cfg(not(feature = "rdf-star"))]{Err("Embedded triple patterns are only available in SPARQL-star")}
} /
v:Var() { v.into() } /
b:BlankNode() { b.into() } /
i:iri() { i.into() } /
l:RDFLiteral() { l.into() } /
l:NumericLiteral() { l.into() } /
l:BooleanLiteral() { l.into() }
rule DataValueTerm() -> GroundTerm = i:iri() { i.into() } /
l:RDFLiteral() { l.into() } /
l:NumericLiteral() { l.into() } /
l:BooleanLiteral() { l.into() } /
t:EmbTriple() {?
#[cfg(feature = "rdf-star")]{Ok(t.into())}
#[cfg(not(feature = "rdf-star"))]{Err("Embedded triples are only available in SPARQL-star")}
}
rule VarOrTermOrEmbTP() -> TermPattern =
t:EmbTP() {?
#[cfg(feature = "rdf-star")]{Ok(t.into())}
#[cfg(not(feature = "rdf-star"))]{Err("Embedded triple patterns are only available in SPARQL-star")}
} /
v:Var() { v.into() } /
t:GraphTerm() { t.into() }
rule AnnotationPattern() -> FocusedTriplePattern<Vec<(NamedNodePattern,Vec<AnnotatedTerm>)>> = "{|" _ a:PropertyListNotEmpty() _ "|}" { a }
rule AnnotationPatternPath() -> FocusedTripleOrPathPattern<Vec<(VariableOrPropertyPath,Vec<AnnotatedTermPath>)>> = "{|" _ a: PropertyListPathNotEmpty() _ "|}" { a }
rule ExprEmbTP() -> Expression = "<<" _ s:ExprVarOrTerm() _ p:Verb() _ o:ExprVarOrTerm() _ ">>" {?
#[cfg(feature = "rdf-star")]{Ok(Expression::FunctionCall(Function::Triple, vec![s, p.into(), o]))}
#[cfg(not(feature = "rdf-star"))]{Err("Embedded triples are only available in SPARQL-star")}
}
rule ExprVarOrTerm() -> Expression =
ExprEmbTP() /
i:iri() { i.into() } /
l:RDFLiteral() { l.into() } /
l:NumericLiteral() { l.into() } /
l:BooleanLiteral() { l.into() } /
v:Var() { v.into() }
//space //space
rule _() = quiet! { ([' ' | '\t' | '\n' | '\r'] / comment())* } rule _() = quiet! { ([' ' | '\t' | '\n' | '\r'] / comment())* }

Loading…
Cancel
Save