Struct LexerBuilder

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

Builder struct for Lexer.

Implementations§

Source§

impl<'r, K> LexerBuilder<'r, K>

Source

pub fn new() -> Self

Create a new LexerBuilder.

Source

pub fn token(self, re: &'r str, kind: K) -> Self

Add a new token that matches the regular expression re. This uses the same syntax as the regex crate.

If the regex matches, it will return a token of kind kind.

use regex_lexer::{LexerBuilder, Token};
 
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum Tok {
    Num,
    // ...
}

let lexer = LexerBuilder::new()
    .token(r"[0-9]*", Tok::Num)
    .ignore(r"\s+") // skip whitespace
    // ...
    .build()?;

assert_eq!(
    lexer.tokens("1 2 3").collect::<Vec<_>>(),
    vec![
        Token { kind: Tok::Num, span: 0..1, text: "1" },
        Token { kind: Tok::Num, span: 2..3, text: "2" },
        Token { kind: Tok::Num, span: 4..5, text: "3" },
    ],
);

If multiple regexes all match, then whichever is defined last will be given priority.

use regex_lexer::{LexerBuilder, Token};
 
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum Tok {
    Ident,
    Let,
    // ...
}

let lexer = LexerBuilder::new()
    .token(r"[a-zA-Z_][a-zA-Z0-9_]*", Tok::Ident)
    .token(r"let\b", Tok::Let)
    // ...
    .ignore(r"\s+")
    .build()?;

assert_eq!(
    lexer.tokens("let lettuce").collect::<Vec<_>>(), 
    vec![
        Token { kind: Tok::Let, span: 0..3, text: "let" },
        Token { kind: Tok::Ident, span: 4..11, text: "lettuce" },
    ],
);
Source

pub fn ignore(self, re: &'r str) -> Self

Add a new regex which if matched will ignore the matched text.

Source

pub fn build(self) -> Result<Lexer<K>, Error>

Construct a Lexer which matches these tokens.

§Errors

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

Trait Implementations§

Source§

impl<'r, K> Default for LexerBuilder<'r, K>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'r, K> Freeze for LexerBuilder<'r, K>

§

impl<'r, K> RefUnwindSafe for LexerBuilder<'r, K>
where K: RefUnwindSafe,

§

impl<'r, K> Send for LexerBuilder<'r, K>
where K: Send,

§

impl<'r, K> Sync for LexerBuilder<'r, K>
where K: Sync,

§

impl<'r, K> Unpin for LexerBuilder<'r, K>
where K: Unpin,

§

impl<'r, K> UnwindSafe for LexerBuilder<'r, K>
where K: UnwindSafe,

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.