@@ -20,6 +20,7 @@ import qualified AST.Literal as L
20import qualified AST.Variable as Variable
20import qualified AST.Variable as Variable
21import Elm.Utils ((|>))
21import Elm.Utils ((|>))
22import qualified Reporting.Annotation as A
22import qualified Reporting.Annotation as A
23+import qualified Reporting.Error.Syntax as Syntax
23import qualified Reporting.Region as R
24import qualified Reporting.Region as R
@@ -37,9 +38,13 @@ reserveds =
40expecting = flip (<?>)
43expecting = flip (<?>)
43type OpTable = Map.Map String (Int, Decl.Assoc)
48type OpTable = Map.Map String (Int, Decl.Assoc)
44type SourceM = State SourcePos
49type SourceM = State SourcePos
45type IParser a = ParsecT String OpTable SourceM a
50type IParser a = ParsecT String OpTable SourceM a
@@ -59,17 +64,17 @@ iParseWithTable sourceName table aParser input =
60var :: IParser String
65var :: IParser String
62− makeVar (letter <|> char '_' <?> "variable")
67+ makeVar (letter <|> char '_') <?> "a variable"
65lowVar :: IParser String
70lowVar :: IParser String
67− makeVar (lower <?> "lower case variable")
72+ makeVar lower <?> "a lower case variable"
70capVar :: IParser String
75capVar :: IParser String
72− makeVar (upper <?> "upper case variable")
77+ makeVar upper <?> "an upper case variable"
75qualifiedVar :: IParser String
80qualifiedVar :: IParser String
@@ -84,15 +89,15 @@ rLabel = lowVar
85innerVarChar :: IParser Char
90innerVarChar :: IParser Char
86innerVarChar =
91innerVarChar =
87− alphaNum <|> char '_' <|> char '\'' <?> ""
92+ alphaNum <|> char '_' <|> char '\'' <?> "more letters in this variable"
90makeVar :: IParser Char -> IParser String
95makeVar :: IParser Char -> IParser String
91−makeVar p =
96+makeVar firstChar =
92− do v <- (:) <$> p <*> many innerVarChar
97+ do variable <- (:) <$> firstChar <*> many innerVarChar
93− if v `elem` reserveds
98+ if variable `elem` reserveds
94− then fail $ "unexpected keyword '" ++ v ++ "', variables cannot be keywords"
99+ then fail (Syntax.keyword variable)
95− else return v
100+ else return variable
98reserved :: String -> IParser String
103reserved :: String -> IParser String
@@ -109,7 +114,7 @@ anyOp :: IParser String
110 betwixt '`' '`' qualifiedVar
115 betwixt '`' '`' qualifiedVar
111 <|> symOp
116 <|> symOp
112− <?> "infix operator"
117+ <?> "an infix operator like (+)"
115symOp :: IParser String
120symOp :: IParser String
@@ -124,13 +129,23 @@ symOp =
124-- COMMON SYMBOLS
129-- COMMON SYMBOLS
126equals :: IParser String
131equals :: IParser String
127−equals = string "="
132+equals =
136+rightArrow :: IParser String
138+ string "->" <|> string "\8594" <?> "->"
141+leftArrow :: IParser String
143+ string "<-" <?> "<- for a record update"
129−arrow :: IParser String
130−arrow = string "->" <|> string "\8594" <?> "arrow (->)"
132hasType :: IParser String
146hasType :: IParser String
133−hasType = string ":" <?> "':' (a type annotation)'"
147+hasType =
148+ string ":" <?> "the \"has type\" symbol ':'"
136commitIf check p =
151commitIf check p =
@@ -155,7 +170,7 @@ spaceyPrefixBy sep parser =
156comma :: IParser Char
171comma :: IParser Char
158− char ',' <?> "comma ','"
173+ char ',' <?> "a comma ','"
161commaSep1 :: IParser a -> IParser [a]
176commaSep1 :: IParser a -> IParser [a]
@@ -170,17 +185,17 @@ commaSep =
171semiSep1 :: IParser a -> IParser [a]
186semiSep1 :: IParser a -> IParser [a]
172semiSep1 =
187semiSep1 =
173− spaceySepBy1 (char ';' <?> "semicolon ';'")
188+ spaceySepBy1 (char ';' <?> "a semicolon ';'")
176pipeSep1 :: IParser a -> IParser [a]
191pipeSep1 :: IParser a -> IParser [a]
177pipeSep1 =
192pipeSep1 =
178− spaceySepBy1 (char '|' <?> "type divider '|'")
193+ spaceySepBy1 (char '|' <?> "a vertical bar '|'")
181consSep1 :: IParser a -> IParser [a]
196consSep1 :: IParser a -> IParser [a]
182consSep1 =
197consSep1 =
183− spaceySepBy1 (string "::" <?> "cons operator '::'")
198+ spaceySepBy1 (string "::" <?> "a cons operator '::'")
186dotSep1 :: IParser a -> IParser [a]
201dotSep1 :: IParser a -> IParser [a]
@@ -197,15 +212,15 @@ spacePrefix p =
197 constrainedSpacePrefix p (\_ -> return ())
212 constrainedSpacePrefix p (\_ -> return ())
200−constrainedSpacePrefix p constraint =
215+constrainedSpacePrefix parser constraint =
201 many $ choice
216 many $ choice
202− [ try (spacing >> lookAhead (oneOf "[({")) >> p
217+ [ try (spacing >> lookAhead (oneOf "[({")) >> parser
203− , try (spacing >> p)
218+ , try (spacing >> parser)
206 spacing = do
221 spacing = do
207 n <- whitespace
222 n <- whitespace
208− constraint n
223+ constraint n <?> Syntax.whitespace
@@ -220,14 +235,14 @@ followedBy a b =
220betwixt a b c =
235betwixt a b c =
221 do char a
236 do char a
223− char b <?> "closing '" ++ [b] ++ "'"
238+ char b <?> "a closing '" ++ [b] ++ "'"
224 return out
239 return out
227surround a z name p = do
242surround a z name p = do
229 v <- padded p
244 v <- padded p
230− char z <?> unwords ["closing", name, show z]
245+ char z <?> unwords ["a closing", name, show z]
@@ -273,7 +288,7 @@ accessible exprParser =
274 annotatedRootExpr@(A.A _ rootExpr) <- exprParser
289 annotatedRootExpr@(A.A _ rootExpr) <- exprParser
276− access <- optionMaybe (try dot <?> "field access (e.g. List.map)")
291+ access <- optionMaybe (try dot <?> "a field access like .name")
278 case access of
293 case access of
279 Nothing ->
294 Nothing ->
@@ -281,7 +296,7 @@ accessible exprParser =
282 Just _ ->
297 Just _ ->
283 accessible $
298 accessible $
284− do v <- var <?> "field access (e.g. List.map)"
299+ do v <- var
285 end <- getMyPosition
300 end <- getMyPosition
286 return . A.at start end $
301 return . A.at start end $
287 case rootExpr of
302 case rootExpr of
@@ -294,7 +309,8 @@ accessible exprParser =
295dot :: IParser ()
310dot :: IParser ()
297− char '.' >> notFollowedBy (char '.')
313+ notFollowedBy (char '.')
300-- WHITESPACE
316-- WHITESPACE
@@ -309,7 +325,7 @@ padded p =
310spaces :: IParser String
326spaces :: IParser String
312− let space = string " " <|> multiComment <?> "whitespace"
328+ let space = string " " <|> multiComment <?> Syntax.whitespace
314 concat <$> many1 space
330 concat <$> many1 space
@@ -338,14 +354,14 @@ whitespace =
339freshLine :: IParser [[String]]
355freshLine :: IParser [[String]]
340freshLine =
356freshLine =
341− try (many1 newline >> many space_nl) <|> try (many1 space_nl) <?> ""
357+ try (many1 newline >> many space_nl) <|> try (many1 space_nl) <?> Syntax.freshLine
343 space_nl = try $ spaces >> many1 newline
359 space_nl = try $ spaces >> many1 newline
346newline :: IParser String
362newline :: IParser String
348− simpleNewline <|> lineComment <?> "a newline"
364+ simpleNewline <|> lineComment <?> Syntax.newline
351simpleNewline :: IParser String
367simpleNewline :: IParser String
@@ -369,7 +385,7 @@ closeComment :: IParser String
369closeComment =
385closeComment =
370 anyUntil $
386 anyUntil $
372− [ try (string "-}") <?> "close comment"
388+ [ try (string "-}") <?> "the end of a comment -}"
373 , concat <$> sequence [ try (string "{-"), closeComment, closeComment ]
389 , concat <$> sequence [ try (string "{-"), closeComment, closeComment ]
@@ -518,9 +534,10 @@ glSource src =
520str :: IParser String
536str :: IParser String
521−str = expecting "String" $ do
538+ expecting "a string" $
522− s <- choice [ multiStr, singleStr ]
539+ do s <- choice [ multiStr, singleStr ]
523− processAs T.stringLiteral . sandwich '\"' $ concat s
540+ processAs T.stringLiteral . sandwich '\"' $ concat s
525 rawString quote insides =
542 rawString quote insides =
526 quote >> manyTill insides quote
543 quote >> manyTill insides quote
@@ -560,7 +577,7 @@ escaped delim =
561chr :: IParser Char
578chr :: IParser Char
563− betwixt '\'' '\'' character <?> "character"
580+ betwixt '\'' '\'' character <?> "a character"
565 nonQuote = satisfy (/='\'')
582 nonQuote = satisfy (/='\'')