less lexer recursion
This commit is contained in:
parent
586ddd1aec
commit
706f4b3763
130
src/lexer.rs
130
src/lexer.rs
|
@ -227,89 +227,91 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lex_comment(&mut self) -> Option<Result<Token>> {
|
|
||||||
while self.peek() != Some('\n') {
|
|
||||||
self.eat();
|
|
||||||
}
|
|
||||||
self.lex()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lex(&mut self) -> Option<Result<Token>> {
|
fn lex(&mut self) -> Option<Result<Token>> {
|
||||||
match self.lex_whitespace()? {
|
loop {
|
||||||
// { and } start/end of code block
|
break match self.lex_whitespace()? {
|
||||||
'{' => self.eat_to(Token::CurlyOpen),
|
// { and } start/end of code block
|
||||||
'}' => self.eat_to(Token::CurlyClose),
|
'{' => self.eat_to(Token::CurlyOpen),
|
||||||
|
'}' => self.eat_to(Token::CurlyClose),
|
||||||
|
|
||||||
// ( and ) start/end of parens (idk)
|
// ( and ) start/end of parens (idk)
|
||||||
'(' => self.eat_to(Token::ParenOpen),
|
'(' => self.eat_to(Token::ParenOpen),
|
||||||
')' => self.eat_to(Token::ParenClose),
|
')' => self.eat_to(Token::ParenClose),
|
||||||
|
|
||||||
// + add
|
// + add
|
||||||
'+' => self.eat_to(Token::Plus),
|
'+' => self.eat_to(Token::Plus),
|
||||||
|
|
||||||
// - subtract
|
// - subtract
|
||||||
'-' => self.eat_to(Token::Minus),
|
'-' => self.eat_to(Token::Minus),
|
||||||
|
|
||||||
// * multiply
|
// * multiply
|
||||||
'*' => self.eat_to(Token::Star),
|
'*' => self.eat_to(Token::Star),
|
||||||
|
|
||||||
// / divide
|
// / divide
|
||||||
'/' => self.eat_to(Token::Slash),
|
'/' => self.eat_to(Token::Slash),
|
||||||
|
|
||||||
// ^ pow
|
// ^ pow
|
||||||
'^' => self.eat_to(Token::Caret),
|
'^' => self.eat_to(Token::Caret),
|
||||||
|
|
||||||
// , comma
|
// , comma
|
||||||
',' => self.eat_to(Token::Comma),
|
',' => self.eat_to(Token::Comma),
|
||||||
|
|
||||||
// = equals
|
// = equals
|
||||||
// or == equal to
|
// or == equal to
|
||||||
'=' => match self.eat_peek() {
|
'=' => match self.eat_peek() {
|
||||||
Some('=') => self.eat_to(Token::EqualTo),
|
Some('=') => self.eat_to(Token::EqualTo),
|
||||||
_ => Some(Ok(Token::Equals)),
|
_ => Some(Ok(Token::Equals)),
|
||||||
},
|
},
|
||||||
|
|
||||||
// ! not
|
// ! not
|
||||||
// or != not equal to
|
// or != not equal to
|
||||||
'!' => match self.eat_peek() {
|
'!' => match self.eat_peek() {
|
||||||
Some('=') => self.eat_to(Token::NotEqualTo),
|
Some('=') => self.eat_to(Token::NotEqualTo),
|
||||||
_ => Some(Ok(Token::Not)),
|
_ => Some(Ok(Token::Not)),
|
||||||
},
|
},
|
||||||
|
|
||||||
// && and
|
// && and
|
||||||
'&' if matches!(self.eat_peek(), Some('&')) => self.eat_to(Token::And),
|
'&' if matches!(self.eat_peek(), Some('&')) => self.eat_to(Token::And),
|
||||||
|
|
||||||
// || or
|
// || or
|
||||||
'|' if matches!(self.eat_peek(), Some('|')) => self.eat_to(Token::Or),
|
'|' if matches!(self.eat_peek(), Some('|')) => self.eat_to(Token::Or),
|
||||||
|
|
||||||
// > greater than
|
// > greater than
|
||||||
// or >= greater than/equal to
|
// or >= greater than/equal to
|
||||||
'>' => match self.eat_peek() {
|
'>' => match self.eat_peek() {
|
||||||
Some('=') => self.eat_to(Token::GreaterThanOrEqualTo),
|
Some('=') => self.eat_to(Token::GreaterThanOrEqualTo),
|
||||||
_ => Some(Ok(Token::GreaterThan)),
|
_ => Some(Ok(Token::GreaterThan)),
|
||||||
},
|
},
|
||||||
|
|
||||||
// < less than
|
// < less than
|
||||||
// or <= less than/equal to
|
// or <= less than/equal to
|
||||||
'<' => match self.eat_peek() {
|
'<' => match self.eat_peek() {
|
||||||
Some('=') => self.eat_to(Token::LessThanOrEqualTo),
|
Some('=') => self.eat_to(Token::LessThanOrEqualTo),
|
||||||
_ => Some(Ok(Token::LessThan)),
|
_ => Some(Ok(Token::LessThan)),
|
||||||
},
|
},
|
||||||
|
|
||||||
// a-zA-Z_ start of word
|
// a-zA-Z_ start of word
|
||||||
'a'..='z' | 'A'..='Z' | '_' => Some(Ok(self.lex_word())),
|
'a'..='z' | 'A'..='Z' | '_' => Some(Ok(self.lex_word())),
|
||||||
|
|
||||||
// 0-9 integer
|
// 0-9 integer
|
||||||
'0'..='9' => Some(Ok(self.lex_integer())),
|
'0'..='9' => Some(Ok(self.lex_integer())),
|
||||||
|
|
||||||
// " strings
|
// " strings
|
||||||
'"' => Some(self.lex_string()),
|
'"' => Some(self.lex_string()),
|
||||||
|
|
||||||
// # comments
|
// # comments
|
||||||
'#' => self.lex_comment(),
|
'#' => {
|
||||||
|
// skip the rest of the line
|
||||||
|
// this leaves the newline btw
|
||||||
|
while !matches!(self.peek(), Some('\n') | None) {
|
||||||
|
self.eat();
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// unexpected character
|
// unexpected character
|
||||||
c => Some(Err(LexError::UnexpectedCharacter(c))),
|
c => Some(Err(LexError::UnexpectedCharacter(c))),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::parser::{Block, Expr};
|
use crate::parser::{Block, Expr};
|
||||||
|
|
||||||
enum Value {
|
pub enum Value {
|
||||||
String(String),
|
String(String),
|
||||||
Integer(i64),
|
Integer(i64),
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
|
|
Loading…
Reference in New Issue