1mod utils;
2use crate::{syntax_kind::SyntaxKind, Sparql, SyntaxNode};
3use rowan::{cursor::SyntaxToken, SyntaxNodeChildren, TextRange, TextSize};
4use std::usize;
5use utils::nth_ancestor;
6
7#[derive(Debug, PartialEq)]
8pub struct QueryUnit {
9 syntax: SyntaxNode,
10}
11
12impl QueryUnit {
13 pub fn select_query(&self) -> Option<SelectQuery> {
14 SelectQuery::cast(
15 self.syntax
16 .first_child()?
17 .first_child_by_kind(&SelectQuery::can_cast)?,
18 )
19 }
20
21 pub fn strip_prologue(&self) -> Option<SyntaxNode> {
22 self.syntax
23 .children()
24 .skip_while(|node| node.kind() == SyntaxKind::Prologue)
25 .next()
26 }
27
28 pub fn prologue(&self) -> Option<Prologue> {
29 Prologue::cast(
30 self.syntax
31 .first_child()?
32 .first_child_by_kind(&Prologue::can_cast)?,
33 )
34 }
35}
36
37#[derive(Debug, PartialEq)]
38pub struct Prologue {
39 syntax: SyntaxNode,
40}
41
42impl Prologue {
43 pub fn prefix_declarations(&self) -> Vec<PrefixDeclaration> {
44 self.syntax
45 .children()
46 .filter_map(&PrefixDeclaration::cast)
47 .collect()
48 }
49}
50
51#[derive(Debug, PartialEq)]
52pub struct SolutionModifier {
53 syntax: SyntaxNode,
54}
55
56impl SolutionModifier {
57 pub fn group_clause(&self) -> Option<GroupClause> {
58 GroupClause::cast(self.syntax.first_child()?)
59 }
60
61 pub fn select_query(&self) -> Option<SelectQuery> {
62 self.syntax.parent().and_then(SelectQuery::cast)
63 }
64}
65
66#[derive(Debug, PartialEq)]
67pub struct GroupClause {
68 syntax: SyntaxNode,
69}
70
71#[derive(Debug, PartialEq)]
72pub struct PrefixDeclaration {
73 syntax: SyntaxNode,
74}
75
76impl PrefixDeclaration {
77 pub fn prefix(&self) -> Option<String> {
78 Some(
79 self.syntax
80 .first_child_or_token_by_kind(&|kind| kind == SyntaxKind::PNAME_NS)?
81 .to_string()
82 .split_once(":")
83 .expect("Every PNAME_NS should contain ':' at the end")
84 .0
85 .to_string(),
86 )
87 }
88
89 pub fn raw_uri_prefix(&self) -> Option<String> {
90 let s = self
91 .syntax
92 .first_child_or_token_by_kind(&|kind| kind == SyntaxKind::IRIREF)?
93 .to_string();
94 (s.len() >= 2).then_some(s[1..(s.len() - 1)].to_string())
95 }
96
97 pub fn uri_prefix(&self) -> Option<String> {
98 Some(
99 self.syntax
100 .first_child_or_token_by_kind(&|kind| kind == SyntaxKind::IRIREF)?
101 .to_string(),
102 )
103 }
104}
105
106#[derive(Debug, PartialEq)]
107pub struct SelectQuery {
108 syntax: SyntaxNode,
109}
110
111impl SelectQuery {
112 pub fn where_clause(&self) -> Option<WhereClause> {
113 WhereClause::cast(self.syntax.first_child_by_kind(&WhereClause::can_cast)?)
114 }
115 pub fn select_clause(&self) -> Option<SelectClause> {
116 SelectClause::cast(self.syntax.first_child_by_kind(&SelectClause::can_cast)?)
117 }
118 pub fn variables(&self) -> Vec<Var> {
119 if let Some(where_clause) = self.where_clause() {
120 if let Some(ggp) = where_clause.group_graph_pattern() {
121 return ggp
122 .triple_blocks()
123 .iter()
124 .flat_map(|triple_block| {
125 triple_block
126 .triples()
127 .iter()
128 .flat_map(|triple| triple.visible_variables())
129 .collect::<Vec<Var>>()
130 })
131 .collect();
132 }
133 }
134 vec![]
135 }
136
137 pub fn soulution_modifier(&self) -> Option<SolutionModifier> {
138 SolutionModifier::cast(self.syntax.last_child()?)
139 }
140}
141
142#[derive(Debug, PartialEq, Clone)]
143pub struct SelectClause {
144 syntax: SyntaxNode,
145}
146
147impl SelectClause {
148 pub fn variables(&self) -> Vec<Var> {
160 self.syntax
161 .children()
162 .into_iter()
163 .filter_map(Var::cast)
164 .filter(|var| {
165 var.syntax()
166 .prev_sibling()
167 .is_none_or(|var| var.kind() != SyntaxKind::Expression)
168 })
169 .collect()
170 }
171
172 pub fn projected_variables(&self) -> Vec<Var> {
183 self.syntax.children().filter_map(Var::cast).collect()
184 }
185
186 pub fn is_star_selection(&self) -> bool {
187 self.syntax()
188 .last_token()
189 .is_some_and(|last| last.kind() == SyntaxKind::Star)
190 }
191
192 pub fn assignments(&self) -> Vec<Assignment> {
194 self.syntax
195 .children()
196 .filter_map(Var::cast)
197 .filter_map(|variable| {
198 variable
199 .syntax()
200 .prev_sibling()
201 .and_then(Expression::cast)
202 .map(|expression| Assignment {
203 expression,
204 variable,
205 })
206 })
207 .collect()
208 }
209
210 pub fn select_query(&self) -> Option<SelectQuery> {
211 SelectQuery::cast(self.syntax.parent()?)
212 }
213}
214
215#[derive(Debug)]
216pub struct Assignment {
217 pub expression: Expression,
218 pub variable: Var,
219}
220
221#[derive(Debug)]
222pub struct Expression {
223 syntax: SyntaxNode,
224}
225
226impl Expression {
227 pub fn unaggregated_variables(&self) -> Vec<Var> {
228 let mut res = vec![];
229 let mut stack = vec![self.syntax.clone()];
230 while let Some(node) = stack.pop() {
231 if node.kind() != SyntaxKind::Aggregate {
232 stack.extend(node.children());
233 }
234 if let Some(var) = Var::cast(node) {
235 res.push(var);
236 }
237 }
238 res
239 }
240}
241
242#[derive(Debug)]
243pub enum GraphPatternNotTriples {
244 GroupOrUnionGraphPattern(GroupOrUnionGraphPattern),
245 OptionalGraphPattern(OptionalGraphPattern),
246 MinusGraphPattern(MinusGraphPattern),
247 GraphGraphPattern(GraphGraphPattern),
248 ServiceGraphPattern(ServiceGraphPattern),
249 Filter(Filter),
250 Bind(Bind),
251 InlineData(InlineData),
252}
253
254impl GraphPatternNotTriples {
255 pub fn group_graph_pattern(&self) -> Option<GraphGraphPattern> {
256 match self {
257 GraphPatternNotTriples::GroupOrUnionGraphPattern(_group_or_union_graph_pattern) => {
258 todo!()
259 }
260 GraphPatternNotTriples::OptionalGraphPattern(_optional_graph_pattern) => todo!(),
261 GraphPatternNotTriples::MinusGraphPattern(_minus_graph_pattern) => todo!(),
262 GraphPatternNotTriples::GraphGraphPattern(_graph_graph_pattern) => todo!(),
263 GraphPatternNotTriples::ServiceGraphPattern(_service_graph_pattern) => todo!(),
264 GraphPatternNotTriples::Filter(_filter) => None,
265 GraphPatternNotTriples::Bind(_bind) => None,
266 GraphPatternNotTriples::InlineData(_inline_data) => None,
267 }
268 }
269}
270
271#[derive(Debug)]
272pub struct GroupOrUnionGraphPattern {
273 syntax: SyntaxNode,
274}
275impl GroupOrUnionGraphPattern {
276 fn group_graph_patterns(&self) -> Vec<GroupGraphPattern> {
277 self.syntax
278 .children()
279 .filter_map(GroupGraphPattern::cast)
280 .collect()
281 }
282}
283
284#[derive(Debug)]
285pub struct OptionalGraphPattern {
286 syntax: SyntaxNode,
287}
288impl OptionalGraphPattern {
289 fn group_graph_pattern(&self) -> Option<GroupGraphPattern> {
290 self.syntax.last_child().and_then(GroupGraphPattern::cast)
291 }
292}
293
294#[derive(Debug)]
295pub struct MinusGraphPattern {
296 syntax: SyntaxNode,
297}
298impl MinusGraphPattern {
299 fn group_graph_pattern(&self) -> Option<GroupGraphPattern> {
300 self.syntax.last_child().and_then(GroupGraphPattern::cast)
301 }
302}
303
304#[derive(Debug)]
305pub struct GraphGraphPattern {
306 syntax: SyntaxNode,
307}
308impl GraphGraphPattern {
309 fn group_graph_pattern(&self) -> Option<GroupGraphPattern> {
310 self.syntax.last_child().and_then(GroupGraphPattern::cast)
311 }
312}
313
314#[derive(Debug)]
315pub struct Filter {
316 syntax: SyntaxNode,
317}
318
319#[derive(Debug)]
320pub struct Bind {
321 syntax: SyntaxNode,
322}
323
324#[derive(Debug, PartialEq, Clone)]
325pub struct InlineData {
326 syntax: SyntaxNode,
327}
328
329#[derive(Debug)]
330pub struct WhereClause {
331 syntax: SyntaxNode,
332}
333
334#[derive(Debug)]
335pub struct ServiceGraphPattern {
336 syntax: SyntaxNode,
337}
338
339impl ServiceGraphPattern {
340 pub fn iri(&self) -> Option<Iri> {
341 self.syntax
342 .children()
343 .find(|child| child.kind() == SyntaxKind::VarOrIri)
344 .and_then(|child| child.first_child().and_then(Iri::cast))
345 }
346
347 pub fn group_graph_pattern(&self) -> Option<GroupGraphPattern> {
348 self.syntax
349 .children()
350 .last()
351 .and_then(GroupGraphPattern::cast)
352 }
353}
354
355impl WhereClause {
356 pub fn group_graph_pattern(&self) -> Option<GroupGraphPattern> {
357 GroupGraphPattern::cast(self.syntax.first_child()?)
358 }
359
360 pub fn where_token(&self) -> Option<SyntaxToken> {
361 match self.syntax.first_child_or_token() {
362 Some(rowan::NodeOrToken::Token(token)) if token.kind() == SyntaxKind::WHERE => {
363 Some(token.into())
364 }
365 _ => None,
366 }
367 }
368}
369
370#[derive(Debug)]
371pub struct GroupGraphPattern {
372 syntax: SyntaxNode,
373}
374
375impl GroupGraphPattern {
376 pub fn triple_blocks(&self) -> Vec<TriplesBlock> {
377 self.syntax()
378 .first_child_by_kind(&|kind| kind == SyntaxKind::GroupGraphPatternSub)
379 .map(|ggp| ggp.children().filter_map(TriplesBlock::cast).collect())
380 .unwrap_or_default()
381 }
382
383 pub fn group_pattern_not_triples(&self) -> Vec<GraphPatternNotTriples> {
384 self.syntax()
385 .first_child_by_kind(&|kind| kind == SyntaxKind::GroupGraphPatternSub)
386 .map(|ggp| {
387 ggp.children()
388 .filter_map(GraphPatternNotTriples::cast)
389 .collect()
390 })
391 .unwrap_or_default()
392 }
393
394 pub fn r_paren_token(&self) -> Option<SyntaxToken> {
395 match self.syntax.last_child_or_token() {
396 Some(rowan::NodeOrToken::Token(token)) if token.kind() == SyntaxKind::RCurly => {
397 Some(token.into())
398 }
399 _ => None,
400 }
401 }
402
403 pub fn l_paren_token(&self) -> Option<SyntaxToken> {
404 match self.syntax.first_child_or_token() {
405 Some(rowan::NodeOrToken::Token(token)) if token.kind() == SyntaxKind::LCurly => {
406 Some(token.into())
407 }
408 _ => None,
409 }
410 }
411
412 pub fn sub_select(&self) -> Option<SelectQuery> {
413 self.syntax().first_child().and_then(SelectQuery::cast)
414 }
415}
416
417#[derive(Debug)]
418pub struct TriplesBlock {
419 syntax: SyntaxNode,
420}
421
422impl TriplesBlock {
423 pub fn triples(&self) -> Vec<Triple> {
425 self.syntax
426 .children()
427 .filter_map(|child| match child.kind() {
428 SyntaxKind::TriplesSameSubjectPath => Some(vec![Triple::cast(child).unwrap()]),
429 SyntaxKind::TriplesBlock => Some(TriplesBlock::cast(child).unwrap().triples()),
430 _ => None,
431 })
432 .flatten()
433 .collect()
434 }
435
436 pub fn group_graph_pattern(&self) -> Option<GroupGraphPattern> {
437 GroupGraphPattern::cast(nth_ancestor(self.syntax.clone(), 2)?)
438 }
439
440 pub fn trailing_dot(&self) -> Option<SyntaxToken> {
443 self.syntax
444 .children_with_tokens()
445 .find_map(|child| match child {
446 rowan::NodeOrToken::Token(token) if token.kind() == SyntaxKind::Dot => {
447 Some(token.into())
448 }
449 _ => None,
450 })
451 }
452}
453
454#[derive(Debug, PartialEq)]
455pub struct Subject {
456 syntax: SyntaxNode,
457}
458
459#[derive(Debug, PartialEq, Clone)]
460pub struct Triple {
461 syntax: SyntaxNode,
462}
463
464impl Triple {
465 pub fn subject(&self) -> Option<Subject> {
466 self.syntax.first_child().and_then(Subject::cast)
467 }
468
469 pub fn properties_list_path(&self) -> Option<PropertyListPath> {
470 PropertyListPath::cast(self.syntax.children().nth(1)?)
471 }
472
473 pub fn triples_block(&self) -> Option<TriplesBlock> {
476 let mut parent = self.syntax.parent()?;
477 if parent.kind() != SyntaxKind::TriplesBlock {
478 return None;
479 }
480 while let Some(node) = parent.parent() {
481 if node.kind() == SyntaxKind::TriplesBlock {
482 parent = node;
483 } else {
484 break;
485 }
486 }
487 Some(TriplesBlock::cast(parent).expect("parent should be a TriplesBlock"))
488 }
489}
490
491#[derive(Debug)]
492pub struct PropertyPath {
493 pub verb: Path,
494 pub object: ObjectList,
495}
496
497impl PropertyPath {
498 pub fn text(&self) -> String {
499 format!("{} {}", self.verb.text(), self.object.text())
500 }
501
502 pub fn text_range(&self) -> TextRange {
503 TextRange::new(
504 self.verb.syntax().text_range().start(),
505 self.object.syntax.text_range().end(),
506 )
507 }
508}
509
510#[derive(Debug)]
511pub struct Path {
512 syntax: SyntaxNode,
513}
514
515impl Path {
516 pub fn sub_paths(&self) -> SubPaths {
517 SubPaths {
518 children: self.syntax.children(),
519 }
520 }
521}
522
523pub struct SubPaths {
524 children: SyntaxNodeChildren<Sparql>,
525}
526
527impl Iterator for SubPaths {
528 type Item = Path;
529
530 fn next(&mut self) -> Option<Self::Item> {
531 while let Some(next_child) = self.children.next() {
532 if let Some(path) = Path::cast(next_child.into()) {
533 return Some(path);
534 }
535 }
536 None
537 }
538}
539
540#[derive(Debug)]
541pub struct ObjectList {
542 syntax: SyntaxNode,
543}
544
545#[derive(Debug, PartialEq, Clone)]
546pub struct BlankPropertyList {
547 syntax: SyntaxNode,
548}
549
550impl BlankPropertyList {
551 pub fn triple(&self) -> Option<Triple> {
552 match self.syntax.kind() {
553 SyntaxKind::BlankNodePropertyListPath => {
554 todo!()
555 }
556 SyntaxKind::BlankNodePropertyList => {
557 todo!()
558 }
559 SyntaxKind::BlankNode => self.syntax.ancestors().nth(7).and_then(Triple::cast),
560 _ => None,
561 }
562 }
563
564 pub fn is_object(&self) -> bool {
565 todo!()
566 }
567
568 pub fn is_subject(&self) -> bool {
569 todo!()
570 }
571
572 pub fn property_list(&self) -> Option<PropertyListPath> {
573 match self.syntax.kind() {
574 SyntaxKind::BlankNodePropertyListPath | SyntaxKind::BlankNodePropertyList => {
575 PropertyListPath::cast(self.syntax.first_child()?)
576 }
577
578 _ => None,
579 }
580 }
581}
582
583#[derive(Debug)]
584pub struct PropertyListPath {
585 syntax: SyntaxNode,
586}
587
588impl PropertyListPath {
589 pub fn properties(&self) -> Vec<PropertyPath> {
590 self.syntax
591 .children()
592 .step_by(2)
593 .filter_map(|child| {
594 match (
595 Path::cast(child.clone()),
596 child.next_sibling().and_then(ObjectList::cast),
597 ) {
598 (None, None) | (None, Some(_)) | (Some(_), None) => None,
599 (Some(path), Some(object_list)) => Some(PropertyPath {
600 verb: path,
601 object: object_list,
602 }),
603 }
604 })
605 .collect()
606 }
607 pub fn variables(&self) -> Vec<Var> {
608 self.syntax
609 .children()
610 .filter_map(|child| match child.kind() {
611 SyntaxKind::VerbSimple => child.first_child().and_then(Var::cast),
612 _ => None,
613 })
614 .collect()
615 }
616}
617
618#[derive(Debug)]
619pub struct Iri {
620 syntax: SyntaxNode,
621}
622
623impl Iri {
624 pub fn prefixed_name(&self) -> Option<PrefixedName> {
625 self.syntax.first_child().and_then(PrefixedName::cast)
626 }
627
628 pub fn raw_iri(&self) -> Option<String> {
631 (self.syntax.first_child_or_token()?.kind() == SyntaxKind::IRIREF
632 && self.syntax.text_range().len() >= 2.into())
633 .then(|| self.text()[1..usize::from(self.syntax.text_range().len()) - 1].to_string())
634 }
635
636 pub fn is_uncompressed(&self) -> bool {
637 self.syntax
638 .first_child_or_token()
639 .is_some_and(|child| child.kind() == SyntaxKind::IRIREF)
640 }
641}
642
643#[derive(Debug)]
644pub struct PrefixedName {
645 syntax: SyntaxNode,
646}
647
648impl PrefixedName {
649 pub fn prefix(&self) -> String {
650 self.syntax
651 .to_string()
652 .split_once(":")
653 .expect("Every PrefixedName should contain a ':'")
654 .0
655 .to_string()
656 }
657
658 pub fn name(&self) -> String {
659 self.syntax
660 .to_string()
661 .split_once(":")
662 .expect("Every PrefixedName should contain a ':'")
663 .1
664 .to_string()
665 }
666}
667
668#[derive(Debug)]
669pub struct VarOrTerm {
670 syntax: SyntaxNode,
671}
672
673impl VarOrTerm {
674 pub fn var(&self) -> Option<Var> {
675 Var::cast(self.syntax.first_child()?)
676 }
677
678 pub fn is_var(&self) -> bool {
679 self.syntax
680 .first_child()
681 .map_or(false, |child| child.kind() == SyntaxKind::Var)
682 }
683
684 pub fn is_term(&self) -> bool {
685 !self.is_var()
686 }
687}
688
689#[derive(Debug)]
690pub struct Var {
691 syntax: SyntaxNode,
692}
693
694impl Var {
695 pub fn triple(&self) -> Option<Triple> {
696 self.syntax.ancestors().find_map(Triple::cast)
697 }
698
699 pub fn var_name(&self) -> String {
705 let text = self.syntax.text().to_string();
706 if !text.is_empty() && text.starts_with('?') {
707 return text[1..].to_string();
708 }
709 return text;
710 }
711}
712
713impl AstNode for Var {
714 #[inline]
715 fn kind() -> SyntaxKind {
716 SyntaxKind::Var
717 }
718
719 fn cast(syntax: SyntaxNode) -> Option<Self> {
720 if Self::can_cast(syntax.kind()) {
721 Some(Self { syntax })
722 } else {
723 None
724 }
725 }
726 #[inline]
727 fn syntax(&self) -> &SyntaxNode {
728 &self.syntax
729 }
730
731 fn visible_variables(&self) -> Vec<Var> {
732 vec![Var::cast(self.syntax().clone()).unwrap()]
733 }
734}
735
736impl AstNode for VarOrTerm {
737 #[inline]
738 fn kind() -> SyntaxKind {
739 SyntaxKind::VarOrTerm
740 }
741
742 fn cast(syntax: SyntaxNode) -> Option<Self> {
743 if Self::can_cast(syntax.kind()) {
744 Some(Self { syntax })
745 } else {
746 None
747 }
748 }
749 #[inline]
750 fn syntax(&self) -> &SyntaxNode {
751 &self.syntax
752 }
753
754 fn visible_variables(&self) -> Vec<Var> {
755 self.syntax
756 .first_child()
757 .and_then(Var::cast)
758 .map(|var| vec![var])
759 .unwrap_or_default()
760 }
761}
762
763impl AstNode for Iri {
764 #[inline]
765 fn kind() -> SyntaxKind {
766 SyntaxKind::iri
767 }
768
769 fn cast(syntax: SyntaxNode) -> Option<Self> {
770 if Self::can_cast(syntax.kind()) {
771 Some(Self { syntax })
772 } else {
773 None
774 }
775 }
776 #[inline]
777 fn syntax(&self) -> &SyntaxNode {
778 &self.syntax
779 }
780
781 fn visible_variables(&self) -> Vec<Var> {
782 vec![]
783 }
784}
785
786impl AstNode for PrefixedName {
787 #[inline]
788 fn kind() -> SyntaxKind {
789 SyntaxKind::PrefixedName
790 }
791
792 fn cast(syntax: SyntaxNode) -> Option<Self> {
793 if Self::can_cast(syntax.kind()) {
794 Some(Self { syntax })
795 } else {
796 None
797 }
798 }
799 #[inline]
800 fn syntax(&self) -> &SyntaxNode {
801 &self.syntax
802 }
803
804 fn visible_variables(&self) -> Vec<Var> {
805 vec![]
806 }
807}
808
809impl AstNode for Path {
810 fn kind() -> SyntaxKind {
811 SyntaxKind::VerbPath
812 }
813
814 fn can_cast(kind: SyntaxKind) -> bool {
815 matches!(
816 kind,
817 SyntaxKind::Path
818 | SyntaxKind::VerbPath
819 | SyntaxKind::VerbSimple
820 | SyntaxKind::PathAlternative
821 | SyntaxKind::PathSequence
822 | SyntaxKind::PathElt
823 | SyntaxKind::PathEltOrInverse
824 | SyntaxKind::PathPrimary
825 | SyntaxKind::PathNegatedPropertySet
826 | SyntaxKind::PathOneInPropertySet
827 )
828 }
829
830 fn cast(syntax: SyntaxNode) -> Option<Self> {
831 if Self::can_cast(syntax.kind()) {
832 Some(Self { syntax })
833 } else {
834 None
835 }
836 }
837
838 fn syntax(&self) -> &SyntaxNode {
839 &self.syntax
840 }
841
842 fn visible_variables(&self) -> Vec<Var> {
843 vec![]
844 }
845}
846
847impl AstNode for ObjectList {
848 fn kind() -> SyntaxKind {
849 SyntaxKind::ObjectListPath
850 }
851 fn can_cast(kind: SyntaxKind) -> bool {
852 matches!(kind, SyntaxKind::ObjectListPath | SyntaxKind::ObjectList)
853 }
854
855 fn cast(syntax: SyntaxNode) -> Option<Self> {
856 if Self::can_cast(syntax.kind()) {
857 Some(Self { syntax })
858 } else {
859 None
860 }
861 }
862
863 fn syntax(&self) -> &SyntaxNode {
864 &self.syntax
865 }
866
867 fn visible_variables(&self) -> Vec<Var> {
868 self.syntax.descendants().filter_map(Var::cast).collect()
869 }
870}
871
872impl AstNode for BlankPropertyList {
873 #[inline]
874 fn kind() -> SyntaxKind {
875 SyntaxKind::BlankNodePropertyListPath
876 }
877
878 fn can_cast(kind: SyntaxKind) -> bool {
879 matches!(
880 kind,
881 SyntaxKind::BlankNodePropertyListPath
882 | SyntaxKind::BlankNodePropertyList
883 | SyntaxKind::BlankNode
884 )
885 }
886
887 #[inline]
888 fn cast(syntax: SyntaxNode) -> Option<Self> {
889 if Self::can_cast(syntax.kind()) {
890 Some(Self { syntax })
891 } else {
892 None
893 }
894 }
895
896 #[inline]
897 fn syntax(&self) -> &SyntaxNode {
898 &self.syntax
899 }
900
901 fn visible_variables(&self) -> Vec<Var> {
902 self.syntax.descendants().filter_map(Var::cast).collect()
903 }
904}
905
906impl AstNode for PropertyListPath {
907 #[inline]
908 fn kind() -> SyntaxKind {
909 SyntaxKind::PropertyListPathNotEmpty
910 }
911
912 fn can_cast(kind: SyntaxKind) -> bool {
913 matches!(
914 kind,
915 SyntaxKind::PropertyListPath | SyntaxKind::PropertyListPathNotEmpty
916 )
917 }
918
919 #[inline]
920 fn cast(syntax: SyntaxNode) -> Option<Self> {
921 if Self::can_cast(syntax.kind()) {
922 Some(Self { syntax })
923 } else {
924 None
925 }
926 }
927
928 #[inline]
929 fn syntax(&self) -> &SyntaxNode {
930 &self.syntax
931 }
932
933 fn visible_variables(&self) -> Vec<Var> {
934 self.syntax.descendants().filter_map(Var::cast).collect()
935 }
936}
937
938impl AstNode for Subject {
939 #[inline]
940 fn kind() -> SyntaxKind {
941 SyntaxKind::VarOrTerm
942 }
943
944 fn can_cast(kind: SyntaxKind) -> bool {
945 matches!(kind, SyntaxKind::VarOrTerm | SyntaxKind::TriplesNodePath)
946 }
947
948 fn cast(syntax: SyntaxNode) -> Option<Self> {
949 if Self::can_cast(syntax.kind()) {
950 Some(Self { syntax })
951 } else {
952 None
953 }
954 }
955 #[inline]
956 fn syntax(&self) -> &SyntaxNode {
957 &self.syntax
958 }
959
960 fn visible_variables(&self) -> Vec<Var> {
961 self.syntax
962 .first_child()
963 .and_then(Var::cast)
964 .map(|var| vec![var])
965 .unwrap_or_default()
966 }
967}
968
969impl AstNode for Triple {
970 #[inline]
971 fn kind() -> SyntaxKind {
972 SyntaxKind::TriplesSameSubjectPath
973 }
974
975 fn cast(syntax: SyntaxNode) -> Option<Self> {
976 if Self::can_cast(syntax.kind()) {
977 Some(Self { syntax })
978 } else {
979 None
980 }
981 }
982 #[inline]
983 fn syntax(&self) -> &SyntaxNode {
984 &self.syntax
985 }
986
987 fn visible_variables(&self) -> Vec<Var> {
988 self.syntax.descendants().filter_map(Var::cast).collect()
989 }
990}
991
992impl AstNode for TriplesBlock {
993 #[inline]
994 fn kind() -> SyntaxKind {
995 SyntaxKind::TriplesBlock
996 }
997
998 fn cast(syntax: SyntaxNode) -> Option<Self> {
999 if Self::can_cast(syntax.kind()) {
1000 Some(Self { syntax })
1001 } else {
1002 None
1003 }
1004 }
1005
1006 #[inline]
1007 fn syntax(&self) -> &SyntaxNode {
1008 &self.syntax
1009 }
1010
1011 fn visible_variables(&self) -> Vec<Var> {
1012 self.syntax.descendants().filter_map(Var::cast).collect()
1013 }
1014}
1015
1016impl AstNode for Expression {
1017 #[inline]
1018 fn kind() -> SyntaxKind {
1019 SyntaxKind::Expression
1020 }
1021
1022 fn cast(syntax: SyntaxNode) -> Option<Self> {
1023 if Self::can_cast(syntax.kind()) {
1024 Some(Self { syntax })
1025 } else {
1026 None
1027 }
1028 }
1029
1030 #[inline]
1031 fn syntax(&self) -> &SyntaxNode {
1032 &self.syntax
1033 }
1034
1035 fn visible_variables(&self) -> Vec<Var> {
1036 self.syntax.descendants().filter_map(Var::cast).collect()
1037 }
1038}
1039
1040impl AstNode for GroupGraphPattern {
1041 #[inline]
1042 fn kind() -> SyntaxKind {
1043 SyntaxKind::GroupGraphPattern
1044 }
1045
1046 fn cast(syntax: SyntaxNode) -> Option<Self> {
1047 if Self::can_cast(syntax.kind()) {
1048 Some(Self { syntax })
1049 } else {
1050 None
1051 }
1052 }
1053
1054 #[inline]
1055 fn syntax(&self) -> &SyntaxNode {
1056 &self.syntax
1057 }
1058
1059 fn visible_variables(&self) -> Vec<Var> {
1060 self.sub_select()
1061 .map(|select| select.visible_variables())
1062 .or(self.syntax.first_child().map(|child| {
1063 child
1064 .children()
1065 .into_iter()
1066 .filter_map(|child| match child.kind() {
1067 SyntaxKind::TriplesBlock => {
1068 TriplesBlock::cast(child).map(|tp| tp.visible_variables())
1069 }
1070 SyntaxKind::GraphPatternNotTriples => GraphPatternNotTriples::cast(child)
1071 .map(|pattern| pattern.visible_variables()),
1072 _ => None,
1073 })
1074 .flatten()
1075 .collect()
1076 }))
1077 .unwrap_or_default()
1078 }
1079}
1080
1081impl AstNode for WhereClause {
1082 #[inline]
1083 fn kind() -> SyntaxKind {
1084 SyntaxKind::WhereClause
1085 }
1086
1087 fn cast(syntax: SyntaxNode) -> Option<Self> {
1088 if Self::can_cast(syntax.kind()) {
1089 Some(Self { syntax })
1090 } else {
1091 None
1092 }
1093 }
1094
1095 #[inline]
1096 fn syntax(&self) -> &SyntaxNode {
1097 &self.syntax
1098 }
1099
1100 fn visible_variables(&self) -> Vec<Var> {
1101 self.group_graph_pattern()
1102 .map(|ggp| ggp.visible_variables())
1103 .unwrap_or_default()
1104 }
1105}
1106impl AstNode for OptionalGraphPattern {
1107 #[inline]
1108 fn kind() -> SyntaxKind {
1109 SyntaxKind::OptionalGraphPattern
1110 }
1111
1112 fn cast(syntax: SyntaxNode) -> Option<Self> {
1113 if Self::can_cast(syntax.kind()) {
1114 Some(Self { syntax })
1115 } else {
1116 None
1117 }
1118 }
1119
1120 #[inline]
1121 fn syntax(&self) -> &SyntaxNode {
1122 &self.syntax
1123 }
1124
1125 fn visible_variables(&self) -> Vec<Var> {
1126 self.group_graph_pattern()
1127 .map(|ggp| ggp.visible_variables())
1128 .unwrap_or_default()
1129 }
1130}
1131
1132impl AstNode for GroupOrUnionGraphPattern {
1133 #[inline]
1134 fn kind() -> SyntaxKind {
1135 SyntaxKind::GroupOrUnionGraphPattern
1136 }
1137
1138 fn cast(syntax: SyntaxNode) -> Option<Self> {
1139 if Self::can_cast(syntax.kind()) {
1140 Some(Self { syntax })
1141 } else {
1142 None
1143 }
1144 }
1145
1146 #[inline]
1147 fn syntax(&self) -> &SyntaxNode {
1148 &self.syntax
1149 }
1150
1151 fn visible_variables(&self) -> Vec<Var> {
1152 self.group_graph_patterns()
1153 .into_iter()
1154 .flat_map(|ggp| ggp.visible_variables())
1155 .collect()
1156 }
1157}
1158
1159impl AstNode for MinusGraphPattern {
1160 #[inline]
1161 fn kind() -> SyntaxKind {
1162 SyntaxKind::MinusGraphPattern
1163 }
1164
1165 fn cast(syntax: SyntaxNode) -> Option<Self> {
1166 if Self::can_cast(syntax.kind()) {
1167 Some(Self { syntax })
1168 } else {
1169 None
1170 }
1171 }
1172
1173 #[inline]
1174 fn syntax(&self) -> &SyntaxNode {
1175 &self.syntax
1176 }
1177
1178 fn visible_variables(&self) -> Vec<Var> {
1179 self.group_graph_pattern()
1180 .map(|ggp| ggp.visible_variables())
1181 .unwrap_or_default()
1182 }
1183}
1184
1185impl AstNode for GraphGraphPattern {
1186 #[inline]
1187 fn kind() -> SyntaxKind {
1188 SyntaxKind::GraphGraphPattern
1189 }
1190
1191 fn cast(syntax: SyntaxNode) -> Option<Self> {
1192 if Self::can_cast(syntax.kind()) {
1193 Some(Self { syntax })
1194 } else {
1195 None
1196 }
1197 }
1198
1199 #[inline]
1200 fn syntax(&self) -> &SyntaxNode {
1201 &self.syntax
1202 }
1203
1204 fn visible_variables(&self) -> Vec<Var> {
1205 self.group_graph_pattern()
1206 .map(|ggp| ggp.visible_variables())
1207 .unwrap_or_default()
1208 }
1209}
1210
1211impl AstNode for ServiceGraphPattern {
1212 #[inline]
1213 fn kind() -> SyntaxKind {
1214 SyntaxKind::ServiceGraphPattern
1215 }
1216
1217 fn cast(syntax: SyntaxNode) -> Option<Self> {
1218 if Self::can_cast(syntax.kind()) {
1219 Some(Self { syntax })
1220 } else {
1221 None
1222 }
1223 }
1224
1225 #[inline]
1226 fn syntax(&self) -> &SyntaxNode {
1227 &self.syntax
1228 }
1229
1230 fn visible_variables(&self) -> Vec<Var> {
1231 self.group_graph_pattern()
1232 .map(|ggp| ggp.visible_variables())
1233 .unwrap_or_default()
1234 }
1235}
1236
1237impl AstNode for Filter {
1238 #[inline]
1239 fn kind() -> SyntaxKind {
1240 SyntaxKind::Filter
1241 }
1242
1243 fn cast(syntax: SyntaxNode) -> Option<Self> {
1244 if Self::can_cast(syntax.kind()) {
1245 Some(Self { syntax })
1246 } else {
1247 None
1248 }
1249 }
1250
1251 #[inline]
1252 fn syntax(&self) -> &SyntaxNode {
1253 &self.syntax
1254 }
1255
1256 fn visible_variables(&self) -> Vec<Var> {
1257 self.syntax().descendants().filter_map(Var::cast).collect()
1258 }
1259}
1260
1261impl AstNode for Bind {
1262 #[inline]
1263 fn kind() -> SyntaxKind {
1264 SyntaxKind::Bind
1265 }
1266
1267 fn cast(syntax: SyntaxNode) -> Option<Self> {
1268 if Self::can_cast(syntax.kind()) {
1269 Some(Self { syntax })
1270 } else {
1271 None
1272 }
1273 }
1274
1275 #[inline]
1276 fn syntax(&self) -> &SyntaxNode {
1277 &self.syntax
1278 }
1279
1280 fn visible_variables(&self) -> Vec<Var> {
1281 self.syntax()
1282 .children()
1283 .last()
1284 .and_then(Var::cast)
1285 .map(|var| vec![var])
1286 .unwrap_or_default()
1287 }
1288}
1289
1290impl AstNode for InlineData {
1291 #[inline]
1292 fn kind() -> SyntaxKind {
1293 SyntaxKind::InlineData
1294 }
1295
1296 fn cast(syntax: SyntaxNode) -> Option<Self> {
1297 if Self::can_cast(syntax.kind()) {
1298 Some(Self { syntax })
1299 } else {
1300 None
1301 }
1302 }
1303
1304 #[inline]
1305 fn syntax(&self) -> &SyntaxNode {
1306 &self.syntax
1307 }
1308
1309 fn visible_variables(&self) -> Vec<Var> {
1310 self.syntax()
1311 .last_child()
1312 .and_then(|data_block| data_block.first_child())
1313 .and_then(|inline_data| match inline_data.kind() {
1314 SyntaxKind::InlineDataOneVar => inline_data
1315 .first_child()
1316 .and_then(Var::cast)
1317 .map(|var| vec![var]),
1318 SyntaxKind::InlineDataFull => Some(
1319 inline_data
1320 .children_with_tokens()
1321 .take_while(|child| child.kind() != SyntaxKind::RParen)
1322 .filter_map(|child| child.into_node().and_then(Var::cast))
1323 .collect(),
1324 ),
1325 _ => None,
1326 })
1327 .unwrap_or_default()
1328 }
1329}
1330
1331impl AstNode for SelectClause {
1332 #[inline]
1333 fn kind() -> SyntaxKind {
1334 SyntaxKind::SelectClause
1335 }
1336
1337 fn cast(syntax: SyntaxNode) -> Option<Self> {
1338 if Self::can_cast(syntax.kind()) {
1339 Some(Self { syntax })
1340 } else {
1341 None
1342 }
1343 }
1344
1345 #[inline]
1346 fn syntax(&self) -> &SyntaxNode {
1347 &self.syntax
1348 }
1349
1350 fn visible_variables(&self) -> Vec<Var> {
1351 self.projected_variables()
1352 }
1353}
1354
1355impl AstNode for Prologue {
1356 #[inline]
1357 fn kind() -> SyntaxKind {
1358 SyntaxKind::Prologue
1359 }
1360
1361 fn cast(syntax: SyntaxNode) -> Option<Self> {
1362 if Self::can_cast(syntax.kind()) {
1363 Some(Self { syntax })
1364 } else {
1365 None
1366 }
1367 }
1368
1369 #[inline]
1370 fn syntax(&self) -> &SyntaxNode {
1371 &self.syntax
1372 }
1373
1374 fn visible_variables(&self) -> Vec<Var> {
1375 vec![]
1376 }
1377}
1378
1379impl AstNode for SolutionModifier {
1380 #[inline]
1381 fn kind() -> SyntaxKind {
1382 SyntaxKind::SolutionModifier
1383 }
1384
1385 fn cast(syntax: SyntaxNode) -> Option<Self> {
1386 if Self::can_cast(syntax.kind()) {
1387 Some(Self { syntax })
1388 } else {
1389 None
1390 }
1391 }
1392
1393 #[inline]
1394 fn syntax(&self) -> &SyntaxNode {
1395 &self.syntax
1396 }
1397
1398 fn visible_variables(&self) -> Vec<Var> {
1399 vec![]
1400 }
1401}
1402
1403impl AstNode for GroupClause {
1404 #[inline]
1405 fn kind() -> SyntaxKind {
1406 SyntaxKind::GroupClause
1407 }
1408
1409 fn cast(syntax: SyntaxNode) -> Option<Self> {
1410 if Self::can_cast(syntax.kind()) {
1411 Some(Self { syntax })
1412 } else {
1413 None
1414 }
1415 }
1416
1417 #[inline]
1418 fn syntax(&self) -> &SyntaxNode {
1419 &self.syntax
1420 }
1421
1422 fn visible_variables(&self) -> Vec<Var> {
1423 self.syntax
1424 .children()
1425 .into_iter()
1426 .filter_map(|group_condition| group_condition.last_child())
1427 .filter_map(Var::cast)
1428 .collect()
1429 }
1430}
1431
1432impl AstNode for PrefixDeclaration {
1433 #[inline]
1434 fn kind() -> SyntaxKind {
1435 SyntaxKind::PrefixDecl
1436 }
1437
1438 fn cast(syntax: SyntaxNode) -> Option<Self> {
1439 if Self::can_cast(syntax.kind()) {
1440 Some(Self { syntax })
1441 } else {
1442 None
1443 }
1444 }
1445
1446 #[inline]
1447 fn syntax(&self) -> &SyntaxNode {
1448 &self.syntax
1449 }
1450
1451 fn visible_variables(&self) -> Vec<Var> {
1452 vec![]
1453 }
1454}
1455
1456impl AstNode for QueryUnit {
1457 #[inline]
1458 fn kind() -> SyntaxKind {
1459 SyntaxKind::QueryUnit
1460 }
1461
1462 fn cast(syntax: SyntaxNode) -> Option<Self> {
1463 if Self::can_cast(syntax.kind()) {
1464 Some(Self { syntax })
1465 } else {
1466 None
1467 }
1468 }
1469
1470 #[inline]
1471 fn syntax(&self) -> &SyntaxNode {
1472 &self.syntax
1473 }
1474
1475 fn visible_variables(&self) -> Vec<Var> {
1476 self.select_query()
1477 .map(|select_query| select_query.visible_variables())
1478 .unwrap_or_default()
1479 }
1480}
1481
1482impl AstNode for SelectQuery {
1483 #[inline]
1484 fn kind() -> SyntaxKind {
1485 SyntaxKind::SelectQuery
1486 }
1487
1488 fn can_cast(kind: SyntaxKind) -> bool {
1489 matches!(kind, SyntaxKind::SelectQuery | SyntaxKind::SubSelect)
1490 }
1491
1492 fn cast(syntax: SyntaxNode) -> Option<Self> {
1493 if Self::can_cast(syntax.kind()) {
1494 Some(Self { syntax })
1495 } else {
1496 None
1497 }
1498 }
1499
1500 #[inline]
1501 fn syntax(&self) -> &SyntaxNode {
1502 &self.syntax
1503 }
1504
1505 fn visible_variables(&self) -> Vec<Var> {
1506 if let Some(select_clause) = self.select_clause() {
1507 if let Some(SyntaxKind::Star) = select_clause
1508 .syntax
1509 .last_child_or_token()
1510 .map(|last| last.kind())
1511 {
1512 self.where_clause()
1513 .map(|where_clause| where_clause.visible_variables())
1514 .unwrap_or_default()
1515 } else {
1516 select_clause.visible_variables()
1517 }
1518 } else {
1519 Vec::new()
1520 }
1521 }
1522}
1523
1524impl AstNode for GraphPatternNotTriples {
1525 #[inline]
1526 fn kind() -> SyntaxKind {
1527 SyntaxKind::GraphPatternNotTriples
1528 }
1529
1530 fn can_cast(kind: SyntaxKind) -> bool {
1531 matches!(
1532 kind,
1533 SyntaxKind::GroupOrUnionGraphPattern
1534 | SyntaxKind::OptionalGraphPattern
1535 | SyntaxKind::MinusGraphPattern
1536 | SyntaxKind::GraphGraphPattern
1537 | SyntaxKind::ServiceGraphPattern
1538 | SyntaxKind::Filter
1539 | SyntaxKind::Bind
1540 | SyntaxKind::InlineData
1541 )
1542 }
1543
1544 fn cast(syntax: SyntaxNode) -> Option<Self> {
1545 let child = syntax.first_child()?;
1546 match child.kind() {
1547 SyntaxKind::GroupOrUnionGraphPattern => {
1548 Some(GraphPatternNotTriples::GroupOrUnionGraphPattern(
1549 GroupOrUnionGraphPattern::cast(child)?,
1550 ))
1551 }
1552 SyntaxKind::OptionalGraphPattern => Some(GraphPatternNotTriples::OptionalGraphPattern(
1553 OptionalGraphPattern::cast(child)?,
1554 )),
1555 SyntaxKind::MinusGraphPattern => Some(GraphPatternNotTriples::MinusGraphPattern(
1556 MinusGraphPattern::cast(child)?,
1557 )),
1558 SyntaxKind::GraphGraphPattern => Some(GraphPatternNotTriples::GraphGraphPattern(
1559 GraphGraphPattern::cast(child)?,
1560 )),
1561 SyntaxKind::ServiceGraphPattern => Some(GraphPatternNotTriples::ServiceGraphPattern(
1562 ServiceGraphPattern::cast(child)?,
1563 )),
1564 SyntaxKind::Filter => Some(GraphPatternNotTriples::Filter(Filter::cast(child)?)),
1565 SyntaxKind::Bind => Some(GraphPatternNotTriples::Bind(Bind::cast(child)?)),
1566 SyntaxKind::InlineData => {
1567 Some(GraphPatternNotTriples::InlineData(InlineData::cast(child)?))
1568 }
1569 _ => None,
1570 }
1571 }
1572
1573 #[inline]
1574 fn syntax(&self) -> &SyntaxNode {
1575 match self {
1576 GraphPatternNotTriples::GroupOrUnionGraphPattern(x) => x.syntax(),
1577 GraphPatternNotTriples::OptionalGraphPattern(x) => x.syntax(),
1578 GraphPatternNotTriples::MinusGraphPattern(x) => x.syntax(),
1579 GraphPatternNotTriples::GraphGraphPattern(x) => x.syntax(),
1580 GraphPatternNotTriples::ServiceGraphPattern(x) => x.syntax(),
1581 GraphPatternNotTriples::Filter(x) => x.syntax(),
1582 GraphPatternNotTriples::Bind(x) => x.syntax(),
1583 GraphPatternNotTriples::InlineData(x) => x.syntax(),
1584 }
1585 }
1586
1587 fn visible_variables(&self) -> Vec<Var> {
1588 match self {
1589 GraphPatternNotTriples::GroupOrUnionGraphPattern(x) => x.visible_variables(),
1590 GraphPatternNotTriples::OptionalGraphPattern(x) => x.visible_variables(),
1591 GraphPatternNotTriples::MinusGraphPattern(x) => x.visible_variables(),
1592 GraphPatternNotTriples::GraphGraphPattern(x) => x.visible_variables(),
1593 GraphPatternNotTriples::ServiceGraphPattern(x) => x.visible_variables(),
1594 GraphPatternNotTriples::Filter(x) => x.visible_variables(),
1595 GraphPatternNotTriples::Bind(x) => x.visible_variables(),
1596 GraphPatternNotTriples::InlineData(x) => x.visible_variables(),
1597 }
1598 }
1599}
1600
1601pub trait AstNode {
1602 fn kind() -> SyntaxKind;
1603
1604 #[inline]
1605 fn can_cast(kind: SyntaxKind) -> bool {
1606 Self::kind() == kind
1607 }
1608
1609 fn cast(syntax: SyntaxNode) -> Option<Self>
1610 where
1611 Self: Sized;
1612
1613 fn syntax(&self) -> &SyntaxNode;
1614
1615 fn visible_variables(&self) -> Vec<Var>;
1617
1618 fn has_error(&self) -> bool {
1619 self.syntax()
1620 .preorder()
1621 .find(|walk_event| match walk_event {
1622 rowan::WalkEvent::Enter(node) if node.kind() == SyntaxKind::Error => true,
1623 _ => false,
1624 })
1625 .is_some()
1626 }
1627
1628 fn collect_decendants(&self, matcher: &impl Fn(SyntaxKind) -> bool) -> Vec<SyntaxNode> {
1629 self.syntax()
1630 .preorder()
1631 .filter_map(|walk_event| match walk_event {
1632 rowan::WalkEvent::Enter(node) if matcher(node.kind()) => Some(node),
1633 _ => None,
1634 })
1635 .collect()
1636 }
1637
1638 fn preorder_find_kind(&self, kind: SyntaxKind) -> Vec<SyntaxNode> {
1639 self.syntax()
1640 .preorder()
1641 .filter_map(|walk_event| match walk_event {
1642 rowan::WalkEvent::Enter(node) if node.kind() == kind => Some(node),
1643 _ => None,
1644 })
1645 .collect()
1646 }
1647
1648 fn used_prefixes(&self) -> Vec<String> {
1649 self.syntax()
1650 .descendants()
1651 .filter_map(PrefixedName::cast)
1652 .map(|prefixed_name| prefixed_name.prefix())
1653 .collect()
1654 }
1655
1656 fn text(&self) -> String {
1657 self.syntax().text().to_string()
1658 }
1659
1660 fn text_until(&self, offset: TextSize) -> String {
1661 let syntax = self.syntax();
1662 assert!(syntax.text_range().start() <= offset);
1663 syntax
1664 .text()
1665 .slice(TextSize::new(0)..(offset - syntax.text_range().start()))
1666 .to_string()
1667 }
1668}
1669
1670#[cfg(test)]
1671mod tests;