summaryrefslogtreecommitdiff
path: root/Pattern.hs
blob: 2a6007d8bfe24055cd8ff3f2991beff92a696b6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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