Small parser, inspired by Parsec, but much less versatile. A bit faster and uses less memory than the transformer in Data.NanoParsec.
Parser Monad
Parser s t r
is the type of parsers that reads tokens of type t from a stream s t and returns either an error message string or a result r paired with the yet unprocessed input.
Works in principle like a combination of State and Either, where the state is the current input to get parsed, and the bind operations will stop on the first Either.Left result, which signals a syntax error.
Parser is also an instance of MonadAlt. The operations MonadAlt.<+> and Alt.<|> can be used to implement choice, i.e. they allow to continue on failure.
The input for a Parser must be something that is an instance of ListView. Ordinary lists, Strings, StringIterator and ArrayIterator are such types. The parser combinators use only the ListView.uncons and ListEmpty.null operations.
access field run
Remember the current state of the input stream.
getPos always succeeds.
Resume parsing with the given input stream.
putPos always succeeds.
fail with given error message
run a parser
run a Parser, return just the result/error
run a Parser on some input and report
Causes the error message to be msg when p fails.
The error will be reported at the position where p was attempted, not necessarily at the position where p failed:
Parser.run (letter >> digit) "a?" unexpected token, found ? Parser.run (label "id expected" $ letter >> digit) "a?" id expected, found a?
p <?> msg
Causes the error message to be msg when p fails.
The error will be reported at the position where p failed.
expect t
This parser succeeds if the input is not empty and the head of the input equals t.
The eos parser succeeds if the input is empty.
satisfy property
Succeeds if there is a next token t and property t is true.
exceptWhen prop
Fails if and only if there is a next token t and prop t is true.
Succeeds on end of stream or when the token does not satisfy the property.
In other words, exceptWhen p succeeds if and only if satisfy p fails.
pany
Fails if and only if eos succeeds, otherwise returns the next token.
symbol p
Is the same parser as p, but skips spaces afterwards
parse a single space character
parse a single digit
parse a single letter
parse a single uppercase letter
skip any spaces
This parser succeeds if the pattern matches the beginning of the string.
For efficiency reasons, the pattern should start with ^
The optional parser always succeeds and returns its result wrapped in Maybe
many p
Collects as many p as possible and returns the results in a list.
Succeeds also when p fails the first time, in that case the result is an empty list.
Must not be applied to a parser that always succeeds!
some p
Like many, except there must be at least one p
Must not be applied to a parser that always succeeds!
Alias for some
skip p
equivalent to
many p >> return ()
but faster, because it does not build up lists.
Must not be applied to a parser that always succeeds!
manyWhile p
Collect tokens as long as they satisfy p
skipWhile p
Skip tokens as long as they satisfy predicate p.
Beware of negative predicates like
skipWhile (!= 'a')
This will loop forever if there is no 'a' in the input stream. Instead use
skipUntil (== 'a')
skipUntil p
Skip tokens as long as they do not satisfy p
When this succeeds, the next token will satisfy p or the stream is exhausted.
cond pcond pthen pelse
if pcond succeeds, then pthen is run, else pelse
To both pthen and pelse it will appear as if pcond didn't consume any tokens.
select [(if1, t1), (if2, t2), ..., (ifn, tn)] e
unfolds as
cond if1 t1 (cond if2 t2 (... (cond ifn tn e)))
choice ps
Tries the parsers in the list from left to right, until success.
count n p
Applies p n times and returns a list of the results
between left right p
Parses left, then p and finally right and returns the result of p
option v p
Applies p, and returns v when it fails.
Always succeeds.
p `sepBy1` q
Parses p and many q followed by p
p `sepBy` q
Like sepBy1, but allows zero p elements
p `endBy` q
Parses zero or more occurrences of p separated and ended by q
p `endBy1` q
Parses one or more occurrences of p separated and ended by q
p `sepEndBy` q
Parses zero or more occurrences of p, separated and optionally ended by q
p `sepEndBy1` q
Parses one or more occurrences of p, separated and optionally ended by q
inherited from Monad.<*>
p <+> q
The result is that of the first parser, if it succeeds, otherwise that of the second one, which is applied to the input left over by p
p <|> q
The result is that of the first parser, if it succeeds, otherwise that of the second one. Note that q is run on the same input as p, even if p already consumed something.
expect 'c' <|> expect 'd'
would succeed if the input starts with 'c' or 'd'.
inherited from Monad.>>
p >>= (r -> q) p >> q
If p succeeds, the overall result is that of q Otherwise, the overall result is failure.
Could be read as p followed by q.
inherited from Monad.fmap
inherited from Monad.join
generic success
generic failure