Skip to content

Commit

Permalink
Replace manually compile tree-sitter-cpp with their Python bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rot127 committed Apr 26, 2024
1 parent 0f66004 commit b15b1d6
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 26 deletions.
6 changes: 0 additions & 6 deletions suite/auto-sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ sudo apt install python3-venv
# Setup virtual environment in Capstone root dir
python3 -m venv ./.venv
source ./.venv/bin/activate
```

Clone C++ grammar

```
cd suite/auto-sync/
git submodule update --init --recursive ./vendor/
pip install -e .
```

Expand Down
3 changes: 2 additions & 1 deletion suite/auto-sync/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ name = "autosync"
version = "0.1.0"
dependencies = [
"termcolor >= 2.3.0",
"tree_sitter < 0.22.0",
"tree_sitter >= 0.21.3",
"tree-sitter-cpp >=0.22.0",
"black >= 24.3.0",
"usort >= 1.0.8",
"setuptools >= 69.2.0",
Expand Down
21 changes: 3 additions & 18 deletions suite/auto-sync/src/autosync/cpptranslator/Configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import logging as log
from pathlib import Path

import tree_sitter_cpp as ts_cpp
from tree_sitter import Language, Parser

from autosync.Helper import fail_exit, get_path
from autosync.Helper import fail_exit


class Configurator:
Expand All @@ -25,9 +26,7 @@ class Configurator:
def __init__(self, arch: str, config_path: Path) -> None:
self.arch = arch
self.config_path = config_path
self.ts_shared_object = get_path("{VENDOR_DIR}").joinpath("ts_cpp.so")
self.load_config()
self.ts_compile_cpp()
self.ts_set_cpp_language()
self.init_parser()

Expand Down Expand Up @@ -69,22 +68,8 @@ def load_config(self) -> None:
)
self.config = conf

def ts_compile_cpp(self) -> None:
log.info("Compile Cpp language")
ts_grammar_path = get_path("{VENDOR_DIR}").joinpath("tree-sitter-cpp")
if not Path.exists(ts_grammar_path):
fail_exit(f"Could not load the tree-sitter grammar at '{ts_grammar_path}'")
# build_library wll be deprecated in 0.22.0. But CPP tree-sitter doesn't have Python bindings.
# So we stick with it.
Language.build_library(str(self.ts_shared_object), [str(ts_grammar_path)])

def ts_set_cpp_language(self) -> None:
log.info(f"Load language '{self.ts_shared_object}'")
if not Path.exists(self.ts_shared_object):
fail_exit(
f"Could not load the tree-sitter language shared object at '{self.ts_shared_object}'"
)
self.ts_cpp_lang = Language(str(self.ts_shared_object), "cpp")
self.ts_cpp_lang = Language(ts_cpp.language(), "cpp")

def init_parser(self) -> None:
log.debug("Init parser")
Expand Down
1 change: 0 additions & 1 deletion suite/auto-sync/vendor/tree-sitter-cpp
Submodule tree-sitter-cpp deleted from e0c167

0 comments on commit b15b1d6

Please sign in to comment.