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




The type of parsers

type Parser s r = [s] -> [(r, [s])]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 :: Parser s aSource

Parser for the empty language, i.e., parser that always fails.

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 :: a -> Parser s aSource

Same as succeed; provided for compatiblity with the applicative interface.

Parser combinators

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

Choice between two parsers with the same result type.

(<<|>) :: (t -> [a]) -> (t -> [a]) -> t -> [a]Source

(<*>) :: Parser s (b -> a) -> Parser s b -> Parser s aSource

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

Sequence of two parsers.

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

Map a function over the results of a parser. The <$> combinator can also be defined in terms of succeed and <*>:

 f <$> p  =  succeed f <*> p

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

Monadic bind. Do not use this combinator unless absolutely required. Most sequencing can be done with <*>.


look :: Parser s [s]Source

Returns the rest of the input without consuming anything.

Running parsers

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

For compatibility with the newtype version of the library: runs a parser on a given string.