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
150 | CmmT_GlobalReg GlobalReg
151 | CmmT_Name FastString
154 | CmmT_Float Rational
160 -- -----------------------------------------------------------------------------
163 type Action = SrcSpan -> StringBuffer -> Int -> P (Located CmmToken)
165 begin :: Int -> Action
166 begin code _span _str _len = do pushLexState code; lexToken
169 pop _span _buf _len = do popLexState; lexToken
171 special_char :: Action
172 special_char span buf len = return (L span (CmmT_SpecChar (currentChar buf)))
174 kw :: CmmToken -> Action
175 kw tok span buf len = return (L span tok)
177 global_regN :: (Int -> GlobalReg) -> Action
178 global_regN con span buf len
179 = return (L span (CmmT_GlobalReg (con (fromIntegral n))))
180 where buf' = stepOn buf
181 n = parseInteger buf' (len-1) 10 octDecDigit
183 global_reg :: GlobalReg -> Action
184 global_reg r span buf len = return (L span (CmmT_GlobalReg r))
186 strtoken :: (String -> CmmToken) -> Action
187 strtoken f span buf len =
188 return (L span $! (f $! lexemeToString buf len))
192 case lookupUFM reservedWordsFM fs of
193 Just tok -> return (L span tok)
194 Nothing -> return (L span (CmmT_Name fs))
196 fs = lexemeToFastString buf len
198 reservedWordsFM = listToUFM $
199 map (\(x, y) -> (mkFastString x, y)) [
200 ( "CLOSURE", CmmT_CLOSURE ),
201 ( "INFO_TABLE", CmmT_INFO_TABLE ),
202 ( "INFO_TABLE_RET", CmmT_INFO_TABLE_RET ),
203 ( "INFO_TABLE_FUN", CmmT_INFO_TABLE_FUN ),
204 ( "INFO_TABLE_CONSTR", CmmT_INFO_TABLE_CONSTR ),
205 ( "INFO_TABLE_SELECTOR",CmmT_INFO_TABLE_SELECTOR ),
206 ( "else", CmmT_else ),
207 ( "export", CmmT_export ),
208 ( "section", CmmT_section ),
209 ( "align", CmmT_align ),
210 ( "goto", CmmT_goto ),
212 ( "jump", CmmT_jump ),
213 ( "foreign", CmmT_foreign ),
214 ( "import", CmmT_import ),
215 ( "switch", CmmT_switch ),
216 ( "case", CmmT_case ),
217 ( "default", CmmT_default ),
218 ( "bits8", CmmT_bits8 ),
219 ( "bits16", CmmT_bits16 ),
220 ( "bits32", CmmT_bits32 ),
221 ( "bits64", CmmT_bits64 ),
222 ( "float32", CmmT_float32 ),
223 ( "float64", CmmT_float64 )
226 tok_decimal span buf len
227 = return (L span (CmmT_Int $! parseInteger buf len 10 octDecDigit))
229 tok_octal span buf len
230 = return (L span (CmmT_Int $! parseInteger (stepOn buf) (len-1) 8 octDecDigit))
232 tok_hexadecimal span buf len
233 = return (L span (CmmT_Int $! parseInteger (stepOnBy 2 buf) (len-2) 16 hexDigit))
235 tok_float str = CmmT_Float $! readRational str
237 tok_string str = CmmT_String (read str)
238 -- urk, not quite right, but it'll do for now
240 -- -----------------------------------------------------------------------------
243 setLine :: Int -> Action
244 setLine code span buf len = do
245 let line = parseInteger buf len 10 octDecDigit
246 setSrcLoc (mkSrcLoc (srcSpanFile span) (fromIntegral line - 1) 0)
247 -- subtract one: the line number refers to the *following* line
248 -- trace ("setLine " ++ show line) $ do
253 setFile :: Int -> Action
254 setFile code span buf len = do
255 let file = lexemeToFastString (stepOn buf) (len-2)
256 setSrcLoc (mkSrcLoc file (srcSpanEndLine span) (srcSpanEndCol span))
261 -- -----------------------------------------------------------------------------
262 -- This is the top-level function: called from the parser each time a
263 -- new token is to be read from the input.
265 cmmlex :: (Located CmmToken -> P a) -> P a
267 tok@(L _ tok__) <- lexToken
268 --trace ("token: " ++ show tok__) $ do
271 lexToken :: P (Located CmmToken)
273 inp@(loc1,buf) <- getInput
275 case alexScan inp sc of
276 AlexEOF -> do let span = mkSrcSpan loc1 loc1
278 return (L span CmmT_EOF)
279 AlexError (loc2,_) -> do failLocMsgP loc1 loc2 "lexical error"
280 AlexSkip inp2 _ -> do
283 AlexToken inp2@(end,buf2) len t -> do
285 let span = mkSrcSpan loc1 end
286 span `seq` setLastToken span len
289 -- -----------------------------------------------------------------------------
292 -- Stuff that Alex needs to know about our input type:
293 type AlexInput = (SrcLoc,StringBuffer)
295 alexInputPrevChar :: AlexInput -> Char
296 alexInputPrevChar (_,s) = prevChar s '\n'
298 alexGetChar :: AlexInput -> Maybe (Char,AlexInput)
301 | otherwise = c `seq` loc' `seq` s' `seq` Just (c, (loc', s'))
302 where c = currentChar s
303 loc' = advanceSrcLoc loc c
306 getInput :: P AlexInput
307 getInput = P $ \s@PState{ loc=l, buffer=b } -> POk s (l,b)
309 setInput :: AlexInput -> P ()
310 setInput (l,b) = P $ \s -> POk s{ loc=l, buffer=b } ()