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