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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
//! # QueryParser - parsing Methods
//!
//! This module contains method implementations for `QueryParser`.
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
use crate::algebra::{Algebra, PropertyPath, Term, TriplePattern, Variable};
use anyhow::{anyhow, bail, Result};
use oxirs_core::model::NamedNode;
use std::collections::{HashMap, HashSet};
use super::types::{DatasetClause, DescribeTarget, ProjectionItem, Query, QueryType, Token};
use super::queryparser_type::QueryParser;
/// Compose the next group-graph-pattern element onto the accumulated pattern.
///
/// `OPTIONAL`, `MINUS` and a leading `UNION` are each parsed with the unit table
/// (`Algebra::Table`) as their left operand, because a group element is written
/// standalone. At the group level, however, they operate on the pattern that
/// PRECEDES them, so they must be re-rooted on the accumulated `prev` rather
/// than joined as an independent unit:
///
/// * `Join(prev, Minus(Table, r))` makes `MINUS` a silent no-op — a `Table` left
/// shares no variables with `r`, and SPARQL `MINUS` removes nothing when the
/// operands are variable-disjoint. Re-rooting to `Minus(prev, r)` restores the
/// difference.
/// * `Join(prev, LeftJoin(Table, r))` collapses `OPTIONAL` into an inner join
/// (dropping `prev` rows without an `r` match). Re-rooting to
/// `LeftJoin(prev, r)` restores proper optional semantics.
fn compose_group_element(prev: Algebra, element: Algebra) -> Algebra {
match element {
Algebra::LeftJoin {
left,
right,
filter,
} if matches!(*left, Algebra::Table) => Algebra::LeftJoin {
left: Box::new(prev),
right,
filter,
},
Algebra::Minus { left, right } if matches!(*left, Algebra::Table) => Algebra::Minus {
left: Box::new(prev),
right,
},
other => Algebra::join(prev, other),
}
}
impl QueryParser {
pub fn new() -> Self {
Self {
tokens: Vec::new(),
position: 0,
prefixes: HashMap::new(),
base_iri: None,
variables: HashSet::new(),
blank_node_counter: 0,
}
}
/// Tokenize SPARQL query string
pub(super) fn tokenize(&mut self, input: &str) -> Result<()> {
let mut chars = input.chars().peekable();
let mut tokens = Vec::new();
while let Some(&ch) = chars.peek() {
match ch {
' ' | '\t' | '\r' => {
chars.next();
}
'\n' => {
chars.next();
tokens.push(Token::Newline);
}
'#' => {
while let Some(&ch) = chars.peek() {
chars.next();
if ch == '\n' {
tokens.push(Token::Newline);
break;
}
}
}
'(' => {
chars.next();
tokens.push(Token::LeftParen);
}
')' => {
chars.next();
tokens.push(Token::RightParen);
}
'{' => {
chars.next();
tokens.push(Token::LeftBrace);
}
'}' => {
chars.next();
tokens.push(Token::RightBrace);
}
'[' => {
chars.next();
tokens.push(Token::LeftBracket);
}
']' => {
chars.next();
tokens.push(Token::RightBracket);
}
'.' => {
chars.next();
tokens.push(Token::Dot);
}
';' => {
chars.next();
tokens.push(Token::Semicolon);
}
',' => {
chars.next();
tokens.push(Token::Comma);
}
':' => {
chars.next();
if chars
.peek()
.map_or(true, |c| !c.is_ascii_alphanumeric() && *c != '_')
{
tokens.push(Token::Colon);
} else {
let mut id = ":".to_string();
id.push_str(&self.parse_identifier(&mut chars));
tokens.push(self.classify_identifier(&id));
}
}
'=' => {
chars.next();
if chars.peek() == Some(&'=') {
chars.next();
tokens.push(Token::Equal);
} else {
tokens.push(Token::Equal);
}
}
'<' => {
chars.next();
if chars.peek() == Some(&'=') {
chars.next();
tokens.push(Token::LessEqual);
} else if chars.peek() == Some(&'h') || chars.peek() == Some(&'/') {
let mut iri = String::new();
while let Some(&ch) = chars.peek() {
if ch == '>' {
chars.next();
break;
}
iri.push(ch);
chars.next();
}
tokens.push(Token::Iri(iri));
} else {
tokens.push(Token::Less);
}
}
'>' => {
chars.next();
if chars.peek() == Some(&'=') {
chars.next();
tokens.push(Token::GreaterEqual);
} else {
tokens.push(Token::Greater);
}
}
'+' => {
chars.next();
tokens.push(Token::Plus);
}
'-' => {
chars.next();
tokens.push(Token::Minus_);
}
'/' => {
chars.next();
tokens.push(Token::Divide);
}
'|' => {
chars.next();
if chars.peek() == Some(&'|') {
chars.next();
tokens.push(Token::Or);
} else {
tokens.push(Token::Pipe);
}
}
'^' => {
chars.next();
tokens.push(Token::Caret);
}
'?' => {
chars.next();
if chars.peek().is_some_and(|c| c.is_ascii_alphabetic()) {
let var = self.parse_identifier(&mut chars);
tokens.push(Token::Variable(var));
} else {
tokens.push(Token::Question);
}
continue;
}
'*' => {
chars.next();
tokens.push(Token::Star);
}
'!' => {
chars.next();
if chars.peek() == Some(&'=') {
chars.next();
tokens.push(Token::NotEqual);
} else {
tokens.push(Token::Bang);
}
continue;
}
'&' => {
chars.next();
if chars.peek() == Some(&'&') {
chars.next();
tokens.push(Token::And);
} else {
return Err(anyhow!("Unexpected '&' - did you mean '&&'?"));
}
continue;
}
'$' => {
chars.next();
let var = self.parse_identifier(&mut chars);
tokens.push(Token::Variable(var));
}
'"' | '\'' => {
let quote = ch;
chars.next();
let mut literal = String::new();
while let Some(&ch) = chars.peek() {
chars.next();
if ch == quote {
break;
}
if ch == '\\' {
if let Some(&escaped) = chars.peek() {
chars.next();
match escaped {
'n' => literal.push('\n'),
't' => literal.push('\t'),
'r' => literal.push('\r'),
'\\' => literal.push('\\'),
'\'' => literal.push('\''),
'"' => literal.push('"'),
_ => {
literal.push('\\');
literal.push(escaped);
}
}
}
} else {
literal.push(ch);
}
}
// Optional RDF literal suffix: a language tag (`@ja`, with
// subtags such as `@ja-JP`) or an explicit datatype
// (`^^<iri>` / `^^prefix:local`). A plain literal keeps the
// simple `StringLiteral` token so existing call sites are
// unaffected.
if chars.peek() == Some(&'@') {
chars.next();
let mut lang = String::new();
while let Some(&c) = chars.peek() {
if c.is_ascii_alphanumeric() || c == '-' {
lang.push(c);
chars.next();
} else {
break;
}
}
tokens.push(Token::RdfLiteral {
value: literal,
language: Some(lang),
datatype: None,
});
} else if chars.peek() == Some(&'^') {
chars.next();
if chars.peek() == Some(&'^') {
chars.next();
let datatype = self.parse_datatype_iri(&mut chars);
tokens.push(Token::RdfLiteral {
value: literal,
language: None,
datatype: Some(datatype),
});
} else {
// A lone `^` after a string is not a datatype marker;
// keep the literal and emit the caret separately.
tokens.push(Token::StringLiteral(literal));
tokens.push(Token::Caret);
}
} else {
tokens.push(Token::StringLiteral(literal));
}
}
'_' => {
chars.next();
if chars.peek() == Some(&':') {
chars.next();
let id = self.parse_identifier(&mut chars);
tokens.push(Token::BlankNode(id));
} else {
let mut id = "_".to_string();
id.push_str(&self.parse_identifier(&mut chars));
tokens.push(self.classify_identifier(&id));
}
}
_ if ch.is_ascii_alphabetic() || ch == '_' => {
let identifier = self.parse_identifier(&mut chars);
tokens.push(self.classify_identifier(&identifier));
}
_ if ch.is_ascii_digit() => {
let number = self.parse_number(&mut chars);
tokens.push(Token::NumericLiteral(number));
}
_ => {
chars.next();
}
}
}
tokens.push(Token::Eof);
self.tokens = tokens;
self.position = 0;
Ok(())
}
pub(super) fn parse_identifier(
&self,
chars: &mut std::iter::Peekable<std::str::Chars>,
) -> String {
let mut identifier = String::new();
let mut found_colon = false;
while let Some(&ch) = chars.peek() {
if ch.is_ascii_alphanumeric() || ch == '_' || ch == '-' || ch == '.' {
identifier.push(ch);
chars.next();
} else if ch == ':' && !found_colon {
identifier.push(ch);
chars.next();
found_colon = true;
} else {
break;
}
}
identifier
}
/// Read the datatype that follows a `^^` marker: either an absolute IRI in
/// angle brackets (`<iri>`, returned without the brackets) or a
/// `prefix:local` name (returned verbatim for parse-time resolution).
pub(super) fn parse_datatype_iri(
&self,
chars: &mut std::iter::Peekable<std::str::Chars>,
) -> String {
if chars.peek() == Some(&'<') {
chars.next();
let mut iri = String::new();
while let Some(&c) = chars.peek() {
chars.next();
if c == '>' {
break;
}
iri.push(c);
}
iri
} else {
self.parse_identifier(chars)
}
}
pub(super) fn parse_number(&self, chars: &mut std::iter::Peekable<std::str::Chars>) -> String {
let mut number = String::new();
while let Some(&ch) = chars.peek() {
if ch.is_ascii_digit() || ch == '.' || ch == 'e' || ch == 'E' || ch == '+' || ch == '-'
{
number.push(ch);
chars.next();
} else {
break;
}
}
number
}
pub(super) fn parse_query(&mut self) -> Result<Query> {
let mut query = Query {
query_type: QueryType::Select,
select_variables: Vec::new(),
where_clause: Algebra::Zero,
order_by: Vec::new(),
group_by: Vec::new(),
having: None,
limit: None,
offset: None,
distinct: false,
reduced: false,
construct_template: Vec::new(),
prefixes: HashMap::new(),
base_iri: None,
dataset: DatasetClause::default(),
projection_items: Vec::new(),
describe_targets: Vec::new(),
describe_all: false,
};
self.skip_whitespace();
self.parse_prologue(&mut query)?;
self.skip_whitespace();
match self.peek() {
Some(Token::Select) => {
query.query_type = QueryType::Select;
self.parse_select_query(&mut query)?;
}
Some(Token::Construct) => {
query.query_type = QueryType::Construct;
self.parse_construct_query(&mut query)?;
}
Some(Token::Ask) => {
query.query_type = QueryType::Ask;
self.parse_ask_query(&mut query)?;
}
Some(Token::Describe) => {
query.query_type = QueryType::Describe;
self.parse_describe_query(&mut query)?;
}
_ => bail!("Expected query type (SELECT, CONSTRUCT, ASK, DESCRIBE)"),
}
Ok(query)
}
pub(super) fn parse_prologue(&mut self, query: &mut Query) -> Result<()> {
while let Some(token) = self.peek() {
match token {
Token::Prefix => {
self.advance();
let prefix = match self.peek() {
Some(Token::PrefixedName(prefix, local)) => {
if prefix.is_empty() && local.is_empty() {
self.advance();
String::new()
} else {
let p = prefix.clone();
self.advance();
p
}
}
Some(Token::Colon) => {
self.advance();
String::new()
}
_ => {
eprintln!("Debug: Got token: {:?}", self.peek());
bail!("Expected prefix name or colon after PREFIX")
}
};
let iri = self.expect_iri()?;
query.prefixes.insert(prefix.clone(), iri.clone());
self.prefixes.insert(prefix, iri);
}
Token::Base => {
self.advance();
let iri = self.expect_iri()?;
query.base_iri = Some(iri.clone());
self.base_iri = Some(iri);
}
Token::Newline => {
self.advance();
}
_ => break,
}
}
Ok(())
}
pub(super) fn parse_select_query(&mut self, query: &mut Query) -> Result<()> {
self.expect_token(Token::Select)?;
if self.match_token(&Token::Distinct) {
query.distinct = true;
} else if self.match_token(&Token::Reduced) {
query.reduced = true;
}
// `SELECT *`: the tokenizer emits `Token::Star` for `*`, so the legacy
// `Token::Multiply` check never matched and `SELECT *` failed to parse.
// Accept either. An empty `select_variables` (and empty
// `projection_items`) means "project all in-scope variables".
if self.match_token(&Token::Star) || self.match_token(&Token::Multiply) {
} else {
while !self.is_at_end()
&& !matches!(self.peek(), Some(Token::Where) | Some(Token::From))
{
match self.peek() {
Some(Token::Variable(var)) => {
let variable = Variable::new(var.clone())?;
self.advance();
query.select_variables.push(variable.clone());
query
.projection_items
.push(ProjectionItem::Variable(variable));
}
// A parenthesized projection: `( Expression AS ?var )`,
// including SPARQL aggregates such as `(COUNT(*) AS ?n)`.
Some(Token::LeftParen) => {
let item = self.parse_projection_paren_item()?;
// The projected output variable is the alias; record it
// in `select_variables` too so the output column set is
// complete regardless of which field a consumer reads.
let alias = match &item {
ProjectionItem::Variable(v) => v.clone(),
ProjectionItem::Expression { alias, .. }
| ProjectionItem::Aggregate { alias, .. } => alias.clone(),
};
query.select_variables.push(alias);
query.projection_items.push(item);
}
_ => break,
}
}
}
self.parse_dataset_clause(&mut query.dataset)?;
self.expect_token(Token::Where)?;
self.expect_token(Token::LeftBrace)?;
query.where_clause = self.parse_group_graph_pattern()?;
self.expect_token(Token::RightBrace)?;
self.parse_solution_modifiers(query)?;
Ok(())
}
pub(super) fn parse_describe_query(&mut self, query: &mut Query) -> Result<()> {
self.expect_token(Token::Describe)?;
self.skip_whitespace_and_newlines();
// `DESCRIBE *` describes every in-scope variable binding; it takes no
// explicit target list.
if self.match_token(&Token::Star) || self.match_token(&Token::Multiply) {
query.describe_all = true;
} else {
// `DESCRIBE VarOrIri+`: one or more IRIs and/or variables. Prefixed
// names are expanded against the prologue exactly like other term
// positions; the raw targets are RETAINED in `describe_targets`.
loop {
self.skip_whitespace_and_newlines();
match self.peek() {
Some(Token::Variable(var)) => {
let variable = Variable::new(var.clone())?;
self.advance();
query
.describe_targets
.push(DescribeTarget::Variable(variable.clone()));
query.select_variables.push(variable);
}
Some(Token::Iri(iri)) => {
let iri = iri.clone();
self.advance();
query
.describe_targets
.push(DescribeTarget::Iri(NamedNode::new_unchecked(iri)));
}
Some(Token::PrefixedName(prefix, local)) => {
let prefix = prefix.clone();
let local = local.clone();
self.advance();
let full_iri = self.resolve_prefixed_name(&prefix, &local)?;
query
.describe_targets
.push(DescribeTarget::Iri(NamedNode::new_unchecked(full_iri)));
}
_ => break,
}
}
if query.describe_targets.is_empty() {
bail!("DESCRIBE requires at least one IRI or variable target, or '*'");
}
}
self.parse_dataset_clause(&mut query.dataset)?;
// `WhereClause ::= 'WHERE'? GroupGraphPattern` — the WHERE keyword is
// optional, so accept `DESCRIBE ?x { … }` as well as
// `DESCRIBE ?x WHERE { … }`.
if self.match_token(&Token::Where) {
self.expect_token(Token::LeftBrace)?;
query.where_clause = self.parse_group_graph_pattern()?;
self.expect_token(Token::RightBrace)?;
} else if self.match_token(&Token::LeftBrace) {
query.where_clause = self.parse_group_graph_pattern()?;
self.expect_token(Token::RightBrace)?;
}
self.parse_solution_modifiers(query)?;
Ok(())
}
pub(super) fn parse_group_graph_pattern(&mut self) -> Result<Algebra> {
// A group graph pattern is a sequence of graph patterns interleaved with
// `FILTER` / `BIND` modifiers, and the two modifiers scope DIFFERENTLY:
//
// * `FILTER` constrains the WHOLE group regardless of its textual
// position, so bare filters are collected and applied once, wrapping
// the fully-joined group (SPARQL 1.1 §18.2.2 "collect FILTERs").
//
// * `BIND` is POSITIONAL: `BIND(expr AS ?v)` extends the solution
// produced by the elements written BEFORE it, and elements written
// AFTER it join against the extended solution. `{ ?s ?p ?o .
// BIND(?o AS ?x) . ?x ?q ?r }` therefore means
// `Join(Extend(BGP(?s ?p ?o), ?x, ?o), BGP(?x ?q ?r))`. Deferring
// the `BIND` to the group end (as `FILTER` is deferred) would join
// both BGPs first and only then extend, letting the second BGP bind
// `?x` and be silently mis-joined/overwritten. A leading `BIND`
// extends the unit table (join identity), so `{ BIND(1 AS ?v) … }`
// still works.
//
// Modifiers are recognised SYNTACTICALLY, by the leading `FILTER` /
// `BIND` token of THIS group — never structurally by matching a
// `Filter/Extend { pattern: Table, .. }` shape. A nested single-element
// group such as `{ ?s ?p ?o { FILTER(?o > 5) } }` also parses to
// `Filter { pattern: Table, .. }`, but it is a JOIN operand of the outer
// group, not an outer-group modifier, and must not be re-scoped.
//
// A top-level `UNION` needs no special-casing: it is parsed by
// `parse_graph_pattern_or_union` as one element of the loop below, so a
// trailing `FILTER`/`BIND` after a union is still scoped to the group.
let mut acc: Option<Algebra> = None;
let mut filters: Vec<Algebra> = Vec::new();
while !self.is_at_end() && !matches!(self.peek(), Some(Token::RightBrace)) {
self.skip_whitespace_and_newlines();
if self.is_at_end() || matches!(self.peek(), Some(Token::RightBrace)) {
break;
}
if matches!(self.peek(), Some(Token::Filter)) {
// Bare FILTER: whole-group scope, deferred to the group end.
filters.push(self.parse_filter_pattern()?);
} else if matches!(self.peek(), Some(Token::Bind)) {
// Bare BIND: positional Extend over the algebra accumulated so
// far (the unit table when the BIND leads the group).
match self.parse_bind_pattern()? {
Algebra::Extend { variable, expr, .. } => {
let base = acc.take().unwrap_or(Algebra::Table);
acc = Some(Algebra::Extend {
pattern: Box::new(base),
variable,
expr,
});
}
other => bail!("BIND parser returned unexpected algebra: {other:?}"),
}
} else {
let pattern = self.parse_graph_pattern_or_union()?;
acc = Some(match acc.take() {
Some(prev) => compose_group_element(prev, pattern),
None => pattern,
});
}
self.match_token(&Token::Dot);
self.skip_whitespace_and_newlines();
}
let mut result = acc.unwrap_or(Algebra::Table);
for modifier in filters {
match modifier {
Algebra::Filter { condition, .. } => {
result = Algebra::Filter {
pattern: Box::new(result),
condition,
};
}
other => bail!("FILTER parser returned unexpected algebra: {other:?}"),
}
}
Ok(result)
}
pub(super) fn parse_graph_pattern_or_union(&mut self) -> Result<Algebra> {
let left = self.parse_graph_pattern()?;
self.skip_whitespace_and_newlines();
if self.match_token(&Token::Union) {
self.skip_whitespace_and_newlines();
let right = self.parse_graph_pattern_or_union()?;
return Ok(Algebra::Union {
left: Box::new(left),
right: Box::new(right),
});
}
Ok(left)
}
pub(super) fn parse_basic_graph_pattern(&mut self) -> Result<Algebra> {
let mut triples = Vec::new();
while !self.is_at_end() {
self.skip_whitespace_and_newlines();
if self.is_pattern_end() {
break;
}
if matches!(self.peek(), Some(Token::Newline)) {
self.advance();
continue;
}
triples.extend(self.parse_triples_same_subject()?);
if !self.match_token(&Token::Dot) {
break;
}
}
Ok(Algebra::Bgp(triples))
}
/// Parse a `TriplesSameSubjectPath`: one subject followed by a
/// predicate-object list, expanding the SPARQL 1.1 abbreviations `;`
/// (predicate-object list) and `,` (object list) into every triple that
/// shares the subject. `{ ?s :p ?o ; :q ?r , ?t }` therefore yields the
/// three triples `(?s :p ?o)`, `(?s :q ?r)`, `(?s :q ?t)`.
pub(super) fn parse_triples_same_subject(&mut self) -> Result<Vec<TriplePattern>> {
self.skip_whitespace_and_newlines();
let subject = self.parse_term()?;
let mut triples = Vec::new();
self.parse_predicate_object_list(&subject, &mut triples)?;
Ok(triples)
}
/// Parse a `PropertyListPathNotEmpty`:
/// `verb objectList ( ';' ( verb objectList )? )*`. A trailing `;` (and a
/// repeated `;;`) with no following verb is valid SPARQL and tolerated.
pub(super) fn parse_predicate_object_list(
&mut self,
subject: &Term,
out: &mut Vec<TriplePattern>,
) -> Result<()> {
loop {
self.skip_whitespace_and_newlines();
let predicate = self.parse_verb()?;
self.parse_object_list(subject, &predicate, out)?;
self.skip_whitespace_and_newlines();
if !self.match_token(&Token::Semicolon) {
break;
}
// After ';', a further `verb objectList` is optional. Skip any run of
// extra `;` and stop when no verb follows (a trailing semicolon).
self.skip_whitespace_and_newlines();
while self.match_token(&Token::Semicolon) {
self.skip_whitespace_and_newlines();
}
if !self.is_verb_start() {
break;
}
}
Ok(())
}
/// Parse an `ObjectListPath`: one or more objects separated by `,`, emitting
/// one triple per object with the shared subject and predicate.
pub(super) fn parse_object_list(
&mut self,
subject: &Term,
predicate: &Term,
out: &mut Vec<TriplePattern>,
) -> Result<()> {
loop {
self.skip_whitespace_and_newlines();
let object = self.parse_term()?;
out.push(TriplePattern::new(
subject.clone(),
predicate.clone(),
object,
));
self.skip_whitespace_and_newlines();
if !self.match_token(&Token::Comma) {
break;
}
}
Ok(())
}
/// Parse a verb (predicate): a property path (`IRI`, `a`, `p1/p2`, `p+`, …)
/// or a plain variable predicate.
pub(super) fn parse_verb(&mut self) -> Result<Term> {
if self.is_property_path_start() {
Ok(Term::PropertyPath(self.parse_property_path()?))
} else {
self.parse_term()
}
}
/// Whether the current token can begin a verb (predicate): a property-path
/// start (`IRI` / prefixed name / `a` / `^` / `(` / `!`) or a variable.
pub(super) fn is_verb_start(&self) -> bool {
self.is_property_path_start() || matches!(self.peek(), Some(Token::Variable(_)))
}
/// Parse primary property path expressions
pub(super) fn parse_property_path_primary(&mut self) -> Result<PropertyPath> {
match self.peek() {
Some(Token::Caret) => {
self.advance();
let path = self.parse_property_path_primary()?;
Ok(PropertyPath::inverse(path))
}
Some(Token::Iri(iri)) => {
let iri = iri.clone();
self.advance();
Ok(PropertyPath::iri(NamedNode::new_unchecked(iri)))
}
Some(Token::PrefixedName(prefix, local)) => {
let prefix = prefix.clone();
let local = local.clone();
self.advance();
let full_iri = self.resolve_prefixed_name(&prefix, &local)?;
Ok(PropertyPath::iri(NamedNode::new_unchecked(full_iri)))
}
Some(Token::A) => {
// `a` is the SPARQL shorthand for the rdf:type predicate.
self.advance();
Ok(PropertyPath::iri(NamedNode::new_unchecked(
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
)))
}
Some(Token::Variable(var)) => {
let var = var.clone();
self.advance();
Ok(PropertyPath::Variable(Variable::new(var)?))
}
Some(Token::LeftParen) => {
self.advance();
let path = self.parse_property_path()?;
self.expect_token(Token::RightParen)?;
Ok(path)
}
Some(Token::Bang) => {
self.advance();
self.expect_token(Token::LeftParen)?;
let mut negated_paths = Vec::new();
loop {
negated_paths.push(self.parse_property_path_primary()?);
if !self.match_token(&Token::Pipe) {
break;
}
}
self.expect_token(Token::RightParen)?;
Ok(PropertyPath::NegatedPropertySet(negated_paths))
}
_ => bail!("Expected property path expression"),
}
}
}