-
Notifications
You must be signed in to change notification settings - Fork 3
Replace haskell-src-exts with ghc-lib-parser #39
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
Draft
mzabani
wants to merge
6
commits into
master
Choose a base branch
from
ghc-lib-parser
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
40ab728
Claude written ghc-lib-parser usage
mzabani 28f1a09
First round of hardening and self-review
mzabani ae35095
Another question and an `if` expression
mzabani 42ea3fe
Tighten version bounds
mzabani 0269fa1
Support GHC 9.8
mzabani 2b45ccc
Keep only partial record in module with disabled warning, appease hlint
mzabani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| {-# LANGUAGE CPP #-} | ||
| {-# LANGUAGE PackageImports #-} | ||
|
|
||
| module Hpgsql.GhcParseExp (parseExp, canParseExp) where | ||
|
|
||
| import Data.Char (isUpper) | ||
| import Data.Either (isRight) | ||
| import GHC.Data.FastString (mkFastString, unpackFS) | ||
| import GHC.Data.StringBuffer (stringToStringBuffer) | ||
| import GHC.Driver.Config.Parser (initParserOpts) | ||
| import GHC.Driver.Session (DynFlags, defaultDynFlags, xopt_set) | ||
| import GHC.Hs hiding (UnicodeSyntax) | ||
| import GHC.LanguageExtensions (Extension (..)) | ||
| import GHC.Parser (parseExpression) | ||
| import GHC.Parser.Lexer (P (..), ParseResult (..), initParserState) | ||
| import GHC.Parser.PostProcess (ECP (..), runPV) | ||
| import GHC.Types.Basic (Boxity (..)) | ||
| import GHC.Types.Name.Occurrence (occNameString) | ||
| import GHC.Types.Name.Reader (RdrName (..)) | ||
| import GHC.Types.SourceText (IntegralLit (..), rationalFromFractionalLit) | ||
| import GHC.Types.SrcLoc (GenLocated (..), mkRealSrcLoc) | ||
| import Hpgsql.GhcParserOpts (fakeSettings) | ||
| import Language.Haskell.Syntax.Basic (FieldLabelString (..)) | ||
| import qualified "template-haskell" Language.Haskell.TH as TH | ||
|
|
||
| -- TODO: How about source locations/lines? Do we need them? | ||
|
|
||
| -- | Parse a Haskell expression string into a Template Haskell Exp. | ||
| -- Drop-in replacement for Language.Haskell.Meta.Parse.parseExp. | ||
| parseExp :: String -> Either String TH.Exp | ||
| parseExp str = do | ||
| hsExpr <- ghcParse str | ||
| convertExpr hsExpr | ||
|
|
||
| -- | Check if a string can be parsed as a Haskell expression. | ||
| -- This only checks parsing validity; it does not convert to TH. | ||
| canParseExp :: String -> Bool | ||
| canParseExp = isRight . ghcParse | ||
|
|
||
| ghcParse :: String -> Either String (HsExpr GhcPs) | ||
| ghcParse str = | ||
| let buf = stringToStringBuffer str | ||
| loc = mkRealSrcLoc (mkFastString "<hpgsql>") 1 1 | ||
| opts = initParserOpts parserDynFlags | ||
| parseExprP = parseExpression >>= \ecp -> runPV (unECP ecp) | ||
| in case unP parseExprP (initParserState opts buf loc) of | ||
| POk _ (L _ expr) -> Right expr | ||
| PFailed _ -> Left "Failed to parse Haskell expression" | ||
| where | ||
| parserDynFlags :: DynFlags | ||
| parserDynFlags = | ||
| foldl | ||
| xopt_set | ||
| (defaultDynFlags fakeSettings) | ||
| [ OverloadedStrings, | ||
| OverloadedRecordDot, | ||
| TupleSections, | ||
| LambdaCase, | ||
| MultiWayIf, | ||
| PostfixOperators, | ||
| QuasiQuotes, | ||
| UnicodeSyntax, | ||
| MagicHash, | ||
| ForeignFunctionInterface, | ||
| TemplateHaskell, | ||
| RankNTypes, | ||
| MultiParamTypeClasses, | ||
| RecursiveDo, | ||
| TypeApplications | ||
| ] | ||
|
|
||
| -- GHC HsExpr to TH Exp conversion | ||
|
|
||
| convertExpr :: HsExpr GhcPs -> Either String TH.Exp | ||
| convertExpr (HsVar _ (L _ rdr)) = Right (rdrToExp rdr) | ||
| convertExpr (HsApp _ (L _ f) (L _ x)) = TH.AppE <$> convertExpr f <*> convertExpr x | ||
| convertExpr (OpApp _ (L _ l) (L _ op) (L _ r)) = do | ||
| l' <- convertExpr l | ||
| op' <- convertExpr op | ||
| r' <- convertExpr r | ||
| Right (TH.UInfixE l' op' r') | ||
| convertExpr (NegApp _ (L _ e) _) = do | ||
| e' <- convertExpr e | ||
| Right $ TH.AppE (TH.VarE 'negate) e' | ||
|
|
||
| #if MIN_VERSION_ghc_lib_parser(9,10,0) | ||
| convertExpr (HsPar _ (L _ e)) = TH.ParensE <$> convertExpr e | ||
| #elif MIN_VERSION_ghc_lib_parser(9,8,0) | ||
| convertExpr (HsPar _ _ (L _ e) _) = TH.ParensE <$> convertExpr e | ||
| #endif | ||
| convertExpr (ExplicitList _ es) = TH.ListE <$> traverse (\(L _ e) -> convertExpr e) es | ||
| convertExpr (ExplicitTuple _ args boxity) = do | ||
| args' <- traverse convertTupArg args | ||
| Right | ||
| ( case boxity of | ||
| Boxed -> TH.TupE args' | ||
| Unboxed -> TH.UnboxedTupE args' | ||
| ) | ||
| convertExpr (SectionL _ (L _ e) (L _ op)) = do | ||
| e' <- convertExpr e | ||
| op' <- convertExpr op | ||
| Right (TH.InfixE (Just e') op' Nothing) | ||
| convertExpr (SectionR _ (L _ op) (L _ e)) = do | ||
| op' <- convertExpr op | ||
| e' <- convertExpr e | ||
| Right (TH.InfixE Nothing op' (Just e')) | ||
| convertExpr (HsIf _ (L _ c) (L _ t) (L _ f)) = do | ||
| c' <- convertExpr c | ||
| t' <- convertExpr t | ||
| f' <- convertExpr f | ||
| Right (TH.CondE c' t' f') | ||
| convertExpr (HsLit _ lit) = TH.LitE <$> convertHsLit lit | ||
| convertExpr (HsOverLit _ ol) = convertOverLit ol | ||
| convertExpr (ExprWithTySig _ (L _ e) sigWcTy) = do | ||
| e' <- convertExpr e | ||
| ty' <- convertSigWcType sigWcTy | ||
| Right (TH.SigE e' ty') | ||
| convertExpr (HsGetField _ (L _ e) (L _ (DotFieldOcc _ (L _ fld)))) = do | ||
| e' <- convertExpr e | ||
| Right (TH.GetFieldE e' (fieldLabelToString fld)) | ||
| #if MIN_VERSION_ghc_lib_parser(9,10,0) | ||
| convertExpr (HsProjection _ flds) = | ||
| Right (TH.ProjectionE (fmap (\(DotFieldOcc _ (L _ fld)) -> fieldLabelToString fld) flds)) | ||
| #elif MIN_VERSION_ghc_lib_parser(9,8,0) | ||
| 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" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| -- Helper functions | ||
|
|
||
| rdrToExp :: RdrName -> TH.Exp | ||
| rdrToExp rdr = | ||
| let name = rdrToName rdr | ||
| in if isConName name then TH.ConE name else TH.VarE name | ||
|
|
||
| rdrToName :: RdrName -> TH.Name | ||
| rdrToName (Unqual occ) = TH.mkName (occNameString occ) | ||
| rdrToName (Qual modN occ) = TH.mkName (moduleNameString modN ++ "." ++ occNameString occ) | ||
| rdrToName _ = TH.mkName "<unknown-name>" | ||
|
|
||
| isConName :: TH.Name -> Bool | ||
| isConName n = case TH.nameBase n of | ||
| -- TODO: No module name check? | ||
| (c : _) -> isUpper c || c == ':' | ||
| _ -> False | ||
|
|
||
| fieldLabelToString :: FieldLabelString -> String | ||
| fieldLabelToString (FieldLabelString fs) = unpackFS fs | ||
|
|
||
| convertTupArg :: HsTupArg GhcPs -> Either String (Maybe TH.Exp) | ||
| convertTupArg (Present _ (L _ e)) = Just <$> convertExpr e | ||
| convertTupArg (Missing _) = Right Nothing | ||
|
|
||
| convertHsLit :: HsLit GhcPs -> Either String TH.Lit | ||
| convertHsLit (HsChar _ c) = Right (TH.CharL c) | ||
| convertHsLit (HsString _ fs) = Right (TH.StringL (unpackFS fs)) | ||
| convertHsLit (HsInt _ il) = Right (TH.IntegerL (il_value il)) | ||
| convertHsLit (HsIntPrim _ i) = Right (TH.IntPrimL i) | ||
| convertHsLit (HsWordPrim _ w) = Right (TH.WordPrimL w) | ||
| convertHsLit (HsFloatPrim _ fl) = Right (TH.FloatPrimL (rationalFromFractionalLit fl)) -- TODO Why rational? | ||
| convertHsLit (HsDoublePrim _ fl) = Right (TH.DoublePrimL (rationalFromFractionalLit fl)) | ||
| convertHsLit _ = Left "Unsupported literal type in SQL quasi-quoter" | ||
|
|
||
| convertOverLit :: HsOverLit GhcPs -> Either String TH.Exp | ||
| convertOverLit ol = case ol_val ol of | ||
| HsIntegral il -> Right (TH.LitE (TH.IntegerL (il_value il))) | ||
| HsFractional fl -> Right (TH.LitE (TH.RationalL (rationalFromFractionalLit fl))) | ||
| HsIsString _ fs -> Right (TH.LitE (TH.StringL (unpackFS fs))) | ||
|
|
||
| -- Type conversion (GHC HsType to TH Type) | ||
|
|
||
| convertSigWcType :: LHsSigWcType GhcPs -> Either String TH.Type | ||
| convertSigWcType (HsWC _ (L _ (HsSig _ _ (L _ ty)))) = convertType ty | ||
|
|
||
| convertType :: HsType GhcPs -> Either String TH.Type | ||
| convertType (HsTyVar _ promo (L _ rdr)) = | ||
| let name = rdrToName rdr | ||
| in Right $ case promo of | ||
| IsPromoted -> TH.PromotedT name | ||
| NotPromoted | ||
| | isConName name -> TH.ConT name | ||
| | otherwise -> TH.VarT name | ||
| convertType (HsAppTy _ (L _ t1) (L _ t2)) = | ||
| TH.AppT <$> convertType t1 <*> convertType t2 | ||
| convertType (HsListTy _ (L _ t)) = | ||
| TH.AppT TH.ListT <$> convertType t | ||
| convertType (HsTupleTy _ _ ts) = do | ||
| ts' <- traverse (\(L _ t) -> convertType t) ts | ||
| let n = length ts' | ||
| Right (foldl TH.AppT (TH.TupleT n) ts') | ||
| convertType (HsFunTy _ _ (L _ t1) (L _ t2)) = | ||
| TH.AppT . TH.AppT TH.ArrowT <$> convertType t1 <*> convertType t2 | ||
| convertType (HsParTy _ (L _ t)) = | ||
| convertType t | ||
| convertType (HsQualTy _ _ (L _ t)) = | ||
| convertType t | ||
| convertType _ = Left "Unsupported type in SQL quasi-quoter type signature" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| {-# OPTIONS_GHC -Wno-missing-fields #-} | ||
|
|
||
| module Hpgsql.GhcParserOpts (fakeSettings) where | ||
|
|
||
| import GHC.Platform (genericPlatform) | ||
| import GHC.Settings | ||
| import GHC.Settings.Config (cProjectVersion) | ||
| import GHC.Utils.Fingerprint (fingerprint0) | ||
|
|
||
| -- | Fake GHC 'Settings' with only the fields the parser needs. | ||
| -- All other fields are left undefined; this is why we suppress | ||
| -- the missing-fields warning for this module only. | ||
| fakeSettings :: Settings | ||
| fakeSettings = | ||
| Settings | ||
| { sGhcNameVersion = GhcNameVersion "ghc" cProjectVersion, | ||
| sFileSettings = FileSettings {}, | ||
| sTargetPlatform = genericPlatform, | ||
| sPlatformMisc = PlatformMisc {}, | ||
| sToolSettings = ToolSettings {toolSettings_opt_P_fingerprint = fingerprint0} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be able to get the currently active extensions in the QuasiQuoter with extsEnabled and pass them through to here