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
64 ^\# pragma .* \n ; -- Apple GCC 3.3 CPP generates pragmas in its output
66 ^\# (line)? { begin line_prag }
68 -- single-line line pragmas, of the form
69 -- # <line> "<file>" <extra-stuff> \n
70 <line_prag> $digit+ { setLine line_prag1 }
71 <line_prag1> \" ($printable # \")* \" { setFile line_prag2 }
72 <line_prag2> .* { pop }
77 [\:\;\{\}\[\]\(\)\=\`\~\/\*\%\-\+\&\^\|\>\<\,\!] { special_char }
79 ".." { kw CmmT_DotDot }
80 "::" { kw CmmT_DoubleColon }
87 "&&" { kw CmmT_BoolAnd }
88 "||" { kw CmmT_BoolOr }
90 R@decimal { global_regN VanillaReg }
91 F@decimal { global_regN FloatReg }
92 D@decimal { global_regN DoubleReg }
93 L@decimal { global_regN LongReg }
95 SpLim { global_reg SpLim }
97 HpLim { global_reg HpLim }
98 CurrentTSO { global_reg CurrentTSO }
99 CurrentNursery { global_reg CurrentNursery }
100 HpAlloc { global_reg HpAlloc }
101 BaseReg { global_reg BaseReg }
103 $namebegin $namechar* { name }
105 0 @octal { tok_octal }
106 @decimal { tok_decimal }
107 0[xX] @hexadecimal { tok_hexadecimal }
108 @floating_point { strtoken tok_float }
110 \" @strchar* \" { strtoken tok_string }
128 | CmmT_INFO_TABLE_RET
129 | CmmT_INFO_TABLE_FUN
130 | CmmT_INFO_TABLE_CONSTR
131 | CmmT_INFO_TABLE_SELECTOR
151 | CmmT_GlobalReg GlobalReg
152 | CmmT_Name FastString
155 | CmmT_Float Rational
161 -- -----------------------------------------------------------------------------
164 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
166 begin :: Int -> Action
167 begin code _span _str _len = do pushLexState code; lexToken
170 pop _span _buf _len = do popLexState; lexToken
172 special_char :: Action
173 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
175 kw :: CmmToken -> Action
176 kw tok span buf len = return (L span tok)
178 global_regN :: (Int -> GlobalReg) -> Action
179 global_regN con span buf len
180 = return (L span (CmmT_GlobalReg (con (fromIntegral n))))
181 where buf' = stepOn buf
182 n = parseInteger buf' (len-1) 10 octDecDigit
184 global_reg :: GlobalReg -> Action
185 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
187 strtoken :: (String -> CmmToken) -> Action
188 strtoken f span buf len =
189 return (L span $! (f $! lexemeToString buf len))
193 case lookupUFM reservedWordsFM fs of
194 Just tok -> return (L span tok)
195 Nothing -> return (L span (CmmT_Name fs))
197 fs = lexemeToFastString buf len
199 reservedWordsFM = listToUFM $
200 map (\(x, y) -> (mkFastString x, y)) [
201 ( "CLOSURE", CmmT_CLOSURE ),
202 ( "INFO_TABLE", CmmT_INFO_TABLE ),
203 ( "INFO_TABLE_RET", CmmT_INFO_TABLE_RET ),
204 ( "INFO_TABLE_FUN", CmmT_INFO_TABLE_FUN ),
205 ( "INFO_TABLE_CONSTR", CmmT_INFO_TABLE_CONSTR ),
206 ( "INFO_TABLE_SELECTOR",CmmT_INFO_TABLE_SELECTOR ),
207 ( "else", CmmT_else ),
208 ( "export", CmmT_export ),
209 ( "section", CmmT_section ),
210 ( "align", CmmT_align ),
211 ( "goto", CmmT_goto ),
213 ( "jump", CmmT_jump ),
214 ( "foreign", CmmT_foreign ),
215 ( "prim", CmmT_prim ),
216 ( "import", CmmT_import ),
217 ( "switch", CmmT_switch ),
218 ( "case", CmmT_case ),
219 ( "default", CmmT_default ),
220 ( "bits8", CmmT_bits8 ),
221 ( "bits16", CmmT_bits16 ),
222 ( "bits32", CmmT_bits32 ),
223 ( "bits64", CmmT_bits64 ),
224 ( "float32", CmmT_float32 ),
225 ( "float64", CmmT_float64 )
228 tok_decimal span buf len
229 = return (L span (CmmT_Int $! parseInteger buf len 10 octDecDigit))
231 tok_octal span buf len
232 = return (L span (CmmT_Int $! parseInteger (offsetBytes 1 buf) (len-1) 8 octDecDigit))
234 tok_hexadecimal span buf len
235 = return (L span (CmmT_Int $! parseInteger (offsetBytes 2 buf) (len-2) 16 hexDigit))
237 tok_float str = CmmT_Float $! readRational str
239 tok_string str = CmmT_String (read str)
240 -- urk, not quite right, but it'll do for now
242 -- -----------------------------------------------------------------------------
245 setLine :: Int -> Action
246 setLine code span buf len = do
247 let line = parseInteger buf len 10 octDecDigit
248 setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 0)
249 -- subtract one: the line number refers to the *following* line
250 -- trace ("setLine " ++ show line) $ do
255 setFile :: Int -> Action
256 setFile code span buf len = do
257 let file = lexemeToFastString (stepOn buf) (len-2)
258 setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
263 -- -----------------------------------------------------------------------------
264 -- This is the top-level function: called from the parser each time a
265 -- new token is to be read from the input.
267 cmmlex :: (Located CmmToken -> P a) -> P a
269 tok@(L _ tok__) <- lexToken
270 --trace ("token: " ++ show tok__) $ do
273 lexToken :: P (Located CmmToken)
275 inp@(loc1,buf) <- getInput
277 case alexScan inp sc of
278 AlexEOF -> do let span = mkSrcSpan loc1 loc1
280 return (L span CmmT_EOF)
281 AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
282 AlexSkip inp2 _ -> do
285 AlexToken inp2@(end,buf2) len t -> do
287 let span = mkSrcSpan loc1 end
288 span `seq` setLastToken span len
291 -- -----------------------------------------------------------------------------
294 -- Stuff that Alex needs to know about our input type:
295 type AlexInput = (SrcLoc,StringBuffer)
297 alexInputPrevChar :: AlexInput -> Char
298 alexInputPrevChar (_,s) = prevChar s '\n'
300 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
303 | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
304 where c = currentChar s
305 loc' = advanceSrcLoc loc c
308 getInput :: P AlexInput
309 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
311 setInput :: AlexInput -> P ()
312 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()