Struct ParserState

Source
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>

Source

pub fn new(s: &'s str) -> Self

Creates a new parser from given input string.

Source

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.

Source

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.

Source

pub fn skip_white(self) -> Self

Returns new parser state with whitespace skipped from the beginning.

Source

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
Source

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

Source

pub fn get_remaining(self) -> usize

Returns the number of remaining bytes to parse.

Source

pub fn is_at_eof(self) -> bool

Returns true if the input string was parsed completely.

Source

pub fn error(self, cause: ParseErrorCause) -> ParseError

Returns an error with given cause, associated with given position.

Source

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>

Source§

fn clone(&self) -> ParserState<'s>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,