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 -- -----------------------------------------------------------------------------
60 = Char Char -- Quotes removed,
61 | String String -- escapes interpreted
62 | Punc String -- Punctuation, eg "(", "::"
63 | Ident String -- Haskell identifiers, e.g. foo, baz
64 | Symbol String -- Haskell symbols, e.g. >>, %
70 -- -----------------------------------------------------------------------------
74 lex = skipSpaces >> lexToken
77 -- ^ Haskell lexer: returns the lexed string, rather than the lexeme
79 (s,_) <- gather lexToken
82 lexToken :: ReadP Lexeme
92 -- ----------------------------------------------------------------------
94 lexEOF :: ReadP Lexeme
99 -- ---------------------------------------------------------------------------
100 -- Single character lexemes
102 lexPunc :: ReadP Lexeme
104 do c <- satisfy isPuncChar
107 isPuncChar c = c `elem` ",;()[]{}`"
109 -- ----------------------------------------------------------------------
112 lexSymbol :: ReadP Lexeme
114 do s <- munch1 isSymbolChar
115 if s `elem` reserved_ops then
116 return (Punc s) -- Reserved-ops count as punctuation
120 isSymbolChar c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
121 reserved_ops = ["..", "::", "=", "\\", "|", "<-", "->", "@", "~", "=>"]
123 -- ----------------------------------------------------------------------
126 lexId :: ReadP Lexeme
127 lexId = lex_nan <++ lex_id
129 -- NaN and Infinity look like identifiers, so
130 -- we parse them first.
131 lex_nan = (string "NaN" >> return (Rat notANumber)) +++
132 (string "Infinity" >> return (Rat infinity))
134 lex_id = do c <- satisfy isIdsChar
138 -- Identifiers can start with a '_'
139 isIdsChar c = isAlpha c || c == '_'
140 isIdfChar c = isAlphaNum c || c `elem` "_'"
142 #ifndef __GLASGOW_HASKELL__
143 infinity, notANumber :: Rational
148 -- ---------------------------------------------------------------------------
149 -- Lexing character literals
151 lexLitChar :: ReadP Lexeme
155 guard (esc || c /= '\'') -- Eliminate '' possibility
159 lexChar :: ReadP Char
160 lexChar = do { (c,_) <- lexCharE; return c }
162 lexCharE :: ReadP (Char, Bool) -- "escaped or not"?
166 then do c <- lexEsc; return (c, True)
167 else do return (c, False)
191 do base <- lexBaseChar <++ return 10
193 guard (n <= toInteger (ord maxBound))
194 return (chr (fromInteger n))
236 [ (string "SOH" >> return '\SOH') <++
237 (string "SO" >> return '\SO')
238 -- \SO and \SOH need maximal-munch treatment
239 -- See the Haskell report Sect 2.6
241 , string "NUL" >> return '\NUL'
242 , string "STX" >> return '\STX'
243 , string "ETX" >> return '\ETX'
244 , string "EOT" >> return '\EOT'
245 , string "ENQ" >> return '\ENQ'
246 , string "ACK" >> return '\ACK'
247 , string "BEL" >> return '\BEL'
248 , string "BS" >> return '\BS'
249 , string "HT" >> return '\HT'
250 , string "LF" >> return '\LF'
251 , string "VT" >> return '\VT'
252 , string "FF" >> return '\FF'
253 , string "CR" >> return '\CR'
254 , string "SI" >> return '\SI'
255 , string "DLE" >> return '\DLE'
256 , string "DC1" >> return '\DC1'
257 , string "DC2" >> return '\DC2'
258 , string "DC3" >> return '\DC3'
259 , string "DC4" >> return '\DC4'
260 , string "NAK" >> return '\NAK'
261 , string "SYN" >> return '\SYN'
262 , string "ETB" >> return '\ETB'
263 , string "CAN" >> return '\CAN'
264 , string "EM" >> return '\EM'
265 , string "SUB" >> return '\SUB'
266 , string "ESC" >> return '\ESC'
267 , string "FS" >> return '\FS'
268 , string "GS" >> return '\GS'
269 , string "RS" >> return '\RS'
270 , string "US" >> return '\US'
271 , string "SP" >> return '\SP'
272 , string "DEL" >> return '\DEL'
276 -- ---------------------------------------------------------------------------
279 lexString :: ReadP Lexeme
285 do (c,esc) <- lexStrItem
291 lexStrItem = (lexEmpty >> lexStrItem)
299 _ | isSpace c -> do skipSpaces; char '\\'; return ()
302 -- ---------------------------------------------------------------------------
308 lexNumber :: ReadP Lexeme
310 = lexHexOct <++ -- First try for hex or octal 0x, 0o etc
311 -- If that fails, try for a decimal number
312 lexDecNumber -- Start with ordinary digits
314 lexHexOct :: ReadP Lexeme
318 digits <- lexDigits base
319 return (Int (val (fromIntegral base) 0 digits))
321 lexBaseChar :: ReadP Int
322 -- Lex a single character indicating the base; fail if not there
323 lexBaseChar = do { c <- get;
331 lexDecNumber :: ReadP Lexeme
333 do xs <- lexDigits 10
334 mFrac <- lexFrac <++ return Nothing
335 mExp <- lexExp <++ return Nothing
336 return (value xs mFrac mExp)
338 value xs mFrac mExp = valueFracExp (val 10 0 xs) mFrac mExp
340 valueFracExp :: Integer -> Maybe Digits -> Maybe Integer
342 valueFracExp a Nothing Nothing
344 valueFracExp a Nothing (Just exp)
345 | exp >= 0 = Int (a * (10 ^ exp)) -- 43e7
346 | otherwise = Rat (valExp (fromInteger a) exp) -- 43e-7
347 valueFracExp a (Just fs) mExp
349 Nothing -> Rat rat -- 4.3
350 Just exp -> Rat (valExp rat exp) -- 4.3e-4
353 rat = fromInteger a + frac 10 0 1 fs
355 valExp :: Rational -> Integer -> Rational
356 valExp rat exp = rat * (10 ^^ exp)
358 lexFrac :: ReadP (Maybe Digits)
359 -- Read the fractional part; fail if it doesn't
360 -- start ".d" where d is a digit
361 lexFrac = do char '.'
365 lexExp :: ReadP (Maybe Integer)
366 lexExp = do char 'e' +++ char 'E'
367 exp <- signedExp +++ lexInteger 10
371 = do c <- char '-' +++ char '+'
373 return (if c == '-' then -n else n)
375 lexDigits :: Int -> ReadP Digits
376 -- Lex a non-empty sequence of digits in specified base
380 guard (not (null xs))
383 scan (c:cs) f = case valDig base c of
384 Just n -> do get; scan cs (f.(n:))
385 Nothing -> do return (f [])
386 scan [] f = do return (f [])
388 lexInteger :: Base -> ReadP Integer
390 do xs <- lexDigits base
391 return (val (fromIntegral base) 0 xs)
393 val :: Num a => a -> a -> Digits -> a
394 -- val base y [d1,..,dn] = y ++ [d1,..,dn], as it were
396 val base y (x:xs) = y' `seq` val base y' xs
398 y' = y * base + fromIntegral x
400 frac :: Integral a => a -> a -> a -> Digits -> Ratio a
401 frac base a b [] = a % b
402 frac base a b (x:xs) = a' `seq` b' `seq` frac base a' b' xs
404 a' = a * base + fromIntegral x
407 valDig :: Num a => a -> Char -> Maybe Int
409 | '0' <= c && c <= '7' = Just (ord c - ord '0')
410 | otherwise = Nothing
412 valDig 10 c = valDecDig c
415 | '0' <= c && c <= '9' = Just (ord c - ord '0')
416 | 'a' <= c && c <= 'f' = Just (ord c - ord 'a' + 10)
417 | 'A' <= c && c <= 'F' = Just (ord c - ord 'A' + 10)
418 | otherwise = Nothing
421 | '0' <= c && c <= '9' = Just (ord c - ord '0')
422 | otherwise = Nothing
424 -- ----------------------------------------------------------------------
425 -- other numeric lexing functions
427 readIntP :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadP a
428 readIntP base isDigit valDigit =
429 do s <- munch1 isDigit
430 return (val base 0 (map valDigit s))
432 readIntP' :: Num a => a -> ReadP a
433 readIntP' base = readIntP base isDigit valDigit
435 isDigit c = maybe False (const True) (valDig base c)
436 valDigit c = maybe 0 id (valDig base c)
438 readOctP, readDecP, readHexP :: Num a => ReadP a
439 readOctP = readIntP' 8
440 readDecP = readIntP' 10
441 readHexP = readIntP' 16