aboutsummaryrefslogtreecommitdiff
path: root/src/z/js/parser/parser.zig
blob: b73d895dab237866438a3737d392d1ac46536ebc (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const std = @import("std");

const mod = @import("root.zig");
const Result = mod.Result;

pub fn Parser(T: type) type {
    const R = Result(T);

    return struct {
        parse: *const fn ([]const u8) anyerror!R,

        pub fn parseAll(self: *const @This(), buffer: []const u8) !T {
            const value, const next = try self.parse(buffer);

            if (next.len > 0) {
                return error.NotExhaustive;
            }

            return value;
        }

        pub fn fromType(P: type) @This() {
            return .{ .parse = P.parse };
        }

        pub fn variants(V: type) @This() {
            if (@typeInfo(T) != .@"union") {
                @compileError("variants is only allowed with an union as result type");
            }

            return .{
                .parse = struct {
                    fn parse(buffer: []const u8) !R {
                        inline for (std.meta.fields(T)) |field| {
                            if (@field(V, field.name).parse(buffer)) |result| {
                                const value, const next = result;
                                return .{
                                    @unionInit(T, field.name, value),
                                    next,
                                };
                            } else |_| { }
                        }

                        return error.Variants;
                    }
                }.parse,
            };
        }

        pub fn any(P: type, comptime parsers: []const struct { Parser(P), T }) @This() {
            return .{
                .parse = struct {
                    fn parse(buffer: []const u8) !R {
                        inline for (parsers) |pair| {
                            const parser, const value = pair;

                            if (parser.parse(buffer)) |result| {
                                _, const next = result;
                                return .{ value, next };
                            } else |_| { }
                        }
                        return error.Any;
                    }
                }.parse,
            };
        }
    };
}

test "variants" {
    const Char = union(enum) {
        upper: u8,
        lower: u8,
        space: void,
    };

    const parser: Parser(Char) = .variants(struct {
        const upper: Parser(u8) = .any(void, &.{
            .{ mod.builtin.literal("A"), 'A' },
            .{ mod.builtin.literal("B"), 'B' },
        });

        const lower: Parser(u8) = .any(void, &.{
            .{ mod.builtin.literal("a"), 'a' },
            .{ mod.builtin.literal("b"), 'b' },
        });

        const space: Parser(void) = mod.builtin.literal(" ");
    });

    {
        const result = try parser.parseAll("a");
        try std.testing.expect(result.lower == 'a');
    }

    {
        const result = try parser.parseAll("b");
        try std.testing.expect(result.lower == 'b');
    }

    {
        const result = try parser.parseAll("A");
        try std.testing.expect(result.upper == 'A');
    }

    {
        const result = try parser.parseAll("B");
        try std.testing.expect(result.upper == 'B');
    }

    {
        const result = try parser.parseAll(" ");
        try std.testing.expect(result == .space);
    }

    try std.testing.expect(parser.parseAll("c") == error.Variants);
}