2 -- This Alex file was machine-generated by the BNF converter
11 $l = [a-zA-Z\192 - \255] # [\215 \247] -- isolatin1 letter FIXME
12 $c = [A-Z\192-\221] # [\215] -- capital isolatin1 letter FIXME
13 $s = [a-z\222-\255] # [\247] -- small isolatin1 letter FIXME
15 $i = [$l $d _ '] -- identifier character
16 $u = [\0-\255] -- universal: any character
18 @rsyms = -- symbols and non-identifier-like reserved words
19 \; | \{ | \} | \= | \( | \) | \+ \+ | \- \- | \- | \! | \, | \< | \< \= | \> | \> \= | \= \= | \! \= | \+ | \* | \/
22 "//" [.]* ; -- Toss single line comments
23 "/*" ([$u # \*] | \* [$u # \/])* ("*")+ "/" ;
26 @rsyms { tok (\p s -> PT p (TS $ share s)) }
28 $l $i* { tok (\p s -> PT p (eitherResIdent (TV . share) s)) }
31 $d+ { tok (\p s -> PT p (TI $ share s)) }
38 share :: String -> String
42 TS !String -- reserved words and symbols
43 | TL !String -- string literals
44 | TI !String -- integer literals
45 | TV !String -- identifiers
46 | TD !String -- double precision float literals
47 | TC !String -- character literals
49 deriving (Eq,Show,Ord)
54 deriving (Eq,Show,Ord)
56 tokenPos (PT (Pn _ l _) _ :_) = "line " ++ show l
57 tokenPos (Err (Pn _ l _) :_) = "line " ++ show l
58 tokenPos _ = "end of file"
60 posLineCol (Pn _ l c) = (l,c)
61 mkPosToken t@(PT p _) = (posLineCol p, prToken t)
72 data BTree = N | B String Tok BTree BTree deriving (Show)
74 eitherResIdent :: (String -> Tok) -> String -> Tok
75 eitherResIdent tv s = treeFind resWords
78 treeFind (B a t left right) | s < a = treeFind left
79 | s > a = treeFind right
82 resWords = b "print" (b "false" (b "else" (b "bool" N N) N) (b "int" (b "if" N N) N)) (b "return" (b "readInt" (b "readBool" N N) N) (b "while" (b "true" N N) N))
83 where b s = B s (TS s)
85 unescapeInitTail :: String -> String
86 unescapeInitTail = unesc . tail where
88 '\\':c:cs | elem c ['\"', '\\', '\''] -> c : unesc cs
89 '\\':'n':cs -> '\n' : unesc cs
90 '\\':'t':cs -> '\t' : unesc cs
95 -------------------------------------------------------------------
97 -- A modified "posn" wrapper.
98 -------------------------------------------------------------------
100 data Posn = Pn !Int !Int !Int
101 deriving (Eq, Show,Ord)
104 alexStartPos = Pn 0 1 1
106 alexMove :: Posn -> Char -> Posn
107 alexMove (Pn a l c) '\t' = Pn (a+1) l (((c+7) `div` 8)*8+1)
108 alexMove (Pn a l c) '\n' = Pn (a+1) (l+1) 1
109 alexMove (Pn a l c) _ = Pn (a+1) l (c+1)
111 type AlexInput = (Posn, -- current position,
112 Char, -- previous char
113 String) -- current input string
115 tokens :: String -> [Token]
116 tokens str = go (alexStartPos, '\n', str)
118 go :: (Posn, Char, String) -> [Token]
119 go inp@(pos, _, str) =
120 case alexScan inp 0 of
122 AlexError (pos, _, _) -> fail $ show pos ++ ": lexical error"
123 AlexSkip inp' len -> go inp'
124 AlexToken inp' len act -> act pos (take len str) : (go inp')
126 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
127 alexGetChar (p, c, []) = Nothing
128 alexGetChar (p, _, (c:s)) =
129 let p' = alexMove p c
130 in p' `seq` Just (c, (p', c, s))
132 alexInputPrevChar :: AlexInput -> Char
133 alexInputPrevChar (p, c, s) = c