1module Language.Haskell.Lexer.Utils
2  ( module Language.Haskell.Lexer.Utils
3  , Token(..)
4  ) where
5
6import Language.Haskell.Lexer.Tokens
7
8gotEOF :: [a] -> [(Token,[a])]
9gotEOF [] = []
10gotEOF as = [(GotEOF, reverse as)]
11
12gotError :: [a] -> [a] -> [(Token, [a])]
13gotError as is =
14  (ErrorToken, reverse as):
15  if null is then [(GotEOF,[])] else [(TheRest,is{-reverse (take 80 is)-})]
16
17-- Inlining the call to output does not make a big difference.
18--output token as cont = (token, reverse as):cont
19
20-- Not reversing the token string seems to save about 10% of the time with HBC.
21-- The difference in speed seems insignificant with ghc-6.0.1 -O.
22
23output :: t -> [a] -> [(t, [a])] -> [(t, [a])]
24output token as cont = (token,reverse as):cont
25
26-- This avoids constructing a closure for the call to reverse.
27-- This saves about 10% too.
28{-
29output token as cont =
30    rev as []
31  where
32    rev [] as' = (token,as'):cont
33    rev (a:as) as' = rev as (a:as')
34--}
35
36nestedComment :: [Char] -> [Char] -> (([a] -> [a] -> [(Token, [a])])
37              -> [Char] -> [Char] -> [(Token, [Char])]) -> [(Token, [Char])]
38
39nestedComment as' is' next = nest (0::Int) as' is'
40  where
41    nest n as is =
42      case is of
43        '-' : '}' : is1   -> if n == 0
44                               then next gotError ('}':'-':as) is1
45                               else nest (n-1) ('}':'-':as) is1
46        '{' : '-' : is1   -> nest (n+1) ('-':'{':as) is1
47        c : is1           -> nest n (c:as) is1
48        []                -> gotError as is -- EOF inside comment
49