on e34245a help sign in
46 lines 1.4 kB history raw
1module SourceSyntax.Pattern where
3import Data.List (intercalate)
4import SourceSyntax.Helpers as Help
5import SourceSyntax.PrettyPrint
6import Text.PrettyPrint as PP
7import SourceSyntax.Literal as Literal
9data Pattern = PData String [Pattern]
10 | PRecord [String]
11 | PAlias String Pattern
12 | PVar String
13 | PAnything
14 | PLiteral Literal.Literal
15 deriving (Eq, Ord, Show)
17cons h t = PData "::" [h,t]
18nil = PData "[]" []
19list = foldr cons nil
20tuple es = PData ("_Tuple" ++ show (length es)) es
23instance Pretty Pattern where
24 pretty pattern =
25 case pattern of
26 PVar x -> variable x
27 PLiteral lit -> pretty lit
28 PRecord fs -> PP.braces (commaCat $ map variable fs)
29 PAlias x p -> prettyParens p <+> PP.text "as" <+> variable x
30 PAnything -> PP.text "_"
31 PData "::" [hd,tl] -> parensIf isCons (pretty hd) <+> PP.text "::" <+> pretty tl
32 where isCons = case hd of
33 PData "::" _ -> True
34 _ -> False
35 PData name ps ->
36 if isTuple name then
37 PP.parens . commaCat $ map pretty ps
38 else sep (PP.text name : map prettyParens ps)
40prettyParens pattern = parensIf needsThem (pretty pattern)
41 where
42 needsThem =
43 case pattern of
44 PData name (_:_) | not (isTuple name) -> True
45 PAlias _ _ -> True
46 _ -> False