Struct LexerBuilder

Source
pub struct LexerBuilder<'r, 't, T: 't> { /* private fields */ }
Expand description

Builder struct for Lexer.

Implementations§

Source§

impl<'r, 't, T: 't> LexerBuilder<'r, 't, T>

Source

pub fn new() -> Self

Create a new LexerBuilder.

Source

pub fn token<F>(self, re: &'r str, f: F) -> Self
where F: Fn(Location, &'t str, Location) -> 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 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_lalrpop::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![Ok(Token::Num(1)), Ok(Token::Num(2)), Ok(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_lalrpop::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(Ok(Token::Then)));
assert_eq!(lexer.tokens("then_perish").next(), Some(Ok(Token::Ident("then_perish"))));
Source

pub fn build(self) -> Result<Lexer<'t, T>, Error>

Construct a Lexer which matches these tokens.

§Errors

If a regex cannot be compiled, a regex::Error is returned.

Trait Implementations§

Source§

impl<'r, 't, T: 't> Debug for LexerBuilder<'r, 't, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Shows the matched regexes

Source§

impl<'r, 't, T: 't> Default for LexerBuilder<'r, 't, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'r, 't, T> Freeze for LexerBuilder<'r, 't, T>

§

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§

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.