tryParse static method

DelimiterRun? tryParse(
  1. SimpleParser parser,
  2. int runStart,
  3. int runEnd, {
  4. required TagSyntax syntax,
  5. required Text node,
  6. bool allowIntraWord = false,
})

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

Implementation

static DelimiterRun? tryParse(SimpleParser parser, int runStart, int runEnd,
    {required TagSyntax syntax,
    required Text node,
    bool allowIntraWord = false}) {
  bool leftFlanking,
      rightFlanking,
      precededByPunctuation,
      followedByPunctuation;
  String preceding, following;
  if (runStart == 0) {
    rightFlanking = false;
    preceding = '\n';
  } else {
    preceding = parser.source!.substring(runStart - 1, runStart);
  }
  precededByPunctuation = punctuation.hasMatch(preceding);

  if (runEnd == parser.source!.length) {
    leftFlanking = false;
    following = '\n';
  } else {
    following = parser.source!.substring(runEnd, runEnd + 1);
  }
  followedByPunctuation = punctuation.hasMatch(following);

  // http://spec.commonmark.org/0.28/#left-flanking-delimiter-run
  if (whitespace.contains(following)) {
    leftFlanking = false;
  } else {
    leftFlanking = !followedByPunctuation ||
        whitespace.contains(preceding) ||
        precededByPunctuation ||
        allowIntraWord;
  }

  // http://spec.commonmark.org/0.28/#right-flanking-delimiter-run
  if (whitespace.contains(preceding)) {
    rightFlanking = false;
  } else {
    rightFlanking = !precededByPunctuation ||
        whitespace.contains(following) ||
        followedByPunctuation ||
        allowIntraWord;
  }

  if (!leftFlanking && !rightFlanking) {
    // Could not parse a delimiter run.
    return null;
  }

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