1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Text.Read.Lex
5 -- Copyright : (c) The University of Glasgow 2002
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : non-portable (uses Text.ParserCombinators.ReadP)
12 -- The cut-down Haskell lexer, used by Text.Read
14 -----------------------------------------------------------------------------
18 ( Lexeme(..) -- :: *; Show, Eq
21 , lex -- :: ReadP Lexeme Skips leading spaces
22 , hsLex -- :: ReadP String
23 , lexChar -- :: ReadP Char Reads just one char, with H98 escapes
25 , readIntP -- :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadP a
26 , readOctP -- :: Num a => ReadP a
27 , readDecP -- :: Num a => ReadP a
28 , readHexP -- :: Num a => ReadP a
32 import Text.ParserCombinators.ReadP
34 #ifdef __GLASGOW_HASKELL__
36 import GHC.Num( Num(..), Integer )
37 import GHC.Show( Show(..) )
39 import {-# SOURCE #-} GHC.Unicode ( isSpace, isAlpha, isAlphaNum )
41 import GHC.Real( Ratio(..), Integral, Rational, (%), fromIntegral,
42 toInteger, (^), (^^), infinity, notANumber )
44 import GHC.Enum( maxBound )
46 import Prelude hiding ( lex )
47 import Data.Char( chr, ord, isSpace, isAlpha, isAlphaNum )
48 import Data.Ratio( Ratio, (%) )
51 import Hugs.Prelude( Ratio(..) )
56 -- -----------------------------------------------------------------------------
61 = Char Char -- ^ Character literal
62 | String String -- ^ String literal, with escapes interpreted
63 | Punc String -- ^ Punctuation or reserved symbol, e.g. @(@, @::@
64 | Ident String -- ^ Haskell identifier, e.g. @foo@, @Baz@
65 | Symbol String -- ^ Haskell symbol, e.g. @>>@, @:%@
66 | Int Integer -- ^ Integer literal
67 | Rat Rational -- ^ Floating point literal
71 -- -----------------------------------------------------------------------------
75 lex = skipSpaces >> lexToken
78 -- ^ Haskell lexer: returns the lexed string, rather than the lexeme
80 (s,_) <- gather lexToken
83 lexToken :: ReadP Lexeme
93 -- ----------------------------------------------------------------------
95 lexEOF :: ReadP Lexeme
100 -- ---------------------------------------------------------------------------
101 -- Single character lexemes
103 lexPunc :: ReadP Lexeme
105 do c <- satisfy isPuncChar
108 isPuncChar c = c `elem` ",;()[]{}`"
110 -- ----------------------------------------------------------------------
113 lexSymbol :: ReadP Lexeme
115 do s <- munch1 isSymbolChar
116 if s `elem` reserved_ops then
117 return (Punc s) -- Reserved-ops count as punctuation
121 isSymbolChar c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
122 reserved_ops = ["..", "::", "=", "\\", "|", "<-", "->", "@", "~", "=>"]
124 -- ----------------------------------------------------------------------
127 lexId :: ReadP Lexeme
128 lexId = lex_nan <++ lex_id
130 -- NaN and Infinity look like identifiers, so
131 -- we parse them first.
132 lex_nan = (string "NaN" >> return (Rat notANumber)) +++
133 (string "Infinity" >> return (Rat infinity))
135 lex_id = do c <- satisfy isIdsChar
139 -- Identifiers can start with a '_'
140 isIdsChar c = isAlpha c || c == '_'
141 isIdfChar c = isAlphaNum c || c `elem` "_'"
143 #ifndef __GLASGOW_HASKELL__
144 infinity, notANumber :: Rational
149 -- ---------------------------------------------------------------------------
150 -- Lexing character literals
152 lexLitChar :: ReadP Lexeme
156 guard (esc || c /= '\'') -- Eliminate '' possibility
160 lexChar :: ReadP Char
161 lexChar = do { (c,_) <- lexCharE; return c }
163 lexCharE :: ReadP (Char, Bool) -- "escaped or not"?
167 then do c <- lexEsc; return (c, True)
168 else do return (c, False)
192 do base <- lexBaseChar <++ return 10
194 guard (n <= toInteger (ord maxBound))
195 return (chr (fromInteger n))
237 [ (string "SOH" >> return '\SOH') <++
238 (string "SO" >> return '\SO')
239 -- \SO and \SOH need maximal-munch treatment
240 -- See the Haskell report Sect 2.6
242 , string "NUL" >> return '\NUL'
243 , string "STX" >> return '\STX'
244 , string "ETX" >> return '\ETX'
245 , string "EOT" >> return '\EOT'
246 , string "ENQ" >> return '\ENQ'
247 , string "ACK" >> return '\ACK'
248 , string "BEL" >> return '\BEL'
249 , string "BS" >> return '\BS'
250 , string "HT" >> return '\HT'
251 , string "LF" >> return '\LF'
252 , string "VT" >> return '\VT'
253 , string "FF" >> return '\FF'
254 , string "CR" >> return '\CR'
255 , string "SI" >> return '\SI'
256 , string "DLE" >> return '\DLE'
257 , string "DC1" >> return '\DC1'
258 , string "DC2" >> return '\DC2'
259 , string "DC3" >> return '\DC3'
260 , string "DC4" >> return '\DC4'
261 , string "NAK" >> return '\NAK'
262 , string "SYN" >> return '\SYN'
263 , string "ETB" >> return '\ETB'
264 , string "CAN" >> return '\CAN'
265 , string "EM" >> return '\EM'
266 , string "SUB" >> return '\SUB'
267 , string "ESC" >> return '\ESC'
268 , string "FS" >> return '\FS'
269 , string "GS" >> return '\GS'
270 , string "RS" >> return '\RS'
271 , string "US" >> return '\US'
272 , string "SP" >> return '\SP'
273 , string "DEL" >> return '\DEL'
277 -- ---------------------------------------------------------------------------
280 lexString :: ReadP Lexeme
286 do (c,esc) <- lexStrItem
292 lexStrItem = (lexEmpty >> lexStrItem)
300 _ | isSpace c -> do skipSpaces; char '\\'; return ()
303 -- ---------------------------------------------------------------------------
309 lexNumber :: ReadP Lexeme
311 = lexHexOct <++ -- First try for hex or octal 0x, 0o etc
312 -- If that fails, try for a decimal number
313 lexDecNumber -- Start with ordinary digits
315 lexHexOct :: ReadP Lexeme
319 digits <- lexDigits base
320 return (Int (val (fromIntegral base) 0 digits))
322 lexBaseChar :: ReadP Int
323 -- Lex a single character indicating the base; fail if not there
324 lexBaseChar = do { c <- get;
332 lexDecNumber :: ReadP Lexeme
334 do xs <- lexDigits 10
335 mFrac <- lexFrac <++ return Nothing
336 mExp <- lexExp <++ return Nothing
337 return (value xs mFrac mExp)
339 value xs mFrac mExp = valueFracExp (val 10 0 xs) mFrac mExp
341 valueFracExp :: Integer -> Maybe Digits -> Maybe Integer
343 valueFracExp a Nothing Nothing
345 valueFracExp a Nothing (Just exp)
346 | exp >= 0 = Int (a * (10 ^ exp)) -- 43e7
347 | otherwise = Rat (valExp (fromInteger a) exp) -- 43e-7
348 valueFracExp a (Just fs) mExp
350 Nothing -> Rat rat -- 4.3
351 Just exp -> Rat (valExp rat exp) -- 4.3e-4
354 rat = fromInteger a + frac 10 0 1 fs
356 valExp :: Rational -> Integer -> Rational
357 valExp rat exp = rat * (10 ^^ exp)
359 lexFrac :: ReadP (Maybe Digits)
360 -- Read the fractional part; fail if it doesn't
361 -- start ".d" where d is a digit
362 lexFrac = do char '.'
366 lexExp :: ReadP (Maybe Integer)
367 lexExp = do char 'e' +++ char 'E'
368 exp <- signedExp +++ lexInteger 10
372 = do c <- char '-' +++ char '+'
374 return (if c == '-' then -n else n)
376 lexDigits :: Int -> ReadP Digits
377 -- Lex a non-empty sequence of digits in specified base
381 guard (not (null xs))
384 scan (c:cs) f = case valDig base c of
385 Just n -> do get; scan cs (f.(n:))
386 Nothing -> do return (f [])
387 scan [] f = do return (f [])
389 lexInteger :: Base -> ReadP Integer
391 do xs <- lexDigits base
392 return (val (fromIntegral base) 0 xs)
394 val :: Num a => a -> a -> Digits -> a
395 -- val base y [d1,..,dn] = y ++ [d1,..,dn], as it were
397 val base y (x:xs) = y' `seq` val base y' xs
399 y' = y * base + fromIntegral x
401 frac :: Integral a => a -> a -> a -> Digits -> Ratio a
402 frac base a b [] = a % b
403 frac base a b (x:xs) = a' `seq` b' `seq` frac base a' b' xs
405 a' = a * base + fromIntegral x
408 valDig :: Num a => a -> Char -> Maybe Int
410 | '0' <= c && c <= '7' = Just (ord c - ord '0')
411 | otherwise = Nothing
413 valDig 10 c = valDecDig c
416 | '0' <= c && c <= '9' = Just (ord c - ord '0')
417 | 'a' <= c && c <= 'f' = Just (ord c - ord 'a' + 10)
418 | 'A' <= c && c <= 'F' = Just (ord c - ord 'A' + 10)
419 | otherwise = Nothing
422 | '0' <= c && c <= '9' = Just (ord c - ord '0')
423 | otherwise = Nothing
425 -- ----------------------------------------------------------------------
426 -- other numeric lexing functions
428 readIntP :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadP a
429 readIntP base isDigit valDigit =
430 do s <- munch1 isDigit
431 return (val base 0 (map valDigit s))
433 readIntP' :: Num a => a -> ReadP a
434 readIntP' base = readIntP base isDigit valDigit
436 isDigit c = maybe False (const True) (valDig base c)
437 valDigit c = maybe 0 id (valDig base c)
439 readOctP, readDecP, readHexP :: Num a => ReadP a
440 readOctP = readIntP' 8
441 readDecP = readIntP' 10
442 readHexP = readIntP' 16