on master help sign in
Make the check for keywords a bit more accurate
commit210d7f94786ff5a5f6bb9e3a7ade891bf84672a0 authorEvan Czaplicki <evancz@users.noreply.github.com> dateThu 9 May 2019, 13:53 (7 years ago) parente5abcf4
1 file changed+28−10
compiler/src/Reporting/Render/Code.hs+28−10
compiler/src/Reporting/Render/Code.hs+28−10
@@ -214,8 +214,9 @@ whatIsNext (Source sourceLines) row col =
214214 Just line ->
215215 let
216216 chars = drop (fromIntegral col - 1) line
217+ keywords = map Name.toChars (Set.toList reservedWords)
217218 in
218− case List.find (\kwd -> startsWith kwd chars) (map Name.toChars (Set.toList reservedWords)) of
219+ case List.find (startsWithKeyword chars) keywords of
219220 Just keyword ->
220221 Keyword keyword
221222
@@ -225,12 +226,12 @@ whatIsNext (Source sourceLines) row col =
225226 Operator op
226227
227228 []
228− | null chars -> Spaces
229− | startsWith " " chars -> Spaces
230− | startsWith ")" chars -> CloseParen
231− | startsWith "]" chars -> CloseSquare
232− | startsWith "}" chars -> CloseCurly
233− | otherwise -> Other
229+ | null chars -> Spaces
230+ | isNext ' ' chars -> Spaces
231+ | isNext ')' chars -> CloseParen
232+ | isNext ']' chars -> CloseSquare
233+ | isNext '}' chars -> CloseCurly
234+ | otherwise -> Other
234235
235236
236237isSymbol :: Char -> Bool
@@ -238,6 +239,23 @@ isSymbol char =
238239 IntSet.member (Char.ord char) binopCharSet
239240
240241
241−startsWith :: [Char] -> [Char] -> Bool
242−startsWith =
243− List.isPrefixOf
242+isNext :: Char -> [Char] -> Bool
243+isNext expected chars =
244+ case chars of
245+ [] ->
246+ False
247+
248+ actual:_ ->
249+ actual == expected
250+
251+
252+startsWithKeyword :: [Char] -> [Char] -> Bool
253+startsWithKeyword restOfLine keyword =
254+ List.isPrefixOf keyword restOfLine
255+ &&
256+ case drop (length keyword) restOfLine of
257+ [] ->
258+ True
259+
260+ c:_ ->
261+ not (Char.isAlphaNum c || c == '_')