aboutsummaryrefslogtreecommitdiff
path: root/src/non-terminal.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-05-15 17:49:24 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2025-05-15 17:49:24 +0200
commit0273079fa0004f9f93ae96615f97934b832f8c51 (patch)
tree07a2d68d856efc24e80d3c0b77dbc1dbb54d9d14 /src/non-terminal.zig
parentf67e7524859f0b5462065a23c4334c97710f4f84 (diff)
minor optimization: add a first set to each rule variant
Diffstat (limited to 'src/non-terminal.zig')
-rw-r--r--src/non-terminal.zig12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/non-terminal.zig b/src/non-terminal.zig
index 1665a80..f5ecf09 100644
--- a/src/non-terminal.zig
+++ b/src/non-terminal.zig
@@ -2,9 +2,9 @@ const std = @import("std");
const CharSet = @import("char-set.zig");
const Character = @import("character.zig").Character;
const string = @import("string.zig");
+const Rule = @import("rule.zig");
const Self = @This();
-pub const Rule = []Character;
id: usize,
name: []const u8,
@@ -26,7 +26,7 @@ pub fn init(name: []const u8, id: usize, allocator: std.mem.Allocator) !Self {
pub fn deinit(self: *Self) void {
for (self.rules()) |rule| {
- self.allocator.free(rule);
+ self.allocator.free(rule.items);
}
self.rule_list.deinit();
@@ -69,7 +69,7 @@ pub fn parse_and_add_variants(
head = std.mem.trimLeft(u8, head, &std.ascii.whitespace);
while (head.len > 0) {
if (head[0] == '|') {
- try self.rule_list.append(try rule.toOwnedSlice());
+ try self.rule_list.append(Rule { .items = try rule.toOwnedSlice() });
rule = std.ArrayList(Character).init(self.allocator);
head = head[1..];
} else if (head[0] == '\'') {
@@ -112,7 +112,7 @@ pub fn parse_and_add_variants(
return error.EmptyVariant;
}
- try self.rule_list.append(try rule.toOwnedSlice());
+ try self.rule_list.append(Rule { .items = try rule.toOwnedSlice() });
}
pub inline fn is_empty(self: *const Self) bool {
@@ -131,13 +131,13 @@ pub fn format(
try writer.print("{s} ->", .{ self.name });
if (self.rule_list.items.len > 0) {
- for (self.rule_list.items[0]) |c| {
+ for (self.rule_list.items[0].items) |c| {
try writer.print(" {}", .{c});
}
for (self.rule_list.items[1..]) |r| {
try writer.print(" |", .{});
- for (r) |c| {
+ for (r.items) |c| {
try writer.print(" {}", .{c});
}
}