A documented way to differentiate between statements, declarations, imports, etc.
I'm working on a little REPL and the GHC API seems to supply two functions for evaluating one line, without compiling and evaluating into an
HValue, which doesn't seem to have an
runDecls. As it turns out, I need slightly different code to run on declarations verses statements, so this makes sense, but given a random string from the user, how can I tell the difference? I don't see anything in the documentation about this.
I took a look at ghci's source (https://github.com/ghc/ghc/blob/a6f2c852d49313fa8acea2deb3741ab86c6ef995/ghc/InteractiveUI.hs\#L862\]) and it looks like they just peek at the first word of the input, and that may be correct and sufficient, but impossible to verify without looking at Haskell's grammar. But even if I did verify it, that doesn't make explicit the difference between what
runStmt will accept.
For a contrived example, consider if GHC implemented a feature that allowed for a new type of decl. It might be enabled by a language extension which means that testing whether a string is a statement or declaration cannot be done purely as we have to check the dynflags. Furthermore, the client may not be aware of the extension and thus send the input to
It would seem to me that requiring the user to manually parse the input violates separation of concerns since truly GHC knows best what source GHC will accept. The user should not have to examine the input except for if they need to modify or interpret it as something other than Haskell source code.
The GHC API should either document an example function for differentiating declarations from statements (and even import statements), supply one itself, or supply several along the lines of
isDecl, isStmt, isImport :: String -> Bool. This would improve ease-of-use by making client code more obvious and correct.