aboutsummaryrefslogtreecommitdiff
path: root/src/character.zig
blob: 10dd254ef3271bfdbc72a175f1df0adf333eaa95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const std = @import("std");

pub const Character = union(enum) {
	const Self = @This();

	pub const EPSILON: u9 = std.math.maxInt(u8) + 1;
	pub const END: u9 = std.math.maxInt(u8) + 2;

	epsilon: void,
	terminal: u8,
	non_terminal: usize,

	pub fn format(
		self: *const Self,
		comptime fmt: []const u8,
		options: std.fmt.FormatOptions,
		writer: anytype,
	) !void {
		_ = fmt;
		_ = options;

		switch (self.*) {
			.epsilon => _ = try writer.writeAll("ε"),
			.terminal => |c| {
				if (c == END) {
					try writer.writeAll("$");
				} else {
					try writer.print("'{c}'", .{c});
				}
			},
			.non_terminal => |index| try writer.print("<{}>", .{index}),
		}
	}
};