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 | SettingsOnNormalModule
24 | SettingsOnForeignModule
25 | DuplicateSettingOnEffectModule String
26 | BadSettingOnEffectModule String
28 | InfixDuplicate String
29 | TypeWithoutDefinition String
30 | DuplicateFieldName String
31 | DuplicateValueDeclaration String
32 | DuplicateTypeDeclaration String
33 | DuplicateDefinition String
34 | UnboundTypeVarsInUnion String [String] [String]
35 | UnboundTypeVarsInAlias String [String] [String]
36 | UnusedTypeVarsInAlias String [String] [String]
37 | MessyTypeVarsInAlias String [String] [String] [String]
42toReport :: RenderType.Localizer -> Error -> Report.Report
43toReport _localizer err =
46 parseErrorReport messages
48 BadFunctionName arity ->
50 "BAD FUNCTION DEFINITION"
52 "Complex patterns cannot be used as function names."
53 ( Help.reflowParagraph $
54 "This function definition has " ++ show arity ++ " arguments, but instead\
55 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
56 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
57 \ be changed to a normal name."
64 ("The free variable `" ++ name ++ "` appears more than once in this pattern.")
66 [ text "Rename the variables so there are no duplicates."
67 , Help.reflowParagraph $
68 "In Elm, pattern matching works by binding these free variables to subsections\
69 \ of the matching value. It does not make sense to have the same name for two\
70 \ different subsections though! When you say `" ++ name ++ "` in some code, there\
71 \ is no way for me to know which subsection you mean!"
75 CommentAfterAnnotation name ->
79 ("The type annotation for `" ++ name ++ "` is followed by a documentation comment.")
80 ( Help.reflowParagraph $
81 "Documentation comments need to be *before* type annotations. Furthermore,\
82 \ type annotations need to be right above the corresponding definition, no\
83 \ comments in between."
90 ("This comment is followed by another documentation comment.")
91 ( Help.reflowParagraph $
92 "All documentation comments need to be right above the declaration they\
93 \ describe. Maybe some code got deleted or commented out by accident?"
100 ("This documentation comment is not followed by anything.")
101 ( text "What is it documenting? Maybe it should just be removed?" )
103 InfixDuplicate opName ->
107 ("The infix declarations for " ++ Help.functionName opName ++ " must be removed.")
108 ( Help.reflowParagraph $
109 "The precedence and associativity of a particular operator can only be set in\
110 \ one place in a code base, and " ++ Help.functionName opName
111 ++ " has already been set somewhere else."
114 TypeWithoutDefinition valueName ->
118 ("There is a type annotation for `" ++ valueName ++ "` but there"
119 ++ " is no corresponding definition!"
122 "Directly below the type annotation, put a definition like:\n\n"
123 ++ " " ++ valueName ++ " = 42"
126 DuplicateFieldName name ->
130 ("This record has more than one field named `" ++ name ++ "`.")
131 (text "There can only be one. Do some renaming to make sure the names are distinct!")
133 DuplicateValueDeclaration name ->
135 "DUPLICATE DEFINITION"
137 ( "Naming multiple top-level values `" ++ name ++ "` makes things\n"
138 ++ "ambiguous. When you say `" ++ name ++ "` which one do you want?"
140 ( Help.reflowParagraph $
141 "Find all the top-level values named " ++ Help.functionName name
142 ++ " and do some renaming. Make sure the names are distinct!"
145 DuplicateTypeDeclaration name ->
147 "DUPLICATE DEFINITION"
149 ( "There are multiple types named `" ++ name ++ "` in this module."
151 ( Help.reflowParagraph $
152 "Search through this module, find all the types named `" ++ name
153 ++ "`, and give each of them a unique name."
156 DuplicateDefinition name ->
158 "DUPLICATE DEFINITION"
160 ("There are multiple values named `" ++ name ++ "` in this let-expression."
162 ( Help.reflowParagraph $
163 "Search through this let-expression, find all the values named `" ++ name
164 ++ "`, and give each of them a unique name."
167 UnboundTypeVarsInUnion typeName givenVars unbound ->
168 unboundTypeVars "type" typeName givenVars unbound
170 UnboundTypeVarsInAlias typeName givenVars unbound ->
171 unboundTypeVars "type alias" typeName givenVars unbound
173 UnusedTypeVarsInAlias typeName givenVars unused ->
175 "UNUSED TYPE VARIABLES"
177 ( "Type alias `" ++ typeName ++ "` cannot have unused type variables: "
178 ++ Help.commaSep unused
181 [ text "You probably need to change the declaration like this:"
182 , dullyellow $ hsep $
183 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ ["=", "..."])
187 MessyTypeVarsInAlias typeName givenVars unused unbound ->
189 "TYPE VARIABLE PROBLEMS"
191 ( "Type alias `" ++ typeName ++ "` has some problems with type variables."
194 [ Help.reflowParagraph $
195 "The declaration says it uses certain type variables ("
196 ++ Help.commaSep unused ++ ") but they do not appear in the aliased type. "
197 ++ "Furthermore, the aliased type says it uses type variables ("
198 ++ Help.commaSep unbound
199 ++ ") that do not appear in the declaration."
200 , text "You probably need to change the declaration like this:"
201 , dullyellow $ hsep $
202 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ unbound ++ ["=", "..."])
207unboundTypeVars :: String -> String -> [String] -> [String] -> Report.Report
208unboundTypeVars declKind typeName givenVars unboundVars =
210 "UNBOUND TYPE VARIABLES"
212 ( Help.capitalize declKind ++ " `" ++ typeName ++ "` must declare its use of type variable"
213 ++ Help.commaSep unboundVars
216 [ text "You probably need to change the declaration like this:"
218 map text (declKind : typeName : givenVars)
219 ++ map (dullyellow . text) unboundVars
220 ++ map text ["=", "..."]
221 , Help.reflowParagraph $
222 "Here's why. Imagine one `" ++ typeName ++ "` where `" ++ head unboundVars ++
223 "` is an Int and another where it is a Bool. When we explicitly list the type\
224 \ variables, type checker can see that they are actually different types."
230-- TAGGING PARSE ERRORS
238freshLine = "FRESH_LINE"
242whitespace = "WHITESPACE"
249keyword :: String -> String
259extractSpecialMessage :: String -> Maybe SpecialMessage
260extractSpecialMessage message =
261 if List.isPrefixOf "KEYWORD=" message then
262 Just $ MsgKeyword (drop (length "KEYWORD=") message)
264 else if tab == message then
271-- REPORTING PARSE ERRORS
273parseErrorReport :: [Parsec.Message] -> Report.Report
274parseErrorReport messages =
276 addMsg message hint =
278 Parsec.SysUnExpect _msg ->
281 Parsec.UnExpect _msg ->
287 if msg `elem` [whitespace, newline, freshLine] then
293 -- Our parser looks for tab so it can throw a custom tab error message.
294 -- Exclude tabs from the list of expected tokens so that no one thinks they are supported.
297 hint { _expected = Set.insert msg' (_expected hint) }
299 Parsec.Message msg ->
300 hint { _messages = msg : _messages hint }
302 (ParseHint msgs expects) =
303 foldr addMsg emptyHint messages
305 (preHint, maybePost) =
308 case extractSpecialMessage msg of
309 Just (MsgKeyword kwd) ->
310 ( "It looks like the keyword `" ++ kwd ++ "` is being used as a variable."
311 , Just "Rename it to something else."
315 ( "A tab character was found, but all whitespace (including indentation) must be spaces not tabs."
316 , Just "I am looking for spaces, not tabs."
325 ( "I ran into something unexpected when parsing your code!"
330 if Set.null expects then
331 "Maybe <http://elm-lang.org/docs/syntax> can help you figure it out."
334 "I am looking for one of the following things:\n"
335 ++ concatMap ("\n "++) (Set.toList expects)
341 (text (maybe postHint id maybePost))
344data ParseHint = ParseHint
345 { _messages :: [String]
346 , _expected :: Set.Set String
351emptyHint :: ParseHint
353 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