Skip to content

Commit

Permalink
Refactor to allow for method call as well as current api
Browse files Browse the repository at this point in the history
  • Loading branch information
Hejsil committed May 10, 2023
1 parent dacf54c commit 8210b37
Show file tree
Hide file tree
Showing 6 changed files with 399 additions and 350 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ const rgb = mecha.combine(.{
test "rgb" {
const testing = std.testing;
const allocator = testing.allocator;
const a = (try rgb(allocator, "#aabbcc")).value;
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(allocator, "#abc")).value;
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(allocator, "#000000")).value;
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(allocator, "#000")).value;
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);
Expand Down
67 changes: 38 additions & 29 deletions example/json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ const value = mecha.oneOf(.{

const members = mecha.combine(.{
member,
mecha.discard(mecha.many(mecha.combine(.{ comma, member }), .{ .collect = false })),
mecha.combine(.{ comma, member })
.many(.{ .collect = false })
.discard(),
});

const elements = mecha.combine(.{
element,
mecha.discard(mecha.many(mecha.combine(.{ comma, element }), .{ .collect = false })),
mecha.combine(.{ comma, element })
.many(.{ .collect = false })
.discard(),
});

const array = mecha.combine(.{ lbracket, mecha.discard(mecha.opt(elements)), rbracket });
const array = mecha.combine(.{ lbracket, elements.opt().discard(), rbracket });
const element = mecha.ref(valueRef);
const member = mecha.combine(.{ jstring, colon, element });
const object = mecha.combine(.{ lcurly, mecha.discard(mecha.opt(members)), rcurly });
const object = mecha.combine(.{ lcurly, members.opt().discard(), rcurly });

fn valueRef() mecha.Parser(void) {
return value;
Expand All @@ -48,16 +52,19 @@ const rbracket = token(mecha.utf8.char(']'));
const rcurly = token(mecha.utf8.char('}'));

fn token(comptime parser: anytype) mecha.Parser(void) {
return mecha.combine(.{ mecha.discard(parser), ws });
return mecha.combine(.{ parser.discard(), ws });
}

const chars = mecha.discard(mecha.many(char, .{ .collect = false }));
const chars = char.many(.{ .collect = false }).discard();

const char = mecha.oneOf(.{
mecha.discard(mecha.utf8.range(0x0020, '"' - 1)),
mecha.discard(mecha.utf8.range('"' + 1, '\\' - 1)),
mecha.discard(mecha.utf8.range('\\' + 1, 0x10FFFF)),
mecha.discard(mecha.combine(.{ mecha.discard(mecha.utf8.char('\\')), escape })),
mecha.utf8.range(0x0020, '"' - 1).discard(),
mecha.utf8.range('"' + 1, '\\' - 1).discard(),
mecha.utf8.range('\\' + 1, 0x10FFFF).discard(),
mecha.combine(.{
mecha.utf8.char('\\').discard(),
escape,
}).discard(),
});

const escape = mecha.oneOf(.{
Expand All @@ -74,62 +81,64 @@ const escape = mecha.oneOf(.{

const hex = mecha.oneOf(.{
jdigit,
mecha.discard(mecha.utf8.range('a', 'f')),
mecha.discard(mecha.utf8.range('A', 'F')),
mecha.utf8.range('a', 'f').discard(),
mecha.utf8.range('A', 'F').discard(),
});

const integer = mecha.oneOf(.{
mecha.combine(.{ onenine, digits }),
jdigit,
mecha.combine(.{ mecha.discard(mecha.utf8.char('-')), onenine, digits }),
mecha.combine(.{ mecha.discard(mecha.utf8.char('-')), jdigit }),
mecha.combine(.{ mecha.utf8.char('-').discard(), onenine, digits }),
mecha.combine(.{ mecha.utf8.char('-').discard(), jdigit }),
});

const digits = mecha.discard(mecha.many(jdigit, .{ .collect = false, .min = 1 }));
const digits = jdigit.many(.{ .collect = false, .min = 1 }).discard();

const jdigit = mecha.oneOf(.{
mecha.discard(mecha.utf8.char('0')),
mecha.utf8.char('0').discard(),
onenine,
});

const onenine = mecha.discard(mecha.utf8.range('1', '9'));
const onenine = mecha.utf8.range('1', '9').discard();

const fraction = mecha.discard(mecha.opt(
mecha.combine(.{ mecha.discard(mecha.utf8.char('.')), digits }),
mecha.combine(.{ mecha.utf8.char('.').discard(), digits }),
));

const exponent = mecha.discard(mecha.opt(mecha.combine(
.{ mecha.discard(mecha.oneOf(.{ mecha.utf8.char('E'), mecha.utf8.char('e') })), sign, digits },
)));
const exponent = mecha.combine(.{
mecha.oneOf(.{ mecha.utf8.char('E'), mecha.utf8.char('e') }).discard(),
sign,
digits,
}).opt().discard();

const sign = mecha.discard(mecha.opt(mecha.oneOf(.{
const sign = mecha.oneOf(.{
mecha.utf8.char('+'),
mecha.utf8.char('-'),
})));
}).opt().discard();

const ws = mecha.discard(mecha.many(mecha.oneOf(.{
const ws = mecha.oneOf(.{
mecha.utf8.char(0x0020),
mecha.utf8.char(0x000A),
mecha.utf8.char(0x000D),
mecha.utf8.char(0x0009),
}), .{ .collect = false }));
}).many(.{ .collect = false }).discard();

fn ok(s: []const u8) !void {
const res = json(testing.allocator, s) catch @panic("test failure");
const res = json.parse(testing.allocator, s) catch @panic("test failure");
try testing.expectEqualStrings("", res.rest);
}

fn err(s: []const u8) !void {
try testing.expectError(error.ParserFailed, json(testing.allocator, s));
try testing.expectError(error.ParserFailed, json.parse(testing.allocator, s));
}

fn errNotAllParsed(s: []const u8) !void {
const res = json(testing.allocator, s) catch @panic("test failure");
const res = json.parse(testing.allocator, s) catch @panic("test failure");
try testing.expect(res.rest.len != 0);
}

fn any(s: []const u8) void {
_ = json(testing.allocator, s) catch {};
_ = json.parse(testing.allocator, s) catch {};
}

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
23 changes: 10 additions & 13 deletions example/rgb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,42 @@ fn toByte(v: u4) u8 {
return @as(u8, v) * 0x10 + v;
}

const hex1 = mecha.map(toByte, mecha.int(u4, .{
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.map(mecha.toStruct(Rgb), mecha.manyN(hex1, 3, .{}));
const rgb2 = mecha.map(mecha.toStruct(Rgb), mecha.manyN(hex2, 3, .{}));
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.discard(mecha.ascii.char('#')),
mecha.oneOf(.{
rgb2,
rgb1,
}),
mecha.ascii.char('#').discard(),
mecha.oneOf(.{ rgb2, rgb1 }),
});

test "rgb" {
const testing = std.testing;
const allocator = testing.allocator;
const a = (try rgb(allocator, "#aabbcc")).value;
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(allocator, "#abc")).value;
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(allocator, "#000000")).value;
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(allocator, "#000")).value;
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);
Expand Down
Loading

0 comments on commit 8210b37

Please sign in to comment.