[−][src]Struct logos::Lexer
Lexer
is the main struct of the crate that allows you to read through a
Source
and produce tokens for enums implementing the Logos
trait.
Fields
extras: Token::Extras
Extras associated with the Token
.
Implementations
impl<'source, Token: Logos<'source>> Lexer<'source, Token>
[src][−]
pub fn new(source: &'source Token::Source) -> Self
[src][−]
Create a new Lexer
.
Due to type inference, it might be more ergonomic to construct
it by calling Token::lexer
on any Token
with derived Logos
.
pub fn source(&self) -> &'source Token::Source
[src][−]
Source from which this Lexer is reading tokens.
pub fn spanned(self) -> SpannedIter<'source, Token>ⓘNotable traits for SpannedIter<'source, Token>
impl<'source, Token> Iterator for SpannedIter<'source, Token> where
Token: Logos<'source>, type Item = (Token, Span);
[src][−]
Notable traits for SpannedIter<'source, Token>
impl<'source, Token> Iterator for SpannedIter<'source, Token> where
Token: Logos<'source>, type Item = (Token, Span);
Wrap the Lexer
in an Iterator
that produces tuples of (Token,
Span
)
.
Example
use logos::Logos; #[derive(Logos, Debug, PartialEq)] enum Example { #[regex(r"[ \n\t\f]+", logos::skip)] #[error] Error, #[regex("-?[0-9]+", |lex| lex.slice().parse())] Integer(i64), #[regex("-?[0-9]+\\.[0-9]+", |lex| lex.slice().parse())] Float(f64), } let tokens: Vec<_> = Example::lexer("42 3.14 -5 f").spanned().collect(); assert_eq!( tokens, &[ (Example::Integer(42), 0..2), (Example::Float(3.14), 3..7), (Example::Integer(-5), 8..10), (Example::Error, 11..12), // 'f' is not a recognized token ], );
pub fn span(&self) -> Span
[src][−]
Get the range for the current token in Source
.
pub fn slice(&self) -> &'source <Token::Source as Source>::Slice
[src][−]
Get a string slice of the current token.
pub fn remainder(&self) -> &'source <Token::Source as Source>::Slice
[src][−]
Get a slice of remaining source, starting at the end of current token.
pub fn morph<Token2>(self) -> Lexer<'source, Token2>ⓘ where
Token2: Logos<'source, Source = Token::Source>,
Token::Extras: Into<Token2::Extras>,
[src][−]
Token2: Logos<'source, Source = Token::Source>,
Token::Extras: Into<Token2::Extras>,
Turn this lexer into a lexer for a new token type.
The new lexer continues to point at the same span as the current lexer,
and the current token becomes the error token of the new token type.
If you want to start reading from the new lexer immediately,
consider using Lexer::advance_as
instead.
pub fn bump(&mut self, n: usize)
[src][−]
Bumps the end of currently lexed token by n
bytes.
Panics
Panics if adding n
to current offset would place the Lexer
beyond the last byte,
or in the middle of an UTF-8 code point (does not apply when lexing raw &[u8]
).
Trait Implementations
impl<'source, Token> Clone for Lexer<'source, Token> where
Token: Logos<'source> + Clone,
Token::Extras: Clone,
[src][+]
Token: Logos<'source> + Clone,
Token::Extras: Clone,
impl<'source, Token> Iterator for Lexer<'source, Token> where
Token: Logos<'source>,
[src][+]
Token: Logos<'source>,
Auto Trait Implementations
impl<'source, Token> RefUnwindSafe for Lexer<'source, Token> where
Token: RefUnwindSafe,
<Token as Logos<'source>>::Extras: RefUnwindSafe,
<Token as Logos<'source>>::Source: RefUnwindSafe,
Token: RefUnwindSafe,
<Token as Logos<'source>>::Extras: RefUnwindSafe,
<Token as Logos<'source>>::Source: RefUnwindSafe,
impl<'source, Token> Send for Lexer<'source, Token> where
Token: Send,
<Token as Logos<'source>>::Extras: Send,
<Token as Logos<'source>>::Source: Sync,
Token: Send,
<Token as Logos<'source>>::Extras: Send,
<Token as Logos<'source>>::Source: Sync,
impl<'source, Token> Sync for Lexer<'source, Token> where
Token: Sync,
<Token as Logos<'source>>::Extras: Sync,
<Token as Logos<'source>>::Source: Sync,
Token: Sync,
<Token as Logos<'source>>::Extras: Sync,
<Token as Logos<'source>>::Source: Sync,
impl<'source, Token> Unpin for Lexer<'source, Token> where
Token: Unpin,
<Token as Logos<'source>>::Extras: Unpin,
Token: Unpin,
<Token as Logos<'source>>::Extras: Unpin,
impl<'source, Token> UnwindSafe for Lexer<'source, Token> where
Token: UnwindSafe,
<Token as Logos<'source>>::Extras: UnwindSafe,
<Token as Logos<'source>>::Source: RefUnwindSafe,
Token: UnwindSafe,
<Token as Logos<'source>>::Extras: UnwindSafe,
<Token as Logos<'source>>::Source: RefUnwindSafe,
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,
impl<T> From<T> for T
[src][+]
impl<T, U> Into<U> for T where
U: From<T>,
[src][+]
U: From<T>,
impl<I> IntoIterator for I where
I: Iterator,
[src][+]
I: Iterator,
impl<T> ToOwned for T where
T: Clone,
[src][+]
T: Clone,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src][+]
U: Into<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src][+]
U: TryFrom<T>,