| License | BSD-style |
|---|---|
| Maintainer | Haskell Foundation |
| Stability | experimental |
| Portability | portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Foundation.Parser
Contents
Description
The current implementation is mainly, if not copy/pasted, inspired from
memory's Parser.
A very simple bytearray parser related to Parsec and Attoparsec
Simple example:
> parse ((,,) <$> take 2 <*> element 0x20 <*> (elements "abc" *> anyElement)) "xx abctest"
ParseOK "est" ("xx", 116)- newtype Parser input a = Parser {}
- data Result input a
- data ParserError input
- = Expected {
- expectedInput :: !input
- receivedInput :: !input
- | NotEnough
- | MonadFail String
- = Expected {
- parse :: Sequential input => Parser input a -> input -> Result input a
- parseFeed :: (Sequential input, Monad m) => m (Maybe input) -> Parser input a -> input -> m (Result input a)
- hasMore :: Sequential input => Parser input Bool
- element :: (Sequential input, Eq (Element input)) => Element input -> Parser input ()
- anyElement :: Sequential input => Parser input (Element input)
- elements :: (Show input, Eq input, Sequential input) => input -> Parser input ()
- string :: String -> Parser String ()
- take :: Sequential input => Int -> Parser input input
- takeWhile :: Sequential input => (Element input -> Bool) -> Parser input input
- takeAll :: Sequential input => Parser input input
- skip :: Sequential input => Int -> Parser input ()
- skipWhile :: Sequential input => (Element input -> Bool) -> Parser input ()
- skipAll :: Sequential input => Parser input ()
- optional :: Alternative f => f a -> f (Maybe a)
- many :: Alternative f => forall a. f a -> f [a]
- some :: Alternative f => forall a. f a -> f [a]
Documentation
Simple parser structure
Constructors
| Parser | |
Simple parsing result, that represent respectively:
- failure: with the error message
- continuation: that need for more input data
- success: the remaining unparsed data and the parser value
data ParserError input #
Constructors
| Expected | |
Fields
| |
| NotEnough | not enough data to complete the parser |
| MonadFail String | only use in the event of Monad.fail function |
Instances
| Eq input => Eq (ParserError input) # | |
| Ord input => Ord (ParserError input) # | |
| Show input => Show (ParserError input) # | |
| (Show input, Typeable * input) => Exception (ParserError input) # | |
run the Parser
parse :: Sequential input => Parser input a -> input -> Result input a #
Run a Parser on a ByteString and return a Result
parseFeed :: (Sequential input, Monad m) => m (Maybe input) -> Parser input a -> input -> m (Result input a) #
Run a parser on an @initial input.
If the Parser need more data than available, the @feeder function is automatically called and fed to the More continuation.
Parser methods
hasMore :: Sequential input => Parser input Bool #
element :: (Sequential input, Eq (Element input)) => Element input -> Parser input () #
Parse a specific `Element input` at current position
if the `Element input` is different than the expected one, this parser will raise a failure.
anyElement :: Sequential input => Parser input (Element input) #
Get the next `Element input` from the parser
elements :: (Show input, Eq input, Sequential input) => input -> Parser input () #
Parse a sequence of elements from current position
if the following `Element input` don't match the expected
input completely, the parser will raise a failure
take :: Sequential input => Int -> Parser input input #
Take @n elements from the current position in the stream
takeWhile :: Sequential input => (Element input -> Bool) -> Parser input input #
Take elements while the @predicate hold from the current position in the stream
takeAll :: Sequential input => Parser input input #
Take the remaining elements from the current position in the stream
skip :: Sequential input => Int -> Parser input () #
Skip @n elements from the current position in the stream
skipWhile :: Sequential input => (Element input -> Bool) -> Parser input () #
Skip `Element input` while the @predicate hold from the current position in the stream
skipAll :: Sequential input => Parser input () #
Skip all the remaining `Element input` from the current position in the stream
optional :: Alternative f => f a -> f (Maybe a) #
One or none.
many :: Alternative f => forall a. f a -> f [a] #
Zero or more.
some :: Alternative f => forall a. f a -> f [a] #
One or more.