const std = @import("std");
const Allocator = std.mem.Allocator;
+const ascii = std.ascii;
+const isDigit = ascii.isDigit;
+const isAlphabetic = ascii.isAlphabetic;
const Token = @import("Token.zig");
const Literal = Token.Literal;
const Scanner = @This();
-const TokenType = @import("token-type.zig").TokenType;
-const Lox = @import("Lox.zig");
+const TokenType = @import("token_type.zig").TokenType;
+const lox = @import("lox.zig");
source: []const u8,
tokens: std.ArrayList(Token) = .empty,
} else if (isAlpha(c)) {
try self.identifier(allocator);
} else {
- try Lox.@"error"(self.line, "Unexpected character.");
+ try lox.@"error"(self.line, "Unexpected character.");
},
}
}
fn identifier(self: *Scanner, allocator: Allocator) !void {
while (isAlphanumeric(self.peek())) _ = self.advance();
const text = self.source[self.start..self.current];
- var @"type" = keyword.get(text);
- if (@"type" == null) @"type" = .identifier;
- try self.addToken(allocator, @"type".?, null);
+ const @"type" = keyword.get(text) orelse .identifier;
+ try self.addToken(allocator, @"type", null);
}
fn number(self: *Scanner, allocator: Allocator) !void {
}
if (self.isAtEnd()) {
- try Lox.@"error"(self.line, "Unterminated string.");
+ try lox.@"error"(self.line, "Unterminated string.");
return;
}
}
fn isAlpha(c: u8) bool {
- return (c >= 'a' and c <= 'z') or
- (c >= 'A' and c <= 'Z') or
- c == '_';
+ return isAlphabetic(c) or c == '_';
}
fn isAlphanumeric(c: u8) bool {
return isAlpha(c) or isDigit(c);
}
-fn isDigit(c: u8) bool {
- return c >= '0' and c <= '9';
-}
-
fn isAtEnd(self: *Scanner) bool {
return self.current >= self.source.len;
}