where method

  1. @useResult
Parser<R> where(
  1. Predicate<R> predicate, {
  2. String? message,
  3. FailureFactory<R>? factory,
  4. @Deprecated('Use `factory` instead') Callback<R, String>? failureMessage,
  5. @Deprecated('Use `factory` instead') Callback<R, int>? failurePosition,
  6. @Deprecated('Use `factory` instead') FailureFactory<R>? failureFactory,
})

Returns a parser that evaluates the predicate with the successful parse result. If the predicate returns true the parser proceeds with the parse result, otherwise a parse failure is created using the optionally specified factory callback, the provided message, or otherwise an automatically created error message.

The following example parses two characters, but only succeeds if they are equal:

final inner = any() & any();
final parser = inner.where(
    (value) => value[0] == value[1],
    factory: (context, success) =>
        context.failure('characters do not match'));
parser.parse('aa');   // ==> Success: ['a', 'a']
parser.parse('ab');   // ==> Failure: characters do not match

Implementation

@useResult
Parser<R> where(
  Predicate<R> predicate, {
  String? message,
  FailureFactory<R>? factory,
  @Deprecated('Use `factory` instead') Callback<R, String>? failureMessage,
  @Deprecated('Use `factory` instead') Callback<R, int>? failurePosition,
  @Deprecated('Use `factory` instead') FailureFactory<R>? failureFactory,
}) =>
    WhereParser<R>(
        this,
        predicate,
        createFactory_<R>(
            message: message,
            factory: factory ?? failureFactory,
            failureMessage: failureMessage,
            failurePosition: failurePosition));