HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulefoundation-0.0.30Haskell2010

Foundation.Parser

The current implementation is mainly, if not copy/pasted, inspired from memory's Parser.

Foundation Parser makes use of the Foundation's Collection and Sequential classes to allow you to define generic parsers over any Sequential of inpu.

This way you can easily implements parsers over LString, String.

flip parseOnly "my.email@address.com" $ do
   EmailAddress
     <$> (takeWhile ((/=) '@' <*  element '@')
     <*> takeAll
  • 5 types
  • 1 class
  • 19 values
  • Packagefoundation-0.0.30
  • Exports28
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceParser.hs
newtypenewtype Parser input result
#

Foundation's Parser monad.

Its implementation is based on the parser in memory.

Instances5Monad, Functor, Applicative, Alternative, MonadPlus
valueparseFeed
  1. :: (ParserSource input, Monad m)
  2. => m (Chunk input)
  3. -> Parser input a
  4. -> input
  5. -> 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.

Result

3 declarations
datadata Result input result
#

result of executing the parser over the given input

Constructors

  • ParseFailed (ParseError input)

    the parser failed with the given ParserError

  • ParseOk (Chunk input) result

    the parser complete successfuly with the remaining Chunk

  • ParseMore (Chunk input -> Result input result)

    the parser needs more input, pass an empty Chunk or mempty to tell the parser you don't have anymore inputs.

Instances2Functor, Show
  • Functor (Result input)Defined in foundation-0.0.30 · Foundation.Parser
  • (Show k, Show input) => Show (Result input k)Defined in foundation-0.0.30 · Foundation.Parser
datadata ParseError input
#

common parser error definition

Constructors

Instances3Show, Exception
valuereportError :: ParseError input -> Parser input a
#

helper function to report error when writing parsers

This way we can provide more detailed error when building custom parsers and still avoid to use the naughty _fail_.

myParser :: Parser input Int
myParser = reportError $ Satisfy (Just "this function is not implemented...")

Parser source

1 declaration
classclass (Sequential input, IndexedCollection input) => ParserSource input where
#

Associated types

Methods

Instances2ParserSource

combinator

20 declarations
method(<|>) :: f a -> f a -> f a
#

An associative binary operation

methodmany :: f a -> f [a]
#

Zero or more.

Examples
Example1 expression
many (putStr "la")lalalalalalalalala... * goes on forever *
Example1 expression
many NothingJust []
Example1 expression
take 5 <$> many (Just 1)* hangs forever *

Note that this function can be used with Parsers based on Applicatives. In that case many parser will attempt to parse parser zero or more times until it fails.

methodsome :: f a -> f [a]
#

One or more.

Examples
Example1 expression
some (putStr "la")lalalalalalalalala... * goes on forever *
Example1 expression
some Nothingnothing
Example1 expression
take 5 <$> some (Just 1)* hangs forever *

Note that this function can be used with Parsers based on Applicatives. In that case some parser will attempt to parse parser one or more times until it fails.

valueoptional :: Alternative f => f a -> f (Maybe a)
#

One or none.

It is useful for modelling any computation that is allowed to fail.

Examples

Using the Alternative instance of Control.Monad.Except, the following functions:

Example1 expression
import Control.Monad.Except
Example2 expressions
canFail = throwError "it failed" :: Except String Intfinal = return 42                :: Except String Int

Can be combined by allowing the first function to fail:

Example1 expression
runExcept $ canFail *> finalLeft "it failed"
Example1 expression
runExcept $ optional canFail *> finalRight 42
valuerepeat
  1. :: ParserSource input
  2. => Condition
  3. -> Parser input a
  4. -> Parser input [a]
#

repeat the given parser a given amount of time

Unlike some or many, this operation will bring more precision on how many times you wish a parser to be sequenced.

## Repeat Exactly a number of time

repeat (Exactly 6) (takeWhile ((/=) ',') <* element ',')

## Repeat Between lower `And` upper times

repeat (Between $ 1 `And` 10) (takeWhile ((/=) ',') <* element ',')
datadata And
#

Constructors

Instances2Eq, Show
  • Eq AndDefined in foundation-0.0.30 · Foundation.Parser
  • Show AndDefined in foundation-0.0.30 · Foundation.Parser