expect method

void expect(
  1. Pattern pattern,
  2. {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.


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

  if (name == null) {
    if (pattern is RegExp) {
      final source = pattern.pattern;
      name = '/$source/';
    } else {
      name =
          pattern.toString().replaceAll(r'\', r'\\').replaceAll('"', r'\"');
      name = '"$name"';