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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
const std = @import("std");
pub const Grammar = @import("grammar.zig");
pub const gss = @import("gss.zig");
pub const recognizer = @import("recognizer.zig");
pub const argument = @import("argument.zig");
pub const Generator = @import("generator.zig").Generator;
const Args = argument.Args;
const RecognizeArgs = argument.RecognizeArgs;
const GenerateArgs = argument.GenerateArgs;
fn write_result(
writer: anytype,
is_tty: bool,
name: []const u8,
index: usize,
input: []const u8,
accepted: bool,
) !void {
if (is_tty) {
try writer.print("{s}[{}] {s}\x1b[0m: \"\x1b[3m{s}\x1b[0m\"\n", .{
name,
index,
if (accepted) "\x1b[32maccept"
else "\x1b[31mreject",
input,
});
} else {
try writer.print("{s}[{}] {s}: \"{s}\"\n", .{
name,
index,
if (accepted) "accept"
else "reject",
input,
});
}
}
fn recognize(args: *RecognizeArgs, allocator: std.mem.Allocator) !void {
const stdout = std.io.getStdOut();
const writer = std.io.getStdOut().writer();
var reader = args.input.file.reader();
var index: usize = 0;
const stderr = std.io.getStdErr();
if (args.input.file.isTty()) {
try stderr.writeAll("> ");
}
var read_arena = std.heap.ArenaAllocator.init(allocator);
defer read_arena.deinit();
while (try reader.readUntilDelimiterOrEofAlloc(
read_arena.allocator(),
'\n',
std.math.maxInt(usize)
)) |buffer| {
const trimmed = std.mem.trim(u8, buffer, &std.ascii.whitespace);
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
try write_result(
writer,
stdout.isTty(),
args.input.name,
index,
trimmed,
try recognizer.check(
&args.grammar,
trimmed,
arena.allocator()
));
index += 1;
if (args.input.file.isTty()) {
try stderr.writeAll("> ");
}
}
}
fn generate(args: *GenerateArgs, allocator: std.mem.Allocator) !void {
var writer = args.output.file.writer();
var count: usize = 0;
var generator = Generator(struct {
const Self = @This();
pub fn next(_: *Self, n: usize) usize {
return std.crypto.random.uintLessThan(usize, n);
}
}){};
while (count < args.count) {
const text = try generator.sentential_from_grammar(&args.grammar, 1000, allocator);
defer allocator.free(text);
if (args.empty or text.len > 0) {
try writer.print("{s}\n", .{text});
count += 1;
}
}
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer {
if (gpa.deinit() == .leak) {
@panic("memory leak detected");
}
}
var arguments = Args.parse(allocator);
defer arguments.deinit(allocator);
try switch(arguments) {
.recognize => |*args| recognize(args, allocator),
.generate => |*args| generate(args, allocator),
};
}
test {
std.testing.refAllDecls(@This());
}
|