Created
July 29, 2021 14:17
-
-
Save amesgen/1dbcaf1557431571e3e52cec4fd6c332 to your computer and use it in GitHub Desktop.
minimal AST dumping utility for GHC 9.2
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
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import GHC.Data.Bag | |
import qualified GHC.Data.EnumSet as EnumSet | |
import GHC.Data.StringBuffer | |
import GHC.Hs.Dump | |
import GHC.Parser | |
import GHC.Parser.Errors.Ppr | |
import GHC.Parser.Lexer | |
import GHC.Types.SrcLoc | |
import GHC.Utils.Outputable | |
import GHC.Utils.Ppr | |
import System.IO | |
main :: IO () | |
main = do | |
src <- stringToStringBuffer <$> readFile "Test.hs" | |
let enabledWarnings = EnumSet.empty | |
enabledExtensions = EnumSet.empty | |
safeImports = False | |
haddockComments = True | |
regularComments = True | |
respectLineColumnPragmas = True | |
parserOpts = | |
mkParserOpts | |
enabledWarnings | |
enabledExtensions | |
safeImports | |
haddockComments | |
regularComments | |
respectLineColumnPragmas | |
srcLoc = mkRealSrcLoc "<input>" 1 1 | |
pState = initParserState parserOpts src srcLoc | |
parseResult = unP parseModule pState | |
case parseResult of | |
PFailed pState -> printErrorsWarnings pState | |
POk pState hsMod -> do | |
printErrorsWarnings pState | |
printSDoc' stdout $ showAstData NoBlankSrcSpan NoBlankEpAnnotations hsMod | |
where | |
printSDoc' = printSDocLn defaultSDocContext (PageMode True) | |
printErrorsWarnings pState = do | |
let (warnings, errors) = getMessages pState | |
msgBag = (pprWarning <$> warnings) `unionBags` (pprError <$> errors) | |
hPutStr stderr . show . bagToList $ msgBag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment