|
| 1 | +use tree_sitter::{LanguageError, Parser}; |
| 2 | + |
| 3 | +pub struct Language { |
| 4 | + pub name: &'static str, |
| 5 | + pub struct_name: &'static str, |
| 6 | + pub node_types: &'static str, |
| 7 | + pub file_extensions: &'static [&'static str], |
| 8 | + pub tree_sitter_language: tree_sitter::Language, |
| 9 | +} |
| 10 | +impl Language { |
| 11 | + pub fn parse_tree_sitter(&self, content: &str) -> Result<tree_sitter::Tree, LanguageError> { |
| 12 | + let mut parser = Parser::new(); |
| 13 | + parser.set_language(&self.tree_sitter_language)?; |
| 14 | + let tree = parser.parse(content, None).unwrap(); |
| 15 | + Ok(tree) |
| 16 | + } |
| 17 | +} |
| 18 | +#[cfg(feature = "typescript")] |
| 19 | +pub mod javascript; |
| 20 | +#[cfg(feature = "typescript")] |
| 21 | +pub mod jsx; |
| 22 | +#[cfg(feature = "python")] |
| 23 | +pub mod python; |
| 24 | +#[cfg(feature = "typescript")] |
| 25 | +pub mod tsx; |
| 26 | +#[cfg(feature = "typescript")] |
| 27 | +pub mod typescript; |
| 28 | +lazy_static! { |
| 29 | + pub static ref LANGUAGES: Vec<&'static Language> = vec![ |
| 30 | + #[cfg(feature = "python")] |
| 31 | + &python::Python, |
| 32 | + #[cfg(feature = "typescript")] |
| 33 | + &typescript::Typescript, |
| 34 | + #[cfg(feature = "typescript")] |
| 35 | + &tsx::TSX, |
| 36 | + #[cfg(feature = "typescript")] |
| 37 | + &jsx::JSX, |
| 38 | + #[cfg(feature = "typescript")] |
| 39 | + &javascript::Javascript, |
| 40 | + ]; |
| 41 | +} |
0 commit comments