External Core typechecker - improve handling of coercions
[ghc-hetmet.git] / utils / hpc / HpcLexer.hs
1 module HpcLexer where
2
3 import Data.Char
4
5 data Token 
6         = ID String
7         | SYM Char
8         | INT Int
9         | STR String
10         | CAT String
11         deriving (Eq,Show)
12
13 initLexer :: String -> [Token]
14 initLexer str = [ t | (_,_,t) <- lexer str 1 0 ]
15
16 lexer :: String -> Int -> Int ->  [(Int,Int,Token)]
17 lexer (c:cs) line column
18   | c == '\n' = lexer cs (succ line) 0
19   | c == '\"' = lexerSTR cs line (succ column)
20   | c == '[' = lexerCAT cs "" line (succ column)
21   | c `elem` "{};-:" 
22               = (line,column,SYM c) : lexer cs line (succ column)
23   | isSpace c = lexer cs        line (succ column)
24   | isAlpha c = lexerKW  cs [c] line (succ column)
25   | isDigit c = lexerINT cs [c] line (succ column)
26   | otherwise = error "lexer failure"
27 lexer [] line colunm = []
28
29 lexerKW  (c:cs) s line column
30   | isAlpha c = lexerKW cs (s ++ [c]) line (succ column)
31 lexerKW  other s line column = (line,column,ID s) : lexer other line column
32
33 lexerINT  (c:cs) s line column
34   | isDigit c = lexerINT cs (s ++ [c]) line (succ column)
35 lexerINT  other s line column = (line,column,INT (read s)) : lexer other line column
36
37 -- not technically correct for the new column count, but a good approximation.
38 lexerSTR cs line column
39   = case lex ('"' : cs) of
40       [(str,rest)] -> (line,succ column,STR (read str))
41                    : lexer rest line (length (show str) + column + 1)
42       _ -> error "bad string"
43
44 lexerCAT (c:cs) s line column
45   | c == ']'  =  (line,column,CAT s) : lexer cs line (succ column)
46   | otherwise = lexerCAT cs (s ++ [c]) line (succ column)
47 lexerCAT  other s line column = error "lexer failure in CAT"
48
49 test = do
50           t <- readFile "EXAMPLE.tc"
51           print (initLexer t)
52