diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-05-06 10:43:09 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-05-06 10:43:09 +0200 |
| commit | eff19cc15a9bf4df60e7f90c3a7ee525c65266c0 (patch) | |
| tree | d6034b574e8e2218054d42caadbf25fa17862abf /src/character.zig | |
| parent | e2f01d5df22704bfe62396a0f9a260f86edbde0e (diff) | |
add full grammar parsing
Diffstat (limited to 'src/character.zig')
| -rw-r--r-- | src/character.zig | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/src/character.zig b/src/character.zig index bd11ccd..10dd254 100644 --- a/src/character.zig +++ b/src/character.zig @@ -3,22 +3,12 @@ const std = @import("std"); pub const Character = union(enum) { const Self = @This(); - pub const EPSILON: u8 = '_'; - pub const END: u8 = 0; + pub const EPSILON: u9 = std.math.maxInt(u8) + 1; + pub const END: u9 = std.math.maxInt(u8) + 2; 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 }; - } - } + non_terminal: usize, pub fn format( self: *const Self, @@ -31,8 +21,14 @@ pub const Character = union(enum) { switch (self.*) { .epsilon => _ = try writer.writeAll("ε"), - .terminal => |c| try writer.writeByte(c), - .non_terminal => |c| try writer.writeByte(c), + .terminal => |c| { + if (c == END) { + try writer.writeAll("$"); + } else { + try writer.print("'{c}'", .{c}); + } + }, + .non_terminal => |index| try writer.print("<{}>", .{index}), } } }; |