Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ to its authors and contributors. This is not an official release of that project
This fork builds on upstream v0.25.0 with grammar, scanner, and highlighting fixes, including Python 3.15
unpacking comprehensions. It ships a compact parser generated by the parser generator of
[WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter). The parser carries that fork's own ABI number, so
it loads only with the WillBooster runtime (`willbooster-tree-sitter` or `@willbooster/web-tree-sitter`) 2.0.0 or later,
it loads only with the WillBooster runtime (`willbooster-tree-sitter` or `@willbooster/web-tree-sitter`) 2.x,
not with upstream's tree-sitter.

## Usage

The npm package ships `tree-sitter-python.wasm` for
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js, Bun,
browsers, and Cloudflare Workers. The parser requires runtime 2.0.0 or later.
browsers, and Cloudflare Workers. The parser requires runtime 2.x.

In Node.js and Bun, load the grammar from its path:

Expand Down Expand Up @@ -63,14 +63,16 @@ parser.setLanguage(await Language.load(python));

The package also ships the queries in `queries/` and the node types in `src/node-types.json`.

Qualified generic bases use `member_type` inside `generic_type`. Structured unpacked annotations and unpacked generic arguments use a `type` child inside `splat_type`. Expression-valued starred parameter annotations, such as `*args: *factory()`, keep a direct expression child instead. Ordinary value unpacks keep their expression tree.

In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-python) and on
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
fuzzed with. The parser requires runtime 2.0.0 or later:
fuzzed with. The parser requires runtime 2.x:

```toml
[dependencies]
tree-sitter = { package = "willbooster-tree-sitter", version = "2" }
tree-sitter-python = { package = "willbooster-tree-sitter-python", version = "2" }
tree-sitter-python = { package = "willbooster-tree-sitter-python", version = "3" }
```

```rust
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This crate provides Python language support for the [tree-sitter] parsing library.
//!
//! The parser loads only with the WillBooster runtime, the [willbooster-tree-sitter] crate 2.0.0 or later.
//! The parser loads only with the WillBooster runtime, the [willbooster-tree-sitter] crate 2.x.
//!
//! Typically, you will use the [`LANGUAGE`] constant to add this language to a
//! tree-sitter [`Parser`], and then use the parser to parse some code:
Expand Down
9 changes: 6 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ module.exports = grammar({
_default_splat_type: ($) => seq('*', choice($.primary_expression, prec.dynamic(1, alias($._default_type, $.type)))),

_generic_type_arguments: ($) =>
seq('[', commaSep1(choice($.type, alias($._list_splat_argument, $.splat_type))), optional(','), ']'),
seq('[', commaSep1(choice($.type, alias($._default_splat_argument, $.splat_type))), optional(','), ']'),

argument_list: ($) =>
seq(
Expand Down Expand Up @@ -832,7 +832,7 @@ module.exports = grammar({
PREC.typed_parameter,
choice(
seq(choice($.identifier, $.dictionary_splat_pattern), ':', field('type', $.type)),
seq($.list_splat_pattern, ':', field('type', choice($.type, alias($.list_splat, $.splat_type))))
seq($.list_splat_pattern, ':', field('type', choice($.type, alias($._default_splat_type, $.splat_type))))
)
),

Expand All @@ -841,7 +841,10 @@ module.exports = grammar({
generic_type: ($) =>
prec.dynamic(
1,
seq(choice($.identifier, alias('type', $.identifier)), alias($._generic_type_arguments, $.type_parameter))
seq(
choice($.identifier, alias('type', $.identifier), alias($._default_member_type, $.member_type)),
alias($._generic_type_arguments, $.type_parameter)
)
),
union_type: ($) => prec.left(seq($.type, '|', $.type)),
constrained_type: ($) => prec.right(seq($.type, ':', $.type)),
Expand Down
3 changes: 2 additions & 1 deletion queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
(class_definition
superclasses: (argument_list
(subscript subscript: (list_splat (identifier) @type))))
(member_type (identifier) @type)
(type (member_type (identifier) @type .))
(generic_type (member_type (identifier) @type .))
(generic_type (identifier) @type)
(default_type_parameter name: (identifier) @type)

Expand Down
15 changes: 12 additions & 3 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2864,7 +2864,7 @@
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_list_splat_argument"
"name": "_default_splat_argument"
},
"named": true,
"value": "splat_type"
Expand All @@ -2891,7 +2891,7 @@
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_list_splat_argument"
"name": "_default_splat_argument"
},
"named": true,
"value": "splat_type"
Expand Down Expand Up @@ -5709,7 +5709,7 @@
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "list_splat"
"name": "_default_splat_type"
},
"named": true,
"value": "splat_type"
Expand Down Expand Up @@ -5798,6 +5798,15 @@
},
"named": true,
"value": "identifier"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_default_member_type"
},
"named": true,
"value": "member_type"
}
]
},
Expand Down
20 changes: 0 additions & 20 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -3066,26 +3066,6 @@
"multiple": false,
"required": true,
"types": [
{
"type": "boolean_operator",
"named": true
},
{
"type": "comparison_operator",
"named": true
},
{
"type": "conditional_expression",
"named": true
},
{
"type": "lambda",
"named": true
},
{
"type": "not_operator",
"named": true
},
{
"type": "primary_expression",
"named": true
Expand Down
Loading
Loading