Verified Commit 0d974e7e authored by Camil Staps's avatar Camil Staps 🙂

Overload compilation; add compile function which just returns a Regex

parent 63433afd
......@@ -4,4 +4,18 @@ from Data.Error import :: MaybeErrorString, :: MaybeError
from Regex import :: Regex
compile :: [Char] -> MaybeErrorString Regex
class toRegexInput a :: a -> [Char]
instance toRegexInput [Char]
instance toRegexInput String
/**
* Compile a regular expression.
*/
compile :: a -> MaybeErrorString Regex | toRegexInput a
/**
* Alternative to {{`compile`}} for hard-coded regular expressions.
* It gives a runtime error if {{`compile`}} fails with {{`Error`}}.
*/
regex :: a -> Regex | toRegexInput a
......@@ -28,8 +28,14 @@ where
(<|>) (Error _) r = r
(<|>) l _ = l
compile :: [Char] -> MaybeErrorString Regex
compile cs = compile` 0 [] cs
instance toRegexInput [Char] where toRegexInput cs = cs
instance toRegexInput String where toRegexInput s = [c \\ c <-: s]
regex :: a -> Regex | toRegexInput a
regex s = fromOk $ compile s
compile :: a -> MaybeErrorString Regex | toRegexInput a
compile cs = compile` 0 [] (toRegexInput cs)
where
compile` :: Int [Regex] [Char] -> MaybeErrorString Regex
compile` _ [] [] = Ok $ Concat []
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment