pub struct Lexer<'a> { /* private fields */ }
Expand description
The tokenizer you write will be passed a Lexer as an argument, and will call methods on it to figure out what the next lexeme is and, if needed, get its text. The Lexer keeps track of the column range of the lexeme.
Implementations§
Source§impl<'a> Lexer<'a>
impl<'a> Lexer<'a>
Sourcepub fn at(&mut self, c: char) -> bool
pub fn at(&mut self, c: char) -> bool
If the next character is c (the argument, not the letter), adds c to the lexeme and returns true. Otherwise just returns false.
Sourcepub fn take_while(&mut self, f: impl Fn(char) -> bool) -> &mut Self
pub fn take_while(&mut self, f: impl Fn(char) -> bool) -> &mut Self
Keeps adding characters to the lexeme while f(char) is true. Returns self to allow chaining.
Sourcepub fn skip_while(&mut self, f: impl Fn(char) -> bool) -> &mut Self
pub fn skip_while(&mut self, f: impl Fn(char) -> bool) -> &mut Self
Skips over characters while f(char) is true; the lexeme will start at the char where f(char) is false. Returns self to allow chaining.
Sourcepub fn into<T: From<&'a str>>(&mut self) -> T
pub fn into<T: From<&'a str>>(&mut self) -> T
Returns the lexeme converted to the needed type (commonly a String). This is just a shortcut for .lexeme().into().
Sourcepub fn map<T>(&mut self, f: impl Fn(&'a str) -> T) -> T
pub fn map<T>(&mut self, f: impl Fn(&'a str) -> T) -> T
Returns the result of applying f() to the lexeme.
Sourcepub fn column_range(&self) -> Range<usize>
pub fn column_range(&self) -> Range<usize>
Gets the range of columns associated with the lexeme. You probably won’t need to use this, since the column range is included in the Iterator’s output, but it could be useful if the interpretation of lexemes depends upon the columns in which you find them.