Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c18a307
Showing
9 changed files
with
407 additions
and
0 deletions.
There are no files selected for viewing
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,7 @@ | ||
build | ||
dist | ||
*.pyc | ||
*.egg-info | ||
*.so | ||
__pycache__ | ||
test/fixtures/tree-sitter-python |
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,3 @@ | ||
[submodule "tree-sitter"] | ||
path = tree_sitter/core | ||
url = https://github.com/tree-sitter/tree-sitter |
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,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 | ||
) |
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,10 @@ | ||
#!/bin/bash | ||
|
||
if which python3 > /dev/null; then | ||
py=python3 | ||
else | ||
py=python | ||
fi | ||
|
||
|
||
CFLAGS="-O0 -g" $py -- setup.py --quiet test |
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,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/', | ||
} | ||
) |
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,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") |
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,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() |
Oops, something went wrong.