Skip to main content

normalize_languages/
sparql.rs

1//! SPARQL query language support.
2
3use crate::external_packages::ResolvedPackage;
4use crate::{Export, Import, Language, Symbol, Visibility, VisibilityMechanism};
5use std::path::{Path, PathBuf};
6use tree_sitter::Node;
7
8/// SPARQL language support.
9pub struct Sparql;
10
11impl Language for Sparql {
12    fn name(&self) -> &'static str {
13        "SPARQL"
14    }
15    fn extensions(&self) -> &'static [&'static str] {
16        &["sparql", "rq"]
17    }
18    fn grammar_name(&self) -> &'static str {
19        "sparql"
20    }
21
22    fn has_symbols(&self) -> bool {
23        true
24    }
25
26    fn container_kinds(&self) -> &'static [&'static str] {
27        &[]
28    }
29    fn function_kinds(&self) -> &'static [&'static str] {
30        &[]
31    }
32    fn type_kinds(&self) -> &'static [&'static str] {
33        &[]
34    }
35    fn import_kinds(&self) -> &'static [&'static str] {
36        &[]
37    }
38    fn public_symbol_kinds(&self) -> &'static [&'static str] {
39        &[]
40    }
41
42    fn visibility_mechanism(&self) -> VisibilityMechanism {
43        VisibilityMechanism::AllPublic
44    }
45
46    fn extract_public_symbols(&self, _node: &Node, _content: &str) -> Vec<Export> {
47        Vec::new()
48    }
49
50    fn scope_creating_kinds(&self) -> &'static [&'static str] {
51        &[]
52    }
53    fn control_flow_kinds(&self) -> &'static [&'static str] {
54        &[]
55    }
56    fn complexity_nodes(&self) -> &'static [&'static str] {
57        &[]
58    }
59    fn nesting_nodes(&self) -> &'static [&'static str] {
60        &[]
61    }
62
63    fn signature_suffix(&self) -> &'static str {
64        ""
65    }
66
67    fn extract_function(
68        &self,
69        _node: &Node,
70        _content: &str,
71        _in_container: bool,
72    ) -> Option<Symbol> {
73        None
74    }
75    fn extract_container(&self, _node: &Node, _content: &str) -> Option<Symbol> {
76        None
77    }
78    fn extract_type(&self, _node: &Node, _content: &str) -> Option<Symbol> {
79        None
80    }
81    fn extract_docstring(&self, _node: &Node, _content: &str) -> Option<String> {
82        None
83    }
84
85    fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
86        Vec::new()
87    }
88    fn extract_imports(&self, _node: &Node, _content: &str) -> Vec<Import> {
89        Vec::new()
90    }
91
92    fn format_import(&self, _import: &Import, _names: Option<&[&str]>) -> String {
93        // SPARQL has no imports
94        String::new()
95    }
96
97    fn is_public(&self, _node: &Node, _content: &str) -> bool {
98        true
99    }
100    fn get_visibility(&self, _node: &Node, _content: &str) -> Visibility {
101        Visibility::Public
102    }
103
104    fn is_test_symbol(&self, _symbol: &crate::Symbol) -> bool {
105        false
106    }
107
108    fn embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
109        None
110    }
111
112    fn container_body<'a>(&self, _node: &'a Node<'a>) -> Option<Node<'a>> {
113        None
114    }
115
116    fn body_has_docstring(&self, _body: &Node, _content: &str) -> bool {
117        false
118    }
119
120    fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
121        node.child_by_field_name("name")
122            .map(|n| &content[n.byte_range()])
123    }
124
125    fn file_path_to_module_name(&self, path: &Path) -> Option<String> {
126        let ext = path.extension()?.to_str()?;
127        if !["sparql", "rq"].contains(&ext) {
128            return None;
129        }
130        let stem = path.file_stem()?.to_str()?;
131        Some(stem.to_string())
132    }
133
134    fn module_name_to_paths(&self, module: &str) -> Vec<String> {
135        vec![format!("{}.sparql", module), format!("{}.rq", module)]
136    }
137
138    fn lang_key(&self) -> &'static str {
139        "sparql"
140    }
141
142    fn is_stdlib_import(&self, _: &str, _: &Path) -> bool {
143        false
144    }
145    fn find_stdlib(&self, _: &Path) -> Option<PathBuf> {
146        None
147    }
148    fn resolve_local_import(&self, _: &str, _: &Path, _: &Path) -> Option<PathBuf> {
149        None
150    }
151    fn resolve_external_import(&self, _: &str, _: &Path) -> Option<ResolvedPackage> {
152        None
153    }
154    fn get_version(&self, _: &Path) -> Option<String> {
155        None
156    }
157    fn find_package_cache(&self, _: &Path) -> Option<PathBuf> {
158        None
159    }
160    fn indexable_extensions(&self) -> &'static [&'static str] {
161        &["sparql", "rq"]
162    }
163    fn package_sources(&self, _: &Path) -> Vec<crate::PackageSource> {
164        Vec::new()
165    }
166
167    fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
168        use crate::traits::{has_extension, skip_dotfiles};
169        if skip_dotfiles(name) {
170            return true;
171        }
172        !is_dir && !has_extension(name, self.indexable_extensions())
173    }
174
175    fn discover_packages(&self, _: &crate::PackageSource) -> Vec<(String, PathBuf)> {
176        Vec::new()
177    }
178
179    fn package_module_name(&self, entry_name: &str) -> String {
180        entry_name
181            .strip_suffix(".sparql")
182            .or_else(|| entry_name.strip_suffix(".rq"))
183            .unwrap_or(entry_name)
184            .to_string()
185    }
186
187    fn find_package_entry(&self, path: &Path) -> Option<PathBuf> {
188        if path.is_file() {
189            Some(path.to_path_buf())
190        } else {
191            None
192        }
193    }
194}
195
196#[cfg(test)]
197mod tests {
198    use super::*;
199    use crate::validate_unused_kinds_audit;
200
201    #[test]
202    fn unused_node_kinds_audit() {
203        #[rustfmt::skip]
204        let documented_unused: &[&str] = &[
205            // Query parts
206            "select_clause", "where_clause", "construct_query", "construct_template",
207            "construct_triples", "triples_block",
208            // Modifiers
209            "solution_modifier", "order_clause", "limit_clause", "offset_clause",
210            "limit_offset_clauses", "group_clause", "having_clause", "values_clause",
211            // Dataset
212            "dataset_clause", "default_graph_clause", "named_graph_clause",
213            // Update
214            "modify", "insert_clause", "delete_clause", "using_clause", "data_block",
215            // Declarations
216            "base_declaration", "prefix_declaration",
217            // Expressions
218            "expression_list", "binary_expression", "unary_expression",
219            "bracketted_expression", "function_call", "build_in_function",
220            "regex_expression", "substring_expression", "string_replace_expression",
221        ];
222        validate_unused_kinds_audit(&Sparql, documented_unused)
223            .expect("SPARQL unused node kinds audit failed");
224    }
225}