1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Error.Syntax where
4import qualified Data.List as List
5import qualified Data.Set as Set
6import qualified Text.Parsec.Error as Parsec
7import Text.PrettyPrint.ANSI.Leijen (dullyellow, hsep, text)
9import qualified Reporting.Error.Helpers as Help
10import qualified Reporting.Render.Type as RenderType
11import qualified Reporting.Report as Report
15 = Parse [Parsec.Message]
19 | CommentAfterAnnotation String
23 | InfixDuplicate String
24 | TypeWithoutDefinition String
25 | DuplicateFieldName String
26 | DuplicateValueDeclaration String
27 | DuplicateTypeDeclaration String
28 | DuplicateDefinition String
29 | UnboundTypeVarsInUnion String [String] [String]
30 | UnboundTypeVarsInAlias String [String] [String]
31 | UnusedTypeVarsInAlias String [String] [String]
32 | MessyTypeVarsInAlias String [String] [String] [String]
37toReport :: RenderType.Localizer -> Error -> Report.Report
38toReport _localizer err =
41 parseErrorReport messages
43 BadFunctionName arity ->
45 "BAD FUNCTION DEFINITION"
47 "Complex patterns cannot be used as function names."
48 ( Help.reflowParagraph $
49 "This function definition has " ++ show arity ++ " arguments, but instead\
50 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
51 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
52 \ be changed to a normal name."
59 ("The free variable `" ++ name ++ "` appears more than once in this pattern.")
61 [ text "Rename the variables so there are no duplicates."
62 , Help.reflowParagraph $
63 "In Elm, pattern matching works by binding these free variables to subsections\
64 \ of the matching value. It does not make sense to have the same name for two\
65 \ different subsections though! When you say `" ++ name ++ "` in some code, there\
66 \ is no way for me to know which subsection you mean!"
70 CommentAfterAnnotation name ->
74 ("The type annotation for `" ++ name ++ "` is followed by a documentation comment.")
75 ( Help.reflowParagraph $
76 "Documentation comments need to be *before* type annotations. Furthermore,\
77 \ type annotations need to be right above the corresponding definition, no\
78 \ comments in between."
85 ("This comment is followed by another documentation comment.")
86 ( Help.reflowParagraph $
87 "All documentation comments need to be right above the declaration they\
88 \ describe. Maybe some code got deleted or commented out by accident?"
95 ("This documentation comment is not followed by anything.")
96 ( text "What is it documenting? Maybe it should just be removed?" )
98 InfixDuplicate opName ->
102 ("The infix declarations for " ++ Help.functionName opName ++ " must be removed.")
103 ( Help.reflowParagraph $
104 "The precedence and associativity of a particular operator can only be set in\
105 \ one place in a code base, and " ++ Help.functionName opName
106 ++ " has already been set somewhere else."
109 TypeWithoutDefinition valueName ->
113 ("There is a type annotation for `" ++ valueName ++ "` but there"
114 ++ " is no corresponding definition!"
117 "Directly below the type annotation, put a definition like:\n\n"
118 ++ " " ++ valueName ++ " = 42"
121 DuplicateFieldName name ->
125 ("This record has more than one field named `" ++ name ++ "`.")
126 (text "There can only be one. Do some renaming to make sure the names are distinct!")
128 DuplicateValueDeclaration name ->
130 "DUPLICATE DEFINITION"
132 ( "Naming multiple top-level values `" ++ name ++ "` makes things\n"
133 ++ "ambiguous. When you say `" ++ name ++ "` which one do you want?"
135 ( Help.reflowParagraph $
136 "Find all the top-level values named " ++ Help.functionName name
137 ++ " and do some renaming. Make sure the names are distinct!"
140 DuplicateTypeDeclaration name ->
142 "DUPLICATE DEFINITION"
144 ( "There are multiple types named `" ++ name ++ "` in this module."
146 ( Help.reflowParagraph $
147 "Search through this module, find all the types named `" ++ name
148 ++ "`, and give each of them a unique name."
151 DuplicateDefinition name ->
153 "DUPLICATE DEFINITION"
155 ("There are multiple values named `" ++ name ++ "` in this let-expression."
157 ( Help.reflowParagraph $
158 "Search through this let-expression, find all the values named `" ++ name
159 ++ "`, and give each of them a unique name."
162 UnboundTypeVarsInUnion typeName givenVars unbound ->
163 unboundTypeVars "type" typeName givenVars unbound
165 UnboundTypeVarsInAlias typeName givenVars unbound ->
166 unboundTypeVars "type alias" typeName givenVars unbound
168 UnusedTypeVarsInAlias typeName givenVars unused ->
170 "UNUSED TYPE VARIABLES"
172 ( "Type alias `" ++ typeName ++ "` cannot have unused type variables: "
173 ++ Help.commaSep unused
176 [ text "You probably need to change the declaration like this:"
177 , dullyellow $ hsep $
178 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ ["=", "..."])
182 MessyTypeVarsInAlias typeName givenVars unused unbound ->
184 "TYPE VARIABLE PROBLEMS"
186 ( "Type alias `" ++ typeName ++ "` has some problems with type variables."
189 [ Help.reflowParagraph $
190 "The declaration says it uses certain type variables ("
191 ++ Help.commaSep unused ++ ") but they do not appear in the aliased type. "
192 ++ "Furthermore, the aliased type says it uses type variables ("
193 ++ Help.commaSep unbound
194 ++ ") that do not appear in the declaration."
195 , text "You probably need to change the declaration like this:"
196 , dullyellow $ hsep $
197 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ unbound ++ ["=", "..."])
202unboundTypeVars :: String -> String -> [String] -> [String] -> Report.Report
203unboundTypeVars declKind typeName givenVars unboundVars =
205 "UNBOUND TYPE VARIABLES"
207 ( Help.capitalize declKind ++ " `" ++ typeName ++ "` must declare its use of type variable"
208 ++ Help.commaSep unboundVars
211 [ text "You probably need to change the declaration like this:"
213 map text (declKind : typeName : givenVars)
214 ++ map (dullyellow . text) unboundVars
215 ++ map text ["=", "..."]
216 , Help.reflowParagraph $
217 "Here's why. Imagine one `" ++ typeName ++ "` where `" ++ head unboundVars ++
218 "` is an Int and another where it is a Bool. When we explicitly list the type\
219 \ variables, type checker can see that they are actually different types."
225-- TAGGING PARSE ERRORS
233freshLine = "FRESH_LINE"
237whitespace = "WHITESPACE"
244keyword :: String -> String
254extractSpecialMessage :: String -> Maybe SpecialMessage
255extractSpecialMessage message =
256 if List.isPrefixOf "KEYWORD=" message then
257 Just $ MsgKeyword (drop (length "KEYWORD=") message)
259 else if tab == message then
266-- REPORTING PARSE ERRORS
268parseErrorReport :: [Parsec.Message] -> Report.Report
269parseErrorReport messages =
271 addMsg message hint =
273 Parsec.SysUnExpect _msg ->
276 Parsec.UnExpect _msg ->
282 if msg `elem` [whitespace, newline, freshLine] then
288 -- Our parser looks for tab so it can throw a custom tab error message.
289 -- Exclude tabs from the list of expected tokens so that no one thinks they are supported.
292 hint { _expected = Set.insert msg' (_expected hint) }
294 Parsec.Message msg ->
295 hint { _messages = msg : _messages hint }
297 (ParseHint msgs expects) =
298 foldr addMsg emptyHint messages
300 (preHint, maybePost) =
303 case extractSpecialMessage msg of
304 Just (MsgKeyword kwd) ->
305 ( "It looks like the keyword `" ++ kwd ++ "` is being used as a variable."
306 , Just "Rename it to something else."
310 ( "A tab character was found, but all whitespace (including indentation) must be spaces not tabs."
311 , Just "I am looking for spaces, not tabs."
320 ( "I ran into something unexpected when parsing your code!"
325 if Set.null expects then
326 "Maybe <http://elm-lang.org/docs/syntax> can help you figure it out."
329 "I am looking for one of the following things:\n"
330 ++ concatMap ("\n "++) (Set.toList expects)
336 (text (maybe postHint id maybePost))
339data ParseHint = ParseHint
340 { _messages :: [String]
341 , _expected :: Set.Set String
346emptyHint :: ParseHint
348 ParseHint [] Set.empty
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