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
:
from pathlib import Path
import tree_sitter_type_provider as tstp
node_types_json = 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, as_class_name=as_class_name
)
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 generic visitor and transformer classes:
Result = TypeVar("Result")
class NodeTransformer:
transform(self, node: tstp.Node) -> Result
transform_ERROR(self, *, text: str, type_name: str, start_position: tstp.Point, end_position: tstp.Point, **kwargs: dict[str, Any]) -> Result
transform_JsArray(self, *, text: str, type_name: str, start_position: tstp.Point, end_position: tstp.Point, **kwargs: dict[str, Any]) -> Result
# NOTE: no transform_JsDeclaration: it's not a concrete node type, but a superclass for several node types
transform_JsWhileStatement(self, *, text: str, type_name: str, start_position: tstp.Point, end_position: tstp.Point, **kwargs: dict[str, Any]) -> Result
...
class NodeVisitor:
generic_visit(self, node: tstp.Node) -> None
visit(self, node: tstp.Node) -> None
visit_ERROR(self, node: tstp.Node) -> None
visit_JsArray(self, node: tstp.Node) -> None
# NOTE: no visit_JsDeclaration: it's not a concrete node type, but a superclass for several node types
visit_JsWhileStatement(self, node: tstp.Node) -> None
...
And 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.2.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a7f4a0870d61512bd712bd4b2cf642d2240f55945b06e898046717cec8260d9 |
|
MD5 | 38f332f672e3e9bd90e9c06ba020098d |
|
BLAKE2b-256 | 352880c7d569a36599c0f4467c3e28942ee53460224e71ef1ed30d090a87e935 |
Close
Hashes for tree_sitter_type_provider-1.2.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17fcd5fc3b89c21a3e5631ff523d2cd815d478a252b7b59bde4e3cb4e6fb3924 |
|
MD5 | 77d34b2c7f502d1160a5be77bc69ff76 |
|
BLAKE2b-256 | 3d1267eca4b5445035f90fe4d70f1f89c4eeb19c4d412cf4adbb99927f7ddff3 |