aboutsummaryrefslogtreecommitdiff
path: root/src/argument.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-05-06 10:43:09 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2025-05-06 10:43:09 +0200
commiteff19cc15a9bf4df60e7f90c3a7ee525c65266c0 (patch)
treed6034b574e8e2218054d42caadbf25fa17862abf /src/argument.zig
parente2f01d5df22704bfe62396a0f9a260f86edbde0e (diff)
add full grammar parsing
Diffstat (limited to 'src/argument.zig')
-rw-r--r--src/argument.zig21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/argument.zig b/src/argument.zig
index b553352..a9c823d 100644
--- a/src/argument.zig
+++ b/src/argument.zig
@@ -15,6 +15,8 @@ fn help(err: ?anyerror) noreturn {
\\
\\ generate [grammar] [options]
\\ Options:
+ \\ -e, --entry label Name of the entry point. (default: main)
+ \\
\\ -o, --output entry Output string to file
\\ By default stdout will be used.
\\
@@ -26,6 +28,8 @@ fn help(err: ?anyerror) noreturn {
\\
\\ recognize [grammar] [options]
\\ Options:
+ \\ -e, --entry label Name of the entry point. (default: main)
+ \\
\\ -i, --input entry Specify input source, if the path
\\ points to a directory it will scan
\\ all files in it. By default stdin will
@@ -114,6 +118,7 @@ pub const Entry = struct {
pub const RecognizeArgs = struct {
input: Entry,
grammar: Grammar,
+ entry: []const u8,
};
pub const GenerateArgs = struct {
@@ -121,6 +126,7 @@ pub const GenerateArgs = struct {
min_length: usize,
output: Entry,
grammar: Grammar,
+ entry: []const u8,
};
pub const Args = union(Mode) {
@@ -138,6 +144,7 @@ pub const Args = union(Mode) {
const text = Entry.read_file(check(args.next()), allocator);
defer allocator.free(text);
const grammar = Grammar.parse(
+ "main",
text,
allocator
) catch |e| help(e);
@@ -145,10 +152,13 @@ pub const Args = union(Mode) {
switch (mode) {
.recognize => {
var input: ?Entry = null;
+ var entry: []const u8 = "main";
while (args.next()) |arg| {
if (check_flags(arg, &[_][]const u8 { "-i", "--input" })) {
input = Entry.open(check(args.next()), false);
+ } else if (check_flags(arg, &[_][]const u8 { "-e", "--entry" })) {
+ entry = check(args.next());
} else help(error.InvalidArgument);
}
@@ -156,6 +166,7 @@ pub const Args = union(Mode) {
.recognize = .{
.input = input orelse Entry.stdin(),
.grammar = grammar,
+ .entry = entry,
},
};
},
@@ -164,6 +175,7 @@ pub const Args = union(Mode) {
var count: usize = 1;
var output: ?Entry = null;
var min_length: usize = 0;
+ var entry: []const u8 = "main";
while (args.next()) |arg| {
if (check_flags(arg, &[_][]const u8 { "-o", "--output" })) {
@@ -174,6 +186,8 @@ pub const Args = union(Mode) {
min_length = 1;
} else if (check_flags(arg, &[_][]const u8 { "-m", "--min-length" })) {
min_length = parse_int(check(args.next()));
+ } else if (check_flags(arg, &[_][]const u8 { "-e", "--entry" })) {
+ entry = check(args.next());
} else help(error.InvalidArgument);
}
@@ -183,19 +197,20 @@ pub const Args = union(Mode) {
.min_length = min_length,
.output = output orelse Entry.stdout(),
.grammar = grammar,
+ .entry = entry,
},
};
},
}
}
- pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
+ pub fn deinit(self: *Self) void {
switch (self.*) {
.recognize => |*rec| {
- rec.grammar.deinit(allocator);
+ rec.grammar.deinit();
},
.generate => |*gen| {
- gen.grammar.deinit(allocator);
+ gen.grammar.deinit();
}
}
}