Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 Haskell 中的 Alex 和 Happy 编写一个单子解析器。
我的错误函数定义如下:
parseError :: Token -> Alex a parseError _ = alexError "error occurred"
如何在解析期间发送自定义错误(例如尝试将字符串添加到数字时的类型不正确)?
更新
解析器不需要进行类型检查,我在生产中进行检查,因为我跟踪操作数类型。正如评论中所说,我不能使用parseError,那么有没有办法打印错误并停止解析器?
parseError
我通过实现这个功能解决了这个问题:
fatalError :: (Show a1, Show a) => [Char] -> a -> a1 -> t fatalError s l c = error ("Error at line " ++ (show l) ++ " column " ++ (show c) ++ ": " ++ s)
当检测到错误时,我从生产中调用它