on master help sign in
detect upper/lower names in whatIsNext
580555aEvan Czaplicki
compiler/src/Reporting/Render/Code.hs+31−20
@@ -201,6 +201,8 @@ data Next
201 = Keyword [Char]
201 = Keyword [Char]
202 | Operator [Char]
202 | Operator [Char]
203 | Close [Char] Char
203 | Close [Char] Char
204+ | Upper Char [Char]
205+ | Lower Char [Char]
204 | Other (Maybe Char)
206 | Other (Maybe Char)
205
207
206
208
@@ -211,25 +213,34 @@ whatIsNext (Source sourceLines) row col =
211 Other Nothing
213 Other Nothing
212
214
213 Just line ->
215 Just line ->
214− let
215− chars = drop (fromIntegral col - 1) line
216+ case drop (fromIntegral col - 1) line of
216− keywords = map Name.toChars (Set.toList reservedWords)
217+ [] ->
217− in
218+ Other Nothing
218− case List.find (startsWithKeyword chars) keywords of
219+
219− Just keyword ->
220+ c:cs
220− Keyword keyword
221+ | Char.isUpper c -> Upper c (takeWhile isInner cs)
221−
222+ | Char.isLower c -> detectKeywords c cs
222− Nothing ->
223− case chars of
224− [] ->
225− Other Nothing
226−
227− c:cs
228− | isSymbol c -> Operator (c : takeWhile isSymbol cs)
223+ | isSymbol c -> Operator (c : takeWhile isSymbol cs)
229− | c == ')' -> Close "parenthesis" ')'
224+ | c == ')' -> Close "parenthesis" ')'
230− | c == ']' -> Close "square bracket" ']'
225+ | c == ']' -> Close "square bracket" ']'
231− | c == '}' -> Close "curly brace" '}'
226+ | c == '}' -> Close "curly brace" '}'
232− | otherwise -> Other (Just c)
227+ | otherwise -> Other (Just c)
228+
229+
230+detectKeywords :: Char -> [Char] -> Next
231+detectKeywords c rest =
232+ let
233+ cs = takeWhile isInner rest
234+ name = c : cs
235+ in
236+ if Set.member (Name.fromChars name) reservedWords
237+ then Keyword name
238+ else Lower c name
239+
240+
241+isInner :: Char -> Bool
242+isInner char =
243+ Char.isAlphaNum char || char == '_'
233
244
234
245
235isSymbol :: Char -> Bool
246isSymbol :: Char -> Bool
@@ -246,7 +257,7 @@ startsWithKeyword restOfLine keyword =
246 True
257 True
247
258
248 c:_ ->
259 c:_ ->
249− not (Char.isAlphaNum c || c == '_')
260+ not (isInner c)
250
261
251
262
252nextLineStartsWithKeyword :: [Char] -> Source -> Row -> Maybe (Row, Col)
263nextLineStartsWithKeyword :: [Char] -> Source -> Row -> Maybe (Row, Col)