Skip to main content

normalize_languages/
x86asm.rs

1//! x86 assembly 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/// x86 Assembly language support.
9pub struct X86Asm;
10
11impl Language for X86Asm {
12    fn name(&self) -> &'static str {
13        "x86 Assembly"
14    }
15    fn extensions(&self) -> &'static [&'static str] {
16        &["asm", "s", "S"]
17    }
18    fn grammar_name(&self) -> &'static str {
19        "x86asm"
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        // x86 assembly has no standard import mechanism
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 !["asm", "s", "S"].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!("{}.asm", module), format!("{}.s", module)]
136    }
137
138    fn lang_key(&self) -> &'static str {
139        "x86asm"
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        &["asm", "s", "S"]
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(".asm")
182            .or_else(|| entry_name.strip_suffix(".s"))
183            .or_else(|| entry_name.strip_suffix(".S"))
184            .unwrap_or(entry_name)
185            .to_string()
186    }
187
188    fn find_package_entry(&self, path: &Path) -> Option<PathBuf> {
189        if path.is_file() {
190            Some(path.to_path_buf())
191        } else {
192            None
193        }
194    }
195}
196
197#[cfg(test)]
198mod tests {
199    use super::*;
200    use crate::validate_unused_kinds_audit;
201
202    #[test]
203    fn unused_node_kinds_audit() {
204        #[rustfmt::skip]
205        let documented_unused: &[&str] = &[
206            "label_definition", "instruction", "identifier",
207            "memory_expression", "binary_expression",
208        ];
209        validate_unused_kinds_audit(&X86Asm, documented_unused)
210            .expect("x86 Assembly unused node kinds audit failed");
211    }
212}