2module Parse.Pattern (term, expr) where
4import Control.Applicative ((<$>))
5import Data.Char (isUpper)
6import qualified Data.List as List
7import Text.Parsec ((<|>), (<?>), char, choice, optionMaybe, try)
10import qualified Parse.Literal as Literal
11import qualified AST.Literal as L
12import qualified AST.Pattern as P
13import qualified AST.Variable as Var
15basic :: IParser P.RawPattern
17 [ char '_' >> return P.Anything
18 , stringToPattern <$> var
19 , P.Literal <$> Literal.literal
24 "True" -> P.Literal (L.Boolean True)
25 "False" -> P.Literal (L.Boolean False)
26 c:_ | isUpper c -> P.Data (Var.Raw v) []
29asPattern :: P.RawPattern -> IParser P.RawPattern
31 var <- optionMaybe (try (whitespace >> reserved "as" >> whitespace >> lowVar))
33 Just v -> P.Alias v pattern
36record :: IParser P.RawPattern
37record = P.Record <$> brackets (commaSep1 lowVar)
39tuple :: IParser P.RawPattern
41 ps <- parens (commaSep expr)
46list :: IParser P.RawPattern
47list = P.list <$> braces (commaSep expr)
49term :: IParser P.RawPattern
51 (choice [ record, tuple, list, basic ]) <?> "pattern"
53patternConstructor :: IParser P.RawPattern
54patternConstructor = do
55 v <- List.intercalate "." <$> dotSep1 capVar
57 "True" -> return $ P.Literal (L.Boolean True)
58 "False" -> return $ P.Literal (L.Boolean False)
59 _ -> P.Data (Var.Raw v) <$> spacePrefix term
61expr :: IParser P.RawPattern
63 patterns <- consSep1 (patternConstructor <|> term)
64 asPattern (foldr1 P.cons patterns) <?> "pattern"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list