Type providers for tree-sitter in Python
Reason this release was yanked:
Breaking changes from tree-sitter.
Project description
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
:
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: 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:
import tree_sitter as ts
import tree_sitter_type_provider as tstp
@dataclass
class JsArray(tstp.Node):
text: str
type_name: str
start_position: tstp.Point
end_position: tstp.Point
children: list[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:
def from_tree_sitter(self, tsnode: ts.Node, *, encoding: str = 'utf-8') -> tstp.Node
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for tree_sitter_type_provider-1.4.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62c6e3c5d09754116952ba3ca9b20a91e483a48cb2e061d83adf8ec7f23b7928 |
|
MD5 | 53b2aecf6299aae92cbcb35567e8eb61 |
|
BLAKE2b-256 | 55dca40ff8d2bf36c6601bca2a6743a4bb875c17941af13267e42693f4f8972a |
Close
Hashes for tree_sitter_type_provider-1.4.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1df2ea804b97254cdb6d85150716205348ba4a07765fa9758c16a2d841a16176 |
|
MD5 | 785275e2f730aa12aa772ce9bdf97020 |
|
BLAKE2b-256 | aae6fedc04d216bee02606f08f1cbc520689e0c99909380c1ccfe8d328785afa |