Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- type Parser (b :: Type -> Type) (i :: Type) (a :: Type) = FreeT ((->) i) b a
- recurse :: forall t b i a. (Parser b i a -> (Parser b i a -> t b a) -> t b a) -> Parser b i a -> t b a
- parseStream :: forall s b i a. Monad b => (s -> b (i, s)) -> Parser (StateT s b) i a -> s -> b (a, s)
- parseString :: forall b i a. MonadPlus b => Parser (StateT [i] b) i a -> [i] -> b (a, [i])
- token :: forall b i. Monad b => Parser b i i
- eof :: Alternative b => Parser (StateT [i] b) i ()
- notFollowedBy :: forall b i a. (Alternative b, Foldable b, MonadPlus b) => Parser (StateT [i] b) i a -> Parser (StateT [i] b) i ()
- satisfy :: forall b i. MonadPlus b => (i -> Bool) -> Parser b i i
- isToken :: forall b i. (MonadPlus b, Eq i) => i -> Parser b i i
- isNotToken :: forall b i. (MonadPlus b, Eq i) => i -> Parser b i i
- scan :: (Alternative m, Monad m) => (s -> a -> Maybe s) -> s -> m a -> m [a]
- atMost :: (Alternative m, Monad m) => Int -> m a -> m [a]
- eitherP :: Alternative f => f a -> f b -> f (Either a b)
- void :: Functor f => f a -> f ()
- combine :: (Applicative f, Semigroup a) => f a -> f a -> f a
- combines :: (Applicative f, Monoid a) => [f a] -> f a
- isString :: (Traversable t, MonadPlus b, Eq i) => t i -> Parser b i (t i)
- string :: MonadPlus b => Parser b i [i]
- intP :: (CharParsing m, Monad m) => m Int
- doubleP :: (CharParsing m, Monad m) => m Double
Documentation
type Parser (b :: Type -> Type) (i :: Type) (a :: Type) = FreeT ((->) i) b a Source #
Parser b i a
is a parser that consumes a stream of i
tokens and as a
result yields a value of type a
, while operating under the b
non-determinism monad.
For most purposes, the non-determinism monad b
should be a MonadPlus
.
Useful examples include []
or Logic
if you want backtracking,
and Maybe
if you want no backtracking.
Use
if you want to maintain a state StateT
s []s
that is
automatically reverted when backtracking via []
.
hoistFreeT
can be used to change the backtracking monad.
FreeT
provides instances for Functor
, Applicative
, Monad
,
Alternative
and MonadPlus
.
recurse :: forall t b i a. (Parser b i a -> (Parser b i a -> t b a) -> t b a) -> Parser b i a -> t b a Source #
Recurse over a parser.
Tears down the free monad transformer over the '(->) i' functor using iteration:
recurse next parser = next parser (parser' -> next parser' (parser'' -> next parser'' (parser''' -> next parser''' (...))))
parseStream :: forall s b i a. Monad b => (s -> b (i, s)) -> Parser (StateT s b) i a -> s -> b (a, s) Source #
token :: forall b i. Monad b => Parser b i i Source #
token
is trivial parser that consumes a single token i
and yields it.
Other parsers can be derived from this one using methods of the
Functor
, Applicative
, Monad
, Alternative
, and MonadPlus
typeclasses
and the parser combinators in this module.
notFollowedBy :: forall b i a. (Alternative b, Foldable b, MonadPlus b) => Parser (StateT [i] b) i a -> Parser (StateT [i] b) i () Source #
satisfy :: forall b i. MonadPlus b => (i -> Bool) -> Parser b i i Source #
satisfy p
is a simple parser that consumes a single token i
and yields it
if and only if p i
evaluates to True
. Otherwise, the parser fails.
isToken :: forall b i. (MonadPlus b, Eq i) => i -> Parser b i i Source #
is i
is a simple parser that consumes a single token and yields it
if and only if it is equal to i
. Otherwise, the parser fails.
isNotToken :: forall b i. (MonadPlus b, Eq i) => i -> Parser b i i Source #
isNot i
is a simple parser that consumes a single token and yields it
if and only if it is not equal to i
. If the token is equal to i
,
the parser fails.
scan :: (Alternative m, Monad m) => (s -> a -> Maybe s) -> s -> m a -> m [a] Source #
Stateful scanner.
>>>
:{
f s a | "ell" `isInfixOf` (s ++ [a]) = Nothing | otherwise = Just (s ++ [a]) :}
>>>
head $ parseString @[] (scan f "" token) "hello 123"
("hel","lo 123")
atMost :: (Alternative m, Monad m) => Int -> m a -> m [a] Source #
atMost n p
applies the parser p
at most n
times and returns
every parsing result. If parsing of p
succeeds less the n
times,
repeatP n p
succeeds as well.
>>>
head $ parseString @[] (atMost 2 (isToken 'a')) "aaaaaab"
("aa","aaaab")
eitherP :: Alternative f => f a -> f b -> f (Either a b) Source #
eitherP p p'
combines the two alternatives p
and p'
.
combine :: (Applicative f, Semigroup a) => f a -> f a -> f a Source #
combine p p'
merges the results of p
and p'
using the Semigroup
instance.
combines :: (Applicative f, Monoid a) => [f a] -> f a Source #
combines ps
merges the results of the parsers ps
using the Monoid
instance.
string :: MonadPlus b => Parser b i [i] Source #
string
matches any string
>>>
parseString @[] string "a string"
[("a string",""),("a strin","g"),("a stri","ng"),("a str","ing"),("a st","ring"),("a s","tring"),("a ","string"),("a"," string"),("","a string")]
- - >>> p = string @[] >>= s -> (guard ("dog"
isInfixOf
s) >> pure s) - - >>> head $ parseString p "this is a string with a dog"
- - ("this is a string with a dog","")
- - >>> p = string @[] >>= s -> (guard (not $ "dog"
isInfixOf
s) >> pure s) - - >>> head $ parseString p "this is also string with a dog"
- - ("this is also string with a do","g")