or method
Returns a parser that accepts the receiver or other
. The resulting
parser returns the parse result of the receiver, if the receiver fails
it returns the parse result of other
(exclusive ordered choice).
An optional failureJoiner
can be specified that determines which
Failure to return in case both parsers fail. By default the last
failure is returned selectLast, but selectFarthest is another
common choice that usually gives better error messages.
For example, the parser letter().or(digit())
accepts a letter or a
digit. An example where the order matters is the following choice between
overlapping parsers: letter().or(char('a'))
. In the example the parser
char('a')
will never be activated, because the input is always consumed
letter()
. This can be problematic if the author intended to attach a
production action to char('a')
.
Due to https://github.com/dart-lang/language/issues/1557 the resulting
parser cannot be properly typed. Please use ChoiceIterableExtension
as a workaround: [first, second].toChoiceParser()
.
Implementation
@useResult
ChoiceParser<dynamic> or(Parser other, {FailureJoiner? failureJoiner}) =>
switch (this) {
ChoiceParser(
children: final children,
failureJoiner: final thisFailureJoiner
) =>
[
...children,
other
].toChoiceParser(failureJoiner: failureJoiner ?? thisFailureJoiner),
_ => [this, other].toChoiceParser(failureJoiner: failureJoiner)
};