1module Nitpick.Pattern where
3import qualified Data.List as List
4import qualified Data.Set as Set
6import qualified AST.Literal as L
7import qualified AST.Pattern as P
8import qualified AST.Variable as Var
9import qualified Reporting.Annotation as A
13 = Data Var.Canonical [Pattern]
15 | Alias String Pattern
19 | AnythingBut (Set.Set L.Literal)
23fromCanonicalPattern :: P.Canonical -> Pattern
24fromCanonicalPattern (A.A _ pattern) =
26 P.Data tag patterns ->
27 Data tag (map fromCanonicalPattern patterns)
32 P.Alias name pattern ->
33 Alias name (fromCanonicalPattern pattern)
41 P.Literal (L.Boolean True) ->
42 Data (Var.builtin "True") []
44 P.Literal (L.Boolean False) ->
45 Data (Var.builtin "False") []
53toString :: Bool -> Pattern -> String
54toString needsParens pattern =
56 Data tag [first,rest] | Var.toString tag == "::" ->
57 toString (isCons first) first
59 ++ toString False rest
62 if Var.isTuple tag then
63 "(" ++ List.intercalate ", " (map (toString False) args) ++ ")"
66 addParensIf (needsParens && not (null args)) $
67 List.intercalate " " (Var.toString tag : map (toString True) args)
70 "{ " ++ List.intercalate ", " fields ++ " }"
72 Alias alias realPattern ->
73 addParensIf needsParens $
74 toString False realPattern ++ " as " ++ alias
85 AnythingBut literalSet ->
86 case map L.toString (Set.toList literalSet) of
88 error "should not have an AnythingBut with no literals"
91 "<values besides " ++ x ++ ">"
94 "<values besides " ++ x ++ " and " ++ y ++ ">"
98 ++ List.intercalate ", " (init literals)
99 ++ ", and " ++ last literals ++ ">"
102addParensIf :: Bool -> String -> String
103addParensIf needsParens str =
111isCons :: Pattern -> Bool
115 Var.toString tag == "::"
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