Skip to content

Fix binders that need to be wrapped in parens #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jan 29, 2022
Merged

Fix binders that need to be wrapped in parens #8

merged 19 commits into from
Jan 29, 2022

Conversation

JordanMartinez
Copy link
Contributor

Fixes #7. I also fixed a similar issue with exprCase and binderTyped

Comment on lines 655 to 661
addParens bnds' = bnds' <#> \b -> case b of
BinderTyped _ _ _ -> binderParens b
BinderOp _ _ -> binderParens b
BinderInt (Just _) _ _ -> binderParens b
BinderNumber (Just _) _ _ -> binderParens b
BinderConstructor _ vars | not $ Array.null vars -> binderParens b
_ -> b
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this should be precBinder2, which should be fixed to use the same guard on BinderConstructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided not to reuse precBinder2 here or update it to use the same guard because it can be used in a caseExpr:

case _ of
  NoParensHere a ->
-- vs
\(ParensHere a) ->

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, then we should move this to another prec function, and reuse it where appropriate. In the parser, this corresponds to all usages of binderAtom (https://github.com/purescript/purescript/blob/53b4117045f5517023f5d22f9fa506e651e4ff3e/lib/purescript-cst/src/Language/PureScript/CST/Parser.y#L575-L586). Seems like there's quite a few places where we should be using this precedence level.

(toGuarded tokRightArrow rhs)
where
addParens b = case b of
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be precBinder0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point.

@@ -896,7 +909,7 @@ binderVar = BinderVar <<< toName

-- | Constructs a named binding pattern (`@`).
binderNamed :: forall e name. ToName name Ident => name -> Binder e -> Binder e
binderNamed n = BinderNamed (toName n) tokAt
binderNamed n = BinderNamed (toName n) tokAt <<< binderParens
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be precBinder2 rather than binderParens, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah.. I thought usage of @ in a binder always required a parens. On the other hand, I don't think precBinder2 is quite right because a constructor doesn't need a parens. I tried this out on Try PureScript and didn't get an error or warning:


foo :: forall a. a -> String
foo = case _ of
 a@b -> ""

foo2 :: forall a. a -> String
foo2 = \a@b -> ""

data Foo = Foo

foo3 :: Foo -> String
foo3 = \a@Foo -> ""

@@ -181,7 +181,7 @@ precBinder2 a = case a of
BinderOp _ _ -> binderParens a
BinderInt (Just _) _ _ -> binderParens a
BinderNumber (Just _) _ _ -> binderParens a
BinderConstructor _ _ -> binderParens a
BinderConstructor _ args | not $ Array.null args -> binderParens a
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that precBinder2 is only used for the binderCtor. So, rather than defining a new precBinder3, I just updated precBinder2 here.

@natefaubion natefaubion merged commit bafc450 into natefaubion:main Jan 29, 2022
@natefaubion
Copy link
Owner

Thanks!

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.

binderCtor doesn't wrap contructor and args in parens
2 participants