[Haskell-cafe] source line annotations

Evan Laforge qdunkan at gmail.com
Wed Jan 19 23:43:49 CET 2011

One of the first things I did when starting a larger haskell project
was to write a preprocessor that would replace certain tokens with
(token_srcpos (filename, func_name, lineno)) and then write x and
x_srcpos versions of the various 'throw' and logging functions.  This
is extremely handy to have, and since logs are part of my app's UI,
it's part of the app itself.

I found out later that jhc has a pragma that basically does the same
thing, only built in.  My preprocessor works but has some problems.
Namely, replacing non-qualified tokens requires parsing the source
because you want to replace only function calls, not declarations,
import / export lists, etc.  I get around this because I always import
qualified, but still I need to do some hacking to get qualified tokens
('lex' mysteriously doesn't understand them... aren't they in haskell
2010 now?) and not screw up on odd corners like backslash string
continuations.  Then I start wanting to e.g. replace 'throw' in the
same module it's defined in, which really lets the dragons out because
now I'm trying to replace unqualified names.

Another problem is that the names to replace are hardcoded in the
preprocessor.  Not a problem for me, but would be for a general tool.

My preprocessor works well, but occasionally I do have to go in and
fix yet another odd corner that came up.  Initially I thought I would
only simplistically replace tokens and avoid using a full syntax
parser, but to really do it correctly a full parser is needed.  And of
course this is specific to my style (qualified imports) and project.
To do this in full generality really requires the compiler's help.
But as I use it now, it's been extremely useful, especially for
exceptions which may not carry a unique or easily greppable msg.  Or
any msg at all.

Are there any more sophisticated tools out there that do this?
Interest in extending ghc to support SRCLOC_ANNOTATE?  Reasons why I
don't actually want this after all?

More information about the Haskell-Cafe mailing list