1/Hejsil/mecha v0.34


Mecha

A parser combinator library for the Zig programming language. Time to make your own parser mech! 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.map(toByte, mecha.int(u4, .{
    .parse_sign = false,
    .base = 16,
    .max_digits = 1,
}));
const hex2 = mecha.int(u8, .{
    .parse_sign = false,
    .base = 16,
    .max_digits = 2,
});
const rgb1 = mecha.map(mecha.toStruct(Rgb), mecha.manyN(hex1, 3, .{}));
const rgb2 = mecha.map(mecha.toStruct(Rgb), mecha.manyN(hex2, 3, .{}));
const rgb = mecha.combine(.{
    mecha.discard(mecha.ascii.char('#')),
    mecha.oneOf(.{
        rgb2,
        rgb1,
    }),
});

test "rgb" {
    const testing = std.testing;
    const allocator = testing.allocator;
    const a = (try rgb.parse(allocator, "#aabbcc")).value;
    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;
    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;
    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;
    try testing.expectEqual(@as(u8, 0), d.r);
    try testing.expectEqual(@as(u8, 0), d.g);
    try testing.expectEqual(@as(u8, 0), d.b);
}

Package Contents

  • example/rgb.zig
  • example/json.zig
  • example/README.md.template
  • gyro.zzz
  • .gitattributes
  • mecha.zig
  • LICENSE
  • .github/workflows/ci.yml
  • .github/dependabot.yml
  • .github/FUNDING.yml
  • build.zig
  • src/ascii.zig
  • src/utf8.zig
  • README.md
  • zig.mod
  • .gitignore

History

Published On Tree @ Commit Size
v0.47 Fri, 02 Feb 2024 14:48:22 UTC Tree 85.009 KB
v0.46 Fri, 02 Feb 2024 14:45:00 UTC Tree 85.017 KB
v0.45 Fri, 02 Feb 2024 14:42:26 UTC Tree 85.277 KB
v0.44 Mon, 08 Jan 2024 07:16:19 UTC Tree 85.208 KB
v0.43 Tue, 02 Jan 2024 06:58:09 UTC Tree 85.223 KB
v0.42 Tue, 05 Sep 2023 13:21:43 UTC Tree 85.223 KB
v0.41 Tue, 11 Jul 2023 07:26:38 UTC Tree 85.223 KB
v0.40 Tue, 27 Jun 2023 07:22:44 UTC Tree 85.223 KB
v0.39 Mon, 26 Jun 2023 14:39:10 UTC Tree 85.284 KB
v0.38 Sun, 18 Jun 2023 16:29:19 UTC Tree 85.284 KB
v0.37 Sun, 18 Jun 2023 11:42:29 UTC Tree 85.223 KB
v0.36 Thu, 01 Jun 2023 13:52:21 UTC Tree 85.227 KB
v0.35 Fri, 19 May 2023 14:49:38 UTC Tree 85.272 KB
v0.34 Fri, 12 May 2023 06:42:16 UTC Tree 85.272 KB
v0.33 Mon, 17 Apr 2023 07:01:18 UTC Tree 84.265 KB
v0.32 Fri, 14 Apr 2023 07:13:49 UTC Tree 84.273 KB
v0.31 Mon, 20 Mar 2023 08:28:46 UTC Tree 84.232 KB
v0.30 Sun, 19 Mar 2023 10:44:38 UTC Tree 84.227 KB
v0.29 Wed, 08 Mar 2023 15:53:37 UTC Tree 84.105 KB
v0.28 Wed, 01 Mar 2023 14:50:05 UTC Tree 84.145 KB
v0.27 Thu, 23 Feb 2023 09:09:34 UTC Tree 84.066 KB
v0.26 Wed, 15 Feb 2023 20:58:07 UTC Tree 84.041 KB
v0.25 Wed, 15 Feb 2023 20:57:38 UTC Tree 84.045 KB
v0.24 Wed, 15 Feb 2023 20:56:03 UTC Tree 84.055 KB
v0.23 Tue, 07 Feb 2023 09:54:38 UTC Tree 83.817 KB
v0.22 Tue, 07 Feb 2023 08:32:52 UTC Tree 83.609 KB
v0.21 Mon, 06 Feb 2023 15:53:39 UTC Tree 82.762 KB
v0.20 Mon, 06 Feb 2023 15:51:52 UTC Tree 82.781 KB
v0.19 Thu, 12 Jan 2023 18:54:42 UTC Tree 82.684 KB
v0.18 Wed, 04 Jan 2023 09:50:39 UTC Tree 82.685 KB
v0.17 Mon, 02 Jan 2023 09:08:51 UTC Tree 82.685 KB
v0.16 Mon, 02 Jan 2023 08:51:12 UTC Tree 82.677 KB
v0.15 Mon, 02 Jan 2023 08:37:52 UTC Tree 82.636 KB
v0.14 Mon, 02 Jan 2023 08:31:50 UTC Tree 82.502 KB
v0.13 Fri, 09 Dec 2022 10:03:40 UTC Tree 82.598 KB
v0.12 Fri, 09 Dec 2022 09:22:05 UTC Tree 82.641 KB
v0.11 Sun, 20 Nov 2022 10:00:57 UTC Tree 82.824 KB
v0.10 Wed, 16 Nov 2022 12:11:56 UTC Tree 82.833 KB
v0.9 Wed, 16 Nov 2022 10:18:31 UTC Tree 82.523 KB
v0.8 Tue, 08 Nov 2022 12:48:38 UTC Tree 82.502 KB
v0.7 Thu, 15 Sep 2022 11:45:25 UTC Tree 82.502 KB
v0.6 Tue, 23 Aug 2022 15:47:27 UTC Tree 82.419 KB
v0.5 Fri, 29 Jul 2022 12:12:52 UTC Tree 82.371 KB
v0.4 Tue, 31 May 2022 08:09:48 UTC Tree 82.362 KB
v0.3 Wed, 02 Mar 2022 16:55:39 UTC Tree 82.357 KB
v0.2 Thu, 17 Feb 2022 14:59:09 UTC Tree 82.365 KB
v0.1 Thu, 03 Feb 2022 20:53:48 UTC Tree 82.299 KB