summaryrefslogtreecommitdiff
path: root/Pattern.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Pattern.hs')
-rw-r--r--Pattern.hs31
1 files changed, 31 insertions, 0 deletions
diff --git a/Pattern.hs b/Pattern.hs
new file mode 100644
index 0000000..2a6007d
--- /dev/null
+++ b/Pattern.hs
@@ -0,0 +1,31 @@
+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