Compare commits

..

3 Commits
main ... andres

Author SHA1 Message Date
Alpha Chen 3707f7be91
mu
2 years ago
Alpha Chen 3dc84e9f28
mu
2 years ago
Alpha Chen e8f5059c9b
mu
2 years ago

@ -4,8 +4,8 @@ pub struct Scanner<'a> {
line: usize,
}
fn is_alpha(c: &char) -> bool {
c.is_ascii_alphabetic() || *c == '_'
fn is_alpha(c: char) -> bool {
c.is_ascii_alphabetic() || c == '_'
}
impl<'a> Scanner<'a> {
@ -17,32 +17,44 @@ impl<'a> Scanner<'a> {
}
}
fn advance(&mut self) -> Option<char> {
fn advance(&mut self) -> char {
self.current += 1;
self.source.get(self.current - 1).map(|&x| x as char)
self.source[self.current - 1] as char
}
fn advance_if<F: Fn(&char) -> bool>(&mut self, f: F) -> bool {
if matches!(self.peek(), Some(c) if f(&c)) {
self.current += 1;
true
} else {
false
fn is_match(&mut self, expected: char) -> bool {
if self.peek() != Some(expected) {
return false;
}
self.current += 1;
true
}
fn skip_whitespace(&mut self) {
loop {
while self.advance_if(|&c| c == ' ' || c == '\r' || c == '\t') {}
if self.peek() == Some('/') && self.peek_next() == Some('/') {
while self.advance_if(|&c| c != '\n') {}
}
if self.advance_if(|&c| c == '\n') {
self.line += 1;
} else {
return;
match self.peek() {
Some(' ') | Some('\r') | Some('\t') => {
self.advance();
break;
}
Some('\n') => {
self.line += 1;
self.advance();
break;
}
Some('/') => {
if matches!(self.peek_next(), Some(c) if c == '/') {
while matches!(self.peek(), Some(c) if c != '\n') {
self.advance();
}
} else {
return;
}
}
_ => {
return;
}
}
}
}
@ -56,36 +68,47 @@ impl<'a> Scanner<'a> {
}
fn string(&mut self) -> Token<'a> {
loop {
match self.advance() {
Some('"') => break,
Some('\n') => {
self.line += 1;
}
Some(_) => {}
None => return Token::error(self, "Unterminated string."),
while matches!(self.peek(), Some(c) if c != '"') {
if self.peek() == Some('\n') {
self.line += 1;
}
self.advance();
}
if self.peek().is_none() {
// Maybe this should return an Err?
return Token::error(&self, "Unterminated string.");
}
Token::new(self, TokenKind::String)
self.advance();
// Token::new(&self, TokenKind::String)
self.make_token(TokenKind::String)
}
fn number(&mut self) -> Token<'a> {
while self.advance_if(char::is_ascii_digit) {}
while matches!(self.peek(), Some(c) if c.is_ascii_digit()) {
self.advance();
}
if self.peek() == Some('.') && matches!(self.peek_next(), Some(c) if c.is_ascii_digit()) {
self.advance();
while self.advance_if(char::is_ascii_digit) {}
while matches!(self.peek(), Some(c) if c.is_ascii_digit()) {
self.advance();
}
}
Token::new(self, TokenKind::Number)
// Token::new(&self, TokenKind::Number)
self.make_token(TokenKind::Number)
}
fn identifier(&mut self) -> Token<'a> {
while self.advance_if(|c| is_alpha(c) || c.is_ascii_digit()) {}
while matches!(self.peek(), Some(c) if is_alpha(c) || c.is_ascii_digit()) {
self.advance();
}
Token::new(self, self.identifier_type())
// Token::new(&self, self.identifier_type())
self.make_token(self.identifier_type())
}
fn identifier_type(&self) -> TokenKind {
@ -115,6 +138,14 @@ impl<'a> Scanner<'a> {
_ => TokenKind::Identifier,
}
}
fn make_token(&self, kind: TokenKind) -> Token<'a> {
Token {
kind: TokenKind::And,
value: std::str::from_utf8(&self.source[..self.current]).unwrap(),
line: self.line,
}
}
}
impl<'a> Iterator for Scanner<'a> {
@ -122,60 +153,54 @@ impl<'a> Iterator for Scanner<'a> {
fn next(&mut self) -> Option<Self::Item> {
self.skip_whitespace();
self.advance()
.map(|c| match c {
c if is_alpha(&c) => self.identifier(),
c if c.is_ascii_digit() => self.number(),
'(' => Token::new(self, TokenKind::LeftParen),
')' => Token::new(self, TokenKind::RightParen),
'{' => Token::new(self, TokenKind::LeftBrace),
'}' => Token::new(self, TokenKind::RightBrace),
';' => Token::new(self, TokenKind::Semicolon),
',' => Token::new(self, TokenKind::Comma),
'.' => Token::new(self, TokenKind::Dot),
'-' => Token::new(self, TokenKind::Minus),
'+' => Token::new(self, TokenKind::Plus),
'/' => Token::new(self, TokenKind::Slash),
'*' => Token::new(self, TokenKind::Star),
'!' => {
if self.advance_if(|&c| c == '=') {
Token::new(self, TokenKind::BangEqual)
} else {
Token::new(self, TokenKind::Bang)
}
}
'=' => {
if self.advance_if(|&c| c == '=') {
Token::new(self, TokenKind::EqualEqual)
} else {
Token::new(self, TokenKind::Equal)
}
}
'<' => {
if self.advance_if(|&c| c == '=') {
Token::new(self, TokenKind::LessEqual)
} else {
Token::new(self, TokenKind::Less)
}
}
'>' => {
if self.advance_if(|&c| c == '=') {
Token::new(self, TokenKind::GreaterEqual)
} else {
Token::new(self, TokenKind::Greater)
}
}
'"' => self.string(),
_ => Token::error(self, "Unexpected character."),
})
.or({
Some(Token {
kind: TokenKind::Eof,
value: "",
line: self.line,
})
})
self.peek()?;
Some(match self.advance() {
c if is_alpha(c) => self.identifier(),
c if c.is_ascii_digit() => self.number(),
'(' => Token::new(self, TokenKind::LeftParen),
// '(' => self.make_token(TokenKind::LeftParen),
// ')' => self.make_token(TokenKind::RightParen),
// '{' => self.make_token(TokenKind::LeftBrace),
// '}' => self.make_token(TokenKind::RightBrace),
// ';' => self.make_token(TokenKind::Semicolon),
// ',' => self.make_token(TokenKind::Comma),
// '.' => self.make_token(TokenKind::Dot),
// '-' => self.make_token(TokenKind::Minus),
// '+' => self.make_token(TokenKind::Plus),
// '/' => self.make_token(TokenKind::Slash),
// '*' => self.make_token(TokenKind::Star),
// '!' => {
// if self.is_match('=') {
// self.make_token(TokenKind::BangEqual)
// } else {
// self.make_token(TokenKind::Bang)
// }
// }
// '=' => {
// if self.is_match('=') {
// self.make_token(TokenKind::EqualEqual)
// } else {
// self.make_token(TokenKind::Equal)
// }
// }
// '<' => {
// if self.is_match('=') {
// self.make_token(TokenKind::LessEqual)
// } else {
// self.make_token(TokenKind::Less)
// }
// }
// '>' => {
// if self.is_match('=') {
// self.make_token(TokenKind::GreaterEqual)
// } else {
// self.make_token(TokenKind::Greater)
// }
// }
// '"' => self.string(),
_ => Token::error(self, "Unexpected character."),
})
}
}
@ -282,7 +307,7 @@ mod tests {
assert_source_scan("fa", Some(TokenKind::Identifier));
assert_source_scan("@", Some(TokenKind::Error));
assert_source_scan("", Some(TokenKind::Eof));
assert_source_scan("", None);
}
#[test]
@ -290,14 +315,14 @@ mod tests {
let mut scanner = Scanner::new("()");
assert_scan(&mut scanner, Some(TokenKind::LeftParen));
assert_scan(&mut scanner, Some(TokenKind::RightParen));
assert_scan(&mut scanner, Some(TokenKind::Eof));
assert_scan(&mut scanner, None);
}
#[test]
fn test_whitespace() {
assert_source_scan(" foo", Some(TokenKind::Identifier));
assert_source_scan("\tfoo", Some(TokenKind::Identifier));
assert_source_scan("// \n", Some(TokenKind::Eof));
assert_source_scan("// \n", None);
}
fn assert_source_scan(source: &str, kind: Option<TokenKind>) {

Loading…
Cancel
Save