Build tree-sitter language Args: language (str): java, python, cpp, c_sharp, etc save_path (str): save path (default create a `/tree-sitter/` dir)
(language: str, save_path: str=None)
| 19 | |
| 20 | |
| 21 | def build_language(language: str, save_path: str=None): |
| 22 | """ |
| 23 | Build tree-sitter language |
| 24 | |
| 25 | Args: |
| 26 | language (str): java, python, cpp, c_sharp, etc |
| 27 | save_path (str): save path (default create a `/tree-sitter/` dir) |
| 28 | """ |
| 29 | language = str(language).lower() |
| 30 | if language == 'c#': |
| 31 | language = 'c_sharp' |
| 32 | elif language == 'c++': |
| 33 | language = 'cpp' |
| 34 | |
| 35 | assert language.lower() in SUPPORTED_LANGUAGE, f"Expect {language} in {SUPPORTED_LANGUAGE}" |
| 36 | if not save_path: |
| 37 | calling_script_path = Path(inspect.getframeinfo(sys._getframe(1)).filename) |
| 38 | save_path = calling_script_path.parent |
| 39 | |
| 40 | # create `tree-sitter` dir |
| 41 | ts_path = os.path.join(save_path, 'tree-sitter') |
| 42 | if not os.path.exists(ts_path): |
| 43 | logger.warning( |
| 44 | f"Not found `tree-sitter` folder, create new one in {ts_path}" |
| 45 | ) |
| 46 | os.mkdir(ts_path) |
| 47 | |
| 48 | # check `tree-sitter/tree-sitter-<language>` |
| 49 | ts_lang_path = os.path.join(ts_path, 'tree-sitter-'+language.replace('_', '-')) |
| 50 | if not os.path.exists(ts_lang_path): |
| 51 | logger.warning( |
| 52 | f"Not found `tree-sitter-{language.replace('_', '-')}`, attempt clone from github to {ts_path}" |
| 53 | ) |
| 54 | command = f"cd {ts_path}; git clone https://github.com/tree-sitter/tree-sitter-{language.replace('_', '-')}.git" |
| 55 | subprocess.Popen(command ,shell=True).wait() |
| 56 | |
| 57 | assert os.path.exists(ts_lang_path)==True, f"Unable to find {language} tree-sitter in {ts_path}" |
| 58 | |
| 59 | # if language == 'c-sharp': language = 'c_sharp' |
| 60 | lang_path = os.path.join(save_path, 'tree-sitter', f'{language}.so') |
| 61 | if not os.path.exists(lang_path): |
| 62 | logger.info( |
| 63 | f"Attempt to build Tree-sitter Language for {language} and store in {lang_path}" |
| 64 | ) |
| 65 | Language.build_library(lang_path, [ts_lang_path]) |
| 66 | assert os.path.exists(lang_path)==True |
| 67 | else: |
| 68 | logger.info(f"Language already existed!") |
| 69 | |
| 70 | |
| 71 | def parse_code(raw_code: str, language: str='Auto', tree_sitter_path: str=None) -> tree_sitter.Tree: |
no outgoing calls