Skip to main content

weavatrix_rust/language/tokenized/
mod.rs

1//! Lossless language adapters backed by `weavatrix-parse`.
2
3mod convert;
4mod domains;
5mod kinds;
6#[cfg(test)]
7mod tests;
8
9use super::{FileFacts, Language, LanguageAdapter, SourceFile};
10use crate::model::Result;
11
12/// One language served by the lossless parser.
13pub struct TokenizedAdapter {
14    language: Language,
15    parse: weavatrix_parse::Language,
16    extensions: &'static [&'static str],
17}
18
19impl TokenizedAdapter {
20    /// Every language the lossless parser extracts structure from.
21    pub fn defaults() -> impl Iterator<Item = Self> {
22        use weavatrix_parse::Language as Parsed;
23        [
24            (
25                Language::JavaScript,
26                Parsed::JavaScript,
27                &["js", "jsx", "mjs", "cjs"][..],
28            ),
29            (
30                Language::TypeScript,
31                Parsed::TypeScript,
32                &["ts", "tsx", "mts", "cts"][..],
33            ),
34            // The syn adapter wins with `lang-rust`; this remains the
35            // dependency-light fallback for no-default-feature builds.
36            (Language::Rust, Parsed::Rust, &["rs"][..]),
37            (Language::Python, Parsed::Python, &["py", "pyi"][..]),
38            (Language::Go, Parsed::Go, &["go"][..]),
39            (Language::Java, Parsed::Java, &["java"][..]),
40            (Language::CSharp, Parsed::CSharp, &["cs"][..]),
41            (Language::C, Parsed::C, &["c", "h"][..]),
42            (
43                Language::Cpp,
44                Parsed::Cpp,
45                &["cc", "cpp", "cxx", "hh", "hpp", "hxx"][..],
46            ),
47            (Language::Sql, Parsed::Sql, &["sql", "psql"][..]),
48            (Language::Bash, Parsed::Bash, &["sh", "bash", "zsh"][..]),
49            (
50                Language::Custom("swift".to_owned()),
51                Parsed::Swift,
52                &["swift"][..],
53            ),
54            (
55                Language::Custom("solidity".to_owned()),
56                Parsed::Solidity,
57                &["sol"][..],
58            ),
59            (
60                Language::Custom("html".to_owned()),
61                Parsed::Html,
62                &["html", "htm", "xhtml", "vue", "svelte"][..],
63            ),
64            (
65                Language::Custom("css".to_owned()),
66                Parsed::Css,
67                &["css"][..],
68            ),
69            (
70                Language::Custom("css".to_owned()),
71                Parsed::Scss,
72                &["scss", "sass", "less"][..],
73            ),
74            (
75                Language::Custom("terraform".to_owned()),
76                Parsed::Terraform,
77                &["tf", "tfvars", "hcl"][..],
78            ),
79            (
80                Language::Custom("xml".to_owned()),
81                Parsed::Xml,
82                &[
83                    "xml", "xsd", "xsl", "xslt", "csproj", "props", "targets", "plist",
84                ][..],
85            ),
86            (
87                Language::Custom("markdown".to_owned()),
88                Parsed::Markdown,
89                &["md", "markdown", "mdown", "mkd", "mkdn"][..],
90            ),
91            (
92                Language::Custom("markdown".to_owned()),
93                Parsed::Mdx,
94                &["mdx"][..],
95            ),
96            (
97                Language::Custom("rst".to_owned()),
98                Parsed::ReStructuredText,
99                &["rst"][..],
100            ),
101            (
102                Language::Custom("asciidoc".to_owned()),
103                Parsed::AsciiDoc,
104                &["adoc", "asciidoc", "asc"][..],
105            ),
106        ]
107        .into_iter()
108        .map(|(language, parse, extensions)| Self {
109            language,
110            parse,
111            extensions,
112        })
113    }
114}
115
116impl LanguageAdapter for TokenizedAdapter {
117    fn language(&self) -> Language {
118        self.language.clone()
119    }
120
121    fn extensions(&self) -> &'static [&'static str] {
122        self.extensions
123    }
124
125    fn extractor(&self) -> &'static str {
126        "weavatrix.parse.tokens"
127    }
128
129    fn parse(&self, source: SourceFile<'_>) -> Result<FileFacts> {
130        Ok(convert::convert(
131            &weavatrix_parse::extract(source.text, self.parse),
132            source.path,
133        ))
134    }
135}