Remove ‘MonadThrow’ instance, add ‘Dec’

‘Dec’ stands for “default error component”. We need this because
‘String’ looses information and thus we cannot write good enough tests
with it.
This commit is contained in:
mrkkrp 2016-04-22 19:44:59 +07:00
parent eedf69761e
commit 17a6bb2baa
8 changed files with 30 additions and 28 deletions

View File

@ -132,6 +132,7 @@ module Text.Megaparsec
-- * Error messages
, MessageItem (..)
, ErrorComponent (..)
, Dec (..)
, ParseError (..)
, ShowToken (..)
, ShowErrorComponent (..)

View File

@ -12,6 +12,7 @@
module Text.Megaparsec.ByteString (Parser) where
import Text.Megaparsec.Error (Dec)
import Text.Megaparsec.Prim
import qualified Data.ByteString as B
@ -20,4 +21,4 @@ import qualified Data.ByteString as B
-- @Parser@ type and easily change it by importing different “type
-- modules”. This one is for strict byte-strings.
type Parser = Parsec String B.ByteString
type Parser = Parsec Dec B.ByteString

View File

@ -12,6 +12,7 @@
module Text.Megaparsec.ByteString.Lazy (Parser) where
import Text.Megaparsec.Error (Dec)
import Text.Megaparsec.Prim
import qualified Data.ByteString.Lazy as B
@ -20,4 +21,4 @@ import qualified Data.ByteString.Lazy as B
-- @Parser@ type and easily change it by importing different “type
-- modules”. This one is for lazy byte-strings.
type Parser = Parsec String B.ByteString
type Parser = Parsec Dec B.ByteString

View File

@ -20,6 +20,7 @@
module Text.Megaparsec.Error
( MessageItem (..)
, ErrorComponent (..)
, Dec (..)
, ParseError (..)
, ShowToken (..)
, ShowErrorComponent (..)
@ -69,11 +70,6 @@ class Ord e => ErrorComponent e where
representFail :: String -> e
-- | Represent exception thrown in parser monad. (The monad implements
-- 'Control.Monad.Catch.MonadThrow').
representException :: Exception e' => e' -> e
-- | Represent information about incorrect indentation.
representIndentation
@ -81,12 +77,19 @@ class Ord e => ErrorComponent e where
-> Pos -- ^ Expected indentation level
-> e
instance ErrorComponent String where
representFail = id
representException = ("exception: " ++) . show
representIndentation actual expected =
"incorrect indentation level (got " ++ show (unPos actual) ++
", but (at least) " ++ show (unPos expected) ++ " is expected"
-- | “Default error component”. This in our instance of 'ErrorComponent'
-- provided out-of-box.
--
-- @since 5.0.0
data Dec
= DecFail String -- ^ 'fail' has been used in parser monad
| DecIndentation Pos Pos -- ^ Incorrect indentation error
deriving (Show, Eq, Ord, Data, Typeable)
instance ErrorComponent Dec where
representFail = DecFail
representIndentation = DecIndentation
-- | The data type @ParseError@ represents parse errors. It provides the
-- stack of source positions, set of expected and unexpected tokens as well
@ -196,8 +199,11 @@ instance (Ord t, ShowToken t) => ShowErrorComponent (MessageItem t) where
showErrorComponent (Label label) = NE.toList label
showErrorComponent EndOfInput = "end of input"
instance ShowErrorComponent String where
showErrorComponent = id
instance ShowErrorComponent Dec where
showErrorComponent (DecFail msg) = msg
showErrorComponent (DecIndentation actual expected) =
"incorrect indentation level (got " ++ show (unPos actual) ++
", but (at least) " ++ show (unPos expected) ++ " is expected"
-- | Pretty-print 'ParseError'. Note that rendered 'String' always ends with
-- a newline.

View File

@ -54,7 +54,6 @@ module Text.Megaparsec.Prim
where
import Control.Monad
import Control.Monad.Catch (Exception, MonadThrow (..))
import Control.Monad.Cont.Class
import Control.Monad.Error.Class
import Control.Monad.Identity
@ -394,15 +393,6 @@ pFail msg = ParsecT $ \s@(State _ pos _) _ _ _ eerr ->
where d = E.singleton (representFail msg)
{-# INLINE pFail #-}
instance (ErrorComponent e, Stream s)
=> MonadThrow (ParsecT e s m) where
throwM = pThrowM
pThrowM :: (Exception e', ErrorComponent e) => e' -> ParsecT e s m a
pThrowM e = ParsecT $ \s@(State _ pos _) _ _ _ eerr ->
eerr (ParseError pos E.empty E.empty d) s
where d = E.singleton (representException e)
-- | Low-level creation of the 'ParsecT' type.
mkPT :: Monad m => (State s -> m (Reply e s a)) -> ParsecT e s m a

View File

@ -12,6 +12,7 @@
module Text.Megaparsec.String (Parser) where
import Text.Megaparsec.Error (Dec)
import Text.Megaparsec.Prim
-- | Different modules corresponding to various types of streams (@String@,
@ -19,4 +20,4 @@ import Text.Megaparsec.Prim
-- @Parser@ type and easily change it by importing different “type
-- modules”. This one is for strings.
type Parser = Parsec String String
type Parser = Parsec Dec String

View File

@ -12,6 +12,7 @@
module Text.Megaparsec.Text (Parser) where
import Text.Megaparsec.Error (Dec)
import Text.Megaparsec.Prim
import qualified Data.Text as T
@ -20,4 +21,4 @@ import qualified Data.Text as T
-- @Parser@ type and easily change it by importing different “type
-- modules”. This one is for strict text.
type Parser = Parsec String T.Text
type Parser = Parsec Dec T.Text

View File

@ -12,6 +12,7 @@
module Text.Megaparsec.Text.Lazy (Parser) where
import Text.Megaparsec.Error (Dec)
import Text.Megaparsec.Prim
import qualified Data.Text.Lazy as T
@ -20,4 +21,4 @@ import qualified Data.Text.Lazy as T
-- @Parser@ type and easily change it by importing different “type
-- modules”. This one is for lazy text.
type Parser = Parsec String T.Text
type Parser = Parsec Dec T.Text