on master help sign in
Improve presentation of incomplete patterns in error messages
commitdb98abe060dadc3075ce52a3371c82388c2a2b4c authorEvan Czaplicki <info@elm-lang.org> dateWed 9 Sep 2015, 13:23 (11 years ago) parent8f6ebdd
2 files changed+34−6
src/Nitpick/Pattern.hs+33−5 src/Reporting/Error/Pattern.hs+1−1
src/Nitpick/Pattern.hs+33−5
@@ -44,17 +44,28 @@ fromCanonicalPattern (A.A _ pattern) =
4444
4545-- TO STRING
4646
47−toString :: Pattern -> String
48−toString pattern =
47+toString :: Bool -> Pattern -> String
48+toString needsParens pattern =
4949 case pattern of
50+ Data tag [first,rest] | Var.toString tag == "::" ->
51+ toString (isCons first) first
52+ ++ " :: "
53+ ++ toString False rest
54+
5055 Data tag args ->
51− List.intercalate " " (Var.toString tag : map toString args)
56+ if Var.isTuple tag then
57+ "(" ++ List.intercalate ", " (map (toString False) args) ++ ")"
58+
59+ else
60+ addParensIf (needsParens && not (null args)) $
61+ List.intercalate " " (Var.toString tag : map (toString True) args)
5262
5363 Record fields ->
5464 "{ " ++ List.intercalate ", " fields ++ " }"
5565
5666 Alias alias realPattern ->
57− toString realPattern ++ " as " ++ alias
67+ addParensIf needsParens $
68+ toString False realPattern ++ " as " ++ alias
5869
5970 Var name ->
6071 name
@@ -66,6 +77,23 @@ toString pattern =
6677 L.toString literal
6778
6879 AnythingBut literalSet ->
69− List.intercalate " " ("<values besides:" : map L.toString (Set.toList literalSet))
80+ List.intercalate " " ("<values besides:" : map L.toString (Set.toList literalSet) ++ [">"])
81+
82+
83+addParensIf :: Bool -> String -> String
84+addParensIf needsParens str =
85+ if needsParens then
86+ "(" ++ str ++ ")"
7087
88+ else
89+ str
90+
91+
92+isCons :: Pattern -> Bool
93+isCons pattern =
94+ case pattern of
95+ Data tag [_,_] ->
96+ Var.toString tag == "::"
7197
98+ _ ->
99+ False
src/Reporting/Error/Pattern.hs+1−1
@@ -48,7 +48,7 @@ viewPatternList unhandledPatterns =
4848 splitAt 4 unhandledPatterns
4949 in
5050 concatMap ((++) "\n ") $
51− map Pattern.toString showPatterns
51+ map (Pattern.toString False) showPatterns
5252 ++ if null rest then [] else ["..."]
5353
5454