1 {-# OPTIONS -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(..) )
38 import {-# SOURCE #-} GHC.Unicode ( isSpace, isAlpha, isAlphaNum )
39 import GHC.Real( Ratio(..), Integral, Rational, (%), fromIntegral,
40 toInteger, (^), (^^), infinity, notANumber )
42 import GHC.Enum( maxBound )
44 import Prelude hiding ( lex )
45 import Data.Char( chr, ord, isSpace, isAlpha, isAlphaNum )
46 import Data.Ratio( Ratio, (%) )
49 import Hugs.Prelude( Ratio(..) )
54 -- -----------------------------------------------------------------------------
58 = Char Char -- Quotes removed,
59 | String String -- escapes interpreted
60 | Punc String -- Punctuation, eg "(", "::"
61 | Ident String -- Haskell identifiers, e.g. foo, baz
62 | Symbol String -- Haskell symbols, e.g. >>, %
68 -- -----------------------------------------------------------------------------
72 lex = skipSpaces >> lexToken
75 -- ^ Haskell lexer: returns the lexed string, rather than the lexeme
77 (s,_) <- gather lexToken
80 lexToken :: ReadP Lexeme
90 -- ----------------------------------------------------------------------
92 lexEOF :: ReadP Lexeme
97 -- ---------------------------------------------------------------------------
98 -- Single character lexemes
100 lexPunc :: ReadP Lexeme
102 do c <- satisfy isPuncChar
105 isPuncChar c = c `elem` ",;()[]{}`"
107 -- ----------------------------------------------------------------------
110 lexSymbol :: ReadP Lexeme
112 do s <- munch1 isSymbolChar
113 if s `elem` reserved_ops then
114 return (Punc s) -- Reserved-ops count as punctuation
118 isSymbolChar c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
119 reserved_ops = ["..", "::", "=", "\\", "|", "<-", "->", "@", "~", "=>"]
121 -- ----------------------------------------------------------------------
124 lexId :: ReadP Lexeme
125 lexId = lex_nan <++ lex_id
127 -- NaN and Infinity look like identifiers, so
128 -- we parse them first.
129 lex_nan = (string "NaN" >> return (Rat notANumber)) +++
130 (string "Infinity" >> return (Rat infinity))
132 lex_id = do c <- satisfy isIdsChar
136 -- Identifiers can start with a '_'
137 isIdsChar c = isAlpha c || c == '_'
138 isIdfChar c = isAlphaNum c || c `elem` "_'"
140 #ifndef __GLASGOW_HASKELL__
141 infinity, notANumber :: Rational
146 -- ---------------------------------------------------------------------------
147 -- Lexing character literals
149 lexLitChar :: ReadP Lexeme
153 guard (esc || c /= '\'') -- Eliminate '' possibility
157 lexChar :: ReadP Char
158 lexChar = do { (c,_) <- lexCharE; return c }
160 lexCharE :: ReadP (Char, Bool) -- "escaped or not"?
164 then do c <- lexEsc; return (c, True)
165 else do return (c, False)
189 do base <- lexBaseChar
191 guard (n <= toInteger (ord maxBound))
192 return (chr (fromInteger n))
234 [ (string "SOH" >> return '\SOH') <++
235 (string "SO" >> return '\SO')
236 -- \SO and \SOH need maximal-munch treatment
237 -- See the Haskell report Sect 2.6
239 , string "NUL" >> return '\NUL'
240 , string "STX" >> return '\STX'
241 , string "ETX" >> return '\ETX'
242 , string "EOT" >> return '\EOT'
243 , string "ENQ" >> return '\ENQ'
244 , string "ACK" >> return '\ACK'
245 , string "BEL" >> return '\BEL'
246 , string "BS" >> return '\BS'
247 , string "HT" >> return '\HT'
248 , string "LF" >> return '\LF'
249 , string "VT" >> return '\VT'
250 , string "FF" >> return '\FF'
251 , string "CR" >> return '\CR'
252 , string "SI" >> return '\SI'
253 , string "DLE" >> return '\DLE'
254 , string "DC1" >> return '\DC1'
255 , string "DC2" >> return '\DC2'
256 , string "DC3" >> return '\DC3'
257 , string "DC4" >> return '\DC4'
258 , string "NAK" >> return '\NAK'
259 , string "SYN" >> return '\SYN'
260 , string "ETB" >> return '\ETB'
261 , string "CAN" >> return '\CAN'
262 , string "EM" >> return '\EM'
263 , string "SUB" >> return '\SUB'
264 , string "ESC" >> return '\ESC'
265 , string "FS" >> return '\FS'
266 , string "GS" >> return '\GS'
267 , string "RS" >> return '\RS'
268 , string "US" >> return '\US'
269 , string "SP" >> return '\SP'
270 , string "DEL" >> return '\DEL'
274 -- ---------------------------------------------------------------------------
277 lexString :: ReadP Lexeme
283 do (c,esc) <- lexStrItem
289 lexStrItem = (lexEmpty >> lexStrItem)
297 _ | isSpace c -> do skipSpaces; char '\\'; return ()
300 -- ---------------------------------------------------------------------------
306 lexNumber :: ReadP Lexeme
308 = lexHexOct <++ -- First try for hex or octal 0x, 0o etc
309 -- If that fails, try for a decimal number
310 lexDecNumber -- Start with ordinary digits
312 lexHexOct :: ReadP Lexeme
316 digits <- lexDigits base
317 return (Int (val (fromIntegral base) 0 digits))
319 lexBaseChar :: ReadP Int
320 -- Lex a single character indicating the base,
321 -- or return 10 if there isn't one
322 lexBaseChar = lex_base <++ return 10
324 lex_base = 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