Skip to content

Instantly share code, notes, and snippets.

@andreastt
Created October 7, 2019 13:42
Show Gist options
  • Save andreastt/7be3678f3e1b0f3a5eb030f599bdba31 to your computer and use it in GitHub Desktop.
Save andreastt/7be3678f3e1b0f3a5eb030f599bdba31 to your computer and use it in GitHub Desktop.
impl<'a> Iterator for Lexer<'a> {
type Item = Token;
fn next(&mut self) -> Option<Token> {
if let Some(ch) = self.chars.next() {
match ch {
ch if ch.is_digit(Self::RADIX) => Some(Token::Num(self.read_number(ch))),
ch if ch.is_alphabetic() => {
if let Some(peek_ch) = self.chars.peek() {
if peek_ch.is_alphabetic() {
return Some(Token::String(self.read_string(ch)));
}
}
Some(Token::Char(ch))
}
'\n' | '\0' => Some(Token::Eof),
ch if ch.is_whitespace() => Some(Token::Sep),
'.' => Some(Token::Sep),
ch => Some(Token::Unexpected(ch)),
}
} else {
None
}
}
}
impl<'a> Iterator for Lexer<'a> {
type Item = Token;
fn next(&mut self) -> Option<Token> {
match (self.chars.next(), self.chars.peek()) {
(Some(ch), _) if ch.is_digit(Self::RADIX) => {
let n = self.read_number(ch);
Some(Token::Num(n))
}
(Some('\n'), _) | (Some('\0'), _) => None,
(Some('.'), _) => Some(Token::Sep),
(Some(ch), _) if ch.is_whitespace() => Some(Token::Sep),
(Some(cur_ch), Some(peek_ch)) if cur_ch.is_alphabetic() && peek_ch.is_alphabetic() => {
let s = self.read_string(cur_ch);
Some(Token::String(s))
}
(Some(ch), _) if ch.is_alphabetic() => Some(Token::Char(ch)),
(Some(ch), _) => Some(Token::Unexpected(ch)),
(None, _) => None,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment