<div dir="ltr"><div>Hello Cafe,</div><div><br></div><div>I need an advice on how to use GHC API to parse big collections of Haskell source files.<br></div><div>Say, I want to collect ASTs of everything that is on Hackage. <br></div><div>I downloaded the whole Hackage (latest versions only) and have it locally now.</div><div>I tried simple advice found in the Parse module documentation:</div><div><a href="https://hackage.haskell.org/package/ghc-8.6.5/docs/Parser.html">https://hackage.haskell.org/package/ghc-8.6.5/docs/Parser.html</a></div><div><pre> runParser :: DynFlags -> String -> P a -> ParseResult a
runParser flags str parser = unP parser parseState
where
filename = "<interactive>"
location = mkRealSrcLoc (mkFastString filename) 1 1
buffer = stringToStringBuffer str
parseState = mkPState flags buffer location</pre></div><div>It mostly works: 75% of .hs files on Hackage seem to parse fine. I looked into the rest 25%</div><div>and noticed that this snippet can't handle files using GHC extensions such as RankNTypes,</div><div>TemplateHaskell, BangPatterns, etc.when given the default DynFlags. This leads me to the question of how should I initialize DynFlags?</div><div><br></div><div>Currently, I use this for getting DynFlags:</div><div><pre>initDflags :: IO DynFlags
initDflags = do
let ldir = Just libdir
mySettings <- initSysTools ldir
myLlvmConfig <- initLlvmConfig ldir
initDynFlags (defaultDynFlags mySettings myLlvmConfig)</pre></div><div></div><div>I understand that simple parsing of individual files can't take into account extensions activated inside .cabal files. But I'd expect that it should be possible to, at least, consider the extensions mentioned in the LANGUAGE pragmas. Currently, this isn't happening. Any suggestions on how to achieve this are welcomed. I probably won't get to parsing 100% of Hackage, but I'd hope for better than the current 75%.</div><div><br></div><div>--</div><div>Best wishes,</div><div>Artem<br></div></div>