Parsing with parsec

A parser is a value

parsec builds a parser from small parsers with combinators, and a parser is a value like any other: it can be passed, returned and named. The type names the input, the state and the result:

parse :: Parsec String () a -> String -> String -> Either ParseError a Parser a = Parsec String () a many1 :: Parser a -> Parser [a] digit :: Parser Char

parse takes the parser, a name for the source (shown in errors) and the input, and gives either an error with a position or the result. Nothing is consumed by naming a parser: it runs when parse is called, which is what makes a parser a pure value and a test of a parser a pure function.

A number and a grammar

Two parsers and one combinator are enough for a sum of numbers: