Show custom errors while parsing Happy Haskell

五迷三道 提交于 2019-12-12 15:13:24

问题


I'm writing a monadic parser using Alex and Happy in Haskell.

My error function is defined like this:

parseError :: Token -> Alex a
parseError _ = alexError "error occurred"

How can I send custom errors (like incorrect type while trying to add a string to a number) during parsing?


UPDATE

The parser doesn't need to do the type checking, I'm doing it inside the production since I keep track of the operands type. As said in a comment, I cannot use the parseError, so is there a way to print an error and stop the parser?


回答1:


I've solved it by implementing this function:

fatalError :: (Show a1, Show a) => [Char] -> a -> a1 -> t
fatalError s l c = error ("Error at line " ++ (show l) ++ " column " ++ (show c) ++ ": " ++ s)

and I call it from the production when an error is detected



来源:https://stackoverflow.com/questions/37059704/show-custom-errors-while-parsing-happy-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!