1 -----------------------------------------------------------------------------
3 -- (c) The University of Glasgow, 2004-2006
5 -- Lexer for concrete Cmm. We try to stay close to the C-- spec, but there
6 -- are a few minor differences:
8 -- * extra keywords for our macros, and float32/float64 types
9 -- * global registers (Sp,Hp, etc.)
11 -----------------------------------------------------------------------------
18 #include "HsVersions.h"
32 $whitechar = [\ \t\n\r\f\v\xa0] -- \xa0 is Unicode no-break space
33 $white_no_nl = $whitechar # \n
36 $unidigit = \x01 -- Trick Alex into handling Unicode. See alexGetChar.
37 $digit = [$ascdigit $unidigit]
39 $hexit = [$digit A-F a-f]
41 $unilarge = \x03 -- Trick Alex into handling Unicode. See alexGetChar.
42 $asclarge = [A-Z \xc0-\xd6 \xd8-\xde]
43 $large = [$asclarge $unilarge]
45 $unismall = \x04 -- Trick Alex into handling Unicode. See alexGetChar.
46 $ascsmall = [a-z \xdf-\xf6 \xf8-\xff]
47 $small = [$ascsmall $unismall \_]
49 $namebegin = [$large $small \. \$ \@]
50 $namechar = [$namebegin $digit]
54 @hexadecimal = $hexit+
55 @exponent = [eE] [\-\+]? @decimal
57 @floating_point = @decimal \. @decimal @exponent? | @decimal @exponent
59 @escape = \\ ([abfnrt\\\'\"\?] | x $hexit{1,2} | $octit{1,3})
60 @strchar = ($printable # [\"\\]) | @escape
65 ^\# pragma .* \n ; -- Apple GCC 3.3 CPP generates pragmas in its output
67 ^\# (line)? { begin line_prag }
69 -- single-line line pragmas, of the form
70 -- # <line> "<file>" <extra-stuff> \n
71 <line_prag> $digit+ { setLine line_prag1 }
72 <line_prag1> \" ($printable # \")* \" { setFile line_prag2 }
73 <line_prag2> .* { pop }
78 [\:\;\{\}\[\]\(\)\=\`\~\/\*\%\-\+\&\^\|\>\<\,\!] { special_char }
80 ".." { kw CmmT_DotDot }
81 "::" { kw CmmT_DoubleColon }
88 "&&" { kw CmmT_BoolAnd }
89 "||" { kw CmmT_BoolOr }
91 R@decimal { global_regN VanillaReg }
92 F@decimal { global_regN FloatReg }
93 D@decimal { global_regN DoubleReg }
94 L@decimal { global_regN LongReg }
96 SpLim { global_reg SpLim }
98 HpLim { global_reg HpLim }
99 CurrentTSO { global_reg CurrentTSO }
100 CurrentNursery { global_reg CurrentNursery }
101 HpAlloc { global_reg HpAlloc }
102 BaseReg { global_reg BaseReg }
104 $namebegin $namechar* { name }
106 0 @octal { tok_octal }
107 @decimal { tok_decimal }
108 0[xX] @hexadecimal { tok_hexadecimal }
109 @floating_point { strtoken tok_float }
111 \" @strchar* \" { strtoken tok_string }
129 | CmmT_INFO_TABLE_RET
130 | CmmT_INFO_TABLE_FUN
131 | CmmT_INFO_TABLE_CONSTR
132 | CmmT_INFO_TABLE_SELECTOR
153 | CmmT_GlobalReg GlobalReg
154 | CmmT_Name FastString
157 | CmmT_Float Rational
163 -- -----------------------------------------------------------------------------
166 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
168 begin :: Int -> Action
169 begin code _span _str _len = do pushLexState code; lexToken
172 pop _span _buf _len = do popLexState; lexToken
174 special_char :: Action
175 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
177 kw :: CmmToken -> Action
178 kw tok span buf len = return (L span tok)
180 global_regN :: (Int -> GlobalReg) -> Action
181 global_regN con span buf len
182 = return (L span (CmmT_GlobalReg (con (fromIntegral n))))
183 where buf' = stepOn buf
184 n = parseUnsignedInteger buf' (len-1) 10 octDecDigit
186 global_reg :: GlobalReg -> Action
187 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
189 strtoken :: (String -> CmmToken) -> Action
190 strtoken f span buf len =
191 return (L span $! (f $! lexemeToString buf len))
195 case lookupUFM reservedWordsFM fs of
196 Just tok -> return (L span tok)
197 Nothing -> return (L span (CmmT_Name fs))
199 fs = lexemeToFastString buf len
201 reservedWordsFM = listToUFM $
202 map (\(x, y) -> (mkFastString x, y)) [
203 ( "CLOSURE", CmmT_CLOSURE ),
204 ( "INFO_TABLE", CmmT_INFO_TABLE ),
205 ( "INFO_TABLE_RET", CmmT_INFO_TABLE_RET ),
206 ( "INFO_TABLE_FUN", CmmT_INFO_TABLE_FUN ),
207 ( "INFO_TABLE_CONSTR", CmmT_INFO_TABLE_CONSTR ),
208 ( "INFO_TABLE_SELECTOR",CmmT_INFO_TABLE_SELECTOR ),
209 ( "else", CmmT_else ),
210 ( "export", CmmT_export ),
211 ( "section", CmmT_section ),
212 ( "align", CmmT_align ),
213 ( "goto", CmmT_goto ),
215 ( "jump", CmmT_jump ),
216 ( "foreign", CmmT_foreign ),
217 ( "prim", CmmT_prim ),
218 ( "return", CmmT_return ),
219 ( "import", CmmT_import ),
220 ( "switch", CmmT_switch ),
221 ( "case", CmmT_case ),
222 ( "default", CmmT_default ),
223 ( "bits8", CmmT_bits8 ),
224 ( "bits16", CmmT_bits16 ),
225 ( "bits32", CmmT_bits32 ),
226 ( "bits64", CmmT_bits64 ),
227 ( "float32", CmmT_float32 ),
228 ( "float64", CmmT_float64 )
231 tok_decimal span buf len
232 = return (L span (CmmT_Int $! parseUnsignedInteger buf len 10 octDecDigit))
234 tok_octal span buf len
235 = return (L span (CmmT_Int $! parseUnsignedInteger (offsetBytes 1 buf) (len-1) 8 octDecDigit))
237 tok_hexadecimal span buf len
238 = return (L span (CmmT_Int $! parseUnsignedInteger (offsetBytes 2 buf) (len-2) 16 hexDigit))
240 tok_float str = CmmT_Float $! readRational str
242 tok_string str = CmmT_String (read str)
243 -- urk, not quite right, but it'll do for now
245 -- -----------------------------------------------------------------------------
248 setLine :: Int -> Action
249 setLine code span buf len = do
250 let line = parseUnsignedInteger buf len 10 octDecDigit
251 setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 0)
252 -- subtract one: the line number refers to the *following* line
253 -- trace ("setLine " ++ show line) $ do
258 setFile :: Int -> Action
259 setFile code span buf len = do
260 let file = lexemeToFastString (stepOn buf) (len-2)
261 setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
266 -- -----------------------------------------------------------------------------
267 -- This is the top-level function: called from the parser each time a
268 -- new token is to be read from the input.
270 cmmlex :: (Located CmmToken -> P a) -> P a
272 tok@(L _ tok__) <- lexToken
273 --trace ("token: " ++ show tok__) $ do
276 lexToken :: P (Located CmmToken)
278 inp@(loc1,buf) <- getInput
280 case alexScan inp sc of
281 AlexEOF -> do let span = mkSrcSpan loc1 loc1
282 setLastToken span 0 0
283 return (L span CmmT_EOF)
284 AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
285 AlexSkip inp2 _ -> do
288 AlexToken inp2@(end,buf2) len t -> do
290 let span = mkSrcSpan loc1 end
291 span `seq` setLastToken span len len
294 -- -----------------------------------------------------------------------------
297 -- Stuff that Alex needs to know about our input type:
298 type AlexInput = (SrcLoc,StringBuffer)
300 alexInputPrevChar :: AlexInput -> Char
301 alexInputPrevChar (_,s) = prevChar s '\n'
303 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
306 | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
307 where c = currentChar s
308 loc' = advanceSrcLoc loc c
311 getInput :: P AlexInput
312 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
314 setInput :: AlexInput -> P ()
315 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()