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 : portable
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
35 import GHC.Num( Num(..), Integer )
36 import GHC.Show( Show(.. ), isSpace, isAlpha, isAlphaNum )
37 import GHC.Real( Ratio(..), Integral, Rational, (%), fromIntegral,
38 toInteger, (^), (^^), infinity, notANumber )
40 import GHC.Enum( maxBound )
44 -- -----------------------------------------------------------------------------
48 = Char Char -- Quotes removed,
49 | String String -- escapes interpreted
50 | Punc String -- Punctuation, eg "(", "::"
51 | Ident String -- Haskell identifiers, e.g. foo, baz
52 | Symbol String -- Haskell symbols, e.g. >>, %
58 -- -----------------------------------------------------------------------------
62 lex = skipSpaces >> lexToken
65 -- ^ Haskell lexer: returns the lexed string, rather than the lexeme
67 (s,_) <- gather lexToken
70 lexToken :: ReadP Lexeme
80 -- ----------------------------------------------------------------------
82 lexEOF :: ReadP Lexeme
87 -- ---------------------------------------------------------------------------
88 -- Single character lexemes
90 lexPunc :: ReadP Lexeme
92 do c <- satisfy isPuncChar
95 isPuncChar c = c `elem` ",;()[]{}`"
97 -- ----------------------------------------------------------------------
100 lexSymbol :: ReadP Lexeme
102 do s <- munch1 isSymbolChar
103 if s `elem` reserved_ops then
104 return (Punc s) -- Reserved-ops count as punctuation
108 isSymbolChar c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
109 reserved_ops = ["..", "::", "=", "\\", "|", "<-", "->", "@", "~", "=>"]
111 -- ----------------------------------------------------------------------
114 lexId :: ReadP Lexeme
115 lexId = lex_nan <++ lex_id
117 -- NaN and Infinity look like identifiers, so
118 -- we parse them first.
119 lex_nan = (string "NaN" >> return (Rat notANumber)) +++
120 (string "Infinity" >> return (Rat infinity))
122 lex_id = do c <- satisfy isIdsChar
126 -- Identifiers can start with a '_'
127 isIdsChar c = isAlpha c || c == '_'
128 isIdfChar c = isAlphaNum c || c `elem` "_'"
130 -- ---------------------------------------------------------------------------
131 -- Lexing character literals
133 lexLitChar :: ReadP Lexeme
137 guard (esc || c /= '\'') -- Eliminate '' possibility
141 lexChar :: ReadP Char
142 lexChar = do { (c,_) <- lexCharE; return c }
144 lexCharE :: ReadP (Char, Bool) -- "escaped or not"?
148 then do c <- lexEsc; return (c, True)
149 else do return (c, False)
173 do base <- lexBaseChar
175 guard (n <= toInteger (ord maxBound))
176 return (chr (fromInteger n))
218 [ (string "SOH" >> return '\SOH') <++
219 (string "SO" >> return '\SO')
220 -- \SO and \SOH need maximal-munch treatment
221 -- See the Haskell report Sect 2.6
223 , string "NUL" >> return '\NUL'
224 , string "STX" >> return '\STX'
225 , string "ETX" >> return '\ETX'
226 , string "EOT" >> return '\EOT'
227 , string "ENQ" >> return '\ENQ'
228 , string "ACK" >> return '\ACK'
229 , string "BEL" >> return '\BEL'
230 , string "BS" >> return '\BS'
231 , string "HT" >> return '\HT'
232 , string "LF" >> return '\LF'
233 , string "VT" >> return '\VT'
234 , string "FF" >> return '\FF'
235 , string "CR" >> return '\CR'
236 , string "SI" >> return '\SI'
237 , string "DLE" >> return '\DLE'
238 , string "DC1" >> return '\DC1'
239 , string "DC2" >> return '\DC2'
240 , string "DC3" >> return '\DC3'
241 , string "DC4" >> return '\DC4'
242 , string "NAK" >> return '\NAK'
243 , string "SYN" >> return '\SYN'
244 , string "ETB" >> return '\ETB'
245 , string "CAN" >> return '\CAN'
246 , string "EM" >> return '\EM'
247 , string "SUB" >> return '\SUB'
248 , string "ESC" >> return '\ESC'
249 , string "FS" >> return '\FS'
250 , string "GS" >> return '\GS'
251 , string "RS" >> return '\RS'
252 , string "US" >> return '\US'
253 , string "SP" >> return '\SP'
254 , string "DEL" >> return '\DEL'
258 -- ---------------------------------------------------------------------------
261 lexString :: ReadP Lexeme
267 do (c,esc) <- lexStrItem
273 lexStrItem = (lexEmpty >> lexStrItem)
281 _ | isSpace c -> do skipSpaces; char '\\'; return ()
284 -- ---------------------------------------------------------------------------
290 lexNumber :: ReadP Lexeme
292 = lexHexOct <++ -- First try for hex or octal 0x, 0o etc
293 -- If that fails, try for a decimal number
294 lexDecNumber -- Start with ordinary digits
296 lexHexOct :: ReadP Lexeme
300 digits <- lexDigits base
301 return (Int (val (fromIntegral base) 0 digits))
303 lexBaseChar :: ReadP Int
304 -- Lex a single character indicating the base,
305 -- or return 10 if there isn't one
306 lexBaseChar = lex_base <++ return 10
308 lex_base = do { c <- get;
316 lexDecNumber :: ReadP Lexeme
318 do xs <- lexDigits 10
319 mFrac <- lexFrac <++ return Nothing
320 mExp <- lexExp <++ return Nothing
321 return (value xs mFrac mExp)
323 value xs mFrac mExp = valueFracExp (val 10 0 xs) mFrac mExp
325 valueFracExp :: Integer -> Maybe Digits -> Maybe Integer
327 valueFracExp a Nothing Nothing
329 valueFracExp a Nothing (Just exp)
330 | exp >= 0 = Int (a * (10 ^ exp)) -- 43e7
331 | otherwise = Rat (valExp (fromInteger a) exp) -- 43e-7
332 valueFracExp a (Just fs) mExp
334 Nothing -> Rat rat -- 4.3
335 Just exp -> Rat (valExp rat exp) -- 4.3e-4
338 rat = fromInteger a + frac 10 0 1 fs
340 valExp :: Rational -> Integer -> Rational
341 valExp rat exp = rat * (10 ^^ exp)
343 lexFrac :: ReadP (Maybe Digits)
344 -- Read the fractional part; fail if it doesn't
345 -- start ".d" where d is a digit
346 lexFrac = do char '.'
350 lexExp :: ReadP (Maybe Integer)
351 lexExp = do char 'e' +++ char 'E'
352 exp <- signedExp +++ lexInteger 10
356 = do c <- char '-' +++ char '+'
358 return (if c == '-' then -n else n)
360 lexDigits :: Int -> ReadP Digits
361 -- Lex a non-empty sequence of digits in specified base
365 guard (not (null xs))
368 scan (c:cs) f = case valDig base c of
369 Just n -> do get; scan cs (f.(n:))
370 Nothing -> do return (f [])
371 scan [] f = do return (f [])
373 lexInteger :: Base -> ReadP Integer
375 do xs <- lexDigits base
376 return (val (fromIntegral base) 0 xs)
378 val :: Num a => a -> a -> Digits -> a
379 -- val base y [d1,..,dn] = y ++ [d1,..,dn], as it were
381 val base y (x:xs) = y' `seq` val base y' xs
383 y' = y * base + fromIntegral x
385 frac :: Integral a => a -> a -> a -> Digits -> Ratio a
386 frac base a b [] = a % b
387 frac base a b (x:xs) = a' `seq` b' `seq` frac base a' b' xs
389 a' = a * base + fromIntegral x
392 valDig :: Num a => a -> Char -> Maybe Int
394 | '0' <= c && c <= '7' = Just (ord c - ord '0')
395 | otherwise = Nothing
397 valDig 10 c = valDecDig c
400 | '0' <= c && c <= '9' = Just (ord c - ord '0')
401 | 'a' <= c && c <= 'f' = Just (ord c - ord 'a' + 10)
402 | 'A' <= c && c <= 'F' = Just (ord c - ord 'A' + 10)
403 | otherwise = Nothing
406 | '0' <= c && c <= '9' = Just (ord c - ord '0')
407 | otherwise = Nothing
409 -- ----------------------------------------------------------------------
410 -- other numeric lexing functions
412 readIntP :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadP a
413 readIntP base isDigit valDigit =
414 do s <- munch1 isDigit
415 return (val base 0 (map valDigit s))
417 readIntP' :: Num a => a -> ReadP a
418 readIntP' base = readIntP base isDigit valDigit
420 isDigit c = maybe False (const True) (valDig base c)
421 valDigit c = maybe 0 id (valDig base c)
423 readOctP, readDecP, readHexP :: Num a => ReadP a
424 readOctP = readIntP' 8
425 readDecP = readIntP' 10
426 readHexP = readIntP' 16