完整lexer介绍
use crate::token::token;
const CHAR0: char = 0 as char;
#[derive(Debug,Clone)]
pub struct Lexer {
input: String,
position: usize,
read_position: usize,
ch: char,
}
impl Lexer {
pub fn new<S: Into<String>>(input: S) -> Lexer {
let mut l = Lexer {
input: input.into(),
position: 0,
read_position: 0,
ch: CHAR0,
};
l.read_char();
l
}
fn read_char(&mut self) {
self.ch = self.input.chars().nth(self.read_position).unwrap_or(CHAR0);
self.position = self.read_position;
self.read_position += 1;
}
pub fn next_token(&mut self) -> token::Token {
let mut tok: token::Token;
self.skip_whitespace();
match self.ch {
'=' => {
if self.peek_char() == '=' {
let ch = self.ch;
self.read_char();
tok = token::Token::new_with_string(token::EQ, {
let mut x = ch.to_string()