on master help sign in
Make a function that figures out what is next in the source code
00f1290Evan Czaplicki
compiler/src/Parse/Variable.hs+1
@@ -10,6 +10,7 @@ module Parse.Variable
10 , chompInnerChars
10 , chompInnerChars
11 , getInnerWidth
11 , getInnerWidth
12 , getInnerWidthHelp
12 , getInnerWidthHelp
13+ , reservedWords
13 )
14 )
14 where
15 where
15
16
compiler/src/Reporting/Render/Code.hs+61
@@ -5,18 +5,27 @@ module Reporting.Render.Code
5 , toSource
5 , toSource
6 , toSnippet
6 , toSnippet
7 , toPair
7 , toPair
8+ , Next(..)
9+ , whatIsNext
8 )
10 )
9 where
11 where
10
12
11
13
12import qualified Data.ByteString as B
14import qualified Data.ByteString as B
13import qualified Data.ByteString.UTF8 as UTF8_BS
15import qualified Data.ByteString.UTF8 as UTF8_BS
16+import qualified Data.Char as Char
17+import qualified Data.IntSet as IntSet
14import qualified Data.List as List
18import qualified Data.List as List
19+import qualified Data.Name as Name
20+import qualified Data.Set as Set
15import Data.Word (Word16)
21import Data.Word (Word16)
16
22
17import qualified Reporting.Annotation as A
23import qualified Reporting.Annotation as A
18import qualified Reporting.Doc as D
24import qualified Reporting.Doc as D
19import Reporting.Doc (Doc)
25import Reporting.Doc (Doc)
26+import Parse.Primitives (Row, Col)
27+import Parse.Symbol (binopCharSet)
28+import Parse.Variable (reservedWords)
20
29
21
30
22
31
@@ -180,3 +189,55 @@ renderPair source@(Source sourceLines) region1 region2 =
180 TwoChunks
189 TwoChunks
181 (render source region1 Nothing)
190 (render source region1 Nothing)
182 (render source region2 Nothing)
191 (render source region2 Nothing)
192+
193+
194+
195+-- WHAT IS NEXT?
196+
197+
198+data Next
199+ = Spaces
200+ | Keyword String
201+ | Operator String
202+ | CloseParen
203+ | CloseSquare
204+ | CloseCurly
205+ | Other
206+
207+
208+whatIsNext :: Source -> Row -> Col -> Next
209+whatIsNext (Source sourceLines) row col =
210+ case List.lookup row sourceLines of
211+ Nothing ->
212+ Other
213+
214+ Just line ->
215+ let
216+ chars = drop (fromIntegral col - 1) line
217+ in
218+ case List.find (\kwd -> startsWith kwd chars) (map Name.toChars (Set.toList reservedWords)) of
219+ Just keyword ->
220+ Keyword keyword
221+
222+ Nothing ->
223+ case takeWhile isSymbol chars of
224+ op@(_:_) ->
225+ Operator op
226+
227+ []
228+ | null chars -> Spaces
229+ | startsWith " " chars -> Spaces
230+ | startsWith ")" chars -> CloseParen
231+ | startsWith "]" chars -> CloseSquare
232+ | startsWith "}" chars -> CloseCurly
233+ | otherwise -> Other
234+
235+
236+isSymbol :: Char -> Bool
237+isSymbol char =
238+ IntSet.member (Char.ord char) binopCharSet
239+
240+
241+startsWith :: [Char] -> [Char] -> Bool
242+startsWith =
243+ List.isPrefixOf