| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Text.Pup.Backend.Megaparsec
Description
This module defines a Text.Megaparsec backend for format descriptors. To that
effect, this module introduces a type class MonadParsec modeled after
Megarparsec's own type class, so that format descriptors can take advantage
of Megaparsec features.
The MonadParsec class in this module isn't exactly the same as the one from
Megaparsec (mainly because it needs to be implementable on pretty-printers as
well). As such, some of the code in this module is similar or identical to
Megaparsec, many of the docstrings are taken directly from Megaparsec as
well.
Synopsis
- type Backend err s = BackendGen (Parsec err s)
- run :: (ShowErrorComponent err, VisualStream s, TraversableStream s) => Backend err s r r' a -> String -> s -> Either String a
- newtype BackendGen (m :: Type -> Type) r r' a = MkBackend (IgnoreIndices m r r' a)
- class (Stream s, MonadPlus m, Stacked m, Tokens (Token s) (Tokens s) m, LookAhead m) => MonadParsec s (m :: Type -> Type -> Type -> Type) | m -> s where
- module Text.Pup.Class
Concrete backend
type Backend err s = BackendGen (Parsec err s) Source #
run :: (ShowErrorComponent err, VisualStream s, TraversableStream s) => Backend err s r r' a -> String -> s -> Either String a Source #
newtype BackendGen (m :: Type -> Type) r r' a Source #
Constructors
| MkBackend (IgnoreIndices m r r' a) |
Instances
Megaparsec-specific format descriptors
class (Stream s, MonadPlus m, Stacked m, Tokens (Token s) (Tokens s) m, LookAhead m) => MonadParsec s (m :: Type -> Type -> Type -> Type) | m -> s where Source #
A Megaparsec-style class for format descriptors. Inheriting from Megaparsec, it is somewhat parsing-oriented.
The documentation of individual methods is taken from
MonadParsec.
Note: some primitive from MonadParsec, in particular
reflection/escape hatch primitives, are omitted from this class because they
aren't implementable for printers. If you need them, you want to act at the
parsec level, not the Pup level.
Methods
try :: m r r' a -> m r r' a Source #
The parser behaves like the parser try pp, except that it
backtracks the parser state when p fails (either consuming input or
not).
This combinator is used whenever arbitrary look ahead is needed. Since
it pretends that it hasn't consumed any input when p fails, the
(<|>) combinator will try its second alternative even if the first
parser failed while consuming input.
For example, here is a parser that is supposed to parse the word “let” or the word “lexical”:
>>>parseTest (string "let" <|> string "lexical") "lexical"1:1: unexpected "lex" expecting "let"
What happens here? The first parser consumes “le” and fails (because it
doesn't see a “t”). The second parser, however, isn't tried, since the
first parser has already consumed some input! try fixes this behavior
and allows backtracking to work:
>>>parseTest (try (string "let") <|> string "lexical") "lexical""lexical"
try also improves error messages in case of overlapping alternatives,
because Megaparsec's hint system can be used:
>>>parseTest (try (string "let") <|> string "lexical") "le"1:1: unexpected "le" expecting "let" or "lexical"
Note that as of Megaparsec 4.4.0, string
backtracks automatically (see tokens), so it does not need try.
However, the examples above demonstrate the idea behind try so well
that it was decided to keep them. You still need to use try when your
alternatives are complex, composite parsers.
Arguments
| :: (Token s -> Maybe a) | Matching function for the token to parse |
| -> Set (ErrorItem (Token s)) | Used in the error message to mention the items that were expected |
| -> (a -> Token s) | Printing function for the token |
| -> m (a -> r) r a |
The parser accepts tokens for which the
matching function token test expectedtest returns Just results. If Nothing is
returned the expected set is used to report the items that were
expected.
For example, the satisfy parser is implemented as:
satisfy f = token testToken Set.empty
where
testToken x = if f x then Just x else NothingInstances
| MonadParsec Text (Backend ann) Source # | |
| (MonadParsec e s m, Stream s) => MonadParsec s (BackendGen m) Source # | |
Defined in Text.Pup.Backend.Megaparsec Methods try :: BackendGen m r r' a -> BackendGen m r r' a Source # token :: (Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> (a -> Token s) -> BackendGen m (a -> r) r a Source # | |
| Ord err => MonadParsec Text (Pup err ann) Source # | |
| (MonadParsec s m1, MonadParsec s m2) => MonadParsec s (m1 :*: m2) Source # | |
Reexports
module Text.Pup.Class