Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell98 |
Text.Appar.String
Contents
Description
Simple Applicative
parser whose input is String
.
The usage is the same as parsec.
Parsec 3 provides features which Parsec 2 does not provide:
Applicative
styleByteString
as input
But Haskell Platform includes Parsec 2, not Parsec 3. Installing Parsec 3 to Haskell Platform environment makes it mess. So, this library was implemented.
Synopsis
- type Parser = MkParser String
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- try :: MkParser inp a -> MkParser inp a
- (<*>) :: Applicative f => f (a -> b) -> f a -> f b
- pure :: Applicative f => a -> f a
- (*>) :: Applicative f => f a -> f b -> f b
- (<*) :: Applicative f => f a -> f b -> f a
- (<|>) :: Alternative f => f a -> f a -> f a
- some :: Alternative f => f a -> f [a]
- many :: Alternative f => f a -> f [a]
- (<$) :: Functor f => a -> f b -> f a
- (<**>) :: Applicative f => f a -> f (a -> b) -> f b
- satisfy :: Input inp => (Char -> Bool) -> MkParser inp Char
- char :: Input inp => Char -> MkParser inp Char
- string :: Input inp => String -> MkParser inp String
- choice :: [MkParser inp a] -> MkParser inp a
- option :: a -> MkParser inp a -> MkParser inp a
- skipMany :: MkParser inp a -> MkParser inp ()
- sepBy1 :: MkParser inp a -> MkParser inp b -> MkParser inp [a]
- manyTill :: MkParser inp a -> MkParser inp b -> MkParser inp [a]
- data MkParser inp a = P {}
- space :: Input inp => MkParser inp Char
- class Eq inp => Input inp where
- parse :: Input inp => MkParser inp a -> inp -> Maybe a
- anyChar :: Input inp => MkParser inp Char
- oneOf :: Input inp => String -> MkParser inp Char
- noneOf :: Input inp => String -> MkParser inp Char
- alphaNum :: Input inp => MkParser inp Char
- digit :: Input inp => MkParser inp Char
- hexDigit :: Input inp => MkParser inp Char
- skipSome :: MkParser inp a -> MkParser inp ()
Documentation
Parser type
(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #
An infix synonym for fmap
.
The name of this operator is an allusion to $
.
Note the similarities between their types:
($) :: (a -> b) -> a -> b (<$>) :: Functor f => (a -> b) -> f a -> f b
Whereas $
is function application, <$>
is function
application lifted over a Functor
.
Examples
Convert from a
to a Maybe
Int
using Maybe
String
show
:
>>>
show <$> Nothing
Nothing>>>
show <$> Just 3
Just "3"
Convert from an
to an
Either
Int
Int
Either
Int
String
using show
:
>>>
show <$> Left 17
Left 17>>>
show <$> Right 17
Right "17"
Double each element of a list:
>>>
(*2) <$> [1,2,3]
[2,4,6]
Apply even
to the second element of a pair:
>>>
even <$> (2,2)
(2,True)
try :: MkParser inp a -> MkParser inp a Source #
The parser try p behaves like parser p, except that it pretends that it hasn't consumed any input when an error occurs.
(<*>) :: Applicative f => f (a -> b) -> f a -> f b infixl 4 #
Sequential application.
A few functors support an implementation of <*>
that is more
efficient than the default one.
Example
Used in combination with (
, <$>
)(
can be used to build a record.<*>
)
>>>
data MyState = MyState {arg1 :: Foo, arg2 :: Bar, arg3 :: Baz}
>>>
produceFoo :: Applicative f => f Foo
>>>
produceBar :: Applicative f => f Bar
>>>
produceBaz :: Applicative f => f Baz
>>>
mkState :: Applicative f => f MyState
>>>
mkState = MyState <$> produceFoo <*> produceBar <*> produceBaz
pure :: Applicative f => a -> f a #
Lift a value.
(*>) :: Applicative f => f a -> f b -> f b infixl 4 #
Sequence actions, discarding the value of the first argument.
Examples
If used in conjunction with the Applicative instance for Maybe
,
you can chain Maybe computations, with a possible "early return"
in case of Nothing
.
>>>
Just 2 *> Just 3
Just 3
>>>
Nothing *> Just 3
Nothing
Of course a more interesting use case would be to have effectful computations instead of just returning pure values.
>>>
import Data.Char
>>>
import Text.ParserCombinators.ReadP
>>>
let p = string "my name is " *> munch1 isAlpha <* eof
>>>
readP_to_S p "my name is Simon"
[("Simon","")]
(<*) :: Applicative f => f a -> f b -> f a infixl 4 #
Sequence actions, discarding the value of the second argument.
(<|>) :: Alternative f => f a -> f a -> f a infixl 3 #
An associative binary operation
some :: Alternative f => f a -> f [a] #
One or more.
many :: Alternative f => f a -> f [a] #
Zero or more.
(<**>) :: Applicative f => f a -> f (a -> b) -> f b infixl 4 #
satisfy :: Input inp => (Char -> Bool) -> MkParser inp Char Source #
The parser satisfy f
succeeds for any character for which the
supplied function f
returns True
. Returns the character that is
actually parsed.
char :: Input inp => Char -> MkParser inp Char Source #
char c
parses a single character c
. Returns the parsed character.
string :: Input inp => String -> MkParser inp String Source #
string s
parses a sequence of characters given by s
. Returns
the parsed string
choice :: [MkParser inp a] -> MkParser inp a Source #
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.
option :: a -> MkParser inp a -> MkParser inp a Source #
option x p
tries to apply parser p
. If p
fails without
consuming input, it returns the value x
, otherwise the value
returned by p
.
skipMany :: MkParser inp a -> MkParser inp () Source #
skipMany p
applies the parser p
zero or more times, skipping
its result.
sepBy1 :: MkParser inp a -> MkParser inp b -> MkParser inp [a] Source #
sepBy1 p sep
parses one or more occurrences of p
, separated
by sep
. Returns a list of values returned by p
.
manyTill :: MkParser inp a -> MkParser inp b -> MkParser inp [a] Source #
manyTill p end
applies parser p
zero or more times until
parser end
succeeds. Returns the list of values returned by p
.
Instances
MonadFail (MkParser inp) Source # | |
Defined in Text.Appar.Parser | |
Alternative (MkParser inp) Source # | |
Applicative (MkParser inp) Source # | |
Defined in Text.Appar.Parser | |
Functor (MkParser inp) Source # | |
Monad (MkParser inp) Source # | |
MonadPlus (MkParser inp) Source # | |
space :: Input inp => MkParser inp Char Source #
Parses a white space character (any character which satisfies isSpace
)
Returns the parsed character.
class Eq inp => Input inp where Source #
The class for parser input.
Methods
The head function for input
The tail function for input
The end of input
The function to check the end of input
Instances
Input ByteString Source # | |
Defined in Text.Appar.Input Methods car :: ByteString -> Char Source # cdr :: ByteString -> ByteString Source # nil :: ByteString Source # isNil :: ByteString -> Bool Source # | |
Input ByteString Source # | |
Defined in Text.Appar.Input Methods car :: ByteString -> Char Source # cdr :: ByteString -> ByteString Source # nil :: ByteString Source # isNil :: ByteString -> Bool Source # | |
Input String Source # | |
anyChar :: Input inp => MkParser inp Char Source #
This parser succeeds for any character. Returns the parsed character.
oneOf :: Input inp => String -> MkParser inp Char Source #
oneOf cs
succeeds if the current character is in the supplied list of
characters cs
. Returns the parsed character.
noneOf :: Input inp => String -> MkParser inp Char Source #
As the dual of oneOf
, noneOf cs
succeeds if the current
character not in the supplied list of characters cs
. Returns the
parsed character.
alphaNum :: Input inp => MkParser inp Char Source #
Parses a letter or digit (a character between '0' and '9'). Returns the parsed character.