scanner: fix issues regarding peekNext

This commit is contained in:
Luna 2019-06-06 01:27:59 -03:00
parent f70f2ea1ae
commit ff5a532c58

View file

@ -186,35 +186,17 @@ pub const Scanner = struct {
/// Peek at the next character in the scanner /// Peek at the next character in the scanner
fn peekNext(self: *Scanner) u8 { fn peekNext(self: *Scanner) u8 {
if (self.current > self.source.len) return 0; if (self.current + 1 > self.source.len) return 0;
return self.source[self.current]; return self.source[self.current];
} }
/// Skip all whitespace, but increments Scanner.line when finding a newline.
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,
}
}
}
/// Consume a number. /// Consume a number.
/// Returns either an Integer or a Float token. Proper typing /// Returns either an Integer or a Float token. Proper typing
/// of the number (i32 i64 u32 u64 f32 f64) are for the parser. /// of the number (i32 i64 u32 u64 f32 f64) are for the parser.
fn doNumber(self: *Scanner) Token { fn doNumber(self: *Scanner) Token {
var ttype = TokenType.Integer; var ttype = TokenType.Integer;
while (isDigit(self.peek())) { while (isDigit(self.peekNext())) {
_ = self.advance(); _ = self.advance();
} }
@ -279,7 +261,6 @@ pub const Scanner = struct {
} }
pub fn nextToken(self: *Scanner) !?Token { pub fn nextToken(self: *Scanner) !?Token {
self.skipWhitespace();
self.start = self.current; self.start = self.current;
if (self.isAtEnd()) return self.makeToken(TokenType.EOF); if (self.isAtEnd()) return self.makeToken(TokenType.EOF);
@ -342,6 +323,12 @@ pub const Scanner = struct {
'\'' => try self.doString('\''), '\'' => try self.doString('\''),
'"' => try self.doString('"'), '"' => try self.doString('"'),
' ', '\r', '\t' => null,
'\n' => blk: {
self.line += 1;
break :blk null;
},
else => return ScannerError.Unexpected, else => return ScannerError.Unexpected,
}; };