Return both the result of a parse and a chunk of input that was
consumed during parsing. This relies on the change of the stateOffset
value to evaluate how many tokens were consumed. If you mess with it
manually in the argument parser, prepare for troubles.
Pretty-print a ParseErrorBundle. All ParseErrors in the bundle will
be pretty-printed in order together with the corresponding offending
lines by doing a single pass over the input stream. The rendered String
always ends with a newline.
The parser try p behaves like the parser p, except that it
backtracks the parser state when p fails (either consuming input or
not).
This combinator is used whenever arbitrary look ahead is needed. Since
it pretends that it hasn't consumed any input when p fails, the
(A.<|>) combinator will try its second alternative even if the first
parser failed while consuming input.
For example, here is a parser that is supposed to parse the word “let”
or the word “lexical”:
What happens here? The first parser consumes “le” and fails (because it
doesn't see a “t”). The second parser, however, isn't tried, since the
first parser has already consumed some input! try fixes this behavior
and allows backtracking to work:
Note that as of Megaparsec 4.4.0, string
backtracks automatically (see tokens), so it does not need try.
However, the examples above demonstrate the idea behind try so well
that it was decided to keep them. You still need to use try when your
alternatives are complex, composite parsers.
Performance note: when you need to parse a single token, it is often
a good idea to use satisfy with the right predicate function instead of
creating a complex parser using the combinators.
Parse an alphabetic or numeric digit Unicode characters.
Note that the numeric digits outside the ASCII range are parsed by this
parser but not by digitChar. Such digits may be part of identifiers but
are not used by the printer and reader to represent numbers.
Parse an integer in the hexadecimal representation. The format of the
hexadecimal number is expected to be according to the Haskell report
except for the fact that this parser doesn't parse “0x” or “0X” prefix.
It is a responsibility of the programmer to parse correct prefix before
parsing the number itself.
For example you can make it conform to the Haskell report like this:
Parse an integer in the octal representation. The format of the octal
number is expected to be according to the Haskell report except for the
fact that this parser doesn't parse “0o” or “0O” prefix. It is a
responsibility of the programmer to parse correct prefix before parsing
the number itself.
For example you can make it conform to the Haskell report like this:
octal = char '0' >> char' 'o' >> L.octal
Note: before version 6.0.0 the function returned Integer, i.e. it
wasn't polymorphic in its return type.
Warning: this function does not perform range checks.
signed space p parses an optional sign character (“+” or “-”), then
if there is a sign it consumes optional white space (using the space
parser), then it runs the parser p which should return a number. Sign
of the number is changed according to the previously parsed sign
character.
For example, to parse signed integer you can write:
Given a comment prefix this function returns a parser that skips line
comments. Note that it stops just before the newline character but
doesn't consume the newline. Newline is either supposed to be consumed by
space parser or picked up manually.
This is a helper to parse symbols, i.e. verbatim strings. You pass the
first argument (parser that consumes white space, probably defined via
space) and then you can use the resulting function to parse strings:
symbol = L.symbol spaceConsumer
parens = between (symbol "(") (symbol ")")
braces = between (symbol "{") (symbol "}")
angles = between (symbol "<") (symbol ">")
brackets = between (symbol "[") (symbol "]")
semicolon = symbol ";"
comma = symbol ","
colon = symbol ":"
dot = symbol "."