A parser combinator library for Zig
Find a file
2026-01-21 20:34:45 +01:00
.github cicd: Use minimum_zig_version instead of master 2025-11-26 20:33:34 +01:00
example feat(parser): add left recursion support via memoized combinator 2025-09-01 18:10:09 +02:00
src chore: Update to zig-0.15.0-dev.1031+61eff7b6d 2025-07-14 17:32:31 +02:00
.gitattributes Add .gitattributes file 2020-06-10 14:35:28 +02:00
.gitignore Update to latest version of zig 2024-05-31 09:41:05 +02:00
build.zig feat(parser): add left recursion support via memoized combinator 2025-09-01 18:10:09 +02:00
build.zig.zon Release 0.11.0 2025-08-21 19:42:38 +02:00
LICENSE LICENSE: Remove year 2024-02-24 02:34:23 +01:00
mecha.zig feat(parser): add inspect function for parsing process tracking 2026-01-21 20:34:45 +01:00
README.md fix copy+paste typo, the variable is called mecha, not clap 2025-08-20 14:52:04 +02:00

Mecha

A parser combinator library for the Zig programming language. Time to make your own parser mech!

const mecha = @import("mecha");
const std = @import("std");

const Rgb = struct {
    r: u8,
    g: u8,
    b: u8,
};

fn toByte(v: u4) u8 {
    return @as(u8, v) * 0x10 + v;
}

const hex1 = mecha.int(u4, .{
    .parse_sign = false,
    .base = 16,
    .max_digits = 1,
}).map(toByte);
const hex2 = mecha.int(u8, .{
    .parse_sign = false,
    .base = 16,
    .max_digits = 2,
});
const rgb1 = mecha.manyN(hex1, 3, .{}).map(mecha.toStruct(Rgb));
const rgb2 = mecha.manyN(hex2, 3, .{}).map(mecha.toStruct(Rgb));
const rgb = mecha.combine(.{
    mecha.ascii.char('#').discard(),
    mecha.oneOf(.{ rgb2, rgb1 }),
});

test "rgb" {
    const testing = std.testing;
    const allocator = testing.allocator;
    const a = (try rgb.parse(allocator, "#aabbcc")).value.ok;
    try testing.expectEqual(@as(u8, 0xaa), a.r);
    try testing.expectEqual(@as(u8, 0xbb), a.g);
    try testing.expectEqual(@as(u8, 0xcc), a.b);

    const b = (try rgb.parse(allocator, "#abc")).value.ok;
    try testing.expectEqual(@as(u8, 0xaa), b.r);
    try testing.expectEqual(@as(u8, 0xbb), b.g);
    try testing.expectEqual(@as(u8, 0xcc), b.b);

    const c = (try rgb.parse(allocator, "#000000")).value.ok;
    try testing.expectEqual(@as(u8, 0), c.r);
    try testing.expectEqual(@as(u8, 0), c.g);
    try testing.expectEqual(@as(u8, 0), c.b);

    const d = (try rgb.parse(allocator, "#000")).value.ok;
    try testing.expectEqual(@as(u8, 0), d.r);
    try testing.expectEqual(@as(u8, 0), d.g);
    try testing.expectEqual(@as(u8, 0), d.b);
}

Installation

Developers tend to either use

  • The latest tagged release of Zig
  • The latest build of Zigs master branch

Depending on which developer you are, you need to run different zig fetch commands:

# Version of mecha that works with a tagged release of Zig
# Replace `<REPLACE ME>` with the version of mecha that you want to use
# See: https://github.com/Hejsil/mecha/releases
zig fetch --save https://github.com/Hejsil/mecha/archive/refs/tags/<REPLACE ME>.tar.gz

# Version of mecha that works with latest build of Zigs master branch
zig fetch --save git+https://github.com/Hejsil/mecha

Then add the following to build.zig:

const mecha = b.dependency("mecha", .{});
exe.root_module.addImport("mecha", mecha.module("mecha"));