pub struct Lexer<'a> { /* private fields */ }Expand description
Reads tokens from a source, one per Lexer::next_token.
It never fails: a token it can’t read whole comes back as far as it
goes (a string with no closing quote ends at the end of the line), or
as Kind::Invalid, and a Diagnostic says what was wrong. Time is
linear in the input, and nothing is allocated per token; only a
diagnostic is.
use parsec_js::{Goal, Kind, Lexer, Punct, SourceType};
let src = "a / b; /b/g";
let mut lexer = Lexer::new(src, SourceType::Script);
let kinds: Vec<Kind> = [Goal::RegExp, Goal::Div, Goal::RegExp, Goal::Div, Goal::RegExp]
.into_iter()
.map(|goal| lexer.next_token(goal).kind)
.collect();
assert_eq!(kinds[1], Kind::Punct(Punct::Slash));
assert_eq!(kinds[4], Kind::RegExp);
assert!(lexer.diagnostics().is_empty());Implementations§
Source§impl<'a> Lexer<'a>
impl<'a> Lexer<'a>
pub fn new(src: &'a str, source: SourceType) -> Lexer<'a>
Sourcepub fn with_limits(
src: &'a str,
source: SourceType,
limits: &Limits,
) -> Lexer<'a>
pub fn with_limits( src: &'a str, source: SourceType, limits: &Limits, ) -> Lexer<'a>
A lexer that refuses input longer than limits.max_input, or than
spans can hold: it reports input-too-large and gives only
Kind::Eof.
pub fn source(&self) -> &'a str
Sourcepub fn diagnostics(&self) -> &[Diagnostic]
pub fn diagnostics(&self) -> &[Diagnostic]
What’s been reported so far, in the order it was found.
pub fn take_diagnostics(&mut self) -> Vec<Diagnostic>
Sourcepub fn next_token(&mut self, goal: Goal) -> Token
pub fn next_token(&mut self, goal: Goal) -> Token
The next token, read with goal. Comments and a hashbang come
back as tokens too (Kind::is_trivia); whitespace and line
terminators don’t, but Token::newline_before says where a line
terminator was.
Trait Implementations§
Auto Trait Implementations§
impl<'a> Forget for Lexer<'a>
impl<'a> Freeze for Lexer<'a>
impl<'a> Move for Lexer<'a>
impl<'a> RefUnwindSafe for Lexer<'a>
impl<'a> Send for Lexer<'a>
impl<'a> Sync for Lexer<'a>
impl<'a> Unpin for Lexer<'a>
impl<'a> UnsafeUnpin for Lexer<'a>
impl<'a> UnwindSafe for Lexer<'a>
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