Skip to content

Warbo/ast-plugin

Repository files navigation

# 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:

 - 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.
 - 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.
 - 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.

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](https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/compiler-plugins.html)
for using compiler plugins. It is highly recommended that you use this plugin
via [Cabal2DB](http://chriswarbo.net/essays/repos/cabal2db.html) since that
provides convenient scripts for setting up the GHC environment.