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(..) )
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
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,
323 -- or return 10 if there isn't one
324 lexBaseChar = lex_base <++ return 10
326 lex_base = do { c <- get;
334 lexDecNumber :: ReadP Lexeme
336 do xs <- lexDigits 10
337 mFrac <- lexFrac <++ return Nothing
338 mExp <- lexExp <++ return Nothing
339 return (value xs mFrac mExp)
341 value xs mFrac mExp = valueFracExp (val 10 0 xs) mFrac mExp
343 valueFracExp :: Integer -> Maybe Digits -> Maybe Integer
345 valueFracExp a Nothing Nothing
347 valueFracExp a Nothing (Just exp)
348 | exp >= 0 = Int (a * (10 ^ exp)) -- 43e7
349 | otherwise = Rat (valExp (fromInteger a) exp) -- 43e-7
350 valueFracExp a (Just fs) mExp
352 Nothing -> Rat rat -- 4.3
353 Just exp -> Rat (valExp rat exp) -- 4.3e-4
356 rat = fromInteger a + frac 10 0 1 fs
358 valExp :: Rational -> Integer -> Rational
359 valExp rat exp = rat * (10 ^^ exp)
361 lexFrac :: ReadP (Maybe Digits)
362 -- Read the fractional part; fail if it doesn't
363 -- start ".d" where d is a digit
364 lexFrac = do char '.'
368 lexExp :: ReadP (Maybe Integer)
369 lexExp = do char 'e' +++ char 'E'
370 exp <- signedExp +++ lexInteger 10
374 = do c <- char '-' +++ char '+'
376 return (if c == '-' then -n else n)
378 lexDigits :: Int -> ReadP Digits
379 -- Lex a non-empty sequence of digits in specified base
383 guard (not (null xs))
386 scan (c:cs) f = case valDig base c of
387 Just n -> do get; scan cs (f.(n:))
388 Nothing -> do return (f [])
389 scan [] f = do return (f [])
391 lexInteger :: Base -> ReadP Integer
393 do xs <- lexDigits base
394 return (val (fromIntegral base) 0 xs)
396 val :: Num a => a -> a -> Digits -> a
397 -- val base y [d1,..,dn] = y ++ [d1,..,dn], as it were
399 val base y (x:xs) = y' `seq` val base y' xs
401 y' = y * base + fromIntegral x
403 frac :: Integral a => a -> a -> a -> Digits -> Ratio a
404 frac base a b [] = a % b
405 frac base a b (x:xs) = a' `seq` b' `seq` frac base a' b' xs
407 a' = a * base + fromIntegral x
410 valDig :: Num a => a -> Char -> Maybe Int
412 | '0' <= c && c <= '7' = Just (ord c - ord '0')
413 | otherwise = Nothing
415 valDig 10 c = valDecDig c
418 | '0' <= c && c <= '9' = Just (ord c - ord '0')
419 | 'a' <= c && c <= 'f' = Just (ord c - ord 'a' + 10)
420 | 'A' <= c && c <= 'F' = Just (ord c - ord 'A' + 10)
421 | otherwise = Nothing
424 | '0' <= c && c <= '9' = Just (ord c - ord '0')
425 | otherwise = Nothing
427 -- ----------------------------------------------------------------------
428 -- other numeric lexing functions
430 readIntP :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadP a
431 readIntP base isDigit valDigit =
432 do s <- munch1 isDigit
433 return (val base 0 (map valDigit s))
435 readIntP' :: Num a => a -> ReadP a
436 readIntP' base = readIntP base isDigit valDigit
438 isDigit c = maybe False (const True) (valDig base c)
439 valDigit c = maybe 0 id (valDig base c)
441 readOctP, readDecP, readHexP :: Num a => ReadP a
442 readOctP = readIntP' 8
443 readDecP = readIntP' 10
444 readHexP = readIntP' 16