parse method

  1. @override
ParseResult parse(
  1. String source
)
override

Attempts to parse the source string matching this element.

Returns a ParseResult containing the parsed lexeme and remaining string on success. On failure, returns a failed ParseResult.

Implementation

@override
ParseResult parse(String source) {
  if (source.isEmpty) {
    return ParseResult(false, source, element: toString());
  }

  final rune = source.runes.first;
  if (rune == value) {
    // Consume exactly one rune. String.fromCharCodes reconstructs the
    // string correctly even if the rune required a surrogate pair.
    final lexeme = String.fromCharCode(rune);
    return ParseResult(
      true,
      source.substring(lexeme.length),
      lexeme: lexeme,
      element: toString(),
    );
  }
  return ParseResult(false, source, element: toString());
}