scanner: add whitespace handling

This commit is contained in:
Luna 2019-06-01 16:54:15 -03:00
parent 2c7cf356b3
commit f4f1fe1fbc

View file

@ -68,9 +68,30 @@ pub const Scanner = struct {
}
}
pub fn scanToken(self: *Scanner) !tokens.Token {
self.start = self.current;
fn peek(self: *Scanner) u8 {
if (self.isAtEnd()) return 0;
return self.source[self.current];
}
fn skipWhitespace(self: *Scanner) void {
while (true) {
var c = self.peek();
switch (c) {
' ', '\r', '\t' => blk: {
_ = self.advance();
},
'\n' => blk: {
self.line += 1;
_ = self.advance();
},
else => return,
}
}
}
pub fn scanToken(self: *Scanner) !tokens.Token {
self.skipWhitespace();
self.start = self.current;
if (self.isAtEnd()) return self.makeToken(TokenType.EOF);
var c = self.advance();