Skip to content

Replace haskell-src-exts with ghc-lib-parser - #39

Merged
mzabani merged 26 commits into
masterfrom
ghc-lib-parser
Aug 8, 2026
Merged

Replace haskell-src-exts with ghc-lib-parser#39
mzabani merged 26 commits into
masterfrom
ghc-lib-parser

Conversation

@mzabani

@mzabani mzabani commented Jul 29, 2026

Copy link
Copy Markdown
Owner

This should fix #36

@mzabani mzabani mentioned this pull request Jul 29, 2026

@brandonchinn178 brandonchinn178 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursory skim looks fine to me; AI is usually pretty good at mechanical transformations like convertExpr

Comment thread hpgsql/src/Hpgsql/GhcParseExp.hs Outdated
Comment thread hpgsql/src/Hpgsql/GhcParseExp.hs Outdated
convertExpr (HsProjection _ flds) =
Right (TH.ProjectionE (fmap (\(L _ (DotFieldOcc _ (L _ fld))) -> fieldLabelToString fld) flds))
#endif
convertExpr _ = Left "Unsupported Haskell expression form in hpgsql's SQL quasi-quoter"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: I'd recommend explicitly enumerating instead of wildcard so that when new expressions are added, you'll get alerted to it and decide if you want to support it or not

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I hope bug reports will come with the expression that failed to be parsed, but I followed your suggestion and found that the error messages we can show are much nicer when we list every language feature. Plus it really helped me see what is still unsupported, and led me to support a few more cases (and add tests as I go).

ea6c015 is a pretty good summary of what we still don't support, and I'm planning on adding support for at least let and do.

What else in that list do you think is worth supporting?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes, that's another good reason to explicitly list out branches - it forces you to make a decision whether you want to support something or not

I'd think all the TH constructs could be represented with ghc-lib constructs. Is it just a matter of doing the work or not? If so, I think what you have is fine for now, and you can always implement incrementally over time if people ask

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

It's just a matter of doing the work and maintaining it, I guess. But it would be nice to keep it relatively small - it's not that little in CPP macros and LOC, and it's not hard to float out expressions from the quasiquoter for users when necessary if they're "exotic".

"Exotic" is subjective, of course, but you and I agree on OverloadedRecordDot being nice to have, and I'm sure there's a lot more users would agree on since I expect most #{} and ^{} to be small Haskell expressions.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yep, makes sense to me. Maybe in the error message, add some detail around "define outside quasiquoter, raise an issue at github if support should be added"?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Good idea, I reworded the error message with 23a67bf

@brandonchinn178 brandonchinn178 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cool stuff!

Comment thread hpgsql/src/Hpgsql/LanguageHaskell/FromThExtension.hs Outdated
Comment thread hpgsql/src/Hpgsql/GhcParseExp.hs Outdated
@@ -0,0 +1,322 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE PackageImports #-}
{- FOURMOLU_DISABLE -}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why use fourmolu if you just disable entire files 😅

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Fourmolu is choking on some of the files with CPP macros :(

hpgsql/src/Hpgsql/GhcParseExp.hs:173:9-10
  The GHC parser (in Haddock mode) failed:
  [GHC-58481] parse error on input `<-'

I read their github, and it seems support for CPP macros is quite limited.

I was able to narrow the range of lines of code with fourmolu disabled in FromThExtension.hs, but in this file failed after a few attempts, so left the entire file unformatted because it didn't feel worth the trouble.

But I'm not very knowledgeable of fourmolu. Do you know if there's a better way of doing this?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's fine for the extensions file, since it's basically one giant pattern match. But this file is a bit more involved. I would recommend just as much as possible, break out CPP into isolated functions and only disable fourmolu for that function as a whole, instead of the entire file.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I'm not sure I'm too concerned about making this file amenable to fourmolu. At least not at the moment, and not for getting this PR to a mergeable state.

Because it's just formatting, not linting, so we're not risking any correctness problems, and because the amount of effort I might have to put in to achieve that doesn't feel worth it.

I tried pattern synonyms a little to remove some CPP macros from here, but I think they require COMPLETE pragmas to avoid producing warnings unless I replicate all the constructors (which change for each ghc-lib-parser version..), so I'm not seeing a simple way to get this file to be formattable, I'm afraid.

Comment thread hpgsql/src/Hpgsql/GhcParseExp.hs Outdated
Comment thread hpgsql/src/Hpgsql/GhcParseExp.hs Outdated
convertPat (AsPat _ (L _ rdr) _ (L _ p)) = TH.AsP (rdrToName rdr) <$> convertPat p
#endif
convertPat (BangPat _ (L _ p)) = TH.BangP <$> convertPat p
convertPat _ = Left "Unsupported pattern form in hpgsql's SQL quasi-quoter. Please file a bug report at https://github.com/mzabani/hpgsql/issues if you want this."

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Explicitly list this out too?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The only wildcard matches now are on lists, but every constructor has been explicitly laid out.

Comment thread hpgsql/src/Hpgsql/GhcParseExp.hs Outdated
Comment thread hpgsql/src/Hpgsql/GhcParseExp.hs Outdated
mzabani added 13 commits August 2, 2026 09:08
This is just Haskell syntax for data constructors checking the first
character. I understand it now.
These are literal numbers, so they're numbers as per the source code,
which of course are well described by rationals.
This shows how the quasi-quoter, when unable to parse a Haskell
expression, treats it like a fragment of SQL.

This was scary at first, but is actually fine, as valid SQL will never
have #{ or ^{ inside not a string or an identifier.

It's not great that this happens, but I'm not sure there's anything we
can do other than detecting invalid but reasonably-Haskell-looking
expressions an err on them? Best not to do anything
… good

At least on GHC 9.10 they have source line information
@mzabani
mzabani marked this pull request as ready for review August 8, 2026 00:15
@mzabani
mzabani requested a review from brandonchinn178 August 8, 2026 00:15
@mzabani

mzabani commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

@brandonchinn178 thank you for your review thus far. I have reduced the scope a little bit and decided not to cover do notation. I also went ahead and renamed some files and functions to what I hope are better names. I reviewed all my own TODOs and I think this is finally ready for a final look.

May I take a bit more of your time and ask you to take a look at this once more?

@mzabani
mzabani merged commit dae636e into master Aug 8, 2026
3 checks passed
@mzabani
mzabani deleted the ghc-lib-parser branch August 8, 2026 12:24
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.

Replace haskell-src-exts

2 participants