use crate::token::Tokens;
use crate::token::collect::token_list::TokenList;
pub trait Lexer {
fn add(&mut self, token: Tokens);
fn progress(&self) -> usize;
fn tokens(&self) -> &TokenList;
fn into_tokens(self) -> Vec<Tokens>;
fn is_at_indentable(&self) -> bool;
fn set_at_indentable(&mut self, indentable: bool);
fn get_indent(&self) -> u32;
fn set_indent(&mut self, new_indent: u32);
}
#[derive(Debug)]
pub struct CodeLexer {
tokens: TokenList,
indent: u32,
indentable: bool,
}
impl CodeLexer {
pub fn new(source_len: usize) -> Self {
CodeLexer {
tokens: TokenList::with_capacity(source_len / 3),
indent: 0,
indentable: true,
}
}
#[cfg(test)]
pub fn test() -> Self {
CodeLexer {
tokens: TokenList::with_capacity(8),
indent: 0,
indentable: true,
}
}
}
impl Lexer for CodeLexer {
fn add(&mut self, token: Tokens) {
self.tokens.add(token);
}
fn progress(&self) -> usize {
self.tokens.len()
}
fn tokens(&self) -> &TokenList {
&self.tokens
}
fn into_tokens(self) -> Vec<Tokens> {
self.tokens.into_vec()
}
fn is_at_indentable(&self) -> bool {
self.indentable
}
fn set_at_indentable(&mut self, indentable: bool) {
self.indentable = indentable;
}
fn get_indent(&self) -> u32 {
self.indent
}
fn set_indent(&mut self, new_indent: u32) {
self.indent = new_indent;
}
}