ast-plugin

Last updated: 2019-01-14 17:33:04 +0000

Upstream URL: git clone http://chriswarbo.net/git/ast-plugin.git

Repo

View repository

View issue tracker

Contents of README follows


Dump Haskell ASTs

This project provides a plugin for the Glasgow Haskell Compiler (GHC) which dumps out Abstract Syntax Trees (ASTs) of Haskell code to stdout.

To get these ASTs, we want to use GHC because it has features like “dependency chasing”, unique renaming, support for CPP, TemplateHaskell, etc. and it works for pretty much all Haskell code ever written (simply because most Haskell programmers don’t target anything else). Other approaches, eg. standalone parsers like haskell-src-exts, would require us to re-implement a lot of these features ourselves.

There are many ways to use GHC:

<ul> <li>The simplest is to provide commandline arguments to dump out the intermediate representations (IR), yet we would still have to parse this output in order to do anything useful with it.</li> <li>We can use the GHC API to parse, rename, typecheck, etc. any Haskell code we provide it. However, this leads to “dependency hell”, where we have to set up all of the required packages, include directories, etc. We would basically end up re-implementing Cabal.</li> <li>We can write a GHC plugin, then tell Cabal to apply it during a build. This is the approach we take, since it prevents us having to reimplement things.</li> </ul>

One disadvantage to writing GHC plugins is that they’re currently limited to using GHC’s “Core” intermediate language. This is a slightly simplified version of Haskell, so the ASTs we get don’t correspond exactly to the Haskell code written in our .hs files. There are proposals to add “source plugins” to GHC, but for now Core is often “close enough” to be useful.

Usage

See the GHC documentation for using compiler plugins. It is highly recommended that you use this plugin via Cabal2DB since that provides convenient scripts for setting up the GHC environment.