Skip to content

Instantly share code, notes, and snippets.

@abhayarawal
Created December 3, 2019 04:41
Show Gist options
  • Save abhayarawal/2407627b6489b7dd0a1cda91c85fdd35 to your computer and use it in GitHub Desktop.
Save abhayarawal/2407627b6489b7dd0a1cda91c85fdd35 to your computer and use it in GitHub Desktop.
Lexer test
use std::iter::Peekable;
use std::str::Chars;
#[derive(Debug, PartialEq)]
pub enum Token {
Plus,
Minus,
Equal,
Number(i32),
Illegal,
EOF
}
pub struct Lexer<'a> {
input: Peekable<Chars<'a>>
}
impl<'a> Lexer<'a> {
pub fn new(input: &'a str) -> Self {
Lexer { input: input.chars().peekable() }
}
pub fn read_number(&mut self, c: char) -> i32 {
let mut numero = c.to_string();
while let Some(&x) = self.input.peek() {
if !x.is_numeric() {
break;
}
numero.push(self.input.next().unwrap());
}
numero.parse::<i32>().unwrap()
}
pub fn skip_whitespace(&mut self) {
while let Some(&c) = self.input.peek() {
if !c.is_whitespace() { break; }
self.input.next();
}
}
pub fn next_token(&mut self) -> Token {
self.skip_whitespace();
match self.input.next() {
Some('+') => Token::Plus,
Some('-') => Token::Minus,
Some(c @ _) => {
if c.is_numeric() {
let n = self.read_number(c);
Token::Number(n)
} else {
Token::Illegal
}
},
None => Token::EOF
}
}
}
fn main() {
let mut l = Lexer::new(" 12+ 64 - 90+983");
let mut tokens: Vec<Token> = vec![];
loop {
tokens.push(l.next_token());
if tokens.last().unwrap() == &Token::EOF {
break;
}
}
println!("{:?}", tokens);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_x() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment