Skip to content
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

Support “fine-grained” operator precedence #1106

Closed
sellout opened this issue Mar 22, 2024 · 0 comments · Fixed by #1133
Closed

Support “fine-grained” operator precedence #1106

sellout opened this issue Mar 22, 2024 · 0 comments · Fixed by #1133

Comments

@sellout
Copy link
Contributor

sellout commented Mar 22, 2024

I sometimes end up with long chains of operators with identical precedence. E.g., in some parser code, I have

infixr 3 >~< -- run two parsers with whitespace between them
infixr 3 |~| -- run two parsers with optional whitespace between them
infixr 3 <~> -- run two parsers without whitespace between them

They should have the same precedence, because they’re mutually associative, so that’s all good. But when Ormolu formats a chain split across multiple lines, it can get noisy

startFormTok
  |~| messageTag
  >~< startMessageTok
  |~| name
  >~< p'
  |~| endMessageTok
  |~| endFormTok

What I would like is for Ormolu to treat them so that required whitespace is lower precedence than optional, is lower than illegal whitespace, so I can have it formatted like

startFormTok |~| messageTag
  >~< startMessageTok |~| name
  >~< p' |~| endMessageTok |~| endFormTok

I think a fairly clean solution would be for the .ormolu file to parse the precedences as floating point, rather than integers. Existing .ormolu files should work as is, but I can edit it to look like

infixr 3 >~<
infixr 3.3 |~|
infixr 3.7 <~>

and get the behavior I want.

As far as working with #846, perhaps regeneration could update the integral part without affecting the fractional part, so the fine-grained relationships are preserved even if the operator precedence has shifted.

What I currently do is fill my .ormolu with lies. Using incorrect precedences like

infixr 3 >~<
infixr 4 |~|
infixr 5 <~>

gets me the behavior I want, but I have to be careful to adjust any other precedences that bump into these.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants