module Pattern where import Data.Char import Data.Text.Lazy (Text) import qualified Data.Text.Lazy as T data Pattern = Pattern Text [Origin] deriving (Show) data Origin = OCopy | ONew Text deriving (Show) parsePattern :: String -> Either String Pattern parsePattern str = do (constr, args) <- case words str of [] -> Left "Empty match pattern" constr : args -> return (constr, args) args' <- traverse parseArg args return (Pattern (T.pack constr) args') where parseArg :: String -> Either String Origin parseArg ('*':name) | isValid name = Right (ONew (T.pack name)) | otherwise = Left ("Invalid type param name '" ++ name ++ "'") parseArg name | isValid name = Right OCopy | otherwise = Left ("Invalid type param name '" ++ name ++ "'") isValid :: String -> Bool isValid "" = False isValid (hd:cs) = isLower hd && all (\c -> isAlpha c || c `elem` "'_") cs