guibou / PyF

Haskell QuasiQuoter for String Formatting

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

More error context when parsing haskell expression

guibou opened this issue · comments

Right now, when parsing haskell expression, we just dump the TH Exp in the AST and are happy with it.

However, if this code is invalid, it will fail in a later stage with an imprecise error. For example:

/home/guillaume/PyFError.hs:6:17: error:
    Variable not in scope: name2
  |
6 |   putStrLn [fmt|Bonjour {name2:o}|]
  |                 ^^^^^^^^^^^^^^^^^^^

We cannot do much, but we can at least evaluate the AST to check for missing variables, using https://hackage.haskell.org/package/template-haskell-2.18.0.0/docs/Language-Haskell-TH.html#v:lookupValueName to check for the existance of the name. I would like an error message such as:

/home/guillaume/PyFError.hs:6:40-45: error:
    Variable not in scope: name2
  |
6 |   putStrLn [fmt|Bonjour {name2:o}|]
  |                          ^^^^^

This is perfectly doable right now, either:

  • the evalExpr function may do the check and reports the errors
  • evalExpr should return the evaluated expression as TH code (i.e. Exp), as well as the original AST (HsExpr GhcPs). Later, we can walk this AST to check for errors.

Working a bit on this topic in #106 I progressively realize that:

  • Instead of failing immediately, we can gather all errors and display all of them at the end, it will give a better experience
  • I'm wondering if it may be possible to use GHC to type check the interpolated expression and based on that, do more check and do the formatter dispatch. I'll evaluate that.