pub struct LexerBuilder<'r, K> { /* private fields */ }
Expand description
Builder struct for Lexer.
Implementations§
Source§impl<'r, K> LexerBuilder<'r, K>
impl<'r, K> LexerBuilder<'r, K>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new LexerBuilder.
Sourcepub fn token(self, re: &'r str, kind: K) -> Self
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" },
],
);
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more