| import '../../context/context.dart'; |
| import '../../context/result.dart'; |
| import '../../core/parser.dart'; |
| import 'predicate.dart'; |
| |
| /// Parser class for individual character classes. |
| class CharacterParser extends Parser<String> { |
| CharacterParser(this.predicate, this.message); |
| |
| /// Predicate indicating whether a character can be consumed. |
| final CharacterPredicate predicate; |
| |
| /// Error message to annotate parse failures with. |
| final String message; |
| |
| @override |
| Result<String> parseOn(Context context) { |
| final buffer = context.buffer; |
| final position = context.position; |
| if (position < buffer.length && |
| predicate.test(buffer.codeUnitAt(position))) { |
| return context.success(buffer[position], position + 1); |
| } |
| return context.failure(message); |
| } |
| |
| @override |
| int fastParseOn(String buffer, int position) => |
| position < buffer.length && predicate.test(buffer.codeUnitAt(position)) |
| ? position + 1 |
| : -1; |
| |
| @override |
| String toString() => '${super.toString()}[$message]'; |
| |
| @override |
| CharacterParser copy() => CharacterParser(predicate, message); |
| |
| @override |
| bool hasEqualProperties(CharacterParser other) => |
| super.hasEqualProperties(other) && |
| predicate.isEqualTo(other.predicate) && |
| message == other.message; |
| } |