OverComplicatedCalculator/src/lexer/mod.rs

43 lines
958 B
Rust

mod errors;
mod tokens;
use std::collections::VecDeque;
use std::fs::File;
use std::path::Path;
use std::str::Chars;
use tokens::Token;
use crate::lexer::errors::LexerErrors;
use crate::lexer::errors::LexerErrors::EmptyTextSequenceError;
pub type Result<T> = std::result::Result<T, errors::LexerErrors>;
pub struct Lexer<'a> {
input: String,
chars: Chars<'a>,
}
impl Lexer<'_> {
/// Create a new Lexer for the given String
/// Example:
/// ```
/// use cb_calculator::lexer;
/// let text = "some text";
/// lexer::Lexer::new(String::from(text));
/// ```
#[inline]
pub fn new(input: String) -> Lexer {
Lexer { input, chars: input.chars() }
}
// Get the next token
pub fn next(&mut self) -> Result<Option<Token>> {
let mut buffer: Vec<char> = Vec::new();
loop {
if let Some(c) = self.chars.next() {
buffer.push(c);
}
}
}
}