Skip to content
Snippets Groups Projects
Commit 52c2122d authored by s200545's avatar s200545
Browse files

WIP: working with christian

parent 755840c5
Branches
No related tags found
No related merge requests found
import click
import os
from tree_sitter import Language, Parser
Language.build_library(
# Store the library in the `build` directory
'build/my-languages.so',
# Include one or more languages
[
'../../vendor/tree-sitter-java'
]
)
JAVA_LANGUAGE = Language('build/my-languages.so', 'java')
parser = Parser()
parser.set_language(JAVA_LANGUAGE)
@click.command()
@click.option("--file", prompt="Path of the file", help="Path of the file")
def cli(file):
formast(file)
#@click.option("--file2", prompt="Path of the second file", help="Path of the second file")
## Formast
def formast(file_path):
tree = parse_java_file(file_path)
save_ast_file(file_path, tree)
print("AST file saved!")
## Tree traversal function
def traverse(tree):
cursor = tree.walk()
descend = [True]
while True:
if descend[-1]:
yield cursor.node
if descend[-1] and cursor.goto_first_child():
descend[-1] = False
descend.append(True)
elif cursor.goto_next_sibling():
descend[-1] = True
elif cursor.goto_parent():
descend.pop()
else:
break
## Parse the java file and return the tree
def parse_java_file(file_path):
if not is_java_file(file_path):
raise ValueError('File must have a .java extension')
with open(file_path, 'r') as f:
src = f.read()
tree_word = parser.parse(src.encode("utf8"))
return tree_word
## Save the ast file
def save_ast_file(file_path, tree):
with open(file_path+".ast", 'w') as f:
for node in traverse(tree):
if node.child_count == 0:
f.write(node.text.decode("utf8"))
f.write("\n")
## Check if the file is a java file
def is_java_file(file_path):
return os.path.splitext(file_path)[1] == '.java'
if __name__ == "__main__":
cli()
......@@ -16,6 +16,39 @@ version = "0.4.6"
requires_python = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
summary = "Cross-platform colored terminal text."
[[package]]
name = "gitdb"
version = "4.0.10"
requires_python = ">=3.7"
summary = "Git Object Database"
dependencies = [
"smmap<6,>=3.0.1",
]
[[package]]
name = "gitpython"
version = "3.1.31"
requires_python = ">=3.7"
summary = "GitPython is a Python library used to interact with Git repositories"
dependencies = [
"gitdb<5,>=4.0.1",
]
[[package]]
name = "smmap"
version = "5.0.0"
requires_python = ">=3.6"
summary = "A pure Python implementation of a sliding window memory map manager"
[[package]]
name = "tqdm"
version = "4.65.0"
requires_python = ">=3.7"
summary = "Fast, Extensible Progress Meter"
dependencies = [
"colorama; platform_system == \"Windows\"",
]
[[package]]
name = "tree-sitter"
version = "0.20.1"
......@@ -24,7 +57,7 @@ summary = "Python bindings to the Tree-sitter parsing library"
[metadata]
lock_version = "4.1"
content_hash = "sha256:ba889c3fb32c86b0d08f06bffbcbc933847495ed8d32a65fb96abf89ade461cb"
content_hash = "sha256:b560d788287a88ad0379594b26f5140b581224bfcb15d79daaf4b0d72d7ca099"
[metadata.files]
"click 8.1.3" = [
......@@ -35,6 +68,22 @@ content_hash = "sha256:ba889c3fb32c86b0d08f06bffbcbc933847495ed8d32a65fb96abf89a
{url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
{url = "https://files.pythonhosted.org/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
]
"gitdb 4.0.10" = [
{url = "https://files.pythonhosted.org/packages/21/a6/35f83efec687615c711fe0a09b67e58f6d1254db27b1013119de46f450bd/gitdb-4.0.10-py3-none-any.whl", hash = "sha256:c286cf298426064079ed96a9e4a9d39e7f3e9bf15ba60701e95f5492f28415c7"},
{url = "https://files.pythonhosted.org/packages/4b/47/dc98f3d5d48aa815770e31490893b92c5f1cd6c6cf28dd3a8ae0efffac14/gitdb-4.0.10.tar.gz", hash = "sha256:6eb990b69df4e15bad899ea868dc46572c3f75339735663b81de79b06f17eb9a"},
]
"gitpython 3.1.31" = [
{url = "https://files.pythonhosted.org/packages/5f/11/2b0f60686dbda49028cec8c66bd18a5e82c96d92eef4bc34961e35bb3762/GitPython-3.1.31.tar.gz", hash = "sha256:8ce3bcf69adfdf7c7d503e78fd3b1c492af782d58893b650adb2ac8912ddd573"},
{url = "https://files.pythonhosted.org/packages/9e/8a/d1e02cc111d65b0346f70abb83c51f8593e7134bf694a4a56d1a470caaf7/GitPython-3.1.31-py3-none-any.whl", hash = "sha256:f04893614f6aa713a60cbbe1e6a97403ef633103cdd0ef5eb6efe0deb98dbe8d"},
]
"smmap 5.0.0" = [
{url = "https://files.pythonhosted.org/packages/21/2d/39c6c57032f786f1965022563eec60623bb3e1409ade6ad834ff703724f3/smmap-5.0.0.tar.gz", hash = "sha256:c840e62059cd3be204b0c9c9f74be2c09d5648eddd4580d9314c3ecde0b30936"},
{url = "https://files.pythonhosted.org/packages/6d/01/7caa71608bc29952ae09b0be63a539e50d2484bc37747797a66a60679856/smmap-5.0.0-py3-none-any.whl", hash = "sha256:2aba19d6a040e78d8b09de5c57e96207b09ed71d8e55ce0959eeee6c8e190d94"},
]
"tqdm 4.65.0" = [
{url = "https://files.pythonhosted.org/packages/3d/78/81191f56abb7d3d56963337dbdff6aa4f55805c8afd8bad64b0a34199e9b/tqdm-4.65.0.tar.gz", hash = "sha256:1871fb68a86b8fb3b59ca4cdd3dcccbc7e6d613eeed31f4c332531977b89beb5"},
{url = "https://files.pythonhosted.org/packages/e6/02/a2cff6306177ae6bc73bc0665065de51dfb3b9db7373e122e2735faf0d97/tqdm-4.65.0-py3-none-any.whl", hash = "sha256:c4f53a17fe37e132815abceec022631be8ffe1b9381c2e6e30aa70edc99e9671"},
]
"tree-sitter 0.20.1" = [
{url = "https://files.pythonhosted.org/packages/54/d4/7cdecaad1c3564470938b631908589b1e2a0f91bf5d817b8b00d5f438d90/tree_sitter-0.20.1-cp39-cp39-macosx_12_0_arm64.whl", hash = "sha256:6f11a1fd909dcf569e7b1d98861a837436799e757bbbc5cd5280989050929e12"},
{url = "https://files.pythonhosted.org/packages/ea/11/8d3f8ed4761c375dca0918a5b170aa2d777f5325c5442c36c0851305b77a/tree_sitter-0.20.1.tar.gz", hash = "sha256:e93f082c545d6649bcfb5d681ed255eb004a6ce22988971a128f40692feec60d"},
......
......@@ -8,6 +8,8 @@ authors = [
dependencies = [
"click>=8.1.3",
"tree-sitter>=0.20.1",
"GitPython>=3.1.31",
"tqdm>=4.65.0",
]
requires-python = ">=3.10"
readme = "README.md"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment