[][src]Struct regex_lexer::LexerBuilder

pub struct LexerBuilder<'r, 't, T: 't> { /* fields omitted */ }

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]

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 returns Some(tok), emit the token tok.
  • 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]

fn fmt(&self, f: &mut Formatter) -> Result[src]

Shows the matched regexes

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.