Copyright | © 2015–2018 Megaparsec contributors |
---|---|
License | FreeBSD |
Maintainer | Mark Karpov <markkarpov92@gmail.com> |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
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.
You probably do not want to import this module directly because Text.Megaparsec re-exports it anyway.
- data ErrorItem t
- data ErrorFancy e
- data ParseError t e
- = TrivialError (NonEmpty SourcePos) (Maybe (ErrorItem t)) (Set (ErrorItem t))
- | FancyError (NonEmpty SourcePos) (Set (ErrorFancy e))
- errorPos :: ParseError t e -> NonEmpty SourcePos
- class ShowToken a where
- class LineToken a where
- class Ord a => ShowErrorComponent a where
- parseErrorPretty :: (Ord t, ShowToken t, ShowErrorComponent e) => ParseError t e -> String
- parseErrorPretty' :: (ShowToken (Token s), LineToken (Token s), ShowErrorComponent e, Stream s) => s -> ParseError (Token s) e -> String
- parseErrorPretty_ :: forall s e. (ShowToken (Token s), LineToken (Token s), ShowErrorComponent e, Stream s) => Pos -> s -> ParseError (Token s) e -> String
- sourcePosStackPretty :: NonEmpty SourcePos -> String
- parseErrorTextPretty :: (Ord t, ShowToken t, ShowErrorComponent e) => ParseError t e -> String
Parse error type
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
Tokens (NonEmpty t) | Non-empty stream of tokens |
Label (NonEmpty Char) | Label (cannot be empty) |
EndOfInput | End of input |
Functor ErrorItem Source # | |
Eq t => Eq (ErrorItem t) Source # | |
Data t => Data (ErrorItem t) Source # | |
Ord t => Ord (ErrorItem t) Source # | |
Read t => Read (ErrorItem t) Source # | |
Show t => Show (ErrorItem t) Source # | |
Generic (ErrorItem t) Source # | |
NFData t => NFData (ErrorItem t) Source # | |
(Ord t, ShowToken t) => ShowErrorComponent (ErrorItem t) Source # | |
type Rep (ErrorItem t) Source # | |
data ErrorFancy e Source #
Additional error data, extendable by user. When no custom data is
necessary, the type is typically indexed by Void
to “cancel” the
ErrorCustom
constructor.
Since: 6.0.0
ErrorFail String |
|
ErrorIndentation Ordering Pos Pos | Incorrect indentation error: desired ordering between reference level and actual level, reference indentation level, actual indentation level |
ErrorCustom e | Custom error data, can be conveniently disabled by indexing
|
Functor ErrorFancy Source # | |
Eq e => Eq (ErrorFancy e) Source # | |
Data e => Data (ErrorFancy e) Source # | |
Ord e => Ord (ErrorFancy e) Source # | |
Read e => Read (ErrorFancy e) Source # | |
Show e => Show (ErrorFancy e) Source # | |
Generic (ErrorFancy e) Source # | |
NFData a => NFData (ErrorFancy a) Source # | |
ShowErrorComponent e => ShowErrorComponent (ErrorFancy e) Source # | |
type Rep (ErrorFancy e) Source # | |
data ParseError t e Source #
represents a parse error parametrized over the token
type ParseError
t et
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
instances of the data type allow 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. Note that
fancy errors take precedence over trivial errors in merging.
Since: 6.0.0
TrivialError (NonEmpty SourcePos) (Maybe (ErrorItem t)) (Set (ErrorItem t)) | Trivial errors, generated by Megaparsec's machinery. The data constructor includes the stack of source positions, unexpected token (if any), and expected tokens. |
FancyError (NonEmpty SourcePos) (Set (ErrorFancy e)) | Fancy, custom errors. |
(Eq e, Eq t) => Eq (ParseError t e) Source # | |
(Ord e, Ord t, Data e, Data t) => Data (ParseError t e) Source # | |
(Ord e, Ord t, Read e, Read t) => Read (ParseError t e) Source # | |
(Show e, Show t) => Show (ParseError t e) Source # | |
Generic (ParseError t e) Source # | |
(Ord t, Ord e) => Semigroup (ParseError t e) Source # | |
(Ord t, Ord e) => Monoid (ParseError t e) Source # | |
(Show t, Ord t, ShowToken t, Typeable * t, Show e, ShowErrorComponent e, Typeable * e) => Exception (ParseError t e) Source # | |
(NFData t, NFData e) => NFData (ParseError t e) Source # | |
type Rep (ParseError t e) Source # | |
errorPos :: ParseError t e -> NonEmpty SourcePos Source #
Get position of given ParseError
.
Since: 6.0.0
Pretty-printing
class ShowToken a where Source #
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.
Since: 5.0.0
showTokens :: NonEmpty a -> String Source #
Pretty-print non-empty stream of tokens. This function is also used to print single tokens (represented as singleton lists).
class LineToken a where Source #
Type class for tokens that support operations necessary for selecting and displaying relevant line of input.
Since: 6.0.0
tokenAsChar :: a -> Char Source #
Convert a token to a Char
. This is used to print relevant line from
input stream by turning a list of tokens into a String
.
tokenIsNewline :: a -> Bool Source #
Check if given token is a newline or contains newline.
class Ord a => ShowErrorComponent a where Source #
The type class defines how to print custom data component of
ParseError
.
Since: 5.0.0
showErrorComponent :: a -> String Source #
Pretty-print custom data component of ParseError
.
ShowErrorComponent Void Source # | |
ShowErrorComponent e => ShowErrorComponent (ErrorFancy e) Source # | |
(Ord t, ShowToken t) => ShowErrorComponent (ErrorItem t) Source # | |
:: (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.
Since: 5.0.0
:: (ShowToken (Token s), LineToken (Token s), ShowErrorComponent e, Stream s) | |
=> s | Original input stream |
-> ParseError (Token s) e | Parse error to render |
-> String | Result of rendering |
Pretty-print a ParseError
and display the line on which the parse
error occurred. The rendered String
always ends with a newline.
Note that if you work with include files and have a stack of
SourcePos
es in ParseError
, it's up to you to provide correct input
stream corresponding to the file in which parse error actually happened.
parseErrorPretty'
is defined in terms of the more general
parseErrorPretty_
function which allows to specify tab width as well:
parseErrorPretty' = parseErrorPretty_ defaultTabWidth
Since: 6.0.0
:: (ShowToken (Token s), LineToken (Token s), ShowErrorComponent e, Stream s) | |
=> Pos | Tab width |
-> s | Original input stream |
-> ParseError (Token s) e | Parse error to render |
-> String | Result of rendering |
Just like parseErrorPretty'
, but allows to specify tab width.
Since: 6.1.0
sourcePosStackPretty :: NonEmpty SourcePos -> String Source #
Pretty-print a stack of source positions.
Since: 5.0.0
:: (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