1{-# OPTIONS_GHC -Wall #-}
2module AST.Pattern where
4import qualified AST.Helpers as Help
6import Text.PrettyPrint as PP
7import qualified Data.Set as Set
9import qualified AST.Variable as Var
10import AST.Literal as Literal
13 = Data var [Pattern var]
15 | Alias String (Pattern var)
18 | Literal Literal.Literal
19 deriving (Eq, Ord, Show)
21type RawPattern = Pattern Var.Raw
22type CanonicalPattern = Pattern Var.Canonical
24cons :: RawPattern -> RawPattern -> RawPattern
25cons h t = Data (Var.Raw "::") [h,t]
28nil = Data (Var.Raw "[]") []
30list :: [RawPattern] -> RawPattern
33tuple :: [RawPattern] -> RawPattern
34tuple es = Data (Var.Raw ("_Tuple" ++ show (length es))) es
36boundVarList :: Pattern var -> [String]
37boundVarList = Set.toList . boundVars
39boundVars :: Pattern var -> Set.Set String
42 Var x -> Set.singleton x
43 Alias x p -> Set.insert x (boundVars p)
44 Data _ ps -> Set.unions (map boundVars ps)
45 Record fields -> Set.fromList fields
47 Literal _ -> Set.empty
50instance Var.ToString var => Pretty (Pattern var) where
54 Literal lit -> pretty lit
55 Record fs -> PP.braces (commaCat $ map variable fs)
56 Alias x p -> prettyParens p <+> PP.text "as" <+> variable x
57 Anything -> PP.text "_"
58 Data name [hd,tl] | Var.toString name == "::" ->
59 parensIf isCons (pretty hd) <+> PP.text "::" <+> pretty tl
62 Data ctor _ -> Var.toString ctor == "::"
66 | Help.isTuple name' -> PP.parens . commaCat $ map pretty ps
67 | otherwise -> hsep (PP.text name' : map prettyParens ps)
69 name' = Var.toString name
71prettyParens :: Var.ToString var => Pattern var -> Doc
73 parensIf needsThem (pretty pattern)
77 Data name (_:_) | not (Help.isTuple (Var.toString name)) -> True
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