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 Elm.Utils ((|>))
13import qualified Reporting.PrettyPrint as P
14import qualified Reporting.Report as Report
18 = Parse [Parsec.Message]
19 | InfixDuplicate String
20 | TypeWithoutDefinition String
21 | PortWithoutAnnotation String
22 | DuplicateValueDeclaration String
23 | DuplicateTypeDeclaration String
24 | DuplicateDefinition String
25 | UnboundTypeVarsInAlias String [String] String [String] Type.Raw
26 | UnboundTypeVarsInUnion String [String] String [String] [(String, [Type.Raw])]
31toReport :: Error -> Report.Report
35 parseErrorReport messages
37 InfixDuplicate opName ->
40 ("The infix declarations for " ++ operator ++ " must be removed.")
41 ("The precedence and associativity can only be set in one place, and\n"
42 ++ "this information has already been set somewhere else."
47 then "(" ++ opName ++ ")"
48 else "`" ++ opName ++ "`"
50 TypeWithoutDefinition valueName ->
53 ("There is a type annotation for '" ++ valueName ++ "' but there"
54 ++ "is no corresponding definition!"
56 ("Directly below the type annotation, put a definition like:\n\n"
57 ++ " " ++ valueName ++ " = 42"
60 PortWithoutAnnotation portName ->
63 ("Port '" ++ portName ++ "' does not have a type annotation!")
64 ("Directly above the port definition, I need something like this:\n\n"
65 ++ " port " ++ portName ++ " : Signal Int"
68 DuplicateValueDeclaration name ->
70 "DUPLICATE DEFINITION"
71 ("Naming multiple top-level values '" ++ name ++ "' makes things\n"
72 ++ "ambiguous. When you say '" ++ name ++ "' which one do you want?!"
74 ("Find all the top-level values named '" ++ name ++ "' and\n"
75 ++ "do some renaming. Make sure the names are distinct!"
78 DuplicateTypeDeclaration name ->
80 "DUPLICATE DEFINITION"
81 ("Naming multiple types '" ++ name ++ "' makes things ambiguous\n"
82 ++ "When you say '" ++ name ++ "' which one do you want?!"
84 ("Find all the types named '" ++ name ++ "' and\n"
85 ++ "do some renaming. Make sure the names are distinct!"
88 DuplicateDefinition name ->
90 "DUPLICATE DEFINITION"
91 ("Naming multiple values '" ++ name ++ "' in a single let-expression makes\n"
92 ++ "things ambiguous. When you say '" ++ name ++ "' which one do you want?!"
94 ("Find all the values named '" ++ name ++ "' in this let-expression and\n"
95 ++ "do some renaming. Make sure the names are distinct!"
98 UnboundTypeVarsInAlias typeName givenVars tvar tvars tipe ->
99 unboundTypeVars typeName tvar tvars $ P.render $
101 (P.text "type alias" <+> P.text typeName <+> P.hsep vars <+> P.equals)
103 (P.pretty False tipe)
105 vars = map P.text (givenVars ++ tvar : tvars)
108 UnboundTypeVarsInUnion typeName givenVars tvar tvars ctors ->
109 unboundTypeVars typeName tvar tvars $ P.render $
111 [ P.text "type" <+> P.text typeName <+> P.hsep vars
113 |> zipWith (<+>) (P.text "=" : repeat (P.text "|"))
118 vars = map P.text (givenVars ++ tvar : tvars)
120 P.text ctor <+> P.hsep (map (P.pretty True) args)
123unboundTypeVars :: String -> String -> [String] -> String -> Report.Report
124unboundTypeVars typeName tvar tvars revisedDeclaration =
127 ( "Not all type variables in '" ++ typeName ++ "' are listed, making sneaky\n"
128 ++ "type errors possible. Unbound type variables include: "
129 ++ List.intercalate ", " (tvar:tvars)
131 ( "You probably want this definition instead:\n"
132 ++ concatMap ("\n "++) (lines revisedDeclaration) ++ "\n\n"
133 ++ "Here's why. Imagine one '" ++ typeName ++ "' where '" ++ tvar ++ "' is an Int and\n"
134 ++ "another where it is a Bool. When we explicitly list the type variables, type\n"
135 ++ "checker can see that they are actually different types."
139-- TAGGING PARSE ERRORS
145freshLine = "FRESH_LINE"
148whitespace = "WHITESPACE"
150keyword :: String -> String
154unkeyword :: String -> Maybe String
156 if List.isPrefixOf "KEYWORD=" message
157 then Just (drop (length "KEYWORD=") message)
161-- REPORTING PARSE ERRORS
163parseErrorReport :: [Parsec.Message] -> Report.Report
164parseErrorReport messages =
166 addMsg message hint =
168 Parsec.SysUnExpect _msg ->
171 Parsec.UnExpect _msg ->
177 if msg `elem` [whitespace, newline, freshLine]
181 hint { _expected = Set.insert msg' (_expected hint) }
183 Parsec.Message msg ->
184 hint { _messages = msg : _messages hint }
186 (ParseHint msgs expects) =
187 foldr addMsg emptyHint messages
192 case unkeyword msg of
194 "It looks like the keyword '" ++ kwd ++ "' is being used as a variable.\n"
195 ++ "Try renaming it to something else."
199 _ -> "I ran into something unexpected when parsing your code!"
205 "I am looking for one of the following things:\n"
206 ++ concatMap ("\n "++) (Set.toList expects)
208 Report.simple "SYNTAX PROBLEM" preHint postHint
211data ParseHint = ParseHint
212 { _messages :: [String]
213 , _expected :: Set.Set String
218emptyHint :: ParseHint
220 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