tree-sitter-type-provider


Nametree-sitter-type-provider JSON
Version 2.1.25 PyPI version JSON
download
home_page
SummaryType providers for tree-sitter in Python.
upload_time2023-09-11 07:49:56
maintainer
docs_urlNone
author
requires_python<3.12,>=3.7.1
licenseMIT License Copyright (c) 2022 Wen Kokke Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords parser tree-sitter type-provider
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![PyPI](https://img.shields.io/pypi/v/tree-sitter-type-provider)
![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/wenkokke/py-tree-sitter-type-provider)
[![GitHub Workflow Status](https://github.com/wenkokke/py-tree-sitter-type-provider/actions/workflows/ci.yml/badge.svg)](https://github.com/wenkokke/py-tree-sitter-talon/actions/workflows/ci.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/wenkokke/py-tree-sitter-type-provider/dev.svg)](https://results.pre-commit.ci/latest/github/wenkokke/py-tree-sitter-type-provider/dev)

# Type Providers for Tree Sitter

Create a type AST from any `node-types.json` file, as well as a generic visitor class and a transformer class, and a function to convert to the AST from the `tree_sitter.Node` type.

For example, the following code defines a module named `tree_sitter_javascript` from `tree-sitter-javascript/src/nodes.json`:

```python
import pathlib
import tree_sitter_type_provider as tstp

node_types_json = pathlib.Path("tree-sitter-javascript/src/node-types.json")
node_types = tstp.NodeType.schema().loads(node_types_json.read_text(), many=True)

def as_class_name(node_type_name: str) -> str:
    class_name_parts: typing.List[str] = ["Js"]
    for part in node_type_name.split("_"):
        class_name_parts.append(part.capitalize())
    return "".join(class_name_parts)

sys.modules[__name__] = tstp.TreeSitterTypeProvider(
    "tree_sitter_javascript",
    node_types,
    error_as_node=True,          # Include ERROR as a node in the AST
    as_class_name=as_class_name, # How to convert node types to Python class names
    extra=["comment"],           # Nodes which are marked as 'extra' in the grammar
)
```

The module contains a number of dataclasses which represent the AST nodes:

```python
import tree_sitter as ts
import tree_sitter_type_provider as tstp
import typing

@dataclass
class JsArray(tstp.Node):
    text: str
    type_name: str
    start_position: tstp.Point
    end_position: tstp.Point
    children: typing.List[typing.Union[JsExpression, JsSpreadElement]]

@dataclass
class JsDeclaration(tstp.Node):
    text: str
    type_name: str
    start_position: tstp.Point
    end_position: tstp.Point

@dataclass
class JsWhileStatement(tstp.Node):
    text: str
    type_name: str
    start_position: tstp.Point
    end_position: tstp.Point
    body: JsStatement
    condition: JsParenthesizedExpression

...
```

As well as a function to convert to the AST:

```python
def from_tree_sitter(self, tsvalue: typing.Union[ts.Tree, ts.Node, ts.TreeCursor], *, encoding: str = 'utf-8') -> tstp.Node
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "tree-sitter-type-provider",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "<3.12,>=3.7.1",
    "maintainer_email": "",
    "keywords": "parser,tree-sitter,type-provider",
    "author": "",
    "author_email": "Wen Kokke <wenkokke@users.noreply.github.com>",
    "download_url": "https://files.pythonhosted.org/packages/b2/51/c1c14c0283fa741fbc61d43438d4df9711129f41019a7327c6065344b612/tree_sitter_type_provider-2.1.25.tar.gz",
    "platform": null,
    "description": "![PyPI](https://img.shields.io/pypi/v/tree-sitter-type-provider)\n![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/wenkokke/py-tree-sitter-type-provider)\n[![GitHub Workflow Status](https://github.com/wenkokke/py-tree-sitter-type-provider/actions/workflows/ci.yml/badge.svg)](https://github.com/wenkokke/py-tree-sitter-talon/actions/workflows/ci.yml)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/wenkokke/py-tree-sitter-type-provider/dev.svg)](https://results.pre-commit.ci/latest/github/wenkokke/py-tree-sitter-type-provider/dev)\n\n# Type Providers for Tree Sitter\n\nCreate a type AST from any `node-types.json` file, as well as a generic visitor class and a transformer class, and a function to convert to the AST from the `tree_sitter.Node` type.\n\nFor example, the following code defines a module named `tree_sitter_javascript` from `tree-sitter-javascript/src/nodes.json`:\n\n```python\nimport pathlib\nimport tree_sitter_type_provider as tstp\n\nnode_types_json = pathlib.Path(\"tree-sitter-javascript/src/node-types.json\")\nnode_types = tstp.NodeType.schema().loads(node_types_json.read_text(), many=True)\n\ndef as_class_name(node_type_name: str) -> str:\n    class_name_parts: typing.List[str] = [\"Js\"]\n    for part in node_type_name.split(\"_\"):\n        class_name_parts.append(part.capitalize())\n    return \"\".join(class_name_parts)\n\nsys.modules[__name__] = tstp.TreeSitterTypeProvider(\n    \"tree_sitter_javascript\",\n    node_types,\n    error_as_node=True,          # Include ERROR as a node in the AST\n    as_class_name=as_class_name, # How to convert node types to Python class names\n    extra=[\"comment\"],           # Nodes which are marked as 'extra' in the grammar\n)\n```\n\nThe module contains a number of dataclasses which represent the AST nodes:\n\n```python\nimport tree_sitter as ts\nimport tree_sitter_type_provider as tstp\nimport typing\n\n@dataclass\nclass JsArray(tstp.Node):\n    text: str\n    type_name: str\n    start_position: tstp.Point\n    end_position: tstp.Point\n    children: typing.List[typing.Union[JsExpression, JsSpreadElement]]\n\n@dataclass\nclass JsDeclaration(tstp.Node):\n    text: str\n    type_name: str\n    start_position: tstp.Point\n    end_position: tstp.Point\n\n@dataclass\nclass JsWhileStatement(tstp.Node):\n    text: str\n    type_name: str\n    start_position: tstp.Point\n    end_position: tstp.Point\n    body: JsStatement\n    condition: JsParenthesizedExpression\n\n...\n```\n\nAs well as a function to convert to the AST:\n\n```python\ndef from_tree_sitter(self, tsvalue: typing.Union[ts.Tree, ts.Node, ts.TreeCursor], *, encoding: str = 'utf-8') -> tstp.Node\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2022 Wen Kokke  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Type providers for tree-sitter in Python.",
    "version": "2.1.25",
    "project_urls": null,
    "split_keywords": [
        "parser",
        "tree-sitter",
        "type-provider"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b1976e5be6363c4d098037013099cfc334859c6d6c57d8579b557eb5b3235190",
                "md5": "f4414425dc5af45c1a416ee5432ba2d5",
                "sha256": "67f17e3de5cc623f2c297dec5cdee7fb3284a2b8d58d5f2772cf61b437bb9497"
            },
            "downloads": -1,
            "filename": "tree_sitter_type_provider-2.1.25-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f4414425dc5af45c1a416ee5432ba2d5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.12,>=3.7.1",
            "size": 9080,
            "upload_time": "2023-09-11T07:49:55",
            "upload_time_iso_8601": "2023-09-11T07:49:55.098066Z",
            "url": "https://files.pythonhosted.org/packages/b1/97/6e5be6363c4d098037013099cfc334859c6d6c57d8579b557eb5b3235190/tree_sitter_type_provider-2.1.25-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b251c1c14c0283fa741fbc61d43438d4df9711129f41019a7327c6065344b612",
                "md5": "bd4b78bef3d05b85c54f1c23460fec97",
                "sha256": "143ca7fa12fd4946836caf1b87eb233ce9624e3ac8be9a92029867771b132bda"
            },
            "downloads": -1,
            "filename": "tree_sitter_type_provider-2.1.25.tar.gz",
            "has_sig": false,
            "md5_digest": "bd4b78bef3d05b85c54f1c23460fec97",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.12,>=3.7.1",
            "size": 10876,
            "upload_time": "2023-09-11T07:49:56",
            "upload_time_iso_8601": "2023-09-11T07:49:56.652744Z",
            "url": "https://files.pythonhosted.org/packages/b2/51/c1c14c0283fa741fbc61d43438d4df9711129f41019a7327c6065344b612/tree_sitter_type_provider-2.1.25.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-11 07:49:56",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "tree-sitter-type-provider"
}
        
Elapsed time: 0.11027s