Skip to content

fix: bind await tighter than the power operator - #360

Open
wotjr1649 wants to merge 1 commit into
tree-sitter:masterfrom
wotjr1649:fix/await-power-precedence
Open

wotjr1649 wants to merge 1 commit into
tree-sitter:masterfrom
wotjr1649:fix/await-power-precedence

Conversation

@wotjr1649

Copy link
Copy Markdown

await f() ** 2 is parsed as await (f() ** 2):

(module (await (binary_operator left: (call ...) right: (integer))))

In Python, await takes a primary, and the power operator takes await_expr | primary on its left (await_expr, power). So the tree should be (await f()) ** 2:

(module (binary_operator left: (await (call ...)) right: (integer)))

await used PREC.unary, which is below PREC.power, so the grammar took the longer match. This gives await its own precedence between power and call (PREC.await = 22, call moves to 23). Calls, attributes and subscripts still bind inside the await operand.

Other inputs: await x ** y ** z gives (await x) ** (y ** z) (right associative, as before). -x ** 2, await f(), not await x, -await x, x ** await f() and await (f() ** 2) give the same trees as before.

The change is a few lines in grammar.js and one corpus test (test/corpus/expressions.txt). src/ was regenerated with tree-sitter 0.25.9, the CLI in package-lock.json. The generated files conflict with several open PRs (#336, #353, #354), so I expect they need regenerating when merged; I can rebase and regenerate on request.

Tested with 0.25.9 on Windows: tree-sitter test passes (118 parses; the new test fails on current master and passes with this change), regenerating gives no diff, and eslint grammar.js is clean. I did not run the "Parse examples" step (numpy/django/flask/cpython) or the Linux/macOS jobs. The last PR runs on this repository show action_required/failure with no jobs, so I do not expect hosted CI results on this PR either.

I used an AI coding assistant to reproduce the issue and draft this text; I checked the results and edited it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant