expect method

void expect (Pattern pattern, { String name })

If pattern matches at the current position of the string, scans forward until the end of the match.

If pattern did not match, throws a FormatException describing the position of the failure. name is used in this error as the expected name of the pattern being matched; if it's null, the pattern itself is used instead.

Implementation

void expect(Pattern pattern, {String name}) {
  if (scan(pattern)) return;

  if (name == null) {
    if (pattern is RegExp) {
      var source = pattern.pattern;
      if (!_slashAutoEscape) source = source.replaceAll("/", "\\/");
      name = "/$source/";
    } else {
      name =
          pattern.toString().replaceAll("\\", "\\\\").replaceAll('"', '\\"');
      name = '"$name"';
    }
  }
  _fail(name);
}