foundation-0.0.3: Alternative prelude with batteries and no dependencies

License | BSD-style
Maintainer | Haskell Foundation
Stability | experimental
Portability | portable
Safe Haskell | None
Language | Haskell2010

Foundation.Parser

Contents

- run the Parser
- Parser methods

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)

Synopsis

- newtype Parser input a = Parser {
  - runParser :: forall r. input -> Failure input r -> Success input a r -> Result input r
  }
- data Result input a
  - = ParseFail (ParserError input)
  - | ParseMore (Maybe input -> Result input a)
  - | ParseOK input a
- data ParserError input
  - = Expected {
    - expectedInput :: !input
    - receivedInput :: !input
    }
  - | NotEnough
  - | MonadFail String
- 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

newtype Parser input a #

Simple parser structure

Constructors

Parser |
Fields runParser :: forall r. input -> Failure input r -> Success input a r -> Result input r

Instances

Monad (Parser input) # |
Methods (>>=) :: Parser input a -> (a -> Parser input b) -> Parser input b # (>>) :: Parser input a -> Parser input b -> Parser input b # return :: a -> Parser input a # fail :: String -> Parser input a #
Functor (Parser input) # |
Methods fmap :: (a -> b) -> Parser input a -> Parser input b # (<$) :: a -> Parser input b -> Parser input a #
Applicative (Parser input) # |
Methods pure :: a -> Parser input a # (<*>) :: Parser input (a -> b) -> Parser input a -> Parser input b # (*>) :: Parser input a -> Parser input b -> Parser input b # (<*) :: Parser input a -> Parser input b -> Parser input a #
Alternative (Parser input) # |
Methods empty :: Parser input a # (<|>) :: Parser input a -> Parser input a -> Parser input a # some :: Parser input a -> Parser input [a] # many :: Parser input a -> Parser input [a] #
MonadPlus (Parser input) # |
Methods mzero :: Parser input a # mplus :: Parser input a -> Parser input a -> Parser input a #

data Result input a #

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

Constructors

ParseFail (ParserError input) |
ParseMore (Maybe input -> Result input a) |
ParseOK input a |

Instances

(Show ba, Show a) => Show (Result ba a) # |
Methods showsPrec :: Int -> Result ba a -> ShowS # show :: Result ba a -> String # showList :: [Result ba a] -> ShowS #

data ParserError input #

Constructors

Expected |
Fields expectedInput :: !input the expected input receivedInput :: !input but received this data
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) # |
Methods (==) :: ParserError input -> ParserError input -> Bool # (/=) :: ParserError input -> ParserError input -> Bool #
Ord input => Ord (ParserError input) # |
Methods compare :: ParserError input -> ParserError input -> Ordering # (<) :: ParserError input -> ParserError input -> Bool # (<=) :: ParserError input -> ParserError input -> Bool # (>) :: ParserError input -> ParserError input -> Bool # (>=) :: ParserError input -> ParserError input -> Bool # max :: ParserError input -> ParserError input -> ParserError input # min :: ParserError input -> ParserError input -> ParserError input #
Show input => Show (ParserError input) # |
Methods showsPrec :: Int -> ParserError input -> ShowS # show :: ParserError input -> String # showList :: [ParserError input] -> ShowS #
(Show input, Typeable * input) => Exception (ParserError input) # |
Methods toException :: ParserError input -> SomeException # fromException :: SomeException -> Maybe (ParserError input) # displayException :: ParserError input -> String #

# 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

string :: String -> Parser String () #

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.