Safe Haskell | None |
---|---|
Language | Haskell2010 |
Module containing internal helpers for our parsers.
Synopsis
- data FailingParser parser
- = FailingParser (forall a. parser a)
- | NoFailingParser
- eliminateFailing :: Alternative parser => FailingParser parser -> parser a
- data Permutation parser a = Permutation !(Maybe a) !(FailingParser parser) [Branch parser a]
- data Branch parser a = forall arg. Branch (Permutation parser (arg -> a)) (parser arg)
- wrapEffect :: forall m a b. Alternative m => m b -> m b -> Permutation m a -> m a
- asParser :: Alternative f => Permutation f a -> f a
- asPermutation :: Alternative f => f a -> Permutation f a
- asPermutationWithDefault :: Alternative f => f a -> a -> Permutation f a
- asPermutationWithFailing :: Alternative f => f a -> (forall b. f b) -> Permutation f a
- asPermutationWithDefaultFailing :: Alternative f => f a -> (forall b. f b) -> a -> Permutation f a
Documentation
data FailingParser parser Source #
FailingParser (forall a. parser a) | |
NoFailingParser |
Instances
Applicative parser => Semigroup (FailingParser parser) Source # | |
Defined in Jordan.FromJSON.Internal.Permutation (<>) :: FailingParser parser -> FailingParser parser -> FailingParser parser # sconcat :: NonEmpty (FailingParser parser) -> FailingParser parser # stimes :: Integral b => b -> FailingParser parser -> FailingParser parser # |
eliminateFailing :: Alternative parser => FailingParser parser -> parser a Source #
data Permutation parser a Source #
A parser for permutations.
Based on the paper Parsing Permutation Phrases by Arthur Baars, Andres Loh, and S. Doaitse Swierstra.
The source code for Permutations
really helped
in writing this, although this type is structured differently (and closer to the actual paper).
Thank you very much to Alex Washburn!
Permutation !(Maybe a) !(FailingParser parser) [Branch parser a] |
Instances
Functor m => Functor (Permutation m) Source # | |
Defined in Jordan.FromJSON.Internal.Permutation fmap :: (a -> b) -> Permutation m a -> Permutation m b # (<$) :: a -> Permutation m b -> Permutation m a # | |
Alternative m => Applicative (Permutation m) Source # | |
Defined in Jordan.FromJSON.Internal.Permutation pure :: a -> Permutation m a # (<*>) :: Permutation m (a -> b) -> Permutation m a -> Permutation m b # liftA2 :: (a -> b -> c) -> Permutation m a -> Permutation m b -> Permutation m c # (*>) :: Permutation m a -> Permutation m b -> Permutation m b # (<*) :: Permutation m a -> Permutation m b -> Permutation m a # |
A branch of a permutation. Permutation parsers work by building up the entire tree of possible parsers, which is efficient in Haskell due to laziness.
forall arg. Branch (Permutation parser (arg -> a)) (parser arg) |
Instances
Functor m => Functor (Branch m) Source # | |
Alternative m => Applicative (Branch m) Source # | |
:: forall m a b. Alternative m | |
=> m b | Consume a single, "junk" field. Used to ignore JSON keys that we do not care about. |
-> m b | Consume a "separator" between items in the permutation. This consumption is not done at the front of the permutation or after the end of it. This is used to parse commas between JSON fields. |
-> Permutation m a | The permutation parser to run. |
-> m a | The final parser. |
Wrap up a permutation parser with two effects:
It will first interleave an infinite number of some effects, which represent parsing "junk" or unwanted fields. At every stage of the permutation, we will first try to run the effect we want, and if it fails we will try to run the "junk" effect instead, then try again.
We attempt to *intersperse* the second effect afterwards. It adds a new effect between every effect. This is used in parsing JSON to add commas.
asParser :: Alternative f => Permutation f a -> f a Source #
asPermutation :: Alternative f => f a -> Permutation f a Source #
asPermutationWithDefault :: Alternative f => f a -> a -> Permutation f a Source #
asPermutationWithFailing :: Alternative f => f a -> (forall b. f b) -> Permutation f a Source #
asPermutationWithDefaultFailing :: Alternative f => f a -> (forall b. f b) -> a -> Permutation f a Source #