summaryrefslogtreecommitdiff
path: root/pkgs/development/python-modules/tree-sitter-grammars/default.nix
blob: a8ea305dc2aee95c8f5c15367bad4ed07dc8a50d (about) (plain)
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
{
  lib,
  buildPythonPackage,
  pytestCheckHook,
  tree-sitter,
  symlinkJoin,
  writeTextDir,
  pythonOlder,
  # `name`: grammar derivation pname in the format of `tree-sitter-<lang>`
  name,
  grammarDrv,
}:
let
  inherit (grammarDrv) version;

  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;

  src = symlinkJoin {
    name = "${drvPrefix}-source";
    paths = [
      (writeTextDir "${snakeCaseName}/__init__.py" ''
        from ._binding import language

        __all__ = ["language"]
      '')
      (writeTextDir "${snakeCaseName}/binding.c" ''
        #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" ''
        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" ''
        [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",
          "License :: OSI Approved :: MIT License",
          "Topic :: Software Development :: Compilers",
          "Topic :: Text Processing :: Linguistic",
        ]

        requires-python = ">=3.8"
        license.text = "MIT"
        readme = "README.md"

        [project.optional-dependencies]
        core = ["tree-sitter~=0.21"]

        [tool.cibuildwheel]
        build = "cp38-*"
        build-frontend = "build"
      '')
      (writeTextDir "tests/test_language.py" ''
        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()
          parser.language = lang
          tree = parser.parse(bytes("", "utf-8"))
          assert tree is not None
      '')
    ];
  };

  preCheck = ''
    # https://github.com/NixOS/nixpkgs/issues/255262
    rm -r ${snakeCaseName}
  '';

  disabled = pythonOlder "3.8";

  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
    ];
  };
}