aboutsummaryrefslogtreecommitdiff
path: root/src/rule-list.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-04-24 20:06:50 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2025-04-24 20:06:50 +0200
commitf593da7580f423b1405f4705081368acef0b3c21 (patch)
tree875e25e99da4f531e44e7537c7d8006da9ee0aa5 /src/rule-list.zig
parent839271627d0cfd2240ec30a603ff60a0165fe6a2 (diff)
fix naming
the struct RuleList is now named NonTerminal and its member rhs is now called Rule.
Diffstat (limited to 'src/rule-list.zig')
-rw-r--r--src/rule-list.zig91
1 files changed, 0 insertions, 91 deletions
diff --git a/src/rule-list.zig b/src/rule-list.zig
deleted file mode 100644
index a438750..0000000
--- a/src/rule-list.zig
+++ /dev/null
@@ -1,91 +0,0 @@
-const std = @import("std");
-const CharSet = @import("char-set.zig");
-const Character = @import("character.zig").Character;
-
-const Self = @This();
-const Rhs = []Character;
-
-name: u8,
-rhs: std.ArrayList(Rhs),
-first: CharSet,
-follows: CharSet,
-
-pub fn init(name: u8, allocator: std.mem.Allocator) Self {
- return Self {
- .name = name,
- .rhs = std.ArrayList(Rhs).init(allocator),
- .first = CharSet {},
- .follows = CharSet {},
- };
-}
-
-pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
- for (self.rhs.items) |item| {
- allocator.free(item);
- }
-
- self.rhs.deinit();
-}
-
-pub fn add_rule(self: *Self, rhs: Rhs) !void {
- try self.rhs.append(rhs);
-}
-
-pub fn parse_rule(
- buffer: []const u8,
- allocator: std.mem.Allocator
-) !struct { u8, Rhs } {
-
- var rhs = std.ArrayList(Character).init(allocator);
- errdefer rhs.deinit();
-
- var tokens = std.mem.tokenizeAny(u8, buffer, &std.ascii.whitespace);
-
- const lhs = tokens.next() orelse return error.MissingLhs;
- if (lhs.len > 1) return error.InvalidLhs;
-
- const arrow = tokens.next() orelse return error.MissingArrow;
- if (!std.mem.eql(u8, arrow, "->")) return error.InvalidArrow;
-
- while (tokens.next()) |token| {
- if (token.len > 1) return error.InvalidCharacter;
- try rhs.append(Character.from_u8(token[0]));
- }
-
- if (rhs.items.len == 0) {
- return error.EmptyProduction;
- }
-
- return .{ lhs[0], try rhs.toOwnedSlice() };
-}
-
-pub inline fn is_empty(self: *const Self) bool {
- return self.rhs.items.len == 0;
-}
-
-pub fn format(
- self: *const Self,
- comptime fmt: []const u8,
- options: std.fmt.FormatOptions,
- writer: anytype,
-) !void {
- _ = fmt;
- _ = options;
-
- try writer.print("[{c} -> ", .{ self.name });
-
- if (self.rhs.items.len > 0) {
- for (self.rhs.items[0]) |c| {
- try writer.print("{}", .{c});
- }
-
- for (self.rhs.items[1..]) |r| {
- try writer.print(" | ", .{});
- for (r) |c| {
- try writer.print("{}", .{c});
- }
- }
- }
-
- try writer.writeByte(']');
-}