Skip to content

Commit

Permalink
python3Packages.tree-sitter-*: init at 0.22.5
Browse files Browse the repository at this point in the history
Co-authored-by: Robert James Hernandez <[email protected]>
Co-authored-by: Yifei Sun <[email protected]>
Co-authored-by: Ali Jamadi <[email protected]>
Co-authored-by: yakampe <[email protected]>
Co-authored-by: GetPsyched <[email protected]>
Co-authored-by: Adrien Faure <[email protected]>
Co-authored-by: Shahar "Dawn" Or <[email protected]>
  • Loading branch information
7 people committed Jun 19, 2024
1 parent 0efbdc8 commit 4eeaa9e
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
136 changes: 136 additions & 0 deletions pkgs/development/python-modules/tree-sitter-grammars/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{ pkgs
, lib
, buildPythonPackage
, pytestCheckHook
, tree-sitter
}:
let
grammarToPythonPkg = name: grammarDrv:
let
inherit (grammarDrv) version;

# `name`: grammar derivation pname in the format of `tree-sitter-<lang>`

snakeCaseName = lib.replaceStrings [ "-" ] [ "_" ] name;
drvPrefix = "python-${name}";
in
buildPythonPackage {
inherit version;
pname = drvPrefix;

src = pkgs.symlinkJoin {
name = "${drvPrefix}-source";
paths = [
(pkgs.writeTextFile {
name = "${drvPrefix}-init";
text = ''
from ._binding import language
__all__ = ["language"]
'';
destination = "/${snakeCaseName}/__init__.py";
})
(pkgs.writeTextFile {
name = "${drvPrefix}-binding";
text = ''
#include <Python.h>
typedef struct TSLanguage TSLanguage;
TSLanguage *${snakeCaseName}(void);
static PyObject* _binding_language(PyObject *self, PyObject *args) {
return PyLong_FromVoidPtr(${snakeCaseName}());
}
static PyMethodDef methods[] = {
{"language", _binding_language, METH_NOARGS,
"Get the tree-sitter language for this grammar."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef module = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "_binding",
.m_doc = NULL,
.m_size = -1,
.m_methods = methods
};
PyMODINIT_FUNC PyInit__binding(void) {
return PyModule_Create(&module);
}
'';
destination = "/${snakeCaseName}/binding.c";
})
(pkgs.writeTextFile {
name = "${drvPrefix}-setup.py";
text = ''
from platform import system
from setuptools import Extension, setup
setup(
name="${snakeCaseName}",
version="${version}",
packages=["${snakeCaseName}"],
ext_package="${snakeCaseName}",
ext_modules=[
Extension(
name="_binding",
sources=["${snakeCaseName}/binding.c"],
extra_objects = ["${grammarDrv}/parser"],
extra_compile_args=(
["-std=c11"] if system() != 'Windows' else []
),
define_macros=[
("Py_LIMITED_API", "0x03080000"),
("PY_SSIZE_T_CLEAN", None)
],
py_limited_api=True,
)
],
)
'';
destination = "/setup.py";
})
(pkgs.writeTextFile {
name = "${drvPrefix}-test";
text = ''
from ${snakeCaseName} import language
from tree_sitter import Language, Parser
def test_language():
lang = Language(language())
assert lang is not None
parser = Parser()
parser.language = lang
tree = parser.parse(bytes("", "utf-8"))
assert tree is not None
'';
destination = "/tests/test_language.py";
})
];
};

preCheck = ''
rm -r ${snakeCaseName}
'';

nativeCheckInputs = [ tree-sitter pytestCheckHook ];
pythonImportsCheck = [ snakeCaseName ];

meta = {
description = "Python bindings for ${name}";
maintainers = with lib.maintainers; [ a-jay98 adfaure mightyiam stepbrobd ];
license = lib.licenses.mit;
};
};
in
# TODO pkgset or flattened?
lib.mapAttrs grammarToPythonPkg (builtins.removeAttrs pkgs.tree-sitter.builtGrammars [
"tree-sitter-perl"
"tree-sitter-ql-dbscheme"
"tree-sitter-org-nvim"
])
2 changes: 2 additions & 0 deletions pkgs/top-level/python-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15483,6 +15483,8 @@ self: super: with self; {

tree-sitter0_21 = callPackage ../development/python-modules/tree-sitter0_21 { };

tree-sitter-grammars = callPackage ../development/python-modules/tree-sitter-grammars { };

tree-sitter-html = callPackage ../development/python-modules/tree-sitter-html { };

tree-sitter-python = callPackage ../development/python-modules/tree-sitter-python { };
Expand Down

0 comments on commit 4eeaa9e

Please sign in to comment.