pub struct ParserState<'s> { /* private fields */ }
Expand description
A utility class for building simple recursive-descent parsers.
Basically, a wrapper over &str with nice methods that help with parsing.
Implementations§
Source§impl<'s> ParserState<'s>
impl<'s> ParserState<'s>
Sourcepub fn parse_while(
self,
parser: impl FnMut(Self) -> ParseResult<(bool, Self)>,
) -> ParseResult<Self>
pub fn parse_while( self, parser: impl FnMut(Self) -> ParseResult<(bool, Self)>, ) -> ParseResult<Self>
Applies given parsing function until it returns false and returns the final parser state.
Sourcepub fn accept(self, part: &'static str) -> ParseResult<Self>
pub fn accept(self, part: &'static str) -> ParseResult<Self>
If the input string contains given string at the beginning, returns a new parser state with given string skipped. Otherwise, returns an error.
Sourcepub fn skip_white(self) -> Self
pub fn skip_white(self) -> Self
Returns new parser state with whitespace skipped from the beginning.
Sourcepub fn parse_u16(self) -> ParseResult<(u16, Self)>
pub fn parse_u16(self) -> ParseResult<(u16, Self)>
Parses a sequence of digits as an integer. Consumes characters until it finds a character that is not a digit.
An error is returned if:
- The first character is not a digit
- The integer is larger than u16
Sourcepub fn take_while(self, pred: impl FnMut(char) -> bool) -> (&'s str, Self)
pub fn take_while(self, pred: impl FnMut(char) -> bool) -> (&'s str, Self)
Skips characters from the beginning while they satisfy given predicate and returns new parser state which
Sourcepub fn get_remaining(self) -> usize
pub fn get_remaining(self) -> usize
Returns the number of remaining bytes to parse.
Sourcepub fn error(self, cause: ParseErrorCause) -> ParseError
pub fn error(self, cause: ParseErrorCause) -> ParseError
Returns an error with given cause, associated with given position.
Sourcepub fn calculate_position(self, original: &str) -> Option<usize>
pub fn calculate_position(self, original: &str) -> Option<usize>
Given the original string, returns the 1-based position of the error in characters. If an incorrect string was given, the function may return None.
Trait Implementations§
Source§impl<'s> Clone for ParserState<'s>
impl<'s> Clone for ParserState<'s>
Source§fn clone(&self) -> ParserState<'s>
fn clone(&self) -> ParserState<'s>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreimpl<'s> Copy for ParserState<'s>
Auto Trait Implementations§
impl<'s> Freeze for ParserState<'s>
impl<'s> RefUnwindSafe for ParserState<'s>
impl<'s> Send for ParserState<'s>
impl<'s> Sync for ParserState<'s>
impl<'s> Unpin for ParserState<'s>
impl<'s> UnwindSafe for ParserState<'s>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more