1 -----------------------------------------------------------------------------
2 -- (c) The University of Glasgow, 2004
4 -- Lexer for concrete Cmm. We try to stay close to the C-- spec, but there
5 -- are a few minor differences:
7 -- * extra keywords for our macros, and float32/float64 types
8 -- * global registers (Sp,Hp, etc.)
10 -----------------------------------------------------------------------------
17 #include "HsVersions.h"
27 import Util ( readRational )
31 $whitechar = [\ \t\n\r\f\v\xa0]
32 $white_no_nl = $whitechar # \n
36 $digit = [$ascdigit $unidigit]
38 $hexit = [$digit A-F a-f]
41 $asclarge = [A-Z \xc0-\xd6 \xd8-\xde]
42 $large = [$asclarge $unilarge]
45 $ascsmall = [a-z \xdf-\xf6 \xf8-\xff]
46 $small = [$ascsmall $unismall \_]
48 $namebegin = [$large $small \_ \. \$ \@]
49 $namechar = [$namebegin $digit]
53 @hexadecimal = $hexit+
54 @exponent = [eE] [\-\+]? @decimal
56 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
58 @escape = \\ ([abfnrt\\\'\"\?] | x @hexadecimal | @octal)
59 @strchar = ($printable # [\"\\]) | @escape
65 ^\# (line)? { begin line_prag }
67 -- single-line line pragmas, of the form
68 -- # <line> "<file>" <extra-stuff> \n
69 <line_prag> $digit+ { setLine line_prag1 }
70 <line_prag1> \" ($printable # \")* \" { setFile line_prag2 }
71 <line_prag2> .* { pop }
76 [\:\;\{\}\[\]\(\)\=\`\~\/\*\%\-\+\&\^\|\>\<\,\!] { special_char }
78 ".." { kw CmmT_DotDot }
79 "::" { kw CmmT_DoubleColon }
86 "&&" { kw CmmT_BoolAnd }
87 "||" { kw CmmT_BoolOr }
89 R@decimal { global_regN VanillaReg }
90 F@decimal { global_regN FloatReg }
91 D@decimal { global_regN DoubleReg }
92 L@decimal { global_regN LongReg }
94 SpLim { global_reg SpLim }
96 HpLim { global_reg HpLim }
97 CurrentTSO { global_reg CurrentTSO }
98 CurrentNursery { global_reg CurrentNursery }
99 HpAlloc { global_reg HpAlloc }
101 $namebegin $namechar* { name }
103 0 @octal { tok_octal }
104 @decimal { tok_decimal }
105 0[xX] @hexadecimal { tok_hexadecimal }
106 @floating_point { strtoken tok_float }
108 \" @strchar* \" { strtoken tok_string }
126 | CmmT_INFO_TABLE_RET
127 | CmmT_INFO_TABLE_FUN
128 | CmmT_INFO_TABLE_CONSTR
129 | CmmT_INFO_TABLE_SELECTOR
148 | CmmT_GlobalReg GlobalReg
149 | CmmT_Name FastString
152 | CmmT_Float Rational
158 -- -----------------------------------------------------------------------------
161 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
163 begin :: Int -> Action
164 begin code _span _str _len = do pushLexState code; lexToken
167 pop _span _buf _len = do popLexState; lexToken
169 special_char :: Action
170 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
172 kw :: CmmToken -> Action
173 kw tok span buf len = return (L span tok)
175 global_regN :: (Int -> GlobalReg) -> Action
176 global_regN con span buf len
177 = return (L span (CmmT_GlobalReg (con (fromIntegral n))))
178 where buf' = stepOn buf
179 n = parseInteger buf' (len-1) 10 octDecDigit
181 global_reg :: GlobalReg -> Action
182 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
184 strtoken :: (String -> CmmToken) -> Action
185 strtoken f span buf len =
186 return (L span $! (f $! lexemeToString buf len))
190 case lookupUFM reservedWordsFM fs of
191 Just tok -> return (L span tok)
192 Nothing -> return (L span (CmmT_Name fs))
194 fs = lexemeToFastString buf len
196 reservedWordsFM = listToUFM $
197 map (\(x, y) -> (mkFastString x, y)) [
198 ( "CLOSURE", CmmT_CLOSURE ),
199 ( "INFO_TABLE", CmmT_INFO_TABLE ),
200 ( "INFO_TABLE_RET", CmmT_INFO_TABLE_RET ),
201 ( "INFO_TABLE_FUN", CmmT_INFO_TABLE_FUN ),
202 ( "INFO_TABLE_CONSTR", CmmT_INFO_TABLE_CONSTR ),
203 ( "INFO_TABLE_SELECTOR",CmmT_INFO_TABLE_SELECTOR ),
204 ( "else", CmmT_else ),
205 ( "export", CmmT_export ),
206 ( "section", CmmT_section ),
207 ( "align", CmmT_align ),
208 ( "goto", CmmT_goto ),
210 ( "jump", CmmT_jump ),
211 ( "foreign", CmmT_foreign ),
212 ( "import", CmmT_import ),
213 ( "switch", CmmT_switch ),
214 ( "case", CmmT_case ),
215 ( "default", CmmT_default ),
216 ( "bits8", CmmT_bits8 ),
217 ( "bits16", CmmT_bits16 ),
218 ( "bits32", CmmT_bits32 ),
219 ( "bits64", CmmT_bits64 ),
220 ( "float32", CmmT_float32 ),
221 ( "float64", CmmT_float64 )
224 tok_decimal span buf len
225 = return (L span (CmmT_Int $! parseInteger buf len 10 octDecDigit))
227 tok_octal span buf len
228 = return (L span (CmmT_Int $! parseInteger (stepOn buf) (len-1) 8 octDecDigit))
230 tok_hexadecimal span buf len
231 = return (L span (CmmT_Int $! parseInteger (stepOnBy 2 buf) (len-2) 16 hexDigit))
233 tok_float str = CmmT_Float $! readRational str
235 tok_string str = CmmT_String (read str)
236 -- urk, not quite right, but it'll do for now
238 -- -----------------------------------------------------------------------------
241 setLine :: Int -> Action
242 setLine code span buf len = do
243 let line = parseInteger buf len 10 octDecDigit
244 setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 0)
245 -- subtract one: the line number refers to the *following* line
246 -- trace ("setLine " ++ show line) $ do
251 setFile :: Int -> Action
252 setFile code span buf len = do
253 let file = lexemeToFastString (stepOn buf) (len-2)
254 setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
259 -- -----------------------------------------------------------------------------
260 -- This is the top-level function: called from the parser each time a
261 -- new token is to be read from the input.
263 cmmlex :: (Located CmmToken -> P a) -> P a
265 tok@(L _ tok__) <- lexToken
266 --trace ("token: " ++ show tok__) $ do
269 lexToken :: P (Located CmmToken)
271 inp@(loc1,buf) <- getInput
273 case alexScan inp sc of
274 AlexEOF -> do let span = mkSrcSpan loc1 loc1
276 return (L span CmmT_EOF)
277 AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
278 AlexSkip inp2 _ -> do
281 AlexToken inp2@(end,buf2) len t -> do
283 let span = mkSrcSpan loc1 end
284 span `seq` setLastToken span len
287 -- -----------------------------------------------------------------------------
290 -- Stuff that Alex needs to know about our input type:
291 type AlexInput = (SrcLoc,StringBuffer)
293 alexInputPrevChar :: AlexInput -> Char
294 alexInputPrevChar (_,s) = prevChar s '\n'
296 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
299 | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
300 where c = currentChar s
301 loc' = advanceSrcLoc loc c
304 getInput :: P AlexInput
305 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
307 setInput :: AlexInput -> P ()
308 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()