on master help sign in
Make errors for primes in variables much more precise
commitcf107f9244f33eb0017a46cc9d4c0c80f71d1f60 authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 10 Oct 2016, 13:39 (10 years ago) parent0d71129
2 files changed+27−23
src/Parse/Helpers.hs+15−11 src/Reporting/Error/Syntax.hs+12−12
src/Parse/Helpers.hs+15−11
@@ -90,28 +90,32 @@ rLabel = lowVar
9090
9191innerVarChar :: IParser Char
9292innerVarChar =
93− choice
94− [ alphaNum
95− , char '_'
96− , char '\'' >> fail Syntax.prime
97− ]
98− <?> "more letters in this name"
93+ alphaNum <|> char '_' <?> "more letters in this name"
9994
10095
10196makeVar :: IParser Char -> IParser String
10297makeVar firstChar =
10398 do variable <- (:) <$> firstChar <*> many innerVarChar
104− if variable `elem` reserveds
105− then fail (Syntax.keyword variable)
106− else return variable
99+ choice
100+ [ do lookAhead (char '\'')
101+ failure (Syntax.prime variable)
102+ , if variable `elem` reserveds then
103+ failure (Syntax.keyword variable)
104+ else
105+ return variable
106+ ]
107107
108108
109109reserved :: String -> IParser String
110110reserved word =
111111 expecting ("reserved word `" ++ word ++ "`") $
112112 do string word
113− notFollowedBy innerVarChar
114− return word
113+ choice
114+ [ do lookAhead (char '\'')
115+ failure (Syntax.prime word)
116+ , do notFollowedBy innerVarChar
117+ return word
118+ ]
115119
116120
117121
src/Reporting/Error/Syntax.hs+12−12
@@ -328,15 +328,15 @@ keyword kwd =
328328 "KEYWORD=" ++ kwd
329329
330330
331−prime :: String
332−prime =
333− "PRIME"
331+prime :: String -> String
332+prime var =
333+ "PRIME=" ++ var
334334
335335
336336data SpecialMessage
337337 = MsgKeyword String
338+ | MsgPrime String
338339 | MsgTab
339− | MsgPrime
340340
341341
342342extractSpecialMessage :: String -> Maybe SpecialMessage
@@ -344,12 +344,12 @@ extractSpecialMessage message =
344344 if List.isPrefixOf "KEYWORD=" message then
345345 Just $ MsgKeyword (drop (length "KEYWORD=") message)
346346
347+ else if List.isPrefixOf "PRIME=" message then
348+ Just $ MsgPrime (drop (length "PRIME=") message)
349+
347350 else if tab == message then
348351 Just MsgTab
349352
350− else if prime == message then
351− Just MsgPrime
352−
353353 else
354354 Nothing
355355
@@ -397,16 +397,16 @@ parseErrorReport messages =
397397 , Just "Rename it to something else."
398398 )
399399
400+ Just (MsgPrime var) ->
401+ ( "Ran into a single quote in a variable name. This was removed in 0.18!"
402+ , Just $ "Change it to a number or an underscore, like " ++ var ++ "_ or " ++ var ++ "1"
403+ )
404+
400405 Just MsgTab ->
401406 ( "A tab character was found, but all whitespace (including indentation) must be spaces not tabs."
402407 , Just "I am looking for spaces, not tabs."
403408 )
404409
405− Just MsgPrime ->
406− ( "Ran into a single quote in a variable name. This was removed in 0.18!"
407− , Just "Change it to a number or an underscore: x' => x1"
408− )
409−
410410 Nothing ->
411411 ( msg
412412 , Nothing