Small refactor for lexer

This commit is contained in:
Daniel M 2022-02-03 17:25:55 +01:00
parent c2b9ee71b8
commit f6152670aa

View File

@ -107,10 +107,29 @@ impl<'a> Lexer<'a> {
'}' => tokens.push(Token::RBraces), '}' => tokens.push(Token::RBraces),
'!' => tokens.push(Token::LNot), '!' => tokens.push(Token::LNot),
// Lex numbers // Special tokens with variable length
ch @ '0'..='9' => {
// Lex multiple characters together as numbers
ch @ '0'..='9' => tokens.push(self.lex_number(ch)?),
// Lex multiple characters together as a string
'"' => tokens.push(self.lex_str()?),
// Lex multiple characters together as identifier
ch @ ('a'..='z' | 'A'..='Z' | '_') => tokens.push(self.lex_identifier(ch)?),
ch => Err(LexErr::UnexpectedChar(ch))?,
}
}
Ok(tokens)
}
/// Lex multiple characters as a number until encountering a non numeric digit. This includes
/// the first character
fn lex_number(&mut self, first_char: char) -> Result<Token, LexErr> {
// String representation of the integer value // String representation of the integer value
let mut sval = String::from(ch); let mut sval = String::from(first_char);
// Do as long as a next char exists and it is a numeric char // Do as long as a next char exists and it is a numeric char
loop { loop {
@ -130,11 +149,11 @@ impl<'a> Lexer<'a> {
// Try to convert the string representation of the value to i64 // Try to convert the string representation of the value to i64
let i64val = sval.parse().map_err(|_| LexErr::NumericParse(sval))?; let i64val = sval.parse().map_err(|_| LexErr::NumericParse(sval))?;
tokens.push(Token::I64(i64val)); Ok(Token::I64(i64val))
} }
// Lex a string /// Lex characters as a string until encountering an unescaped closing doublequoute char '"'
'"' => { fn lex_str(&mut self) -> Result<Token, LexErr> {
// Opening " was consumed in match // Opening " was consumed in match
let mut text = String::new(); let mut text = String::new();
@ -164,12 +183,12 @@ impl<'a> Lexer<'a> {
// Consume closing " // Consume closing "
self.next(); self.next();
tokens.push(Token::String(text)) Ok(Token::String(text))
} }
// Lex characters as identifier /// Lex characters from the text as an identifier. This includes the first character passed in
ch @ ('a'..='z' | 'A'..='Z' | '_') => { fn lex_identifier(&mut self, first_char: char) -> Result<Token, LexErr> {
let mut ident = String::from(ch); let mut ident = String::from(first_char);
// Do as long as a next char exists and it is a valid char for an identifier // Do as long as a next char exists and it is a valid char for an identifier
loop { loop {
@ -194,14 +213,7 @@ impl<'a> Lexer<'a> {
_ => Token::Ident(ident), _ => Token::Ident(ident),
}; };
tokens.push(token); Ok(token)
}
ch => Err(LexErr::UnexpectedChar(ch))?,
}
}
Ok(tokens)
} }
/// Advance to next character and return the removed char /// Advance to next character and return the removed char