on master help sign in
Merge pull request #1256 from laszlopandy/tab-error-message
c18189bEvan Czaplicki
src/Parse/Helpers.hs+11−1
@@ -327,7 +327,9 @@ spaces =
327
327
328forcedWS :: IParser String
328forcedWS :: IParser String
329forcedWS =
329forcedWS =
330+ do failIfTabFound
330− do ws <- many1 (spaces <|> newline)
331+ ws <- many1 (spaces <|> newline)
332+ failIfTabFound
331 column <- sourceColumn <$> getPosition
333 column <- sourceColumn <$> getPosition
332 if column == 1
334 if column == 1
333 then fail badWhiteSpaceMessage
335 then fail badWhiteSpaceMessage
@@ -342,6 +344,14 @@ badWhiteSpaceMessage =
342 ++ "some spaces here:"
344 ++ "some spaces here:"
343
345
344
346
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+
345-- Just eats whitespace until the next meaningful character.
355-- Just eats whitespace until the next meaningful character.
346dumbWhitespace :: IParser String
356dumbWhitespace :: IParser String
347dumbWhitespace =
357dumbWhitespace =
src/Reporting/Error/Syntax.hs+28−6
@@ -228,15 +228,27 @@ whitespace :: String
228whitespace = "WHITESPACE"
228whitespace = "WHITESPACE"
229
229
230
230
231+tab :: String
232+tab = "TAB"
233+
234+
231keyword :: String -> String
235keyword :: String -> String
232keyword kwd =
236keyword kwd =
233 "KEYWORD=" ++ kwd
237 "KEYWORD=" ++ kwd
234
238
235
239
236−unkeyword :: String -> Maybe String
240+data SpecialMessage
237−unkeyword message =
241+ = MsgKeyword String
242+ | MsgTab
243+
244+
245+extractSpecialMessage :: String -> Maybe SpecialMessage
246+extractSpecialMessage message =
238 if List.isPrefixOf "KEYWORD=" message then
247 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
240
252
241 else
253 else
242 Nothing
254 Nothing
@@ -263,7 +275,12 @@ parseErrorReport messages =
263 else
275 else
264 msg
276 msg
265 in
277 in
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
266− hint { _expected = Set.insert msg' (_expected hint) }
283+ hint { _expected = Set.insert msg' (_expected hint) }
267
284
268 Parsec.Message msg ->
285 Parsec.Message msg ->
269 hint { _messages = msg : _messages hint }
286 hint { _messages = msg : _messages hint }
@@ -274,12 +291,17 @@ parseErrorReport messages =
274 (preHint, maybePost) =
291 (preHint, maybePost) =
275 case msgs of
292 case msgs of
276 [msg] ->
293 [msg] ->
277− case unkeyword msg of
294+ case extractSpecialMessage msg of
278− Just kwd ->
295+ Just (MsgKeyword kwd) ->
279 ( "It looks like the keyword `" ++ kwd ++ "` is being used as a variable."
296 ( "It looks like the keyword `" ++ kwd ++ "` is being used as a variable."
280 , Just "Rename it to something else."
297 , Just "Rename it to something else."
281 )
298 )
282
299
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+
283 Nothing ->
305 Nothing ->
284 ( msg
306 ( msg
285 , Nothing
307 , Nothing