1mod utils;
2
3use std::usize;
4
5use rowan::{cursor::SyntaxToken, SyntaxNodeChildren};
6use utils::nth_ancestor;
7
8use crate::{syntax_kind::SyntaxKind, Sparql, SyntaxNode};
9
10#[derive(Debug, PartialEq)]
11pub struct QueryUnit {
12 syntax: SyntaxNode,
13}
14
15impl QueryUnit {
16 pub fn select_query(&self) -> Option<SelectQuery> {
17 SelectQuery::cast(
18 self.syntax
19 .first_child()?
20 .first_child_by_kind(&SelectQuery::can_cast)?,
21 )
22 }
23
24 pub fn prologue(&self) -> Option<Prologue> {
25 Prologue::cast(
26 self.syntax
27 .first_child()?
28 .first_child_by_kind(&Prologue::can_cast)?,
29 )
30 }
31}
32
33#[derive(Debug, PartialEq)]
34pub struct Prologue {
35 syntax: SyntaxNode,
36}
37
38impl Prologue {
39 pub fn prefix_declarations(&self) -> Vec<PrefixDeclaration> {
40 self.syntax
41 .children()
42 .filter_map(&PrefixDeclaration::cast)
43 .collect()
44 }
45}
46
47#[derive(Debug, PartialEq)]
48pub struct SolutionModifier {
49 syntax: SyntaxNode,
50}
51
52#[derive(Debug, PartialEq)]
53pub struct PrefixDeclaration {
54 syntax: SyntaxNode,
55}
56
57impl PrefixDeclaration {
58 pub fn prefix(&self) -> Option<String> {
59 Some(
60 self.syntax
61 .first_child_or_token_by_kind(&|kind| kind == SyntaxKind::PNAME_NS)?
62 .to_string()
63 .split_once(":")
64 .expect("Every PNAME_NS should contain ':' at the end")
65 .0
66 .to_string(),
67 )
68 }
69 pub fn uri_prefix(&self) -> Option<String> {
70 Some(
71 self.syntax
72 .first_child_or_token_by_kind(&|kind| kind == SyntaxKind::IRIREF)?
73 .to_string(),
74 )
75 }
76}
77
78#[derive(Debug, PartialEq)]
79pub struct SelectQuery {
80 syntax: SyntaxNode,
81}
82
83impl SelectQuery {
84 pub fn where_clause(&self) -> Option<WhereClause> {
85 WhereClause::cast(self.syntax.first_child_by_kind(&WhereClause::can_cast)?)
86 }
87 pub fn select_clause(&self) -> Option<SelectClause> {
88 SelectClause::cast(self.syntax.first_child_by_kind(&SelectClause::can_cast)?)
89 }
90 pub fn variables(&self) -> Vec<Var> {
91 if let Some(where_clause) = self.where_clause() {
92 if let Some(ggp) = where_clause.group_graph_pattern() {
93 return ggp
94 .triple_blocks()
95 .iter()
96 .flat_map(|triple_block| {
97 triple_block
98 .triples()
99 .iter()
100 .flat_map(|triple| triple.variables())
101 .collect::<Vec<Var>>()
102 })
103 .collect();
104 }
105 }
106 vec![]
107 }
108
109 pub fn soulution_modifier(&self) -> Option<SolutionModifier> {
110 SolutionModifier::cast(self.syntax.last_child()?)
111 }
112}
113
114#[derive(Debug, PartialEq)]
115pub struct SelectClause {
116 syntax: SyntaxNode,
117}
118
119impl SelectClause {
120 pub fn variables(&self) -> Vec<Var> {
121 self.syntax
122 .children()
123 .filter_map(|child| {
124 if child.kind() == SyntaxKind::Var {
125 Some(Var::cast(child).expect("Node of kind Var should be castable to Var"))
126 } else {
127 None
128 }
129 })
130 .collect()
131 }
132
133 pub fn select_query(&self) -> Option<SelectQuery> {
134 SelectQuery::cast(self.syntax.parent()?)
135 }
136}
137
138#[derive(Debug)]
139pub enum GroupPatternNotTriples {
140 GroupOrUnionGraphPattern(GroupOrUnionGraphPattern),
141 OptionalGraphPattern(OptionalGraphPattern),
142 MinusGraphPattern(MinusGraphPattern),
143 GraphGraphPattern(GraphGraphPattern),
144 ServiceGraphPattern(ServiceGraphPattern),
145 Filter(Filter),
146 Bind(Bind),
147 InlineData(InlineData),
148}
149
150impl GroupPatternNotTriples {
151 pub fn group_graph_pattern(&self) -> Option<GraphGraphPattern> {
152 match self {
153 GroupPatternNotTriples::GroupOrUnionGraphPattern(_group_or_union_graph_pattern) => {
154 todo!()
155 }
156 GroupPatternNotTriples::OptionalGraphPattern(_optional_graph_pattern) => todo!(),
157 GroupPatternNotTriples::MinusGraphPattern(_minus_graph_pattern) => todo!(),
158 GroupPatternNotTriples::GraphGraphPattern(_graph_graph_pattern) => todo!(),
159 GroupPatternNotTriples::ServiceGraphPattern(_service_graph_pattern) => todo!(),
160 GroupPatternNotTriples::Filter(_filter) => None,
161 GroupPatternNotTriples::Bind(_bind) => None,
162 GroupPatternNotTriples::InlineData(_inline_data) => None,
163 }
164 }
165}
166
167#[derive(Debug)]
168pub struct GroupOrUnionGraphPattern {
169 syntax: SyntaxNode,
170}
171
172#[derive(Debug)]
173pub struct OptionalGraphPattern {
174 syntax: SyntaxNode,
175}
176
177#[derive(Debug)]
178pub struct MinusGraphPattern {
179 syntax: SyntaxNode,
180}
181
182#[derive(Debug)]
183pub struct GraphGraphPattern {
184 syntax: SyntaxNode,
185}
186
187#[derive(Debug)]
188pub struct Filter {
189 syntax: SyntaxNode,
190}
191
192#[derive(Debug)]
193pub struct Bind {
194 syntax: SyntaxNode,
195}
196
197#[derive(Debug)]
198pub struct InlineData {
199 syntax: SyntaxNode,
200}
201
202#[derive(Debug)]
203pub struct WhereClause {
204 syntax: SyntaxNode,
205}
206
207#[derive(Debug)]
208pub struct ServiceGraphPattern {
209 syntax: SyntaxNode,
210}
211
212impl ServiceGraphPattern {
213 pub fn iri(&self) -> Option<Iri> {
214 self.syntax
215 .children()
216 .find(|child| child.kind() == SyntaxKind::VarOrIri)
217 .and_then(|child| child.first_child().and_then(Iri::cast))
218 }
219}
220
221impl WhereClause {
222 pub fn group_graph_pattern(&self) -> Option<GroupGraphPattern> {
223 GroupGraphPattern::cast(self.syntax.first_child()?)
224 }
225
226 pub fn where_token(&self) -> Option<SyntaxToken> {
227 match self.syntax.first_child_or_token() {
228 Some(rowan::NodeOrToken::Token(token)) if token.kind() == SyntaxKind::WHERE => {
229 Some(token.into())
230 }
231 _ => None,
232 }
233 }
234}
235
236#[derive(Debug)]
237pub struct GroupGraphPattern {
238 syntax: SyntaxNode,
239}
240
241impl GroupGraphPattern {
242 pub fn triple_blocks(&self) -> Vec<TriplesBlock> {
243 self.syntax()
244 .first_child_by_kind(&|kind| kind == SyntaxKind::GroupGraphPatternSub)
245 .map(|ggp| ggp.children().filter_map(TriplesBlock::cast).collect())
246 .unwrap_or_default()
247 }
248
249 pub fn group_pattern_not_triples(&self) -> Vec<GroupPatternNotTriples> {
250 self.syntax()
251 .first_child_by_kind(&|kind| kind == SyntaxKind::GroupGraphPatternSub)
252 .map(|ggp| {
253 ggp.children()
254 .filter_map(GroupPatternNotTriples::cast)
255 .collect()
256 })
257 .unwrap_or_default()
258 }
259
260 pub fn r_paren_token(&self) -> Option<SyntaxToken> {
261 match self.syntax.last_child_or_token() {
262 Some(rowan::NodeOrToken::Token(token)) if token.kind() == SyntaxKind::RCurly => {
263 Some(token.into())
264 }
265 _ => None,
266 }
267 }
268 pub fn l_paren_token(&self) -> Option<SyntaxToken> {
269 match self.syntax.first_child_or_token() {
270 Some(rowan::NodeOrToken::Token(token)) if token.kind() == SyntaxKind::LCurly => {
271 Some(token.into())
272 }
273 _ => None,
274 }
275 }
276}
277
278#[derive(Debug)]
279pub struct TriplesBlock {
280 syntax: SyntaxNode,
281}
282
283impl TriplesBlock {
284 pub fn triples(&self) -> Vec<Triple> {
286 self.syntax
287 .children()
288 .filter_map(|child| match child.kind() {
289 SyntaxKind::TriplesSameSubjectPath => Some(vec![Triple::cast(child).unwrap()]),
290 SyntaxKind::TriplesBlock => Some(TriplesBlock::cast(child).unwrap().triples()),
291 _ => None,
292 })
293 .flatten()
294 .collect()
295 }
296
297 pub fn group_graph_pattern(&self) -> Option<GroupGraphPattern> {
298 GroupGraphPattern::cast(nth_ancestor(self.syntax.clone(), 2)?)
299 }
300}
301
302#[derive(Debug, PartialEq)]
303pub struct Subject {
304 syntax: SyntaxNode,
305}
306
307#[derive(Debug, PartialEq)]
308pub struct Triple {
309 syntax: SyntaxNode,
310}
311
312impl Triple {
313 pub fn subject(&self) -> Option<Subject> {
314 self.syntax.first_child().and_then(Subject::cast)
315 }
316
317 pub fn properties_list_path(&self) -> Option<PropertyListPath> {
318 PropertyListPath::cast(self.syntax.children().nth(1)?)
319 }
320
321 pub fn triples_block(&self) -> Option<TriplesBlock> {
324 let mut parent = self.syntax.parent()?;
325 if parent.kind() != SyntaxKind::TriplesBlock {
326 return None;
327 }
328 while let Some(node) = parent.parent() {
329 if node.kind() == SyntaxKind::TriplesBlock {
330 parent = node;
331 } else {
332 break;
333 }
334 }
335 Some(TriplesBlock::cast(parent).expect("parent should be a TriplesBlock"))
336 }
337
338 fn variables(&self) -> Vec<Var> {
339 self.syntax
340 .preorder()
341 .filter_map(|walk_event| match walk_event {
342 rowan::WalkEvent::Enter(node) => Var::cast(node),
343 rowan::WalkEvent::Leave(_) => None,
344 })
345 .collect()
346 }
347}
348
349#[derive(Debug)]
350pub struct PropertyPath {
351 pub verb: Path,
352 pub object: ObjectList,
353}
354
355impl PropertyPath {
356 pub fn text(&self) -> String {
357 format!("{} {}", self.verb.text(), self.object.text())
358 }
359}
360
361#[derive(Debug)]
362pub struct Path {
363 syntax: SyntaxNode,
364}
365
366impl Path {
367 pub fn sub_paths(&self) -> SubPaths {
368 SubPaths {
369 children: self.syntax.children(),
370 }
371 }
372}
373
374pub struct SubPaths {
375 children: SyntaxNodeChildren<Sparql>,
376}
377
378impl Iterator for SubPaths {
379 type Item = Path;
380
381 fn next(&mut self) -> Option<Self::Item> {
382 while let Some(next_child) = self.children.next() {
383 if let Some(path) = Path::cast(next_child.into()) {
384 return Some(path);
385 }
386 }
387 None
388 }
389}
390
391#[derive(Debug)]
392pub struct ObjectList {
393 syntax: SyntaxNode,
394}
395
396#[derive(Debug, PartialEq)]
397pub struct BlankPropertyList {
398 syntax: SyntaxNode,
399}
400
401impl BlankPropertyList {
402 pub fn triple(&self) -> Option<Triple> {
403 match self.syntax.kind() {
404 SyntaxKind::BlankNodePropertyListPath => {
405 todo!()
406 }
407 SyntaxKind::BlankNodePropertyList => {
408 todo!()
409 }
410 SyntaxKind::BlankNode => self.syntax.ancestors().nth(7).and_then(Triple::cast),
411 _ => None,
412 }
413 }
414
415 pub fn is_object(&self) -> bool {
416 todo!()
417 }
418
419 pub fn is_subject(&self) -> bool {
420 todo!()
421 }
422
423 pub fn property_list(&self) -> Option<PropertyListPath> {
424 match self.syntax.kind() {
425 SyntaxKind::BlankNodePropertyListPath | SyntaxKind::BlankNodePropertyList => {
426 PropertyListPath::cast(self.syntax.first_child()?)
427 }
428
429 _ => None,
430 }
431 }
432
433 pub fn used_prefixes(&self) -> Vec<String> {
434 self.syntax
435 .descendants()
436 .filter_map(PrefixedName::cast)
437 .map(|prefixed_name| prefixed_name.prefix())
438 .collect()
439 }
440}
441
442#[derive(Debug)]
443pub struct PropertyListPath {
444 syntax: SyntaxNode,
445}
446
447impl PropertyListPath {
448 pub fn properties(&self) -> Vec<PropertyPath> {
449 self.syntax
450 .children()
451 .step_by(2)
452 .filter_map(|child| {
453 match (
454 Path::cast(child.clone()),
455 child.next_sibling().and_then(ObjectList::cast),
456 ) {
457 (None, None) | (None, Some(_)) | (Some(_), None) => None,
458 (Some(path), Some(object_list)) => Some(PropertyPath {
459 verb: path,
460 object: object_list,
461 }),
462 }
463 })
464 .collect()
465 }
466 pub fn variables(&self) -> Vec<Var> {
467 self.syntax
468 .children()
469 .filter_map(|child| match child.kind() {
470 SyntaxKind::VerbSimple => child.first_child().and_then(Var::cast),
471 _ => None,
472 })
473 .collect()
474 }
475}
476
477#[derive(Debug)]
478pub struct Iri {
479 syntax: SyntaxNode,
480}
481
482impl Iri {
483 pub fn prefixed_name(&self) -> Option<PrefixedName> {
484 self.syntax.first_child().and_then(PrefixedName::cast)
485 }
486
487 pub fn raw_iri(&self) -> Option<String> {
490 (self.syntax.first_child_or_token()?.kind() == SyntaxKind::IRIREF
491 && self.syntax.text_range().len() >= 2.into())
492 .then(|| self.text()[1..usize::from(self.syntax.text_range().len()) - 1].to_string())
493 }
494
495 pub fn is_uncompressed(&self) -> bool {
496 self.syntax
497 .first_child()
498 .map_or(false, |child| child.kind() == SyntaxKind::IRIREF)
499 }
500}
501
502#[derive(Debug)]
503pub struct PrefixedName {
504 syntax: SyntaxNode,
505}
506
507impl PrefixedName {
508 pub fn prefix(&self) -> String {
509 self.syntax
510 .to_string()
511 .split_once(":")
512 .expect("Every PrefixedName should contain a ':'")
513 .0
514 .to_string()
515 }
516
517 pub fn name(&self) -> String {
518 self.syntax
519 .to_string()
520 .split_once(":")
521 .expect("Every PrefixedName should contain a ':'")
522 .1
523 .to_string()
524 }
525}
526
527#[derive(Debug)]
528pub struct VarOrTerm {
529 syntax: SyntaxNode,
530}
531
532impl VarOrTerm {
533 pub fn var(&self) -> Option<Var> {
534 Var::cast(self.syntax.first_child()?)
535 }
536
537 pub fn is_var(&self) -> bool {
538 self.syntax
539 .first_child()
540 .map_or(false, |child| child.kind() == SyntaxKind::Var)
541 }
542
543 pub fn is_term(&self) -> bool {
544 !self.is_var()
545 }
546}
547
548#[derive(Debug)]
549pub struct Var {
550 syntax: SyntaxNode,
551}
552
553impl Var {
554 pub fn triple(&self) -> Option<Triple> {
555 self.syntax.ancestors().find_map(Triple::cast)
556 }
557 pub fn var_name(&self) -> String {
558 self.syntax.text().to_string()[1..].to_string()
559 }
560}
561
562impl AstNode for Var {
563 #[inline]
564 fn kind() -> SyntaxKind {
565 SyntaxKind::Var
566 }
567
568 fn cast(syntax: SyntaxNode) -> Option<Self> {
569 if Self::can_cast(syntax.kind()) {
570 Some(Self { syntax })
571 } else {
572 None
573 }
574 }
575 #[inline]
576 fn syntax(&self) -> &SyntaxNode {
577 &self.syntax
578 }
579}
580
581impl AstNode for VarOrTerm {
582 #[inline]
583 fn kind() -> SyntaxKind {
584 SyntaxKind::VarOrTerm
585 }
586
587 fn cast(syntax: SyntaxNode) -> Option<Self> {
588 if Self::can_cast(syntax.kind()) {
589 Some(Self { syntax })
590 } else {
591 None
592 }
593 }
594 #[inline]
595 fn syntax(&self) -> &SyntaxNode {
596 &self.syntax
597 }
598}
599
600impl AstNode for Iri {
601 #[inline]
602 fn kind() -> SyntaxKind {
603 SyntaxKind::iri
604 }
605
606 fn cast(syntax: SyntaxNode) -> Option<Self> {
607 if Self::can_cast(syntax.kind()) {
608 Some(Self { syntax })
609 } else {
610 None
611 }
612 }
613 #[inline]
614 fn syntax(&self) -> &SyntaxNode {
615 &self.syntax
616 }
617}
618
619impl AstNode for PrefixedName {
620 #[inline]
621 fn kind() -> SyntaxKind {
622 SyntaxKind::PrefixedName
623 }
624
625 fn cast(syntax: SyntaxNode) -> Option<Self> {
626 if Self::can_cast(syntax.kind()) {
627 Some(Self { syntax })
628 } else {
629 None
630 }
631 }
632 #[inline]
633 fn syntax(&self) -> &SyntaxNode {
634 &self.syntax
635 }
636}
637
638impl AstNode for Path {
639 fn kind() -> SyntaxKind {
640 SyntaxKind::VerbPath
641 }
642
643 fn can_cast(kind: SyntaxKind) -> bool {
644 matches!(
645 kind,
646 SyntaxKind::Path
647 | SyntaxKind::VerbPath
648 | SyntaxKind::PathAlternative
649 | SyntaxKind::PathSequence
650 | SyntaxKind::PathElt
651 | SyntaxKind::PathEltOrInverse
652 | SyntaxKind::PathPrimary
653 | SyntaxKind::PathNegatedPropertySet
654 | SyntaxKind::PathOneInPropertySet
655 )
656 }
657
658 fn cast(syntax: SyntaxNode) -> Option<Self> {
659 if Self::can_cast(syntax.kind()) {
660 Some(Self { syntax })
661 } else {
662 None
663 }
664 }
665
666 fn syntax(&self) -> &SyntaxNode {
667 &self.syntax
668 }
669}
670
671impl AstNode for ObjectList {
672 fn kind() -> SyntaxKind {
673 SyntaxKind::ObjectListPath
674 }
675 fn can_cast(kind: SyntaxKind) -> bool {
676 matches!(kind, SyntaxKind::ObjectListPath | SyntaxKind::ObjectList)
677 }
678
679 fn cast(syntax: SyntaxNode) -> Option<Self> {
680 if Self::can_cast(syntax.kind()) {
681 Some(Self { syntax })
682 } else {
683 None
684 }
685 }
686
687 fn syntax(&self) -> &SyntaxNode {
688 &self.syntax
689 }
690}
691
692impl AstNode for BlankPropertyList {
693 #[inline]
694 fn kind() -> SyntaxKind {
695 SyntaxKind::BlankNodePropertyListPath
696 }
697
698 fn can_cast(kind: SyntaxKind) -> bool {
699 matches!(
700 kind,
701 SyntaxKind::BlankNodePropertyListPath
702 | SyntaxKind::BlankNodePropertyList
703 | SyntaxKind::BlankNode
704 )
705 }
706
707 #[inline]
708 fn cast(syntax: SyntaxNode) -> Option<Self> {
709 if Self::can_cast(syntax.kind()) {
710 Some(Self { syntax })
711 } else {
712 None
713 }
714 }
715
716 #[inline]
717 fn syntax(&self) -> &SyntaxNode {
718 &self.syntax
719 }
720}
721
722impl AstNode for PropertyListPath {
723 #[inline]
724 fn kind() -> SyntaxKind {
725 SyntaxKind::PropertyListPathNotEmpty
726 }
727
728 fn can_cast(kind: SyntaxKind) -> bool {
729 matches!(
730 kind,
731 SyntaxKind::PropertyListPath | SyntaxKind::PropertyListPathNotEmpty
732 )
733 }
734
735 #[inline]
736 fn cast(syntax: SyntaxNode) -> Option<Self> {
737 if Self::can_cast(syntax.kind()) {
738 Some(Self { syntax })
739 } else {
740 None
741 }
742 }
743
744 #[inline]
745 fn syntax(&self) -> &SyntaxNode {
746 &self.syntax
747 }
748}
749
750impl AstNode for Subject {
751 #[inline]
752 fn kind() -> SyntaxKind {
753 SyntaxKind::VarOrTerm
754 }
755
756 fn can_cast(kind: SyntaxKind) -> bool {
757 matches!(kind, SyntaxKind::VarOrTerm | SyntaxKind::TriplesNodePath)
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
773impl AstNode for Triple {
774 #[inline]
775 fn kind() -> SyntaxKind {
776 SyntaxKind::TriplesSameSubjectPath
777 }
778
779 fn cast(syntax: SyntaxNode) -> Option<Self> {
780 if Self::can_cast(syntax.kind()) {
781 Some(Self { syntax })
782 } else {
783 None
784 }
785 }
786 #[inline]
787 fn syntax(&self) -> &SyntaxNode {
788 &self.syntax
789 }
790}
791
792impl AstNode for TriplesBlock {
793 #[inline]
794 fn kind() -> SyntaxKind {
795 SyntaxKind::TriplesBlock
796 }
797
798 fn cast(syntax: SyntaxNode) -> Option<Self> {
799 if Self::can_cast(syntax.kind()) {
800 Some(Self { syntax })
801 } else {
802 None
803 }
804 }
805
806 #[inline]
807 fn syntax(&self) -> &SyntaxNode {
808 &self.syntax
809 }
810}
811
812impl AstNode for GroupGraphPattern {
813 #[inline]
814 fn kind() -> SyntaxKind {
815 SyntaxKind::GroupGraphPattern
816 }
817
818 fn cast(syntax: SyntaxNode) -> Option<Self> {
819 if Self::can_cast(syntax.kind()) {
820 Some(Self { syntax })
821 } else {
822 None
823 }
824 }
825
826 #[inline]
827 fn syntax(&self) -> &SyntaxNode {
828 &self.syntax
829 }
830}
831
832impl AstNode for WhereClause {
833 #[inline]
834 fn kind() -> SyntaxKind {
835 SyntaxKind::WhereClause
836 }
837
838 fn cast(syntax: SyntaxNode) -> Option<Self> {
839 if Self::can_cast(syntax.kind()) {
840 Some(Self { syntax })
841 } else {
842 None
843 }
844 }
845
846 #[inline]
847 fn syntax(&self) -> &SyntaxNode {
848 &self.syntax
849 }
850}
851impl AstNode for OptionalGraphPattern {
852 #[inline]
853 fn kind() -> SyntaxKind {
854 SyntaxKind::OptionalGraphPattern
855 }
856
857 fn cast(syntax: SyntaxNode) -> Option<Self> {
858 if Self::can_cast(syntax.kind()) {
859 Some(Self { syntax })
860 } else {
861 None
862 }
863 }
864
865 #[inline]
866 fn syntax(&self) -> &SyntaxNode {
867 &self.syntax
868 }
869}
870
871impl AstNode for GroupOrUnionGraphPattern {
872 #[inline]
873 fn kind() -> SyntaxKind {
874 SyntaxKind::GroupOrUnionGraphPattern
875 }
876
877 fn cast(syntax: SyntaxNode) -> Option<Self> {
878 if Self::can_cast(syntax.kind()) {
879 Some(Self { syntax })
880 } else {
881 None
882 }
883 }
884
885 #[inline]
886 fn syntax(&self) -> &SyntaxNode {
887 &self.syntax
888 }
889}
890
891impl AstNode for MinusGraphPattern {
892 #[inline]
893 fn kind() -> SyntaxKind {
894 SyntaxKind::MinusGraphPattern
895 }
896
897 fn cast(syntax: SyntaxNode) -> Option<Self> {
898 if Self::can_cast(syntax.kind()) {
899 Some(Self { syntax })
900 } else {
901 None
902 }
903 }
904
905 #[inline]
906 fn syntax(&self) -> &SyntaxNode {
907 &self.syntax
908 }
909}
910
911impl AstNode for GraphGraphPattern {
912 #[inline]
913 fn kind() -> SyntaxKind {
914 SyntaxKind::GraphGraphPattern
915 }
916
917 fn cast(syntax: SyntaxNode) -> Option<Self> {
918 if Self::can_cast(syntax.kind()) {
919 Some(Self { syntax })
920 } else {
921 None
922 }
923 }
924
925 #[inline]
926 fn syntax(&self) -> &SyntaxNode {
927 &self.syntax
928 }
929}
930
931impl AstNode for ServiceGraphPattern {
932 #[inline]
933 fn kind() -> SyntaxKind {
934 SyntaxKind::ServiceGraphPattern
935 }
936
937 fn cast(syntax: SyntaxNode) -> Option<Self> {
938 if Self::can_cast(syntax.kind()) {
939 Some(Self { syntax })
940 } else {
941 None
942 }
943 }
944
945 #[inline]
946 fn syntax(&self) -> &SyntaxNode {
947 &self.syntax
948 }
949}
950
951impl AstNode for Filter {
952 #[inline]
953 fn kind() -> SyntaxKind {
954 SyntaxKind::Filter
955 }
956
957 fn cast(syntax: SyntaxNode) -> Option<Self> {
958 if Self::can_cast(syntax.kind()) {
959 Some(Self { syntax })
960 } else {
961 None
962 }
963 }
964
965 #[inline]
966 fn syntax(&self) -> &SyntaxNode {
967 &self.syntax
968 }
969}
970
971impl AstNode for Bind {
972 #[inline]
973 fn kind() -> SyntaxKind {
974 SyntaxKind::Bind
975 }
976
977 fn cast(syntax: SyntaxNode) -> Option<Self> {
978 if Self::can_cast(syntax.kind()) {
979 Some(Self { syntax })
980 } else {
981 None
982 }
983 }
984
985 #[inline]
986 fn syntax(&self) -> &SyntaxNode {
987 &self.syntax
988 }
989}
990
991impl AstNode for InlineData {
992 #[inline]
993 fn kind() -> SyntaxKind {
994 SyntaxKind::InlineData
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
1011impl AstNode for SelectClause {
1012 #[inline]
1013 fn kind() -> SyntaxKind {
1014 SyntaxKind::SelectClause
1015 }
1016
1017 fn cast(syntax: SyntaxNode) -> Option<Self> {
1018 if Self::can_cast(syntax.kind()) {
1019 Some(Self { syntax })
1020 } else {
1021 None
1022 }
1023 }
1024
1025 #[inline]
1026 fn syntax(&self) -> &SyntaxNode {
1027 &self.syntax
1028 }
1029}
1030
1031impl AstNode for Prologue {
1032 #[inline]
1033 fn kind() -> SyntaxKind {
1034 SyntaxKind::Prologue
1035 }
1036
1037 fn cast(syntax: SyntaxNode) -> Option<Self> {
1038 if Self::can_cast(syntax.kind()) {
1039 Some(Self { syntax })
1040 } else {
1041 None
1042 }
1043 }
1044
1045 #[inline]
1046 fn syntax(&self) -> &SyntaxNode {
1047 &self.syntax
1048 }
1049}
1050
1051impl AstNode for SolutionModifier {
1052 #[inline]
1053 fn kind() -> SyntaxKind {
1054 SyntaxKind::SolutionModifier
1055 }
1056
1057 fn cast(syntax: SyntaxNode) -> Option<Self> {
1058 if Self::can_cast(syntax.kind()) {
1059 Some(Self { syntax })
1060 } else {
1061 None
1062 }
1063 }
1064
1065 #[inline]
1066 fn syntax(&self) -> &SyntaxNode {
1067 &self.syntax
1068 }
1069}
1070
1071impl AstNode for PrefixDeclaration {
1072 #[inline]
1073 fn kind() -> SyntaxKind {
1074 SyntaxKind::PrefixDecl
1075 }
1076
1077 fn cast(syntax: SyntaxNode) -> Option<Self> {
1078 if Self::can_cast(syntax.kind()) {
1079 Some(Self { syntax })
1080 } else {
1081 None
1082 }
1083 }
1084
1085 #[inline]
1086 fn syntax(&self) -> &SyntaxNode {
1087 &self.syntax
1088 }
1089}
1090
1091impl AstNode for QueryUnit {
1092 #[inline]
1093 fn kind() -> SyntaxKind {
1094 SyntaxKind::QueryUnit
1095 }
1096
1097 fn cast(syntax: SyntaxNode) -> Option<Self> {
1098 if Self::can_cast(syntax.kind()) {
1099 Some(Self { syntax })
1100 } else {
1101 None
1102 }
1103 }
1104
1105 #[inline]
1106 fn syntax(&self) -> &SyntaxNode {
1107 &self.syntax
1108 }
1109}
1110
1111impl AstNode for SelectQuery {
1112 #[inline]
1113 fn kind() -> SyntaxKind {
1114 SyntaxKind::SelectQuery
1115 }
1116
1117 fn can_cast(kind: SyntaxKind) -> bool {
1118 matches!(kind, SyntaxKind::SelectQuery | SyntaxKind::SubSelect)
1119 }
1120
1121 fn cast(syntax: SyntaxNode) -> Option<Self> {
1122 if Self::can_cast(syntax.kind()) {
1123 Some(Self { syntax })
1124 } else {
1125 None
1126 }
1127 }
1128
1129 #[inline]
1130 fn syntax(&self) -> &SyntaxNode {
1131 &self.syntax
1132 }
1133}
1134
1135impl AstNode for GroupPatternNotTriples {
1136 #[inline]
1137 fn kind() -> SyntaxKind {
1138 SyntaxKind::GraphPatternNotTriples
1139 }
1140
1141 fn can_cast(kind: SyntaxKind) -> bool {
1142 matches!(
1143 kind,
1144 SyntaxKind::GroupOrUnionGraphPattern
1145 | SyntaxKind::OptionalGraphPattern
1146 | SyntaxKind::MinusGraphPattern
1147 | SyntaxKind::GraphGraphPattern
1148 | SyntaxKind::ServiceGraphPattern
1149 | SyntaxKind::Filter
1150 | SyntaxKind::Bind
1151 | SyntaxKind::InlineData
1152 )
1153 }
1154
1155 fn cast(syntax: SyntaxNode) -> Option<Self> {
1156 let child = syntax.first_child()?;
1157 match child.kind() {
1158 SyntaxKind::GroupOrUnionGraphPattern => {
1159 Some(GroupPatternNotTriples::GroupOrUnionGraphPattern(
1160 GroupOrUnionGraphPattern::cast(child)?,
1161 ))
1162 }
1163 SyntaxKind::OptionalGraphPattern => Some(GroupPatternNotTriples::OptionalGraphPattern(
1164 OptionalGraphPattern::cast(child)?,
1165 )),
1166 SyntaxKind::MinusGraphPattern => Some(GroupPatternNotTriples::MinusGraphPattern(
1167 MinusGraphPattern::cast(child)?,
1168 )),
1169 SyntaxKind::GraphGraphPattern => Some(GroupPatternNotTriples::GraphGraphPattern(
1170 GraphGraphPattern::cast(child)?,
1171 )),
1172 SyntaxKind::ServiceGraphPattern => Some(GroupPatternNotTriples::ServiceGraphPattern(
1173 ServiceGraphPattern::cast(child)?,
1174 )),
1175 SyntaxKind::Filter => Some(GroupPatternNotTriples::Filter(Filter::cast(child)?)),
1176 SyntaxKind::Bind => Some(GroupPatternNotTriples::Bind(Bind::cast(child)?)),
1177 SyntaxKind::InlineData => Some(GroupPatternNotTriples::InlineData(InlineData::cast(
1178 syntax,
1179 )?)),
1180 _ => None,
1181 }
1182 }
1183
1184 #[inline]
1185 fn syntax(&self) -> &SyntaxNode {
1186 match self {
1187 GroupPatternNotTriples::GroupOrUnionGraphPattern(x) => x.syntax(),
1188 GroupPatternNotTriples::OptionalGraphPattern(x) => x.syntax(),
1189 GroupPatternNotTriples::MinusGraphPattern(x) => x.syntax(),
1190 GroupPatternNotTriples::GraphGraphPattern(x) => x.syntax(),
1191 GroupPatternNotTriples::ServiceGraphPattern(x) => x.syntax(),
1192 GroupPatternNotTriples::Filter(x) => x.syntax(),
1193 GroupPatternNotTriples::Bind(x) => x.syntax(),
1194 GroupPatternNotTriples::InlineData(x) => x.syntax(),
1195 }
1196 }
1197}
1198
1199pub trait AstNode {
1200 fn kind() -> SyntaxKind;
1201
1202 #[inline]
1203 fn can_cast(kind: SyntaxKind) -> bool {
1204 Self::kind() == kind
1205 }
1206
1207 fn cast(syntax: SyntaxNode) -> Option<Self>
1208 where
1209 Self: Sized;
1210
1211 fn syntax(&self) -> &SyntaxNode;
1212
1213 fn has_error(&self) -> bool {
1214 self.syntax()
1215 .preorder()
1216 .find(|walk_event| match walk_event {
1217 rowan::WalkEvent::Enter(node) if node.kind() == SyntaxKind::Error => true,
1218 _ => false,
1219 })
1220 .is_some()
1221 }
1222
1223 fn collect_decendants(&self, matcher: &impl Fn(SyntaxKind) -> bool) -> Vec<SyntaxNode> {
1224 self.syntax()
1225 .preorder()
1226 .filter_map(|walk_event| match walk_event {
1227 rowan::WalkEvent::Enter(node) if matcher(node.kind()) => Some(node),
1228 _ => None,
1229 })
1230 .collect()
1231 }
1232
1233 fn preorder_find_kind(&self, kind: SyntaxKind) -> Vec<SyntaxNode> {
1234 self.syntax()
1235 .preorder()
1236 .filter_map(|walk_event| match walk_event {
1237 rowan::WalkEvent::Enter(node) if node.kind() == kind => Some(node),
1238 _ => None,
1239 })
1240 .collect()
1241 }
1242
1243 fn used_prefixes(&self) -> Vec<String> {
1244 self.syntax()
1245 .descendants()
1246 .filter_map(PrefixedName::cast)
1247 .map(|prefixed_name| prefixed_name.prefix())
1248 .collect()
1249 }
1250
1251 fn text(&self) -> String {
1252 self.syntax().text().to_string()
1253 }
1254}
1255
1256#[cfg(test)]
1257mod tests;