pub struct BufferedInput<'a> { /* private fields */ }
Implementations§
Source§impl<'a> BufferedInput<'a>
impl<'a> BufferedInput<'a>
Sourcepub fn take_buffer(&mut self) -> String
pub fn take_buffer(&mut self) -> String
Copy out the buffer and clear it
Sourcepub fn skip_if<P: Fn(&char) -> bool>(&mut self, predicate: P) -> Option<char>
pub fn skip_if<P: Fn(&char) -> bool>(&mut self, predicate: P) -> Option<char>
Skip if the given predicate is true
Sourcepub fn skip_while<P: Fn(&char) -> bool>(&mut self, predicate: P)
pub fn skip_while<P: Fn(&char) -> bool>(&mut self, predicate: P)
Skip while the given predicate is true
Sourcepub fn accept(&mut self) -> Option<char>
pub fn accept(&mut self) -> Option<char>
Retrieve the next character and increment the input position
Sourcepub fn accept_if<P: Fn(&char) -> bool>(&mut self, predicate: P) -> Option<char>
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
Sourcepub fn accept_while<P: Fn(&char) -> bool>(&mut self, predicate: P)
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}
Sourcepub fn accept_or<P: Fn(&char) -> bool, T>(
&mut self,
predicate: P,
ok: T,
default: T,
) -> T
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),
_ => {},
}
Sourcepub fn skip_whitespace(&mut self)
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> 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
Mutably borrows from an owned value. Read more