I’d like to share Abelha, a parser combinator library inspired by Rust’s nom.
It provides a structured way to build parsers in Zig, focusing on composability and ease of use.
Features
- Nom-style combinators – Intuitive API for constructing parsers.
- Pre-built parsers – Common parsing functions are already available.
- API reference – Hosted on GitHub Pages.
- Used in real projects – Check out markdown-zig, a Markdown parser built with Abelha.
const std = @import("std");
const ab = @import("abelha");
const ParseResult = ab.ParseResult;
const tag = ab.bytes.tag;
const take = ab.bytes.take;
const separated_list1 = ab.multi.separated_list1;
fn parseHex(input: []const u8) !ParseResult(u8) {
const res = try take(2)(input);
const hex = try std.fmt.parseInt(u8, res.result, 16);
return ParseResult(u8){ .rest = res.rest, .result = hex };
}
fn hexColor(input: []const u8) !ParseResult([]const u8) {
const result = try tag("#")(input);
const res = try separated_list1(
u8,
tag(""),
parseHex,
)(result.rest);
return res;
}
test {
const text = "#1A2B3C";
const result = try hexColor(text);
const answer = [_]u8{ 0x1a, 0x2b, 0x3c };
try std.testing.expectEqualSlices(u8, &answer, result.result);
}
If you’re looking for a parser combinator in Zig, feel free to take a look:
Feedback and contributions are always welcome!