Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbrunsfeld committed Mar 20, 2019
0 parents commit c18a307
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
build
dist
*.pyc
*.egg-info
*.so
__pycache__
test/fixtures/tree-sitter-python
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "tree-sitter"]
path = tree_sitter/core
url = https://github.com/tree-sitter/tree-sitter
14 changes: 14 additions & 0 deletions script/fetch-fixtures
@@ -0,0 +1,14 @@
#!/bin/bash

language_dir=test/fixtures/tree-sitter-python
language_url=https://github.com/tree-sitter/tree-sitter-python

if [ ! -d $language_dir ]; then
git clone $language_url $language_dir --depth=1
fi

(
cd $language_dir
git fetch origin master --depth 1
git reset --hard FETCH_HEAD
)
10 changes: 10 additions & 0 deletions script/test
@@ -0,0 +1,10 @@
#!/bin/bash

if which python3 > /dev/null; then
py=python3
else
py=python
fi


CFLAGS="-O0 -g" $py -- setup.py --quiet test
45 changes: 45 additions & 0 deletions setup.py
@@ -0,0 +1,45 @@
"""
Py-Tree-sitter
"""


from setuptools import setup, Extension


setup(
name = "tree_sitter",
version = "0.0.1",
maintainer = "Max Brunsfeld",
maintainer_email = "maxbrunsfeld@gmail.com",
author = "Max Brunsfeld",
author_email = "maxbrunsfeld@gmail.com",
url = "https://github.com/tree-sitter/py-tree-sitter",
license = "MIT",
platforms = ["any"],
python_requires = ">=2.7",
description = "Python bindings to the Tree-sitter parsing library",
classifiers = [
"Topic :: Parsing",
],
packages = ['tree_sitter'],
ext_modules = [
Extension(
"tree_sitter_binding",
[
"tree_sitter/core/lib/src/lib.c",
"tree_sitter/binding.c",
],
include_dirs = [
"tree_sitter/core/lib/include",
"tree_sitter/core/lib/utf8proc",
],
extra_compile_args = [
"-std=c99",
],
)
],
project_urls = {
'Source': 'https://github.com/tree-sitter/py-tree-sitter',
'Documentation': 'http://initd.org/psycopg/docs/',
}
)
15 changes: 15 additions & 0 deletions tests/__init__.py
@@ -0,0 +1,15 @@
import unittest
from tree_sitter import Parser, Language


lib_path = "build/python.so"
Language.build("tests/fixtures/tree-sitter-python", lib_path)
language = Language(lib_path, "python")


class TestTreeSitter(unittest.TestCase):
def test_upper(self):
parser = Parser()
parser.set_language(language)
tree = parser.parse("def foo():\n bar()")
self.assertEqual(tree.root_node.type, "module")
41 changes: 41 additions & 0 deletions tree_sitter/__init__.py
@@ -0,0 +1,41 @@
from ctypes import cdll, c_void_p
from tree_sitter_binding import Parser
import subprocess
import os.path as path


INCLUDE_PATH = path.join(path.dirname(__file__), "core", "lib", "include")


class Language:
def build(repo_path, output_path):
compiler = "clang++"
src_path = path.join(repo_path, "src")
parser_path = path.join(src_path, "parser.c")

command = [
compiler,
"-shared",
"-o",
output_path,
"-I",
INCLUDE_PATH,
"-xc",
path.join(src_path, "parser.c")
]

if path.exists(path.join(src_path, "scanner.cc")):
command.append("-xc++")
command.append(path.join(src_path, "scanner.cc"))
elif path.exists(path.join(src_path, "scanner.c")):
command.append(path.join(src_path, "scanner.c"))

subprocess.run(command)

def __init__(self, path, name):
self.path = path
self.name = name
self.lib = cdll.LoadLibrary(path)
function = getattr(self.lib, "tree_sitter_%s" % name)
function.restype = c_void_p
self.language_id = function()

0 comments on commit c18a307

Please sign in to comment.