on master help sign in
Merge pull request #1256 from laszlopandy/tab-error-message

Add custom error message if tab character is found in parsing.

commitc18189b191b7ebbba21a1e2a428ed50b191572be authorEvan Czaplicki <info@elm-lang.org> dateWed 13 Jan 2016, 09:12 (11 years ago) parent29ab83c8d1e258
2 files changed+39−7
src/Parse/Helpers.hs+11−1 src/Reporting/Error/Syntax.hs+28−6
src/Parse/Helpers.hs+11−1
@@ -327,7 +327,9 @@ spaces =
327327
328328forcedWS :: IParser String
329329forcedWS =
330− do ws <- many1 (spaces <|> newline)
330+ do failIfTabFound
331+ ws <- many1 (spaces <|> newline)
332+ failIfTabFound
331333 column <- sourceColumn <$> getPosition
332334 if column == 1
333335 then fail badWhiteSpaceMessage
@@ -342,6 +344,14 @@ badWhiteSpaceMessage =
342344 ++ "some spaces here:"
343345
344346
347+failIfTabFound :: IParser ()
348+failIfTabFound =
349+ do foundTab <- option False (lookAhead (char '\t') >> return True) <?> Syntax.tab
350+ if foundTab
351+ then fail Syntax.tab
352+ else return ()
353+
354+
345355-- Just eats whitespace until the next meaningful character.
346356dumbWhitespace :: IParser String
347357dumbWhitespace =
src/Reporting/Error/Syntax.hs+28−6
@@ -228,15 +228,27 @@ whitespace :: String
228228whitespace = "WHITESPACE"
229229
230230
231+tab :: String
232+tab = "TAB"
233+
234+
231235keyword :: String -> String
232236keyword kwd =
233237 "KEYWORD=" ++ kwd
234238
235239
236−unkeyword :: String -> Maybe String
237−unkeyword message =
240+data SpecialMessage
241+ = MsgKeyword String
242+ | MsgTab
243+
244+
245+extractSpecialMessage :: String -> Maybe SpecialMessage
246+extractSpecialMessage message =
238247 if List.isPrefixOf "KEYWORD=" message then
239− Just (drop (length "KEYWORD=") message)
248+ Just $ MsgKeyword (drop (length "KEYWORD=") message)
249+
250+ else if tab == message then
251+ Just MsgTab
240252
241253 else
242254 Nothing
@@ -263,7 +275,12 @@ parseErrorReport messages =
263275 else
264276 msg
265277 in
266− hint { _expected = Set.insert msg' (_expected hint) }
278+ if msg == tab then
279+ -- Our parser looks for tab so it can throw a custom tab error message.
280+ -- Exclude tabs from the list of expected tokens so that no one thinks they are supported.
281+ hint
282+ else
283+ hint { _expected = Set.insert msg' (_expected hint) }
267284
268285 Parsec.Message msg ->
269286 hint { _messages = msg : _messages hint }
@@ -274,12 +291,17 @@ parseErrorReport messages =
274291 (preHint, maybePost) =
275292 case msgs of
276293 [msg] ->
277− case unkeyword msg of
278− Just kwd ->
294+ case extractSpecialMessage msg of
295+ Just (MsgKeyword kwd) ->
279296 ( "It looks like the keyword `" ++ kwd ++ "` is being used as a variable."
280297 , Just "Rename it to something else."
281298 )
282299
300+ Just MsgTab ->
301+ ( "A tab character was found, but all whitespace (including indentation) must be spaces not tabs."
302+ , Just "I am looking for spaces, not tabs."
303+ )
304+
283305 Nothing ->
284306 ( msg
285307 , Nothing