-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support dynamic language in pyo3
fix #1143
- Loading branch information
1 parent
9240e7b
commit 09aedaf
Showing
5 changed files
with
126 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,114 @@ | ||
use ast_grep_core::language::TSLanguage; | ||
use ast_grep_dynamic::{DynamicLang, Registration}; | ||
use ast_grep_language::{Language, SupportLang}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use std::borrow::Cow; | ||
use std::collections::HashMap; | ||
use std::fmt::{Debug, Display, Formatter}; | ||
use std::path::{Path, PathBuf}; | ||
use std::str::FromStr; | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
pub struct CustomLang { | ||
library_path: PathBuf, | ||
/// the dylib symbol to load ts-language, default is `tree_sitter_{name}` | ||
language_symbol: Option<String>, | ||
meta_var_char: Option<char>, | ||
expando_char: Option<char>, | ||
extensions: Vec<String>, | ||
} | ||
|
||
impl CustomLang { | ||
pub fn register(base: PathBuf, langs: HashMap<String, CustomLang>) { | ||
let registrations = langs | ||
.into_iter() | ||
.map(|(name, custom)| to_registration(name, custom, &base)) | ||
.collect(); | ||
// TODO, add error handling | ||
unsafe { DynamicLang::register(registrations).expect("TODO") } | ||
} | ||
} | ||
|
||
fn to_registration(name: String, custom_lang: CustomLang, base: &Path) -> Registration { | ||
let path = base.join(custom_lang.library_path); | ||
let sym = custom_lang | ||
.language_symbol | ||
.unwrap_or_else(|| format!("tree_sitter_{name}")); | ||
Registration { | ||
lang_name: name, | ||
lib_path: path, | ||
symbol: sym, | ||
meta_var_char: custom_lang.meta_var_char, | ||
expando_char: custom_lang.expando_char, | ||
extensions: custom_lang.extensions, | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, PartialEq, Eq, Hash)] | ||
pub enum PyLang { | ||
// inlined support lang expando char | ||
Builtin(SupportLang), | ||
Custom(DynamicLang), | ||
} | ||
#[derive(Debug)] | ||
pub enum PyLangErr { | ||
LanguageNotSupported(String), | ||
} | ||
|
||
impl Display for PyLangErr { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { | ||
use PyLangErr::*; | ||
match self { | ||
LanguageNotSupported(lang) => write!(f, "{} is not supported!", lang), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for PyLangErr {} | ||
|
||
impl FromStr for PyLang { | ||
type Err = PyLangErr; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
if let Ok(b) = SupportLang::from_str(s) { | ||
Ok(PyLang::Builtin(b)) | ||
} else if let Ok(c) = DynamicLang::from_str(s) { | ||
Ok(PyLang::Custom(c)) | ||
} else { | ||
Err(PyLangErr::LanguageNotSupported(s.into())) | ||
} | ||
} | ||
} | ||
|
||
use PyLang::*; | ||
impl Language for PyLang { | ||
fn get_ts_language(&self) -> TSLanguage { | ||
match self { | ||
Builtin(b) => b.get_ts_language(), | ||
Custom(c) => c.get_ts_language(), | ||
} | ||
} | ||
|
||
fn pre_process_pattern<'q>(&self, query: &'q str) -> Cow<'q, str> { | ||
match self { | ||
Builtin(b) => b.pre_process_pattern(query), | ||
Custom(c) => c.pre_process_pattern(query), | ||
} | ||
} | ||
|
||
#[inline] | ||
fn meta_var_char(&self) -> char { | ||
match self { | ||
Builtin(b) => b.meta_var_char(), | ||
Custom(c) => c.meta_var_char(), | ||
} | ||
} | ||
|
||
#[inline] | ||
fn expando_char(&self) -> char { | ||
match self { | ||
Builtin(b) => b.expando_char(), | ||
Custom(c) => c.expando_char(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters