1module SourceSyntax.Pattern where
3import Data.List (intercalate)
4import SourceSyntax.Helpers as Help
5import SourceSyntax.PrettyPrint
6import Text.PrettyPrint as PP
7import qualified Data.Set as Set
8import SourceSyntax.Literal as Literal
10data Pattern = PData String [Pattern]
12 | PAlias String Pattern
15 | PLiteral Literal.Literal
16 deriving (Eq, Ord, Show)
18cons h t = PData "::" [h,t]
21tuple es = PData ("_Tuple" ++ show (length es)) es
23boundVars :: Pattern -> Set.Set String
26 PVar x -> Set.singleton x
27 PAlias x p -> Set.insert x (boundVars p)
28 PData _ ps -> Set.unions (map boundVars ps)
29 PRecord fields -> Set.fromList fields
30 PAnything -> Set.empty
31 PLiteral _ -> Set.empty
34instance Pretty Pattern where
38 PLiteral lit -> pretty lit
39 PRecord fs -> PP.braces (commaCat $ map variable fs)
40 PAlias x p -> prettyParens p <+> PP.text "as" <+> variable x
41 PAnything -> PP.text "_"
42 PData "::" [hd,tl] -> parensIf isCons (pretty hd) <+> PP.text "::" <+> pretty tl
43 where isCons = case hd of
48 PP.parens . commaCat $ map pretty ps
49 else hsep (PP.text name : map prettyParens ps)
51prettyParens pattern = parensIf needsThem (pretty pattern)
55 PData name (_:_) | not (isTuple 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