summaryrefslogtreecommitdiff
path: root/src/estd/parser/common.zig
blob: 280283837b43d7977dc769039e45bc125e6ce3e8 (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
const std = @import("std");
const root = @import("root.zig");
const Context = @import("context.zig").Context;
const ParseFn = root.ParseFn;

pub fn LiteralFn(comptime I: type, comptime O: type) type {
	return *const fn (comptime literal: []const I, value: O) ParseFn(I, O);
}

pub fn Literal(comptime I: type, comptime O: type) LiteralFn(I, O) {
	return struct {
		pub fn literal(comptime lit: []const I, value: O) ParseFn(I, O) {
			return struct {
				pub fn parse(ctx: *Context(I)) !O {
					const snapshot = ctx.cursor.snapshot();
					errdefer ctx.cursor.rollback(snapshot);

					const slice = try ctx.cursor.consume(lit.len);
					if (std.mem.eql(I, lit, slice)) {
						return value;
					}

					return error.UnexpectedLiteral;
				}
			}.parse;
		}
	}.literal;
}