elm/core

Regex, runtime error

Closed this issue · 10 comments

In core package, module Regex

regex : String -> Regex

This function can produce runtime error if string contain invalid pattern.

Here is an example form elm-repl

b = Regex.regex("[a-z")
SyntaxError: Invalid regular expression: /[a-z/: Unterminated character class

It would be better if we change regex to be

regex : String -> Maybe Regex

it will return Nothing if string contains invalid pattern.

I think it makes sense for regex to be regex : String -> Result String Regex here, so that the error message is captured.
If it's okay, I'll make a pull request for this so that it can be merged if it's approved.

Yeah, it's either that or keep the runtime error. Most regexes won't be generated dynamically (in signals or functions), so there's a case for keeping the runtime error since it will be easier to work with the absence of a Result or Maybe. (We seem to be getting useful error messages, so I support a Result in the API.)

In the spirit of these guidelines I've made a gist for a potentially interesting solution to this. Any discussion of that idea is welcome over on the gist itself, but let's not derail this issue with that stuff. 😄

Is this "the same" as https://github.com/elm-lang/core/issues/157? Should we close that other issue, then?

I've created #380 that will change regex to return a Result instead, but I think it needs further discussion before merging. Having to use case..of everywhere doesn't quite fit right.

Perhaps leaving regex as it's own function and introducing safeRegex : String -> Result String Regex might be a better solution, with regex intended for static expressions and safeRegex for dynamic ones.

This would remove the need to introduce a new syntax as per rtfeldman's gist, though leave a runtime error possible.

Regex literals might be a solution. That way invalid regexps are a compile time error

That's been brought up before, and I agree would be a good solution. But it involves a compiler change, and you'd have to validate JS regexes in Haskell.

tmcw commented

For my (admittedly rare) usecase, I'm creating regular expressions based on user input, in order to filter a list and extract results with matching groups - so unfortunately a literal syntax only would make that usecase more difficult - right now I'm porting from a JavaScript version which uses new RegExp(variable) in order to create regular expressions on the fly.

Returning a Maybe Regex would fit my usage perfectly, though I understand if that's not the most common way people interact with regular expressions.

Consolidated regex stuff in #722. Follow along there!

The plan is to move regular expressions to elm-lang/regexp or something, and it will have something like this:

fromString : String -> Result String RegExp

As seen here: https://github.com/evancz/regexp/blob/master/src/RegExp.elm#L44