从编译regexp获取错误描述

时间:2011-12-14 14:19:41

标签: regex haskell

我正在尝试编译正则表达式并获取可以呈现给用户的错误消息。我用Text.Regex.TDFA和Text.Regex.Posix尝试了这个,它的行为似乎相同:

Prelude Text.Regex.TDFA Data.Maybe Data.Either.Utils> fromLeft $ (makeRegexM ".[" :: Either String Regex)
"*** Exception: parseRegex for Text.Regex.TDFA.String failed:".[" (line 1, column 3):
unexpected end of input
expecting "^", "]", "-" or Failed to parse bracketed string
Prelude Text.Regex.TDFA Data.Maybe Data.Either.Utils> isJust $ (makeRegexM ".[" :: Maybe Regex)
False
Prelude Text.Regex.TDFA Data.Maybe Data.Either.Utils> isJust $ (makeRegexM "." :: Maybe Regex)
True

Maybe monad似乎有效;要么没有。然而文档说,它应该使用'fail' - 据我所知,它是在Either monad中定义的。我做错了吗?

1 个答案:

答案 0 :(得分:2)

原因可能是Either e的monad实例最近发生了变化。在mtl-1.*中,曾经有过

instance Error e => Monad (Either e) where
    ...
    fail msg = Left (strMsg msg)  -- I may misremember the exact names

所以调用fail没有引起异常。现在,base(Control.Monad.Instances

中有一个monad实例
instance Monad (Either e) where
    ...
    fail s = error s  -- implicitly from the default method for fail

所以你得到了上述内容。