-% -------------------------------------------------------------
-% $Id: ReadP.lhs
-%
-% (c) The University of Glasgow, 1994-2000
-%
-
-\begin{code}
-{-# OPTIONS -fglasgow-exts -fno-implicit-prelude #-}
+{-# OPTIONS -fno-implicit-prelude #-}
+-----------------------------------------------------------------------------
+-- |
+-- Module : Text.ParserCombinators.ReadP
+-- Copyright : (c) The University of Glasgow 2002
+-- License : BSD-style (see the file libraries/base/LICENSE)
+--
+-- Maintainer : libraries@haskell.org
+-- Stability : provisional
+-- Portability : portable
+--
+-----------------------------------------------------------------------------
+
module Text.ParserCombinators.ReadP
- ( ReadP -- :: * -> *; instance Functor, Monad, MonadPlus
+ (
+ -- * The 'ReadP' type
+ ReadP -- :: * -> *; instance Functor, Monad, MonadPlus
- -- primitive operations
+ -- * Primitive operations
, get -- :: ReadP Char
, look -- :: ReadP String
, (+++) -- :: ReadP a -> ReadP a -> ReadP a
- -- other operations
+ -- * Other operations
, pfail -- :: ReadP a
, satisfy -- :: (Char -> Bool) -> ReadP Char
, char -- :: Char -> ReadP Char
, skipSpaces -- :: ReadP ()
, choice -- :: [ReadP a] -> ReadP a
- -- converting
+ -- * Conversions
, readP_to_S -- :: ReadP a -> ReadS a
, readS_to_P -- :: ReadS a -> ReadP a
)
import Control.Monad( MonadPlus(..) )
import GHC.Show( isSpace )
import GHC.Base
-\end{code}
-
-%*********************************************************
-%* *
-\subsection{The @ReadP@ type}
-%* *
-%*********************************************************
+-- ---------------------------------------------------------------------------
+-- The ReadP type
-\begin{code}
newtype ReadP a = R (forall b . (a -> P b) -> P b)
data P a
instance MonadPlus ReadP where
mzero = pfail
mplus = (+++)
-\end{code}
-
-%*********************************************************
-%* *
-\subsection{Operations over ReadP}
-%* *
-%*********************************************************
+-- ---------------------------------------------------------------------------
+-- Operations over ReadP
-\begin{code}
get :: ReadP Char
get = R (\k -> Get k)
run (Result x p) s = (x,s) : run p s
run (ReadS r) s = r s
run Fail _ = []
-\end{code}
+-- ---------------------------------------------------------------------------
+-- Derived operations
-%*********************************************************
-%* *
-\subsection{Derived operations}
-%* *
-%*********************************************************
-
-\begin{code}
pfail :: ReadP a
pfail = fail ""
where
skip (c:s) | isSpace c = do get; skip s
skip _ = do return ()
-\end{code}
-
-%*********************************************************
-%* *
-\subsection{Converting between ReadP and ReadS
-%* *
-%*********************************************************
+-- ---------------------------------------------------------------------------
+-- Converting between ReadP and Read
-\begin{code}
readP_to_S :: ReadP a -> ReadS a
readP_to_S (R f) = run (f (\x -> Result x Fail))
| (a,s') <- r s
, bs'' <- run (k a) s'
]))
-\end{code}
\ No newline at end of file
-% ----------------------------------------------------------------
-% $Id: ReadPrec.lhs
-%
-% (c) The University of Glasgow, 1994-2000
-%
-
-\begin{code}
{-# OPTIONS -fno-implicit-prelude #-}
+-----------------------------------------------------------------------------
+-- |
+-- Module : Text.ParserCombinators.ReadPrec
+-- Copyright : (c) The University of Glasgow 2002
+-- License : BSD-style (see the file libraries/base/LICENSE)
+--
+-- Maintainer : libraries@haskell.org
+-- Stability : provisional
+-- Portability : portable
+--
+-----------------------------------------------------------------------------
module Text.ParserCombinators.ReadPrec
( ReadPrec -- :: * -> *; instance Functor, Monad, MonadPlus
- -- precedences
+ -- * Precedences
, Prec -- :: *; = Int
, minPrec -- :: Prec; = 0
- -- primitive operations
+ -- * Primitive operations
, lift -- :: ReadP a -> ReadPrec a
, prec -- :: Prec -> ReadPrec a -> ReadPrec a
, step -- :: ReadPrec a -> ReadPrec a
, reset -- :: ReadPrec a -> ReadPrec a
- -- other operations
+ -- * Other operations
, get -- :: ReadPrec Char
, look -- :: ReadPrec String
, (+++) -- :: ReadPrec a -> ReadPrec a -> ReadPrec a
import Control.Monad( MonadPlus(..) )
import GHC.Num( Num(..) )
import GHC.Base
-\end{code}
-
-%*********************************************************
-%* *
-\subsection{The readPrec type}
-%* *
-%*********************************************************
+-- ---------------------------------------------------------------------------
+-- The readPrec type
-\begin{code}
newtype ReadPrec a = P { unP :: Prec -> ReadP a }
-- Functor, Monad, MonadPlus
minPrec :: Prec
minPrec = 0
-\end{code}
+-- ---------------------------------------------------------------------------
+-- Operations over ReadPrec
-%*********************************************************
-%* *
-\subsection{Operations over ReadPrec
-%* *
-%*********************************************************
-
-\begin{code}
lift :: ReadP a -> ReadPrec a
lift m = P (\_ -> m)
-- if not, fails
-- if so, parses p in context n
prec n (P f) = P (\c -> if c <= n then f n else ReadP.pfail)
-\end{code}
-%*********************************************************
-%* *
-\subsection{Derived operations}
-%* *
-%*********************************************************
+-- ---------------------------------------------------------------------------
+-- Derived operations
-\begin{code}
get :: ReadPrec Char
get = lift ReadP.get
choice :: [ReadPrec a] -> ReadPrec a
choice ps = foldr (+++) pfail ps
-\end{code}
-
-%*********************************************************
-%* *
-\subsection{Converting between ReadPrec and ReadS
-%* *
-%*********************************************************
+-- ---------------------------------------------------------------------------
+-- Converting between ReadPrec and Read
-\begin{code}
-- We define a local version of ReadS here,
-- because its "real" definition site is in GHC.Read
type ReadS a = String -> [(a,String)]
readS_to_Prec :: (Int -> ReadS a) -> ReadPrec a
readS_to_Prec f = P (\n -> readS_to_P (f n))
-\end{code}
-% ----------------------------------------------------------------
-% $Id: Lex.lhs
-%
-% (c) The University of Glasgow, 1994-2000
-%
-
-\begin{code}
{-# OPTIONS -fno-implicit-prelude #-}
+-----------------------------------------------------------------------------
+-- |
+-- Module : Text.Read.Lex
+-- Copyright : (c) The University of Glasgow 2002
+-- License : BSD-style (see the file libraries/base/LICENSE)
+--
+-- Maintainer : libraries@haskell.org
+-- Stability : provisional
+-- Portability : portable
+--
+-- The cut-down Haskell lexer, used by Text.Read
+--
+-----------------------------------------------------------------------------
module Text.Read.Lex
-- lexing types
import Data.Maybe
import Data.Either
import Control.Monad
-\end{code}
-%*********************************************************
-%* *
-\subsection{Lexing types}
-%* *
-%*********************************************************
+-- -----------------------------------------------------------------------------
+-- Lexing types
-\begin{code}
type LexP = ReadP Lexeme
data Lexeme
showsPrec _ (Ident s) = showString s
showsPrec _ (Symbol s) = showString s
showsPrec n (Number x) = showsPrec n x
-\end{code}
-
-%*********************************************************
-%* *
-\subsection{Lexing}
-%* *
-%*********************************************************
+-- -----------------------------------------------------------------------------
+-- Lexing
-\begin{code}
lex :: LexP
lex =
do skipSpaces
+++ lexSymbol
+++ lexIdf
+++ lexNumber)
-\end{code}
-\begin{code}
-------------------------------------------------------------------------
+-- ----------------------------------------------------------------------
-- symbols
lexSymbol :: LexP
where
isSymbolChar c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
-------------------------------------------------------------------------
+-- ----------------------------------------------------------------------
-- identifiers
lexIdf :: LexP
return (Ident (c:s))
where
isIdfChar c = isAlphaNum c || c `elem` "_'"
-\end{code}
-
-%*********************************************************
-%* *
-\subsection{Lexing characters and strings}
-%* *
-%*********************************************************
-
-\begin{code}
-------------------------------------------------------------------------
--- char literal
+-- ---------------------------------------------------------------------------
+-- Lexing character literals
lexLitChar :: LexP
lexLitChar =
, string "DEL" >> return '\DEL'
]
-------------------------------------------------------------------------
+
+-- ---------------------------------------------------------------------------
-- string literal
lexString :: LexP
_ | isSpace c -> do skipSpaces; char '\\'; return ()
_ -> do pfail
-------------------------------------------------------------------------
+-- ---------------------------------------------------------------------------
-- single character lexemes
lexSingle :: LexP
return (Single c)
where
isSingleChar c = c `elem` ",;()[]{=}_`"
-\end{code}
-
-%*********************************************************
-%* *
-\subsection{Lexing numbers}
-%* *
-%*********************************************************
+-- ---------------------------------------------------------------------------
+-- Lexing numbers
-\begin{code}
data Number
= MkNumber
{ value :: Either Integer Rational
| 'A' <= c && c <= 'F' = Just (ord c - ord 'A' + 10)
| otherwise = Nothing
-------------------------------------------------------------------------
+-- ----------------------------------------------------------------------
-- conversion
numberToInt :: Number -> Maybe Int
Left n -> Just (fromInteger n)
Right r -> Just (fromRational r)
-------------------------------------------------------------------------
+-- ----------------------------------------------------------------------
-- other numeric lexing functions
readIntP :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadP a
readOctP = readIntP' 8
readDecP = readIntP' 10
readHexP = readIntP' 16
-\end{code}