[Haskell-cafe] Locate errors in TH for Haskell code

Michal J Gajda mgajda at mimuw.edu.pl
Sun Nov 7 04:47:51 UTC 2021


Dear Anton,

> I have a task to convert one Haskell expression to another.
> And I'd like to report errors properly.
>
> [interpret|   -- error reported here
>   valid haskell code
>   invalid code           <- error
> |]
>
> I can parse it with haskell-src-exts or haskell-src-meta.
> but if I have error in the code (type-check) error is positioned on the
> first line of the QQ-expression.
>
> Do you know is it possible to report error at the line where it has
> happened or
> to keep original source code location in the Exp.
>
> In the function I do transformation Exp -> Exp.
>
> Or maybe there is a better way to do it instead of QQ?

QQ is a good way.

The simplest way would be to use `Language.Haskell.TH.location :: Q
Loc` [1] and update it with the `Language.Haskell.Exts.SrcLoc.Loc` you
get from `haskell-src-exts`[2].
[1] https://hackage.haskell.org/package/template-haskell-2.18.0.0/docs/Language-Haskell-TH.html#v:location
[2] https://hackage.haskell.org/package/haskell-src-exts-1.23.1/docs/Language-Haskell-Exts-SrcLoc.html

Note that [1] is available since template-haskell version 2.8.0.0.
-- 
  Best regards
    MichaƂ


More information about the Haskell-Cafe mailing list