diff --git a/Text/Megaparsec.hs b/Text/Megaparsec.hs index 6eb0754..3adba68 100644 --- a/Text/Megaparsec.hs +++ b/Text/Megaparsec.hs @@ -132,6 +132,7 @@ module Text.Megaparsec -- * Error messages , MessageItem (..) , ErrorComponent (..) + , Dec (..) , ParseError (..) , ShowToken (..) , ShowErrorComponent (..) diff --git a/Text/Megaparsec/ByteString.hs b/Text/Megaparsec/ByteString.hs index 58b1c01..17e301c 100644 --- a/Text/Megaparsec/ByteString.hs +++ b/Text/Megaparsec/ByteString.hs @@ -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 diff --git a/Text/Megaparsec/ByteString/Lazy.hs b/Text/Megaparsec/ByteString/Lazy.hs index b4f851e..b1b6ed5 100644 --- a/Text/Megaparsec/ByteString/Lazy.hs +++ b/Text/Megaparsec/ByteString/Lazy.hs @@ -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 diff --git a/Text/Megaparsec/Error.hs b/Text/Megaparsec/Error.hs index 574aa57..04a676f 100644 --- a/Text/Megaparsec/Error.hs +++ b/Text/Megaparsec/Error.hs @@ -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. diff --git a/Text/Megaparsec/Prim.hs b/Text/Megaparsec/Prim.hs index 3804266..86c9e40 100644 --- a/Text/Megaparsec/Prim.hs +++ b/Text/Megaparsec/Prim.hs @@ -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 diff --git a/Text/Megaparsec/String.hs b/Text/Megaparsec/String.hs index 78e8e3e..270213f 100644 --- a/Text/Megaparsec/String.hs +++ b/Text/Megaparsec/String.hs @@ -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 diff --git a/Text/Megaparsec/Text.hs b/Text/Megaparsec/Text.hs index a5ed98b..822bcf4 100644 --- a/Text/Megaparsec/Text.hs +++ b/Text/Megaparsec/Text.hs @@ -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 diff --git a/Text/Megaparsec/Text/Lazy.hs b/Text/Megaparsec/Text/Lazy.hs index 6989a83..3f3632e 100644 --- a/Text/Megaparsec/Text/Lazy.hs +++ b/Text/Megaparsec/Text/Lazy.hs @@ -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