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