1module Parse.Pattern (term, expr) where
3import Control.Applicative ((<$>))
4import Data.Char (isUpper)
5import qualified Data.List as List
6import Text.Parsec ((<|>), (<?>), char, choice, optionMaybe, try)
9import qualified Parse.Literal as Literal
10import qualified AST.Literal as L
11import qualified AST.Pattern as P
12import qualified AST.Variable as Var
14basic :: IParser P.RawPattern
16 [ char '_' >> return P.Anything
17 , stringToPattern <$> var
18 , P.Literal <$> Literal.literal
23 "True" -> P.Literal (L.Boolean True)
24 "False" -> P.Literal (L.Boolean False)
25 c:_ | isUpper c -> P.Data (Var.Raw v) []
28asPattern :: P.RawPattern -> IParser P.RawPattern
30 var <- optionMaybe (try (whitespace >> reserved "as" >> whitespace >> lowVar))
32 Just v -> P.Alias v pattern
35record :: IParser P.RawPattern
36record = P.Record <$> brackets (commaSep1 lowVar)
38tuple :: IParser P.RawPattern
40 ps <- parens (commaSep expr)
45list :: IParser P.RawPattern
46list = P.list <$> braces (commaSep expr)
48term :: IParser P.RawPattern
50 (choice [ record, tuple, list, basic ]) <?> "pattern"
52patternConstructor :: IParser P.RawPattern
53patternConstructor = do
54 v <- List.intercalate "." <$> dotSep1 capVar
56 "True" -> return $ P.Literal (L.Boolean True)
57 "False" -> return $ P.Literal (L.Boolean False)
58 _ -> P.Data (Var.Raw v) <$> spacePrefix term
60expr :: IParser P.RawPattern
62 patterns <- consSep1 (patternConstructor <|> term)
63 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