- newtype Parser t a = P ([t] -> (EitherE String a, [t]))
- runParser :: Parser t a -> [t] -> (Either String a, [t])
- failBad :: String -> Parser t a
- commit :: Parser t a -> Parser t a
- next :: Parser t t
- satisfy :: (t -> Bool) -> Parser t t
- apply :: Parser t (a -> b) -> Parser t a -> Parser t b
- discard :: Parser t a -> Parser t b -> Parser t a
- adjustErr :: Parser t a -> (String -> String) -> Parser t a
- adjustErrBad :: Parser t a -> (String -> String) -> Parser t a
- indent :: Int -> String -> String
- onFail :: Parser t a -> Parser t a -> Parser t a
- oneOf :: [Parser t a] -> Parser t a
- oneOf' :: [(String, Parser t a)] -> Parser t a
- optional :: Parser t a -> Parser t (Maybe a)
- exactly :: Int -> Parser t a -> Parser t [a]
- many :: Parser t a -> Parser t [a]
- many1 :: Parser t a -> Parser t [a]
- sepBy :: Parser t a -> Parser t sep -> Parser t [a]
- sepBy1 :: Parser t a -> Parser t sep -> Parser t [a]
- bracketSep :: Parser t bra -> Parser t sep -> Parser t ket -> Parser t a -> Parser t [a]
- bracket :: Parser t bra -> Parser t ket -> Parser t a -> Parser t a
- manyFinally :: Parser t a -> Parser t z -> Parser t [a]
- reparse :: [t] -> Parser t ()
The Parser datatype
The Parser
datatype is a fairly generic parsing monad with error
reporting. It can be used for arbitrary token types, not just
String input. (If you require a running state, use module PolyState
instead)
runParser :: Parser t a -> [t] -> (Either String a, [t])Source
Apply a parser to an input token sequence.
failBad :: String -> Parser t aSource
When a simple fail is not strong enough, use failBad for emphasis. An emphasised (severe) error can propagate out through choice operators.
commit :: Parser t a -> Parser t aSource
Commit is a way of raising the severity of any errors found within its argument. Used in the middle of a parser definition, it means that any operations prior to commitment fail softly, but after commitment, they fail hard.
Combinators:
Primitives
apply :: Parser t (a -> b) -> Parser t a -> Parser t bSource
Apply a parsed function to a parsed value
discard :: Parser t a -> Parser t b -> Parser t aSource
x
parses both x and y, but discards the result of y
discard
y
Error-handling
adjustErr :: Parser t a -> (String -> String) -> Parser t aSource
p
applies the transformation adjustErr
ff
to any error message
generated in p
, having no effect if p
succeeds.
adjustErrBad :: Parser t a -> (String -> String) -> Parser t aSource
adjustErrBad
is just like adjustErr
except it also raises the
severity of the error.
indent :: Int -> String -> StringSource
Helper for formatting error messages: indents all lines by a fixed amount.
Choices
onFail :: Parser t a -> Parser t a -> Parser t aSource
p
means parse p unless p fails in which case parse q instead.
Can be chained together to give multiple attempts to parse something.
(Note that q could itself be a failing parser, e.g. to change the error
message from that defined in p to something different.)
However, a *severe* failure in p cannot be ignored.
onFail
q
oneOf' :: [(String, Parser t a)] -> Parser t aSource
Parse the first alternative that succeeds, but if none succeed, report only the severe errors, and if none of those, then report all the soft errors.
optional :: Parser t a -> Parser t (Maybe a)Source
optional
indicates whether the parser succeeded through the Maybe type.
Sequences
exactly :: Int -> Parser t a -> Parser t [a]Source
'exactly n p' parses a precise number of items, n, using the parser p, in sequence.
many :: Parser t a -> Parser t [a]Source
'many p' parses a list of elements with individual parser p. Cannot fail, since an empty list is a valid return value.
sepBy :: Parser t a -> Parser t sep -> Parser t [a]Source
Parse a list of items separated by discarded junk.
sepBy1 :: Parser t a -> Parser t sep -> Parser t [a]Source
Parse a non-empty list of items separated by discarded junk.
bracketSep :: Parser t bra -> Parser t sep -> Parser t ket -> Parser t a -> Parser t [a]Source
Parse a list of items, discarding the start, end, and separator items.
bracket :: Parser t bra -> Parser t ket -> Parser t a -> Parser t aSource
Parse a bracketed item, discarding the brackets.
manyFinally :: Parser t a -> Parser t z -> Parser t [a]Source
'manyFinally e t' parses a possibly-empty sequence of e's, terminated by a t. Any parse failures could be due either to a badly-formed terminator or a badly-formed element, so raise both possible errors.
re-parsing
reparse :: [t] -> Parser t ()Source
Push some tokens back onto the front of the input stream and reparse. This is useful e.g. for recursively expanding macros. When the user-parser recognises a macro use, it can lookup the macro expansion from the parse state, lex it, and then stuff the lexed expansion back down into the parser.