fix: improve handling of matched/unmatched expressions #88
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 >>> eIs parsed like this by Spitfire:
(+(case a do _ -> b end) |> c ** d) >>> eBut 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.