@@ -7,7 +7,7 @@ module Text.Lexer.Inchworm.Char
77 , scanStringIO
88
99 -- * Locations
10- , Location (.. )
10+ , Range ( .. ), Location (.. )
1111 , bumpLocationWithChar
1212
1313 -- * Scanners
@@ -33,7 +33,7 @@ scanStringIO
3333
3434scanStringIO str scanner
3535 = scanListIO
36- (Location 1 1 )
36+ (Location 0 0 )
3737 bumpLocationWithChar
3838 str scanner
3939
@@ -45,35 +45,35 @@ scanStringIO str scanner
4545bumpLocationWithChar :: Char -> Location -> Location
4646bumpLocationWithChar c (Location line col)
4747 = case c of
48- ' \n ' -> Location (line + 1 ) 1
48+ ' \n ' -> Location (line + 1 ) 0
4949 _ -> Location line (col + 1 )
5050
5151
5252-- Integers -------------------------------------------------------------------
5353-- | Scan a decimal integer, with optional @-@ and @+@ sign specifiers.
5454scanInteger
5555 :: Monad m
56- => Scanner m loc [Char ] (loc , Integer )
56+ => Scanner m loc [Char ] (Range loc , Integer )
5757
5858scanInteger
5959 = munchPred Nothing matchInt acceptInt
6060 where
6161 matchInt 0 ! c
6262 = c == ' -' || c == ' +' || Char. isDigit c
6363
64- matchInt _ ! c = Char. isDigit c
64+ matchInt _ ! c = Char. isDigit c
6565
6666 acceptInt (' +' : cs)
67- | null cs = Nothing
67+ | null cs = Nothing
6868
6969 acceptInt (' -' : cs)
70- | null cs = Nothing
70+ | null cs = Nothing
7171
72- acceptInt cs = Just $ read cs
72+ acceptInt cs = Just $ read cs
7373
7474{-# SPECIALIZE INLINE
7575 scanInteger
76- :: Scanner IO Location [Char] (Location, Integer)
76+ :: Scanner IO Location [Char] (Range Location, Integer)
7777 #-}
7878
7979-- Strings --------------------------------------------------------------------
@@ -83,7 +83,7 @@ scanInteger
8383--
8484scanHaskellString
8585 :: Monad m
86- => Scanner m loc [Char ] (loc , String )
86+ => Scanner m loc [Char ] (Range loc , String )
8787
8888scanHaskellString
8989 = munchFold Nothing matchC (False , False ) acceptC
@@ -112,7 +112,7 @@ scanHaskellString
112112
113113{-# SPECIALIZE INLINE
114114 scanHaskellString
115- :: Scanner IO Location [Char] (Location, String)
115+ :: Scanner IO Location [Char] (Range Location, String)
116116 #-}
117117
118118
@@ -123,7 +123,7 @@ scanHaskellString
123123--
124124scanHaskellChar
125125 :: Monad m
126- => Scanner m loc [Char ] (loc , Char )
126+ => Scanner m loc [Char ] (Range loc , Char )
127127
128128scanHaskellChar
129129 = munchFold Nothing matchC (False , False ) acceptC
@@ -141,23 +141,24 @@ scanHaskellChar
141141
142142 acceptC (' \' ' : cs)
143143 = case readChar cs of
144- -- Character literals do not support gaps or escape terminators
144+ -- Character literals do not support gaps or
145+ -- escape terminators
145146 Just (Just c, " \' " ) -> Just c
146147 _ -> Nothing
147148
148149 acceptC _ = Nothing
149150
150151{-# SPECIALIZE INLINE
151152 scanHaskellChar
152- :: Scanner IO Location [Char] (Location, Char)
153+ :: Scanner IO Location [Char] (Range Location, Char)
153154 #-}
154155
155156
156157-- Comments -------------------------------------------------------------------
157158-- | Scan a Haskell block comment.
158159scanHaskellCommentBlock
159160 :: Monad m
160- => Scanner m loc [Char ] (loc , String )
161+ => Scanner m loc [Char ] (Range loc , String )
161162
162163scanHaskellCommentBlock
163164 = munchFold Nothing matchC (' ' , True ) acceptC
@@ -177,14 +178,14 @@ scanHaskellCommentBlock
177178
178179{-# SPECIALIZE INLINE
179180 scanHaskellCommentBlock
180- :: Scanner IO Location [Char] (Location, String)
181+ :: Scanner IO Location [Char] (Range Location, String)
181182 #-}
182183
183184
184185-- | Scan a Haskell line comment.
185186scanHaskellCommentLine
186187 :: Monad m
187- => Scanner m loc [Char ] (loc , String )
188+ => Scanner m loc [Char ] (Range loc , String )
188189
189190scanHaskellCommentLine
190191 = munchPred Nothing matchC acceptC
@@ -201,7 +202,7 @@ scanHaskellCommentLine
201202
202203{-# SPECIALIZE INLINE
203204 scanHaskellCommentLine
204- :: Scanner IO Location [Char] (Location, String)
205+ :: Scanner IO Location [Char] (Range Location, String)
205206 #-}
206207
207208
@@ -223,12 +224,13 @@ decodeString ss0
223224 Just (Nothing , cs') -> go acc cs'
224225 Nothing -> go (c : acc) cs
225226
226- -- | Result of reading a character: either a real char, or an empty string that is a
227- -- successful read, but contains no characters.
228- -- These empty strings are sometimes required to remove ambiguity: for example,
229- -- '\SO' and '\SOH' are both valid escapes.
230- -- To distinguish between the strings ['\SO', 'H'] and ['\SOH'], it is necessary
231- -- to explicitly terminate the escape for the former: '\SO\&H' means ['\SO', 'H'].
227+ -- | Result of reading a character: either a real char, or an empty string
228+ -- that is a successful read, but contains no characters.
229+ -- These empty strings are sometimes required to remove ambiguity:
230+ -- for example,'\SO' and '\SOH' are both valid escapes.
231+ -- To distinguish between the strings ['\SO', 'H'] and ['\SOH'],
232+ -- it is necessary to explicitly terminate the escape for the former:
233+ -- '\SO\&H' means ['\SO', 'H'].
232234type CharGap = Maybe Char
233235
234236-- | Read a character literal, handling escape codes.
@@ -246,13 +248,13 @@ readChar ('\\' : 'o' : cs)
246248
247249-- Control characters defined by carret characters, like \^G
248250readChar (' \\ ' : ' ^' : c : rest)
249- | c >= ' A' && c <= ' Z' = Just (Just $ Char. chr (Char. ord c - 1 ), rest)
250- | c == ' @' = Just (Just $ Char. chr 0 , rest)
251- | c == ' [' = Just (Just $ Char. chr 27 , rest)
252- | c == ' \\ ' = Just (Just $ Char. chr 28 , rest)
253- | c == ' ]' = Just (Just $ Char. chr 29 , rest)
254- | c == ' ^' = Just (Just $ Char. chr 30 , rest)
255- | c == ' _' = Just (Just $ Char. chr 31 , rest)
251+ | c >= ' A' && c <= ' Z' = Just (Just $ Char. chr (Char. ord c - 1 ), rest)
252+ | c == ' @' = Just (Just $ Char. chr 0 , rest)
253+ | c == ' [' = Just (Just $ Char. chr 27 , rest)
254+ | c == ' \\ ' = Just (Just $ Char. chr 28 , rest)
255+ | c == ' ]' = Just (Just $ Char. chr 29 , rest)
256+ | c == ' ^' = Just (Just $ Char. chr 30 , rest)
257+ | c == ' _' = Just (Just $ Char. chr 31 , rest)
256258
257259-- Control characters defined by decimal escape codes.
258260readChar (' \\ ' : cs)
@@ -276,16 +278,16 @@ readChar ('\\' : cs)
276278 = let go [] = Nothing
277279 go ((str, c) : moar)
278280 = case List. stripPrefix str cs of
279- Nothing -> go moar
280- Just rest -> Just (Just c, rest)
281+ Nothing -> go moar
282+ Just rest -> Just (Just c, rest)
281283
282284 in go escapedChars
283285
284286-- Just a regular character.
285- readChar (c : rest) = Just (Just c, rest)
287+ readChar (c : rest) = Just (Just c, rest)
286288
287289-- Nothing to read.
288- readChar _ = Nothing
290+ readChar _ = Nothing
289291
290292escapedChars :: [(String , Char )]
291293escapedChars
0 commit comments