]> Repositories - zlox.git/blob - src/Token.zig
Make scanner managed
[zlox.git] / src / Token.zig
1 const std = @import("std");
2 const TokenType = @import("token_type.zig").TokenType;
3 const Token = @This();
4
5 type: TokenType,
6 lexeme: []const u8,
7 literal: ?Literal,
8 line: u32,
9
10 pub const Literal = union {
11     string: []const u8,
12     number: f64,
13 };
14
15 pub fn init(@"type": TokenType, lexeme: []const u8, literal: ?Literal, line: u32) Token {
16     return .{
17         .type = @"type",
18         .lexeme = lexeme,
19         .literal = literal,
20         .line = line,
21     };
22 }
23
24 pub fn format(self: Token, writer: *std.io.Writer) !void {
25     try writer.print("{} {s} {any}", .{ self.type, self.lexeme, self.literal });
26 }