Skip to content

Conversation

@doorgan
Copy link
Contributor

@doorgan doorgan commented Jan 31, 2026

Unary operators behave differently depending on wether they are applied to a matched or unmatched expression in Elixir's grammar. Calls with do/end blocks are unmatched expressions, and Elixir allows unary operators to wrap a full expression in that position, not just the immediate block.

Spitfire doesn't make that distinction, it always parses the unary operand at unary precedence, making it bind too tightly when the operand is an unparenthesized do/end block.

So, this code from the tests in #78:
+case a do _ -> b end |> c ** d >>> e

Is parsed like this by Spitfire:
(+(case a do _ -> b end) |> c ** d) >>> e

But Elixir parses it like this:
+(case a do _ -> b end |> c ** d >>> e)

The fix is to detect when the unary operand is an unparenthesized do/end block and reparse the RHS with the lowest precedence, making the unary operand a full expression, forcing it to behave like Elixir.

More generally, we probably want to make the distinction more explicit in Spitfires' parser, but for now I'm fixing one issue at a time to achieve parity with Elixir first and have a baseline for more improvements.

Unary operators behave differently depending on wether they are applied
to a matched or unmatched expression in Elixir's grammar. calls with do/end
blocks are unmatched expressions, and Elixir allows unary operators to
wrap a full expression in that position, not just the immediate block.

Spitfire doesn't make that distinction, it always parses the unary
operand at unary precedence, making it bind too tightly when the operand
is an unparenthesized do/end block.

So, this code from the tests in elixir-tools#78:
`+case a do _ -> b end |> c ** d >>> e`

Is parsed like this by Spitfire:
`(+(case a do _ -> b end) |> c ** d) >>> e`

But Elixir parses it like this:
`+(case a do _ -> b end |> c ** d >>> e)`

The fix is to detect when the unary operand is an unparenthesized do/end
block and reparse the RHS with the lowest precedence, making the unary
operand a full expression, forcing it to behave like Elixir.

More generally, we probably want to make the distinction more explicit
in Spitfires' parser, but for now I'm fixing one issue at a time to
achieve parity with Elixir first and have a baseline for more
improvements.
@mhanberg mhanberg merged commit dd45cb1 into elixir-tools:main Jan 31, 2026
37 checks passed
@doorgan doorgan deleted the doorgan/unary-unmatched-expr branch January 31, 2026 16:53
mhanberg pushed a commit that referenced this pull request Feb 4, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.3.2](v0.3.1...v0.3.2)
(2026-02-01)


### Bug Fixes

* associativity in bracketless kw list
([#89](#89))
([91b5d5b](91b5d5b))
* extract newlines only from newline carrying tokens
([#86](#86))
([edba800](edba800))
* improve handling of matched/unmatched expressions
([#88](#88))
([dd45cb1](dd45cb1))
* keyword list associativity
([#85](#85))
([a7d2a54](a7d2a54))
* parse ellipsis_op as unary operator
([#83](#83))
([e86f1f4](e86f1f4))
* rearrange `!(left) in right`
([#91](#91))
([fae2579](fae2579))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

2 participants