13 initLexer :: String -> [Token]
14 initLexer str = [ t | (_,_,t) <- lexer str 1 0 ]
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)
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 = []
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
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
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"
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"
50 t <- readFile "EXAMPLE.tc"