lexer: Make the Lexer IntoIter'able

This commit is contained in:
John 2023-10-16 22:46:38 -05:00
parent 2ce89eec82
commit c7d1aa4d2c

View File

@ -991,7 +991,24 @@ pub mod lexer {
use crate::token::{Token, Type};
use lerox::Combinator;
#[allow(dead_code)]
pub struct IntoIter<'t> {
lexer: Lexer<'t>,
}
impl<'t> Iterator for IntoIter<'t> {
type Item = Token;
fn next(&mut self) -> Option<Self::Item> {
self.lexer.any()
}
}
impl<'t> IntoIterator for Lexer<'t> {
type Item = Token;
type IntoIter = IntoIter<'t>;
fn into_iter(self) -> Self::IntoIter {
IntoIter { lexer: self }
}
}
#[derive(Clone, Debug)]
pub struct Lexer<'t> {
text: &'t str,
cursor: usize,
@ -1003,6 +1020,12 @@ pub mod lexer {
pub fn new(text: &'t str) -> Self {
Self { text, cursor: 0, line: 1, col: 1 }
}
/// Consumes the entire [`Lexer`], producing a [`Vec<Token>`]
/// and returning the original string
pub fn consume(self) -> (Vec<Token>, &'t str) {
let text = self.text;
(self.into_iter().collect(), text)
}
/// Counts some length
#[inline]
fn count_len(&mut self, len: usize) -> &mut Self {