| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Text.Pup.MPR
Description
This module defines a pup with a Megaparsec backend for the parser side and a Prettyprinter backend for the parser.
Start with Pup' if you don't have any advanced need, change to the more
general Pup if you want pretty-printing annotations or custom parse error.
Synopsis
- type Pup' = Pup Void ()
- print :: Pup err ann (a -> Maybe (Doc ann)) (Maybe (Doc ann)) b -> a -> Maybe (Doc ann)
- parse :: ShowErrorComponent err => Pup err ann r r' a -> String -> Text -> Either String a
- newtype Pup err ann r r' a = MkPup ((Backend ann :*: Backend err Text) r r' a)
- module Text.Pup.Class
- module Text.Pup.Class.Char
- 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
A simple pup to get you started
More general version
newtype Pup err ann r r' a Source #
Instances
| Annotations ann (Pup err ann :: Type -> Type -> Type -> Type) Source # | |
| Fail (Pup err ann :: Type -> Type -> Type -> Type) Source # | |
Defined in Text.Pup.MPR | |
| Ord err => Tokens Char Text (Pup err ann) Source # | |
Defined in Text.Pup.MPR Methods eof :: Pup err ann r r () Source # single :: Char -> Pup err ann r r Char Source # satisfy :: (Char -> Bool) -> Pup err ann (Char -> r) r Char Source # tokens :: (Text -> Text -> Bool) -> Text -> Pup err ann (Text -> r) r Text Source # takeWhileP :: Maybe String -> (Char -> Bool) -> Pup err ann (Text -> r) r Text Source # takeWhile1P :: Maybe String -> (Char -> Bool) -> Pup err ann (Text -> r) r Text Source # takeP :: Maybe String -> Int -> Pup err ann (Text -> r) r Text Source # label :: String -> Pup err ann r r' a -> Pup err ann r r' a Source # | |
| Ord err => MonadParsec Text (Pup err ann) Source # | |
| Ord err => Breaks (Pup err ann :: Type -> Type -> Type -> Type) Source # | |
| Ord err => WadlerLeijen (Pup err ann :: Type -> Type -> Type -> Type) Source # | |
| Applicative (Pup err ann :: Type -> Type -> Type -> Type) Source # | |
Defined in Text.Pup.MPR Methods pure :: a -> Pup err ann i i a # (<*>) :: Pup err ann i j (a -> b) -> Pup err ann j k1 a -> Pup err ann i k1 b # liftA2 :: (a -> b -> c) -> Pup err ann i j a -> Pup err ann j k1 b -> Pup err ann i k1 c # (*>) :: Pup err ann i j a -> Pup err ann j k1 b -> Pup err ann i k1 b # (<*) :: Pup err ann i j a -> Pup err ann j k1 b -> Pup err ann i k1 a # | |
| Monad (Pup err ann :: Type -> Type -> Type -> Type) Source # | |
| Ord err => LookAhead (Pup err ann) Source # | |
| Stacked (Pup err ann) Source # | |
Defined in Text.Pup.MPR | |
| Ord err => ParseErrors (ParseError Text err) (Pup err ann) Source # | |
Defined in Text.Pup.MPR Methods parseError :: ParseError Text err -> Pup err ann r r' a Source # withRecovery :: (ParseError Text err -> Pup err ann r r' a) -> Pup err ann r r' a -> Pup err ann r r' a Source # observing :: Pup err ann (b -> r) r a -> Pup err ann (Either (ParseError Text err) b -> r) r (Either (ParseError Text err) a) Source # | |
| Ord err => Alternative (Pup err ann r r) Source # | |
| Applicative (Pup err ann r r) Source # | |
Defined in Text.Pup.MPR Methods pure :: a -> Pup err ann r r a # (<*>) :: Pup err ann r r (a -> b) -> Pup err ann r r a -> Pup err ann r r b # liftA2 :: (a -> b -> c) -> Pup err ann r r a -> Pup err ann r r b -> Pup err ann r r c # (*>) :: Pup err ann r r a -> Pup err ann r r b -> Pup err ann r r b # (<*) :: Pup err ann r r a -> Pup err ann r r b -> Pup err ann r r a # | |
| Functor (Pup err ann r r') Source # | |
| Monad (Pup err ann r r) Source # | |
| Ord err => MonadPlus (Pup err ann r r) Source # | |
| (Ord err, r ~ r', a ~ ()) => IsString (Pup err ann r r' a) Source # | |
Defined in Text.Pup.MPR Methods fromString :: String -> Pup err ann r r' a # | |
| Ord err => Additive (Pup err ann r r' a) Source # | |
Re-exports
module Text.Pup.Class
module Text.Pup.Class.Char
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 # | |