1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
{
lib,
buildPythonPackage,
setuptools,
pytestCheckHook,
tree-sitter,
symlinkJoin,
writeTextDir,
# `name`: grammar derivation pname in the format of `tree-sitter-<lang>`
name,
grammarDrv,
}:
let
# Map nix style `0-unstable-YYYY-MM-DD` version identifiers to a PEP 440
# compatible form (`0+unstableYYYYMMDD`).
version = lib.pipe grammarDrv [
lib.getVersion
(lib.splitString "-")
(
parts:
let
version = lib.head parts;
metadata = lib.join "" (lib.tail parts);
in
if (metadata == "") then version else "${version}+${metadata}"
)
];
snakeCaseName = lib.replaceStrings [ "-" ] [ "_" ] name;
drvPrefix = "python-${name}";
# If the name of the grammar attribute differs from the grammar's symbol name,
# it could cause a symbol mismatch at load time. This manually curated collection
# of overrides ensures the binding can find the correct symbol
langIdentOverrides = {
tree_sitter_org_nvim = "tree_sitter_org";
};
langIdent = langIdentOverrides.${snakeCaseName} or snakeCaseName;
in
buildPythonPackage {
inherit version;
pname = drvPrefix;
pyproject = true;
build-system = [ setuptools ];
src = symlinkJoin {
name = "${drvPrefix}-source";
paths = [
(writeTextDir "${snakeCaseName}/__init__.py" ''
# AUTO-GENERATED DO NOT EDIT
# preload the parser object before importing c binding
# this way we can avoid dynamic linker kicking in when
# downstream code imports this python module
import ctypes
import sys
import os
parser = "${grammarDrv}/parser"
try:
ctypes.CDLL(parser, mode=ctypes.RTLD_GLOBAL) # cached
except OSError as e:
raise ImportError(f"cannot load tree-sitter parser object from {parser}: {e}")
# expose binding
from ._binding import language
__all__ = ["language"]
'')
(writeTextDir "${snakeCaseName}/binding.c" ''
// AUTO-GENERATED DO NOT EDIT
#include <Python.h>
typedef struct TSLanguage TSLanguage;
TSLanguage *${langIdent}(void);
static PyObject* _binding_language(PyObject *self, PyObject *args) {
return PyLong_FromVoidPtr(${langIdent}());
}
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);
}
'')
(writeTextDir "setup.py" ''
# AUTO-GENERATED DO NOT EDIT
from platform import system
from setuptools import Extension, setup
setup(
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 []
),
)
],
)
'')
(writeTextDir "pyproject.toml" ''
# AUTO-GENERATED DO NOT EDIT
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name="${snakeCaseName}"
description = "${langIdent} grammar for tree-sitter"
version = "${version}"
keywords = ["parsing", "incremental", "python"]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Software Development :: Compilers",
"Topic :: Text Processing :: Linguistic",
]
requires-python = ">=3.8"
license = "MIT"
readme = "README.md"
[project.optional-dependencies]
core = ["tree-sitter~=0.21"]
[tool.cibuildwheel]
build = "cp38-*"
build-frontend = "build"
'')
(writeTextDir "tests/test_language.py" ''
# AUTO-GENERATED DO NOT EDIT
from ${snakeCaseName} import language
from tree_sitter import Language, Parser
# This test only checks that the binding can load the grammar from the compiled shared object.
# It does not verify the grammar itself; that is tested in
# `pkgs/development/tools/parsing/tree-sitter/grammar.nix`.
def test_language():
lang = Language(language())
assert lang is not None
parser = Parser(lang)
tree = parser.parse(bytes("", "utf-8"))
assert tree is not None
'')
];
};
preCheck = ''
# https://github.com/NixOS/nixpkgs/issues/255262
rm -r ${snakeCaseName}
'';
nativeCheckInputs = [
tree-sitter
pytestCheckHook
];
pythonImportsCheck = [ snakeCaseName ];
meta = {
description = "Python bindings for ${name}";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [
a-jay98
adfaure
mightyiam
stepbrobd
];
};
}
|