tryMatch method

bool tryMatch(
  1. InlineParser parser,
  2. [int? startMatchPos]
)

Tries to match at the parser's current position.

The parser's position can be overriden with startMatchPos. Returns whether or not the pattern successfully matched.

Implementation

bool tryMatch(InlineParser parser, [int? startMatchPos]) {
  startMatchPos ??= parser.pos;

  // Before matching with the regular expression [pattern], which can be
  // expensive on some platforms, check if even the first character matches
  // this syntax.
  if (_startCharacter != null &&
      parser.source.codeUnitAt(startMatchPos) != _startCharacter) {
    return false;
  }

  final startMatch = pattern.matchAsPrefix(parser.source, startMatchPos);
  if (startMatch == null) return false;

  // Write any existing plain text up to this point.
  parser.writeText();

  if (onMatch(parser, startMatch)) parser.consume(startMatch.match.length);
  return true;
}