简体   繁体   English

解析Happy Haskell时显示自定义错误

[英]Show custom errors while parsing Happy Haskell

I'm writing a monadic parser using Alex and Happy in Haskell. 我在Haskell中使用Alex和Happy编写了一个monadic解析器。

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 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? 正如在评论中所说,我不能使用parseError ,所以有没有办法打印错误并停止解析器?

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 当检测到错误时,我从生产中调用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM