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 qualified Text.PrettyPrint as P
8import Text.PrettyPrint ((<+>))
10import qualified AST.Helpers as Help
11import qualified AST.Type as Type
12import qualified Reporting.PrettyPrint as P
13import qualified Reporting.Report as Report
17 = Parse [Parsec.Message]
20 | InfixDuplicate String
21 | TypeWithoutDefinition String
22 | PortWithoutAnnotation String
24 | DuplicateValueDeclaration String
25 | DuplicateTypeDeclaration String
26 | DuplicateDefinition String
27 | UnboundTypeVarsInAlias String [String] String [String] Type.Raw
28 | UnboundTypeVarsInUnion String [String] String [String] [(String, [Type.Raw])]
33toReport :: P.Dealiaser -> Error -> Report.Report
34toReport dealiaser err =
37 parseErrorReport messages
39 BadFunctionName arity ->
41 "BAD FUNCTION DEFINITION"
42 "Complex patterns cannot be used as function names."
43 ( "This function definition has " ++ show arity ++ " arguments, but instead of a normal name like\n"
44 ++ "`add` or `reverse` it has a pattern. There is no way to \"deconstruct\" a\n"
45 ++ "function with pattern matching, so this needs to be changed to a normal name."
51 ("The free variable `" ++ name ++ "` appears more than once in this pattern.")
52 ( "Rename the variables so there are no duplicates.\n\n"
53 ++ "In Elm, pattern matching works by binding these free variables to subsections\n"
54 ++ "of the matching value. It does not make sense to have the same name for two\n"
55 ++ "different subsections though! When you say `" ++ name ++ "` in some code, there\n"
56 ++ "is no way for me to know which subsection you mean!"
59 InfixDuplicate opName ->
62 ("The infix declarations for " ++ operator ++ " must be removed.")
63 ("The precedence and associativity can only be set in one place, and\n"
64 ++ "this information has already been set somewhere else."
69 then "(" ++ opName ++ ")"
70 else "`" ++ opName ++ "`"
72 TypeWithoutDefinition valueName ->
75 ("There is a type annotation for `" ++ valueName ++ "` but there"
76 ++ " is no corresponding definition!"
78 ("Directly below the type annotation, put a definition like:\n\n"
79 ++ " " ++ valueName ++ " = 42"
82 PortWithoutAnnotation portName ->
85 ("Port `" ++ portName ++ "` does not have a type annotation!")
86 ("Directly above the port definition, I need something like this:\n\n"
87 ++ " port " ++ portName ++ " : Signal Int"
94 "This module has ports, but ports can only appear in the main module."
95 ( "Ports in library code would create hidden dependencies where importing a\n"
96 ++ "module could bring in constraints not captured in the public API. Furthermore,\n"
97 ++ "if the module is imported twice, do we send values out the port twice?"
100 DuplicateValueDeclaration name ->
102 "DUPLICATE DEFINITION"
103 ("Naming multiple top-level values `" ++ name ++ "` makes things\n"
104 ++ "ambiguous. When you say `" ++ name ++ "` which one do you want?"
106 ("Find all the top-level values named `" ++ name ++ "` and\n"
107 ++ "do some renaming. Make sure the names are distinct!"
110 DuplicateTypeDeclaration name ->
112 "DUPLICATE DEFINITION"
113 ("Naming multiple types `" ++ name ++ "` makes things ambiguous\n"
114 ++ "When you say `" ++ name ++ "` which one do you want?"
116 ("Find all the types named `" ++ name ++ "` and\n"
117 ++ "do some renaming. Make sure the names are distinct!"
120 DuplicateDefinition name ->
122 "DUPLICATE DEFINITION"
123 ("Naming multiple values `" ++ name ++ "` in a single let-expression makes\n"
124 ++ "things ambiguous. When you say `" ++ name ++ "` which one do you want?"
126 ("Find all the values named `" ++ name ++ "` in this let-expression and\n"
127 ++ "do some renaming. Make sure the names are distinct!"
130 UnboundTypeVarsInAlias typeName givenVars tvar tvars tipe ->
131 unboundTypeVars typeName tvar tvars $ P.render $
133 (P.text "type alias" <+> P.text typeName <+> P.hsep vars <+> P.equals)
135 (P.pretty dealiaser False tipe)
137 vars = map P.text (givenVars ++ tvar : tvars)
140 UnboundTypeVarsInUnion typeName givenVars tvar tvars ctors ->
141 unboundTypeVars typeName tvar tvars $ P.render $
143 [ P.text "type" <+> P.text typeName <+> P.hsep vars
145 |> zipWith (<+>) (P.text "=" : repeat (P.text "|"))
151 vars = map P.text (givenVars ++ tvar : tvars)
153 P.text ctor <+> P.hsep (map (P.pretty dealiaser True) args)
156unboundTypeVars :: String -> String -> [String] -> String -> Report.Report
157unboundTypeVars typeName tvar tvars revisedDeclaration =
160 ( "Not all type variables in `" ++ typeName ++ "` are listed, making sneaky\n"
161 ++ "type errors possible. Unbound type variables include: "
162 ++ List.intercalate ", " (tvar:tvars)
164 ( "You probably want this definition instead:\n"
165 ++ concatMap ("\n "++) (lines revisedDeclaration) ++ "\n\n"
166 ++ "Here's why. Imagine one `" ++ typeName ++ "` where `" ++ tvar ++ "` is an Int and\n"
167 ++ "another where it is a Bool. When we explicitly list the type variables, type\n"
168 ++ "checker can see that they are actually different types."
172-- TAGGING PARSE ERRORS
178freshLine = "FRESH_LINE"
181whitespace = "WHITESPACE"
183keyword :: String -> String
187unkeyword :: String -> Maybe String
189 if List.isPrefixOf "KEYWORD=" message
190 then Just (drop (length "KEYWORD=") message)
194-- REPORTING PARSE ERRORS
196parseErrorReport :: [Parsec.Message] -> Report.Report
197parseErrorReport messages =
199 addMsg message hint =
201 Parsec.SysUnExpect _msg ->
204 Parsec.UnExpect _msg ->
210 if msg `elem` [whitespace, newline, freshLine]
214 hint { _expected = Set.insert msg' (_expected hint) }
216 Parsec.Message msg ->
217 hint { _messages = msg : _messages hint }
219 (ParseHint msgs expects) =
220 foldr addMsg emptyHint messages
225 case unkeyword msg of
227 "It looks like the keyword `" ++ kwd ++ "` is being used as a variable.\n"
228 ++ "Try renaming it to something else."
232 _ -> "I ran into something unexpected when parsing your code!"
238 "I am looking for one of the following things:\n"
239 ++ concatMap ("\n "++) (Set.toList expects)
241 Report.simple "SYNTAX PROBLEM" preHint postHint
244data ParseHint = ParseHint
245 { _messages :: [String]
246 , _expected :: Set.Set String
251emptyHint :: ParseHint
253 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