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]
18 | InfixDuplicate String
19 | TypeWithoutDefinition String
20 | PortWithoutAnnotation String
22 | DuplicateArgument String String
23 | DuplicateFieldName String
24 | DuplicateValueDeclaration String
25 | DuplicateTypeDeclaration String
26 | DuplicateDefinition String
27 | UnboundTypeVarsInUnion String [String] [String]
28 | UnboundTypeVarsInAlias String [String] [String]
29 | UnusedTypeVarsInAlias String [String] [String]
30 | MessyTypeVarsInAlias String [String] [String] [String]
35toReport :: RenderType.Localizer -> Error -> Report.Report
36toReport _localizer err =
39 parseErrorReport messages
41 BadFunctionName arity ->
43 "BAD FUNCTION DEFINITION"
45 "Complex patterns cannot be used as function names."
46 ( Help.reflowParagraph $
47 "This function definition has " ++ show arity ++ " arguments, but instead\
48 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
49 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
50 \ be changed to a normal name."
57 ("The free variable `" ++ name ++ "` appears more than once in this pattern.")
59 [ text "Rename the variables so there are no duplicates."
60 , Help.reflowParagraph $
61 "In Elm, pattern matching works by binding these free variables to subsections\
62 \ of the matching value. It does not make sense to have the same name for two\
63 \ different subsections though! When you say `" ++ name ++ "` in some code, there\
64 \ is no way for me to know which subsection you mean!"
68 InfixDuplicate opName ->
72 ("The infix declarations for " ++ Help.functionName opName ++ " must be removed.")
73 ( Help.reflowParagraph $
74 "The precedence and associativity of a particular operator can only be set in\
75 \ one place in a code base, and " ++ Help.functionName opName
76 ++ " has already been set somewhere else."
79 TypeWithoutDefinition valueName ->
83 ("There is a type annotation for `" ++ valueName ++ "` but there"
84 ++ " is no corresponding definition!"
87 "Directly below the type annotation, put a definition like:\n\n"
88 ++ " " ++ valueName ++ " = 42"
91 PortWithoutAnnotation portName ->
95 ("Port `" ++ portName ++ "` does not have a type annotation!")
97 "Directly above the port definition, I need something like this:\n\n"
98 ++ " port " ++ portName ++ " : Signal Int"
106 "This module has ports, but ports can only appear in the main module."
107 ( Help.reflowParagraph $
108 "Ports in library code would create hidden dependencies where importing a\
109 \ module could bring in constraints not captured in the public API. Furthermore,\
110 \ if the module is imported twice, do we send values out the port twice?"
113 DuplicateArgument funcName argName ->
117 ( "The name `" ++ argName
118 ++ "` is used more than once in the arguments of `"
121 ( Help.reflowParagraph $
122 "Rename things until `" ++ argName ++ "` is used only once.\
123 \ Otherwise how can we tell which one you want when you\
124 \ say `" ++ argName ++ "` it in the body of your function?"
127 DuplicateFieldName name ->
131 ("This record has more than one field named `" ++ name ++ "`.")
132 (text "There can only be one. Do some renaming to make sure the names are distinct!")
134 DuplicateValueDeclaration name ->
136 "DUPLICATE DEFINITION"
138 ( "Naming multiple top-level values `" ++ name ++ "` makes things\n"
139 ++ "ambiguous. When you say `" ++ name ++ "` which one do you want?"
141 ( Help.reflowParagraph $
142 "Find all the top-level values named " ++ Help.functionName name
143 ++ " and do some renaming. Make sure the names are distinct!"
146 DuplicateTypeDeclaration name ->
148 "DUPLICATE DEFINITION"
150 ( "There are multiple types named `" ++ name ++ "` in this module."
152 ( Help.reflowParagraph $
153 "Search through this module, find all the types named `" ++ name
154 ++ "`, and give each of them a unique name."
157 DuplicateDefinition name ->
159 "DUPLICATE DEFINITION"
161 ("There are multiple values named `" ++ name ++ "` in this let-expression."
163 ( Help.reflowParagraph $
164 "Search through this let-expression, find all the values named `" ++ name
165 ++ "`, and give each of them a unique name."
168 UnboundTypeVarsInUnion typeName givenVars unbound ->
169 unboundTypeVars "type" typeName givenVars unbound
171 UnboundTypeVarsInAlias typeName givenVars unbound ->
172 unboundTypeVars "type alias" typeName givenVars unbound
174 UnusedTypeVarsInAlias typeName givenVars unused ->
176 "UNUSED TYPE VARIABLES"
178 ( "Type alias `" ++ typeName ++ "` cannot have unused type variables: "
179 ++ Help.commaSep unused
182 [ text "You probably need to change the declaration like this:"
183 , dullyellow $ hsep $
184 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ ["=", "..."])
188 MessyTypeVarsInAlias typeName givenVars unused unbound ->
190 "TYPE VARIABLE PROBLEMS"
192 ( "Type alias `" ++ typeName ++ "` has some problems with type variables."
195 [ Help.reflowParagraph $
196 "The declaration says it uses certain type variables ("
197 ++ Help.commaSep unused ++ ") but they do not appear in the aliased type. "
198 ++ "Furthermore, the aliased type says it uses type variables ("
199 ++ Help.commaSep unbound
200 ++ ") that do not appear in the declaration."
201 , text "You probably need to change the declaration like this:"
202 , dullyellow $ hsep $
203 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ unbound ++ ["=", "..."])
208unboundTypeVars :: String -> String -> [String] -> [String] -> Report.Report
209unboundTypeVars declKind typeName givenVars unboundVars@(tvar:_) =
211 "UNBOUND TYPE VARIABLES"
213 ( Help.capitalize declKind ++ " `" ++ typeName ++ "` must declare its use of type variable"
214 ++ Help.commaSep unboundVars
217 [ text "You probably need to change the declaration like this:"
219 map text (declKind : typeName : givenVars)
220 ++ map (dullyellow . text) unboundVars
221 ++ map text ["=", "..."]
222 , Help.reflowParagraph $
223 "Here's why. Imagine one `" ++ typeName ++ "` where `" ++ tvar ++ "` is an Int and\
224 \ another where it is a Bool. When we explicitly list the type variables, type\
225 \ checker can see that they are actually different types."
231-- TAGGING PARSE ERRORS
239freshLine = "FRESH_LINE"
243whitespace = "WHITESPACE"
250keyword :: String -> String
260extractSpecialMessage :: String -> Maybe SpecialMessage
261extractSpecialMessage message =
262 if List.isPrefixOf "KEYWORD=" message then
263 Just $ MsgKeyword (drop (length "KEYWORD=") message)
265 else if tab == message then
272-- REPORTING PARSE ERRORS
274parseErrorReport :: [Parsec.Message] -> Report.Report
275parseErrorReport messages =
277 addMsg message hint =
279 Parsec.SysUnExpect _msg ->
282 Parsec.UnExpect _msg ->
288 if msg `elem` [whitespace, newline, freshLine] then
294 -- Our parser looks for tab so it can throw a custom tab error message.
295 -- Exclude tabs from the list of expected tokens so that no one thinks they are supported.
298 hint { _expected = Set.insert msg' (_expected hint) }
300 Parsec.Message msg ->
301 hint { _messages = msg : _messages hint }
303 (ParseHint msgs expects) =
304 foldr addMsg emptyHint messages
306 (preHint, maybePost) =
309 case extractSpecialMessage msg of
310 Just (MsgKeyword kwd) ->
311 ( "It looks like the keyword `" ++ kwd ++ "` is being used as a variable."
312 , Just "Rename it to something else."
316 ( "A tab character was found, but all whitespace (including indentation) must be spaces not tabs."
317 , Just "I am looking for spaces, not tabs."
326 ( "I ran into something unexpected when parsing your code!"
331 if Set.null expects then
332 "Maybe <http://elm-lang.org/docs/syntax> can help you figure it out."
335 "I am looking for one of the following things:\n"
336 ++ concatMap ("\n "++) (Set.toList expects)
342 (text (maybe postHint id maybePost))
345data ParseHint = ParseHint
346 { _messages :: [String]
347 , _expected :: Set.Set String
352emptyHint :: ParseHint
354 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