uu-tc-2009.2.2: Haskell 98 parser combintors for INFOB3TC at Utrecht University




The type of parsers

data Parser s r Source

An input string is mapped to a list of successful parses. For each succesful parse, we return the result of type r, and the remaining input string. The input must be a list of symbols.

Elementary parsers

anySymbol :: Parser s sSource

Parses any single symbol.

satisfy :: (s -> Bool) -> Parser s sSource

Takes a predicate and returns a parser that parses a single symbol satisfying that predicate.

empty :: Alternative f => forall a. f a

The identity of <|>

failp :: Parser s aSource

Same as empty; provided for compatibility with the lecture notes.

succeed :: a -> Parser s aSource

Parser that always succeeds, i.e., for epsilon.

pure :: Applicative f => forall a. a -> f a

Lift a value.

Parser combinators

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

An associative binary operation

(<<|>) :: Parser s a -> Parser s a -> Parser s aSource

Biased choice. If the left hand side parser succeeds, the right hand side is not considered. Use with care!

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

Sequential application.

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

An infix synonym for fmap.

(>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b

Sequentially compose two actions, passing any value produced by the first as an argument to the second.


look :: Parser s [s]Source

Returns the rest of the input without consuming anything.

Running parsers

parse :: Parser s a -> [s] -> [(a, [s])]Source

Runs a parser on a given string.