tryParse static method

DelimiterRun? tryParse(
  1. InlineParser parser,
  2. int runStart,
  3. int runEnd, {
  4. required DelimiterSyntax syntax,
  5. required List<DelimiterTag> tags,
  6. required Text node,
  7. bool allowIntraWord = false,
})

Tries to parse a delimiter run from runStart (inclusive) to runEnd (exclusive).

Implementation

static DelimiterRun? tryParse(
  InlineParser parser,
  int runStart,
  int runEnd, {
  required DelimiterSyntax syntax,
  required List<DelimiterTag> tags,
  required Text node,
  bool allowIntraWord = false,
}) {
  bool precededByWhitespace;
  bool followedByWhitespace;
  bool precededByPunctuation;
  bool followedByPunctuation;

  if (runStart == 0) {
    precededByWhitespace = true;
    precededByPunctuation = false;
  } else {
    final preceding = parser.source.substring(runStart - 1, runStart);
    precededByWhitespace = unicodeWhitespace.contains(preceding);
    precededByPunctuation = !precededByWhitespace &&
        unicodePunctuationPattern.hasMatch(preceding);
  }

  if (runEnd == parser.source.length) {
    followedByWhitespace = true;
    followedByPunctuation = false;
  } else {
    final following = parser.source.substring(runEnd, runEnd + 1);
    followedByWhitespace = unicodeWhitespace.contains(following);
    followedByPunctuation = !followedByWhitespace &&
        unicodePunctuationPattern.hasMatch(following);
  }

  // If it is a left-flanking delimiter run, see
  // http://spec.commonmark.org/0.30/#left-flanking-delimiter-run.
  final isLeftFlanking = !followedByWhitespace &&
      (!followedByPunctuation ||
          precededByWhitespace ||
          precededByPunctuation);

  // If it is a right-flanking delimiter run, see
  // http://spec.commonmark.org/0.30/#right-flanking-delimiter-run.
  final isRightFlanking = !precededByWhitespace &&
      (!precededByPunctuation ||
          followedByWhitespace ||
          followedByPunctuation);

  // Make sure the shorter delimiter takes precedence.
  tags.sort((a, b) => a.indicatorLength.compareTo(b.indicatorLength));

  return DelimiterRun._(
    node: node,
    char: parser.charAt(runStart),
    syntax: syntax,
    tags: tags,
    isLeftFlanking: isLeftFlanking,
    isRightFlanking: isRightFlanking,
    isPrecededByPunctuation: precededByPunctuation,
    isFollowedByPunctuation: followedByPunctuation,
    allowIntraWord: allowIntraWord,
  );
}