-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Lightweight package providing commonly useful parser combinators -- -- Lightweight package providing commonly useful parser combinators. @package parser-combinators @version 0.2.0 -- | This module is a generalization of the package -- parsec-permutation authored by Samuel Hoffstaetter: -- -- https://hackage.haskell.org/package/parsec-permutation -- -- This module also takes inspiration from the algorithm is described in: -- Parsing Permutation Phrases, by Arthur Baars, Andres Loh and -- Doaitse Swierstra. Published as a functional pearl at the Haskell -- Workshop 2001. -- -- From these two works we derive a flexible and general method for -- parsing permutations over an Applicative structure. Quite -- useful in conjunction with "Free" constructions of Applicatives, -- Monads, etc. -- -- Other permutation parsing libraries tend towards using special "almost -- applicative" combinators for construction which denies the library -- user the ability to lift and unlift permutation parsing into any -- Applicative computational context. We redefine these -- combinators as convenience operators here alongside the equivalent -- Applicative instance. -- -- For example, suppose we want to parse a permutation of: an optional -- string of a's, the character b and an optional -- c. Using a standard parsing library combinator char, -- this can be described using the Applicative instance by: -- --
-- test = runPermutation $ -- (,,) <$> toPermutationWithDefault "" (some (char 'a')) -- <*> toPermutation (char 'b') -- <*> toPermutationWithDefault '_' (char 'c') ---- -- Equivalently, this can also be describe using the convenience -- operators reminiscent of other parsing libraries: -- --
-- test = runPermutation $
-- (,,) <$?> ("", some (char 'a'))
-- <||> char 'b'
-- <|?> ('_', char 'c')
--
module Control.Applicative.Permutations
-- | An Applicative wrapper-type for constructing permutation
-- parsers.
data Permutation m a
-- | "Unlifts" a permutation parser into a parser to be evaluated.
runPermutation :: (Alternative m, Monad m) => Permutation m a -> m a
-- | "Unlifts" a permutation parser into a parser to be evaluated with an
-- intercalated effect. Useful for separators between permutation
-- elements.
--
-- For example, suppose that similar to above we want to parse a
-- permutation of: an optional string of a's, the character
-- b and an optional c. However, we also want
-- each element of the permutation to be separated by a colon. Using a
-- standard parsing library combinator char, this can be
-- described using the Applicative instance by:
--
--
-- test = intercalateEffect (char ':') $
-- (,,) <$?> ("", some (char 'a'))
-- <||> char 'b'
-- <|?> ('_', char 'c')
--
--
-- This will accept strings such as: "a:b:c", "b:c:a", "b:aa", "b", etc.
--
-- Note that the effect is intercalated between permutation
-- components and that:
--
--
-- braces = between (symbol "{") (symbol "}")
--
between :: Applicative m => m open -> m close -> m a -> m a
-- | choice ps tries to apply the parsers in the list
-- ps in order, until one of them succeeds. Returns the value of
-- the succeeding parser.
choice :: (Foldable f, Alternative m) => f (m a) -> m a
-- | count n p parses n occurrences of p.
-- If n is smaller or equal to zero, the parser equals to
-- pure []. Returns a list of n values.
--
-- See also: count'.
count :: Applicative m => Int -> m a -> m [a]
-- | count' m n p parses from m to n
-- occurrences of p. If n is not positive or m >
-- n, the parser equals to pure []. Returns a list
-- of parsed values.
--
-- Please note that m may be negative, in this case
-- effect is the same as if it were equal to zero.
--
-- See also: count.
count' :: Alternative m => Int -> Int -> m a -> m [a]
-- | Combine two alternatives.
eitherP :: Alternative m => m a -> m b -> m (Either a b)
-- | endBy p sep parses zero or more occurrences of
-- p, separated and ended by sep. Returns a list of
-- values returned by p.
--
-- -- cStatements = cStatement `endBy` semicolon --endBy :: Alternative m => m a -> m sep -> m [a] -- | endBy1 p sep parses one or more occurrences of -- p, separated and ended by sep. Returns a list of -- values returned by p. endBy1 :: Alternative m => m a -> m sep -> m [a] -- | manyTill p end applies parser p zero -- or more times until parser end succeeds. Returns the list of -- values returned by p. -- -- See also: skipMany, skipManyTill. manyTill :: Alternative m => m a -> m end -> m [a] -- | someTill p end works similarly to manyTill -- p end, but p should succeed at least once. -- -- See also: skipSome, skipSomeTill. someTill :: Alternative m => m a -> m end -> m [a] -- | option x p tries to apply the parser p. If -- p fails without consuming input, it returns the value -- x, otherwise the value returned by p. -- --
-- priority = option 0 (digitToInt <$> digitChar) ---- -- See also: optional. option :: Alternative m => a -> m a -> m a -- | sepBy p sep parses zero or more occurrences of -- p, separated by sep. Returns a list of values -- returned by p. -- --
-- commaSep p = p `sepBy` comma --sepBy :: Alternative m => m a -> m sep -> m [a] -- | sepBy1 p sep parses one or more occurrences of -- p, separated by sep. Returns a list of values -- returned by p. sepBy1 :: Alternative m => m a -> m sep -> m [a] -- | sepEndBy p sep parses zero or more occurrences -- of p, separated and optionally ended by sep. Returns -- a list of values returned by p. sepEndBy :: Alternative m => m a -> m sep -> m [a] -- | sepEndBy1 p sep parses one or more occurrences -- of p, separated and optionally ended by sep. Returns -- a list of values returned by p. sepEndBy1 :: Alternative m => m a -> m sep -> m [a] -- | skipMany p applies the parser p zero -- or more times, skipping its result. -- -- See also: manyTill, skipManyTill. skipMany :: Alternative m => m a -> m () -- | skipSome p applies the parser p one or -- more times, skipping its result. -- -- See also: someTill, skipSomeTill. skipSome :: Alternative m => m a -> m () -- | skipManyTill p end applies the parser p -- zero or more times skipping results until parser end -- succeeds. Result parsed by end is then returned. -- -- See also: manyTill, skipMany. skipManyTill :: Alternative m => m a -> m end -> m end -- | skipSomeTill p end applies the parser p -- one or more times skipping results until parser end -- succeeds. Result parsed by end is then returned. -- -- See also: someTill, skipSome. skipSomeTill :: Alternative m => m a -> m end -> m end -- | This module provides NonEmpty list variants of some of the -- functions in Control.Applicative.Combinators. module Control.Applicative.Combinators.NonEmpty -- | some p applies the parser p one or -- more times and returns a list of the returned values of p. -- --
-- word = some letter --some :: Alternative m => m a -> m (NonEmpty a) -- | endBy1 p sep parses one or more occurrences of -- p, separated and ended by sep. Returns a non-empty -- list of values returned by p. endBy1 :: Alternative m => m a -> m sep -> m (NonEmpty a) -- | someTill p end works similarly to manyTill -- p end, but p should succeed at least once. -- -- See also: skipSome, skipSomeTill. someTill :: Alternative m => m a -> m end -> m (NonEmpty a) -- | sepBy1 p sep parses one or more occurrences of -- p, separated by sep. Returns a non-empty list of -- values returned by p. sepBy1 :: Alternative m => m a -> m sep -> m (NonEmpty a) -- | sepEndBy1 p sep parses one or more occurrences -- of p, separated and optionally ended by sep. Returns -- a non-empty list of values returned by p. sepEndBy1 :: Alternative m => m a -> m sep -> m (NonEmpty a)