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