[−][src]Struct regex_lexer::LexerBuilder
Builder struct for Lexer.
Implementations
impl<'r, 't, T: 't> LexerBuilder<'r, 't, T>
[src]
pub fn new() -> Self
[src]
Create a new LexerBuilder.
pub fn token<F>(self, re: &'r str, f: F) -> Self where
F: Fn(&'t str) -> Option<T> + 'static,
[src]
F: Fn(&'t str) -> Option<T> + 'static,
Add a new token that matches the regular expression re
.
This uses the same syntax as the regex crate.
If re
gives the longest match, then f
is called on the matched string.
- If
f
returnsSome(tok)
, emit the tokentok
. - Otherwise, skip this token and emit nothing.
#[derive(Debug, PartialEq, Eq)] enum Token { Num(usize), // ... } let lexer = regex_lexer::LexerBuilder::new() .token(r"[0-9]*", |num| Some(Token::Num(num.parse().unwrap()))) .token(r"\s+", |_| None) // skip whitespace // ... .build()?; assert_eq!( lexer.tokens("1 2 3").collect::<Vec<_>>(), vec![Token::Num(1), Token::Num(2), Token::Num(3)], );
If multiple regexes all have the same longest match, then whichever is defined last is given priority.
#[derive(Debug, PartialEq, Eq)] enum Token<'t> { Ident(&'t str), Then, // ... } let lexer = regex_lexer::LexerBuilder::new() .token(r"[a-zA-Z_][a-zA-Z0-9_]*", |id| Some(Token::Ident(id))) .token(r"then", |_| Some(Token::Then)) // ... .build()?; assert_eq!(lexer.tokens("then").next(), Some(Token::Then)); assert_eq!(lexer.tokens("then_perish").next(), Some(Token::Ident("then_perish")));
pub fn build(self) -> Result<Lexer<'t, T>, Error>
[src]
Construct a Lexer which matches these tokens.
Errors
If a regex cannot be compiled, a regex::Error is returned.
Trait Implementations
impl<'r, 't, T: 't> Debug for LexerBuilder<'r, 't, T>
[src]
impl<'r, 't, T: 't> Default for LexerBuilder<'r, 't, T>
[src]
Auto Trait Implementations
impl<'r, 't, T> !RefUnwindSafe for LexerBuilder<'r, 't, T>
impl<'r, 't, T> !Send for LexerBuilder<'r, 't, T>
impl<'r, 't, T> !Sync for LexerBuilder<'r, 't, T>
impl<'r, 't, T> Unpin for LexerBuilder<'r, 't, T>
impl<'r, 't, T> !UnwindSafe for LexerBuilder<'r, 't, T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,