MaintainerThomas Hallgren
Safe HaskellSafe

Parsing

Contents

Description

A Simple Monadic Parsing Library

Used in the course Functional Programming GU/Chalmers.

Original author: David Sands.

Synopsis

The Parser type

data Parser a Source #

The abstract data type representing a Parser

Instances

Monad Parser Source # 

Methods

(>>=) :: Parser a -> (a -> Parser b) -> Parser b

(>>) :: Parser a -> Parser b -> Parser b

return :: a -> Parser a #

fail :: String -> Parser a

Functor Parser Source # 

Methods

fmap :: (a -> b) -> Parser a -> Parser b

(<$) :: a -> Parser b -> Parser a

Applicative Parser Source # 

Methods

pure :: a -> Parser a

(<*>) :: Parser (a -> b) -> Parser a -> Parser b #

(*>) :: Parser a -> Parser b -> Parser b #

(<*) :: Parser a -> Parser b -> Parser a #

Alternative Parser Source # 

Methods

empty :: Parser a

(<|>) :: Parser a -> Parser a -> Parser a #

some :: Parser a -> Parser [a]

many :: Parser a -> Parser [a]

parse :: Parser a -> String -> Maybe (a, String) Source #

Runs the parser on the given string to return maybe a thing and a string

Basic parsers

sat :: (Char -> Bool) -> Parser Char Source #

parse a single character satisfying property p

item :: Parser Char Source #

Parse any single character

digit :: Parser Char Source #

parse a digit character

readsP :: Read a => Parser a Source #

A parser for anything in the Read class, satisfying

parse readsP s == listToMaybe (reads s)

char :: Char -> Parser Char Source #

Parse a specific character

failure :: Parser a Source #

Parser than can never succeed

Combining parsers

oneOrMore :: Parser a -> Parser [a] Source #

Parse one or more things

zeroOrMore :: Parser a -> Parser [a] Source #

Parse zero or more things

chain :: Parser a -> Parser b -> Parser [a] Source #

Parse a list of as, separated by bs

(<:>) :: Parser a -> Parser [a] -> Parser [a] Source #

Parse a thing, then parse a list of things, and return the first thing followed by the list of things

More general operators useful in parser construction

Try two alternatives

(<|>) :: Alternative f => forall a. f a -> f a -> f a #

Apply a function to the result of a parser

(<$>) :: Functor f => (a -> b) -> f a -> f b #

(<*>) :: Applicative f => forall a b. f (a -> b) -> f a -> f b #

Parse two things, return only the first

(<*) :: Applicative f => forall a b. f a -> f b -> f a #

Parse two things, return only the second

(*>) :: Applicative f => forall a b. f a -> f b -> f b #

Return a result without consuming any input

return :: Monad m => forall a. a -> m a #