show a clearer error message on encountering a malformed regexp

This commit is contained in:
Simon Michael 2020-05-07 17:15:24 -07:00
parent 647a77225d
commit 3ef2fc9567

View File

@ -50,13 +50,13 @@ where
import Data.Array import Data.Array
import Data.Char import Data.Char
import Data.List (foldl') import Data.List (foldl')
import Data.Maybe (fromMaybe)
import Data.MemoUgly (memo) import Data.MemoUgly (memo)
import Text.Regex.TDFA ( import Text.Regex.TDFA (
Regex, CompOption(..), ExecOption(..), defaultCompOpt, defaultExecOpt, Regex, CompOption(..), ExecOption(..), defaultCompOpt, defaultExecOpt,
makeRegexOpts, AllMatches(getAllMatches), match, (=~), MatchText makeRegexOptsM, AllMatches(getAllMatches), match, (=~), MatchText
) )
-- import Hledger.Utils.Debug
import Hledger.Utils.UTF8IOCompat (error') import Hledger.Utils.UTF8IOCompat (error')
@ -66,19 +66,20 @@ type Regexp = String
-- | A replacement pattern. May include numeric backreferences (\N). -- | A replacement pattern. May include numeric backreferences (\N).
type Replacement = String type Replacement = String
-- | Convert our string-based regexps to real ones. Can fail if the -- | Convert our string-based Regexp to a real Regex.
-- string regexp is malformed. -- Or if it's not well formed, call error with a "malformed regexp" message.
toRegex :: Regexp -> Regex toRegex :: Regexp -> Regex
toRegex = memo (makeRegexOpts compOpt execOpt) toRegex = memo (compileRegexOrError defaultCompOpt defaultExecOpt)
-- | Like toRegex but make a case-insensitive Regex.
toRegexCI :: Regexp -> Regex toRegexCI :: Regexp -> Regex
toRegexCI = memo (makeRegexOpts compOpt{caseSensitive=False} execOpt) toRegexCI = memo (compileRegexOrError defaultCompOpt{caseSensitive=False} defaultExecOpt)
compOpt :: CompOption compileRegexOrError :: CompOption -> ExecOption -> Regexp -> Regex
compOpt = defaultCompOpt compileRegexOrError compopt execopt r =
fromMaybe
execOpt :: ExecOption (errorWithoutStackTrace $ "this regular expression could not be compiled: " ++ show r) $
execOpt = defaultExecOpt makeRegexOptsM compopt execopt r
-- regexMatch' :: RegexContext Regexp String a => Regexp -> String -> a -- regexMatch' :: RegexContext Regexp String a => Regexp -> String -> a
-- regexMatch' r s = s =~ (toRegex r) -- regexMatch' r s = s =~ (toRegex r)