aboutsummaryrefslogtreecommitdiff
path: root/src/character.zig
blob: bd11ccd833e206207afe464158f1dd907154c6b1 (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
35
36
37
38
const std = @import("std");

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

	pub const EPSILON: u8 = '_';
	pub const END: u8 = 0;

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

	pub fn from_u8(c: u8) Self {
		if (c == '_') {
			return Self { .epsilon = void{} };
		} else if (std.ascii.isUpper(c)) {
			return Self { .non_terminal = c };
		} else {
			return Self { .terminal = c };
		}
	}

	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| try writer.writeByte(c),
			.non_terminal => |c| try writer.writeByte(c),
		}
	}
};