2
-- alex scanner for use with uulib
3
-- compile with alex -o Scanner.hs -g Scanner.x
4
module Scanner(tokenize) where
10
$identChar = [a-zA-Z0-9\'_]
14
$white+ ; -- whitespace
17
\" ($litChar | \\ \\ | \\ \" )* \" { valueToken TkString } -- string
18
[0-9]+ { valueToken TkInteger16 } -- int
20
( let | in ) { reserved } -- reserved keywords
21
[\(\)\=] { reserved } -- reserved symbols
22
[\+\*] { reserved } -- operators
24
[a-zA-Z] $identChar* { valueToken TkVarid } -- identifier
28
-- boilerplate code needed for Alex
29
type AlexInput = (Pos, String)
31
alexInputPrevChar :: AlexInput -> Char
32
alexInputPrevChar = error "alexInputPrevChar: there is no need to go back in the input."
34
alexGetChar :: AlexInput -> Maybe (Char, AlexInput)
35
alexGetChar (_, []) = Nothing
36
alexGetChar (p, (c:cs))
40
-- use the Alex scanner to generate a list of tokens for the uulib token parsers
41
tokenize :: String -> String -> [Token]
45
initpos = Pos 1 1 filename
48
= case alexScan inp 0 of
50
AlexError inp' -> valueToken TkError [head cs] pos : go inp'
51
AlexSkip inp' _ -> go inp'
52
AlexToken inp' len act -> act (take len cs) pos : go inp'