megaparsec-5.3.1: Monadic parser combinators

Copyright | © 2015–2017 Megaparsec contributors
License | FreeBSD
Maintainer | Mark Karpov <markkarpov92@gmail.com>
Stability | experimental
Portability | portable
Safe Haskell | None
Language | Haskell2010

Text.Megaparsec.Error

Description

Parse errors. Current version of Megaparsec supports well-typed errors instead of `String`-based ones. This gives a lot of flexibility in describing what exactly went wrong as well as a way to return arbitrary data in case of failure.

Synopsis

- data ErrorItem t
  - = Tokens (NonEmpty t)
  - | Label (NonEmpty Char)
  - | EndOfInput
- class Ord e => ErrorComponent e where
- data Dec
  - = DecFail String
  - | DecIndentation Ordering Pos Pos
- data ParseError t e = ParseError {
  - errorPos :: NonEmpty SourcePos
  - errorUnexpected :: Set (ErrorItem t)
  - errorExpected :: Set (ErrorItem t)
  - errorCustom :: Set e
  }
- class ShowToken a where
- class Ord a => ShowErrorComponent a where
- parseErrorPretty :: (Ord t, ShowToken t, ShowErrorComponent e) => ParseError t e -> String
- sourcePosStackPretty :: NonEmpty SourcePos -> String
- parseErrorTextPretty :: (Ord t, ShowToken t, ShowErrorComponent e) => ParseError t e -> String

# Documentation

data ErrorItem t #

Data type that is used to represent “unexpected/expected” items in `ParseError`. The data type is parametrized over the token type `t`.

Since: 5.0.0

Constructors

Tokens (NonEmpty t) | Non-empty stream of tokens
Label (NonEmpty Char) | Label (cannot be empty)
EndOfInput | End of input

Instances

Eq t => Eq (ErrorItem t) # |
Methods (==) :: ErrorItem t -> ErrorItem t -> Bool # (/=) :: ErrorItem t -> ErrorItem t -> Bool #
Data t => Data (ErrorItem t) # |
Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ErrorItem t -> c (ErrorItem t) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (ErrorItem t) # toConstr :: ErrorItem t -> Constr # dataTypeOf :: ErrorItem t -> DataType # dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c (ErrorItem t)) # dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (ErrorItem t)) # gmapT :: (forall b. Data b => b -> b) -> ErrorItem t -> ErrorItem t # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ErrorItem t -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ErrorItem t -> r # gmapQ :: (forall d. Data d => d -> u) -> ErrorItem t -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> ErrorItem t -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> ErrorItem t -> m (ErrorItem t) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ErrorItem t -> m (ErrorItem t) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ErrorItem t -> m (ErrorItem t) #
Ord t => Ord (ErrorItem t) # |
Methods compare :: ErrorItem t -> ErrorItem t -> Ordering # (<) :: ErrorItem t -> ErrorItem t -> Bool # (<=) :: ErrorItem t -> ErrorItem t -> Bool # (>) :: ErrorItem t -> ErrorItem t -> Bool # (>=) :: ErrorItem t -> ErrorItem t -> Bool # max :: ErrorItem t -> ErrorItem t -> ErrorItem t # min :: ErrorItem t -> ErrorItem t -> ErrorItem t #
Read t => Read (ErrorItem t) # |
Methods readsPrec :: Int -> ReadS (ErrorItem t) # readList :: ReadS [ErrorItem t] # readPrec :: ReadPrec (ErrorItem t) # readListPrec :: ReadPrec [ErrorItem t] #
Show t => Show (ErrorItem t) # |
Methods showsPrec :: Int -> ErrorItem t -> ShowS # show :: ErrorItem t -> String # showList :: [ErrorItem t] -> ShowS #
Generic (ErrorItem t) # |
Associated Types type Rep (ErrorItem t) :: * -> * # Methods from :: ErrorItem t -> Rep (ErrorItem t) x # to :: Rep (ErrorItem t) x -> ErrorItem t #
Arbitrary t => Arbitrary (ErrorItem t) # |
Methods arbitrary :: Gen (ErrorItem t) # shrink :: ErrorItem t -> [ErrorItem t] #
NFData t => NFData (ErrorItem t) # |
Methods rnf :: ErrorItem t -> () #
(Ord t, ShowToken t) => ShowErrorComponent (ErrorItem t) # |
Methods showErrorComponent :: ErrorItem t -> String #
type Rep (ErrorItem t) # |
type Rep (ErrorItem t) = D1 (MetaData "ErrorItem" "Text.Megaparsec.Error" "megaparsec-5.3.1-GxZCUFyxxwCHfCqiAPkdT1" False) ((:+:) (C1 (MetaCons "Tokens" PrefixI False) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (NonEmpty t)))) ((:+:) (C1 (MetaCons "Label" PrefixI False) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (NonEmpty Char)))) (C1 (MetaCons "EndOfInput" PrefixI False) U1)))

class Ord e => ErrorComponent e where #

The type class defines how to represent information about various exceptional situations. Data types that are used as custom data component in `ParseError` must be instances of this type class.

Since: 5.0.0

Minimal complete definition

representFail, representIndentation

Methods

representFail :: String -> e #

Represent the message passed to `fail` in parser monad.

Since: 5.0.0

representIndentation :: Ordering -> Pos -> Pos -> e #

Represent information about incorrect indentation.

Since: 5.0.0

Instances

ErrorComponent () # |
Methods representFail :: String -> () # representIndentation :: Ordering -> Pos -> Pos -> () #
ErrorComponent Dec # |
Methods representFail :: String -> Dec # representIndentation :: Ordering -> Pos -> Pos -> Dec #

data Dec #

“Default error component”. This is our instance of `ErrorComponent` provided out-of-box.

Since: 5.0.0

Constructors

DecFail String | `fail` has been used in parser monad
DecIndentation Ordering Pos Pos | Incorrect indentation error: desired ordering between reference level and actual level, reference indentation level, actual indentation level

Instances

Eq Dec # |
Methods (==) :: Dec -> Dec -> Bool # (/=) :: Dec -> Dec -> Bool #
Data Dec # |
Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Dec -> c Dec # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Dec # toConstr :: Dec -> Constr # dataTypeOf :: Dec -> DataType # dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c Dec) # dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Dec) # gmapT :: (forall b. Data b => b -> b) -> Dec -> Dec # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Dec -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Dec -> r # gmapQ :: (forall d. Data d => d -> u) -> Dec -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Dec -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Dec -> m Dec # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Dec -> m Dec # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Dec -> m Dec #
Ord Dec # |
Methods compare :: Dec -> Dec -> Ordering # (<) :: Dec -> Dec -> Bool # (<=) :: Dec -> Dec -> Bool # (>) :: Dec -> Dec -> Bool # (>=) :: Dec -> Dec -> Bool # max :: Dec -> Dec -> Dec # min :: Dec -> Dec -> Dec #
Read Dec # |
Methods readsPrec :: Int -> ReadS Dec # readList :: ReadS [Dec] # readPrec :: ReadPrec Dec # readListPrec :: ReadPrec [Dec] #
Show Dec # |
Methods showsPrec :: Int -> Dec -> ShowS # show :: Dec -> String # showList :: [Dec] -> ShowS #
Arbitrary Dec # |
Methods arbitrary :: Gen Dec # shrink :: Dec -> [Dec] #
NFData Dec # |
Methods rnf :: Dec -> () #
ShowErrorComponent Dec # |
Methods showErrorComponent :: Dec -> String #
ErrorComponent Dec # |
Methods representFail :: String -> Dec # representIndentation :: Ordering -> Pos -> Pos -> Dec #

data ParseError t e #

`ParseError` represents… parse errors. It provides the stack of source positions, a set of expected and unexpected tokens as well as a set of custom associated data. The data type is parametrized over the token type `t` and the custom data `e`.

Note that the stack of source positions contains current position as its head, and the rest of positions allows to track full sequence of include files with topmost source file at the end of the list.

`Semigroup` (and `Monoid`) instance of the data type allows to merge parse errors from different branches of parsing. When merging two `ParseError`s, the longest match is preferred; if positions are the same, custom data sets and collections of message items are combined.

Constructors

ParseError |
Fields errorPos :: NonEmpty SourcePos Stack of source positions errorUnexpected :: Set (ErrorItem t) Unexpected items errorExpected :: Set (ErrorItem t) Expected items errorCustom :: Set e Associated data, if any

Instances

(Eq e, Eq t) => Eq (ParseError t e) # |
Methods (==) :: ParseError t e -> ParseError t e -> Bool # (/=) :: ParseError t e -> ParseError t e -> Bool #
(Ord e, Ord t, Data e, Data t) => Data (ParseError t e) # |
Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ParseError t e -> c (ParseError t e) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (ParseError t e) # toConstr :: ParseError t e -> Constr # dataTypeOf :: ParseError t e -> DataType # dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c (ParseError t e)) # dataCast2 :: Typeable (* -> * -> *) t => (forall d a. (Data d, Data a) => c (t d a)) -> Maybe (c (ParseError t e)) # gmapT :: (forall b. Data b => b -> b) -> ParseError t e -> ParseError t e # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ParseError t e -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ParseError t e -> r # gmapQ :: (forall d. Data d => d -> u) -> ParseError t e -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> ParseError t e -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> ParseError t e -> m (ParseError t e) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ParseError t e -> m (ParseError t e) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ParseError t e -> m (ParseError t e) #
(Ord e, Ord t, Read e, Read t) => Read (ParseError t e) # |
Methods readsPrec :: Int -> ReadS (ParseError t e) # readList :: ReadS [ParseError t e] # readPrec :: ReadPrec (ParseError t e) # readListPrec :: ReadPrec [ParseError t e] #
(Show e, Show t) => Show (ParseError t e) # |
Methods showsPrec :: Int -> ParseError t e -> ShowS # show :: ParseError t e -> String # showList :: [ParseError t e] -> ShowS #
Generic (ParseError t e) # |
Associated Types type Rep (ParseError t e) :: * -> * # Methods from :: ParseError t e -> Rep (ParseError t e) x # to :: Rep (ParseError t e) x -> ParseError t e #
(Ord t, Ord e) => Semigroup (ParseError t e) # |
Methods (<>) :: ParseError t e -> ParseError t e -> ParseError t e # sconcat :: NonEmpty (ParseError t e) -> ParseError t e # stimes :: Integral b => b -> ParseError t e -> ParseError t e #
(Ord t, Ord e) => Monoid (ParseError t e) # |
Methods mempty :: ParseError t e # mappend :: ParseError t e -> ParseError t e -> ParseError t e # mconcat :: [ParseError t e] -> ParseError t e #
(Arbitrary t, Ord t, Arbitrary e, Ord e) => Arbitrary (ParseError t e) # |
Methods arbitrary :: Gen (ParseError t e) # shrink :: ParseError t e -> [ParseError t e] #
(Show t, Typeable * t, Ord t, ShowToken t, Show e, Typeable * e, ShowErrorComponent e) => Exception (ParseError t e) # |
Methods toException :: ParseError t e -> SomeException # fromException :: SomeException -> Maybe (ParseError t e) # displayException :: ParseError t e -> String #
(NFData t, NFData e) => NFData (ParseError t e) # |
Methods rnf :: ParseError t e -> () #
type Rep (ParseError t e) # |
type Rep (ParseError t e) = D1 (MetaData "ParseError" "Text.Megaparsec.Error" "megaparsec-5.3.1-GxZCUFyxxwCHfCqiAPkdT1" False) (C1 (MetaCons "ParseError" PrefixI True) ((:*:) ((:*:) (S1 (MetaSel (Just Symbol "errorPos") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (NonEmpty SourcePos))) (S1 (MetaSel (Just Symbol "errorUnexpected") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Set (ErrorItem t))))) ((:*:) (S1 (MetaSel (Just Symbol "errorExpected") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Set (ErrorItem t)))) (S1 (MetaSel (Just Symbol "errorCustom") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Set e))))))

class ShowToken a where #

Type class `ShowToken` includes methods that allow to pretty-print single token as well as stream of tokens. This is used for rendering of error messages.

Minimal complete definition

showTokens

Methods

showTokens :: NonEmpty a -> String #

Pretty-print non-empty stream of tokens. This function is also used to print single tokens (represented as singleton lists).

Since: 5.0.0

Instances

ShowToken Char # |
Methods showTokens :: NonEmpty Char -> String #

class Ord a => ShowErrorComponent a where #

The type class defines how to print custom data component of `ParseError`.

Since: 5.0.0

Minimal complete definition

showErrorComponent

Methods

showErrorComponent :: a -> String #

Pretty-print custom data component of `ParseError`.

Instances

ShowErrorComponent Dec # |
Methods showErrorComponent :: Dec -> String #
(Ord t, ShowToken t) => ShowErrorComponent (ErrorItem t) # |
Methods showErrorComponent :: ErrorItem t -> String #

parseErrorPretty #

Arguments

:: (Ord t, ShowToken t, ShowErrorComponent e) |
=> ParseError t e | Parse error to render
-> String | Result of rendering

Pretty-print a `ParseError`. The rendered `String` always ends with a newline.

The function is defined as:

    parseErrorPretty e =
      sourcePosStackPretty (errorPos e) ++ ":\n" ++ parseErrorTextPretty e

Since: 5.0.0

sourcePosStackPretty :: NonEmpty SourcePos -> String #

Pretty-print a stack of source positions.

Since: 5.0.0

parseErrorTextPretty #

Arguments

:: (Ord t, ShowToken t, ShowErrorComponent e) |
=> ParseError t e | Parse error to render
-> String | Result of rendering

Pretty-print a textual part of a `ParseError`, that is, everything except stack of source positions. The rendered staring always ends with a new line.

Since: 5.1.0