From ff5a532c5815dbac1e08f35186b85909e8500bf1 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 6 Jun 2019 01:27:59 -0300 Subject: [PATCH] scanner: fix issues regarding peekNext --- src/scanner.zig | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/scanner.zig b/src/scanner.zig index 0a632ee..94faf6b 100644 --- a/src/scanner.zig +++ b/src/scanner.zig @@ -186,35 +186,17 @@ pub const Scanner = struct { /// Peek at the next character in the scanner 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]; } - /// 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. /// Returns either an Integer or a Float token. Proper typing /// of the number (i32 i64 u32 u64 f32 f64) are for the parser. fn doNumber(self: *Scanner) Token { var ttype = TokenType.Integer; - while (isDigit(self.peek())) { + while (isDigit(self.peekNext())) { _ = self.advance(); } @@ -279,7 +261,6 @@ pub const Scanner = struct { } pub fn nextToken(self: *Scanner) !?Token { - self.skipWhitespace(); self.start = self.current; if (self.isAtEnd()) return self.makeToken(TokenType.EOF); @@ -342,6 +323,12 @@ pub const Scanner = struct { '\'' => try self.doString('\''), '"' => try self.doString('"'), + ' ', '\r', '\t' => null, + '\n' => blk: { + self.line += 1; + break :blk null; + }, + else => return ScannerError.Unexpected, };