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
24 , readIntP -- :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadP a
25 , readOctP -- :: Num a => ReadP a
26 , readDecP -- :: Num a => ReadP a
27 , readHexP -- :: Num a => ReadP a
31 import Text.ParserCombinators.ReadP
34 import GHC.Num( Num(..), Integer )
35 import GHC.Show( Show(.. ), showChar, showString,
36 isSpace, isAlpha, isAlphaNum,
37 isOctDigit, isHexDigit, toUpper )
38 import GHC.Real( Ratio(..), Integral, Rational, (%), fromIntegral, fromRational,
39 toInteger, (^), (^^), infinity, notANumber )
40 import GHC.Float( Float, Double )
42 import GHC.Show( ShowS, shows )
43 import GHC.Enum( minBound, maxBound )
48 -- -----------------------------------------------------------------------------
51 type LexP = ReadP Lexeme
54 = Char Char -- Quotes removed,
55 | String String -- escapes interpreted
56 | Punc String -- Punctuation, eg "(", "::"
57 | Ident String -- Haskell identifiers, e.g. foo, baz
58 | Symbol String -- Haskell symbols, e.g. >>, %
64 -- -----------------------------------------------------------------------------
68 lex = skipSpaces >> lexToken
71 -- ^ Haskell lexer: returns the lexed string, rather than the lexeme
73 (s,_) <- gather lexToken
76 lexToken :: ReadP Lexeme
86 -- ----------------------------------------------------------------------
88 lexEOF :: ReadP Lexeme
93 -- ---------------------------------------------------------------------------
94 -- Single character lexemes
96 lexPunc :: ReadP Lexeme
98 do c <- satisfy isPuncChar
101 isPuncChar c = c `elem` ",;()[]{}_`"
103 -- ----------------------------------------------------------------------
106 lexSymbol :: ReadP Lexeme
108 do s <- munch1 isSymbolChar
109 if s `elem` reserved_ops then
110 return (Punc s) -- Reserved-ops count as punctuation
114 isSymbolChar c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
115 reserved_ops = ["..", "::", "=", "\\", "|", "<-", "->", "@", "~", "=>"]
117 -- ----------------------------------------------------------------------
120 lexId :: ReadP Lexeme
122 do c <- satisfy isAlpha
126 isIdfChar c = isAlphaNum c || c `elem` "_'"
128 -- ---------------------------------------------------------------------------
129 -- Lexing character literals
131 lexLitChar :: ReadP Lexeme
135 guard (esc || c /= '\'')
139 lexChar :: ReadP (Char, Bool) -- "escaped or not"?
143 then do c <- lexEsc; return (c, True)
144 else do return (c, False)
170 guard (n <= toInteger (ord maxBound))
171 return (chr (fromInteger n))
176 'o':_ -> do get; return 8
177 'x':_ -> do get; return 16
220 [ string "NUL" >> return '\NUL'
221 , string "SOH" >> return '\SOH'
222 , string "STX" >> return '\STX'
223 , string "ETX" >> return '\ETX'
224 , string "EOT" >> return '\EOT'
225 , string "ENQ" >> return '\ENQ'
226 , string "ACK" >> return '\ACK'
227 , string "BEL" >> return '\BEL'
228 , string "BS" >> return '\BS'
229 , string "HT" >> return '\HT'
230 , string "LF" >> return '\LF'
231 , string "VT" >> return '\VT'
232 , string "FF" >> return '\FF'
233 , string "CR" >> return '\CR'
234 , string "SO" >> return '\SO'
235 , string "SI" >> return '\SI'
236 , string "DLE" >> return '\DLE'
237 , string "DC1" >> return '\DC1'
238 , string "DC2" >> return '\DC2'
239 , string "DC3" >> return '\DC3'
240 , string "DC4" >> return '\DC4'
241 , string "NAK" >> return '\NAK'
242 , string "SYN" >> return '\SYN'
243 , string "ETB" >> return '\ETB'
244 , string "CAN" >> return '\CAN'
245 , string "EM" >> return '\EM'
246 , string "SUB" >> return '\SUB'
247 , string "ESC" >> return '\ESC'
248 , string "FS" >> return '\FS'
249 , string "GS" >> return '\GS'
250 , string "RS" >> return '\RS'
251 , string "US" >> return '\US'
252 , string "SP" >> return '\SP'
253 , string "DEL" >> return '\DEL'
257 -- ---------------------------------------------------------------------------
260 lexString :: ReadP Lexeme
266 do (c,esc) <- lexStrItem
273 (lexEmpty >> lexStrItem)
281 _ | isSpace c -> do skipSpaces; char '\\'; return ()
284 -- ---------------------------------------------------------------------------
290 showDigit :: Int -> ShowS
291 showDigit n | n <= 9 = shows n
292 | otherwise = showChar (chr (n + ord 'A' - 10))
294 lexNumber :: ReadP Lexeme
295 lexNumber = do { string "NaN"; return (Rat notANumber) } +++
296 do { string "Infinity"; return (Rat infinity) } +++
297 do { base <- lexBase ; lexNumberBase base }
302 '0':'o':_ -> do get; get; return 8
303 '0':'O':_ -> do get; get; return 8
304 '0':'x':_ -> do get; get; return 16
305 '0':'X':_ -> do get; get; return 16
308 lexNumberBase :: Base -> ReadP Lexeme
310 do xs <- lexDigits base
311 mFrac <- lexFrac base
313 return (value xs mFrac mExp)
315 baseInteger :: Integer
316 baseInteger = fromIntegral base
318 value xs mFrac mExp = valueFracExp (val baseInteger 0 xs) mFrac mExp
320 valueFracExp :: Integer -> Maybe Digits -> Maybe Integer
322 valueFracExp a Nothing Nothing
324 valueFracExp a Nothing (Just exp)
325 | exp >= 0 = Int (a * (baseInteger ^ exp)) -- 43e7
326 | otherwise = Rat (valExp (fromInteger a) exp) -- 43e-7
327 valueFracExp a (Just fs) mExp
329 Nothing -> Rat rat -- 4.3
330 Just exp -> Rat (valExp rat exp) -- 4.3e-4
333 rat = fromInteger a + frac (fromIntegral base) 0 1 fs
335 valExp :: Rational -> Integer -> Rational
336 valExp rat exp = rat * (fromIntegral base ^^ exp)
338 lexFrac :: Base -> ReadP (Maybe Digits)
342 '.' : d : _ | isJust (valDig base d) ->
343 -- The lookahead checks for point and at least one
344 -- valid following digit. For example 1..n must
345 -- lex the "1" off rather than failing.
347 frac <- lexDigits base
353 lexExp :: Base -> ReadP (Maybe Integer)
357 e : _ | e `elem` "eE" && base == 10 ->
362 do c <- char '-' +++ char '+'
364 return (Just (if c == '-' then -n else n))
367 do n <- lexInteger 10
373 lexDigits :: Int -> ReadP Digits
374 -- Lex a non-empty sequence of digits in specified base
378 guard (not (null xs))
381 scan (c:cs) f = case valDig base c of
382 Just n -> do get; scan cs (f.(n:))
383 Nothing -> do return (f [])
384 scan [] f = do return (f [])
386 lexInteger :: Base -> ReadP Integer
388 do xs <- lexDigits base
389 return (val (fromIntegral base) 0 xs)
391 val :: Num a => a -> a -> Digits -> a
392 -- val base y [d1,..,dn] = y ++ [d1,..,dn], as it were
394 val base y (x:xs) = y' `seq` val base y' xs
396 y' = y * base + fromIntegral x
398 frac :: Integral a => a -> a -> a -> Digits -> Ratio a
399 frac base a b [] = a % b
400 frac base a b (x:xs) = a' `seq` b' `seq` frac base a' b' xs
402 a' = a * base + fromIntegral x
405 valDig :: Num a => a -> Char -> Maybe Int
407 | '0' <= c && c <= '7' = Just (ord c - ord '0')
408 | otherwise = Nothing
411 | '0' <= c && c <= '9' = Just (ord c - ord '0')
412 | otherwise = Nothing
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
420 -- ----------------------------------------------------------------------
421 -- other numeric lexing functions
423 readIntP :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadP a
424 readIntP base isDigit valDigit =
425 do s <- munch1 isDigit
426 return (val base 0 (map valDigit s))
428 readIntP' :: Num a => a -> ReadP a
429 readIntP' base = readIntP base isDigit valDigit
431 isDigit c = maybe False (const True) (valDig base c)
432 valDigit c = maybe 0 id (valDig base c)
434 readOctP, readDecP, readHexP :: Num a => ReadP a
435 readOctP = readIntP' 8
436 readDecP = readIntP' 10
437 readHexP = readIntP' 16