在解析Happy Haskell时显示自定义错误 [英] Show custom errors while parsing Happy Haskell

查看:145
本文介绍了在解析Happy Haskell时显示自定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的错误函数是这样定义的:

  parseError :: Token  - > Alex a 
parseError _ = alexError发生错误

如何发送自定义错误(像尝试向数字中添加字符串时的类型不正确)?




更新



解析器不需要进行类型检查,因为我跟踪了操作数的类型,所以我在生产中进行了检查。
正如评论中所说,我不能使用 parseError ,所以有没有办法打印错误并停止解析器?

解决方案

我已经通过实现这个函数解决了它:

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

我在检测到错误时从产品中调用它

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?

解决方案

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

这篇关于在解析Happy Haskell时显示自定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆