go-luapatterns
go-luapatterns copied to clipboard
Compile patterns, make search/replace methods on pattern object
To create uniformity with the regexp package and to keep from parsing a commonly used pattern each time it's used, it would make a great deal of since to have a function that compiled patterns into some kind of struct that had Find/Replace/etc methods on it.
This solves a problem that I was having elsewhere, but is going to require a significant code change and I'm not sure what the best way to manage it is.
There are a few places in the code (marked with a comment beginning with error:) where an invalid pattern is found. Right now, I just ensure these patterns return a nil result but there's no way to propagate an error message out to the user other than by using panic(), which I'm not happy with.
A common idiom is to recover() the panic at the library boundary, in this case in the hypothetical Compile function and return the panic'd value as error. that way you could, say, replace //error: invalid capture index return nil with panic(InvalidCaptureIndex)
The issue is that there is no 'compile', and adding one requires a 100% fundamental change to the way the library is written. These errors are not encountered until a match is actually attempted, making it very difficult.