Handle Regex syntax errors?
sharkdp opened this issue ยท 5 comments
Currently, the function
regex :: String -> RegexFlags -> Regexfails with a runtime error if the Regex has a syntax error:
Uncaught SyntaxError: Invalid regular expression: /+/: Nothing to repeat
or if one of the flags is not supported ('y' is not supported in Chrome, for example):
Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'y'
I'm not sure if the design goal of PureScript libraries is to avoid runtime errors at all costs, but if this is the case, I would suggest modifying the type signature to return Maybe Regex or Either Error Regex. Of course, most of the time a Regex will be hard-coded and this might not be such a big issue.
(found this bug while playing with FlareCheck ๐)
Sounds sensible to me ๐
Is there any way we could make this a compile-time error? If not, is this the sort of thing Idris enables?
Yes, we could take global out of the flags object, or use a type argument and Void that field out in the types of safe functions.
Ah right, yes ๐
I think https://github.com/VerbalExpressions/purescript-verbal-expressions is the compile-time safe version, no?