aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 7a0513ec57455a8c5a062bd8c9f2002dc94906af (plain)
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
const std = @import("std");
pub const Grammar = @import("grammar.zig");
pub const recognizer = @import("recognizer.zig");

fn help() !noreturn {
	const stderr = std.io.getStdErr().writer();
	try stderr.print("gll [grammar] [input]\n", .{});
	std.process.exit(1);
}

fn read_whole_file(path: []const u8, allocator: std.mem.Allocator) ![]const u8 {
	const file = try std.fs.cwd().openFile(path, .{});
	defer file.close();
	const stat = try file.stat();
	return file.readToEndAlloc(allocator, stat.size);
}

pub fn main() !void {
	var gpa = std.heap.GeneralPurposeAllocator(.{}){};
	const allocator = gpa.allocator();
	defer {
		if (gpa.deinit() == .leak) {
			@panic("memory leak detected");
		}
	}
	
	var args = std.process.args();
	_ = args.next();

	const grammar_path = args.next() orelse try help();
	const input_path = args.next() orelse try help();

	const grammar_text = try read_whole_file(grammar_path, allocator);
	defer allocator.free(grammar_text);
	const input = try read_whole_file(input_path, allocator);
	defer allocator.free(input);

	var grammar = try Grammar.parse(grammar_text, allocator);
	defer grammar.deinit(allocator);
}

test {
	std.testing.refAllDecls(@This());
}