<div dir="ltr"><div>Looks like exactly what I need, many thanks!</div><div><br></div><div>--</div><div>Best, Artem<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 15 Nov 2019 at 12:16, Matthew Pickering <<a href="mailto:matthewtpickering@gmail.com">matthewtpickering@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Artem,<br>
<br>
You can look at these functions from ghc-exactprint for inspiration.<br>
<br>
<a href="http://hackage.haskell.org/package/ghc-exactprint-0.6.2/docs/Language-Haskell-GHC-ExactPrint-Parsers.html" rel="noreferrer" target="_blank">http://hackage.haskell.org/package/ghc-exactprint-0.6.2/docs/Language-Haskell-GHC-ExactPrint-Parsers.html</a><br>
<br>
Cheers,<br>
<br>
Matt<br>
<br>
On Fri, Nov 15, 2019 at 4:52 PM Artem Pelenitsyn <<a href="mailto:a.pelenitsyn@gmail.com" target="_blank">a.pelenitsyn@gmail.com</a>> wrote:<br>
><br>
> Hello Cafe,<br>
><br>
> I need an advice on how to use GHC API to parse big collections of Haskell source files.<br>
> Say, I want to collect ASTs of everything that is on Hackage.<br>
> I downloaded the whole Hackage (latest versions only) and have it locally now.<br>
> I tried simple advice found in the Parse module documentation:<br>
> <a href="https://hackage.haskell.org/package/ghc-8.6.5/docs/Parser.html" rel="noreferrer" target="_blank">https://hackage.haskell.org/package/ghc-8.6.5/docs/Parser.html</a><br>
><br>
> runParser :: DynFlags -> String -> P a -> ParseResult a<br>
> runParser flags str parser = unP parser parseState<br>
> where<br>
> filename = "<interactive>"<br>
> location = mkRealSrcLoc (mkFastString filename) 1 1<br>
> buffer = stringToStringBuffer str<br>
> parseState = mkPState flags buffer location<br>
><br>
> It mostly works: 75% of .hs files on Hackage seem to parse fine. I looked into the rest 25%<br>
> and noticed that this snippet can't handle files using GHC extensions such as RankNTypes,<br>
> TemplateHaskell, BangPatterns, etc.when given the default DynFlags. This leads me to the question of how should I initialize DynFlags?<br>
><br>
> Currently, I use this for getting DynFlags:<br>
><br>
> initDflags :: IO DynFlags<br>
> initDflags = do<br>
> let ldir = Just libdir<br>
> mySettings <- initSysTools ldir<br>
> myLlvmConfig <- initLlvmConfig ldir<br>
> initDynFlags (defaultDynFlags mySettings myLlvmConfig)<br>
><br>
> 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%.<br>
><br>
> --<br>
> Best wishes,<br>
> Artem<br>
> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> To (un)subscribe, modify options or view archives go to:<br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
> Only members subscribed via the mailman list are allowed to post.<br>
</blockquote></div>