Struct BufferedInput

Source
pub struct BufferedInput<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> BufferedInput<'a>

Source

pub fn take_buffer(&mut self) -> String

Copy out the buffer and clear it

Source

pub fn peek(&mut self) -> Option<char>

Peek at the next character

Source

pub fn skip(&mut self) -> Option<char>

Get the next character but don’t push it to the buffer

Source

pub fn skip_if<P: Fn(&char) -> bool>(&mut self, predicate: P) -> Option<char>

Skip if the given predicate is true

Source

pub fn skip_while<P: Fn(&char) -> bool>(&mut self, predicate: P)

Skip while the given predicate is true

Source

pub fn accept(&mut self) -> Option<char>

Retrieve the next character and increment the input position

Source

pub fn accept_if<P: Fn(&char) -> bool>(&mut self, predicate: P) -> Option<char>

Call self.next() if the peeked character is identical to b

Examples found in repository?
examples/calc.rs (line 12)
10fn lex_int(input: &mut BufferedInput) -> TokenKind {
11    input.accept_while(char::is_ascii_digit);
12    if let Some(_) = input.accept_if(|c| *c == '.') {
13        return lex_float(input);
14    }
15    TokenKind::Int
16}
Source

pub fn accept_while<P: Fn(&char) -> bool>(&mut self, predicate: P)

Call self.next() while the peeked character fulfils predicate

Examples found in repository?
examples/calc.rs (line 11)
10fn lex_int(input: &mut BufferedInput) -> TokenKind {
11    input.accept_while(char::is_ascii_digit);
12    if let Some(_) = input.accept_if(|c| *c == '.') {
13        return lex_float(input);
14    }
15    TokenKind::Int
16}
17
18fn lex_float(input: &mut BufferedInput) -> TokenKind {
19    input.accept_while(char::is_ascii_digit);
20    TokenKind::Float
21}
22
23fn lex_name(input: &mut BufferedInput) -> TokenKind {
24    input.accept_while(|c| *c == '_' || c.is_ascii_alphabetic());
25    TokenKind::Name
26}
Source

pub fn accept_or<P: Fn(&char) -> bool, T>( &mut self, predicate: P, ok: T, default: T, ) -> T

Accept the given byte and return ok, or else return default

This is useful for matching multi-character tokens:

match c {
    '=' => input.accept_or(|&c| c == '=', TokenKind::DoubleEquals, TokenKind::Equals),
    _ => {},
}
Source

pub fn skip_whitespace(&mut self)

Skip whitespace, preserving the original buffer before any whitespace was encountered

Auto Trait Implementations§

§

impl<'a> Freeze for BufferedInput<'a>

§

impl<'a> RefUnwindSafe for BufferedInput<'a>

§

impl<'a> Send for BufferedInput<'a>

§

impl<'a> Sync for BufferedInput<'a>

§

impl<'a> Unpin for BufferedInput<'a>

§

impl<'a> UnwindSafe for BufferedInput<'a>

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> 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, 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.