| ... | ... | @@ -100,6 +100,15 @@ const Parser = struct { |
| 100 | 100 | return ret; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | fn raise(comptime line: usize, comptime pos: usize, comptime expected: []const u8, comptime found: []const u8) void { |
| 104 | @compileError(std.fmt.comptimePrint("pek: file:{d}:{d}: expected {s}, found {s}", .{ line, pos, expected, found })); |
| 105 | } |
| 106 | |
| 107 | fn expect(comptime expected: []const u8, comptime found: []const u8, comptime line: usize, comptime pos: usize) void { |
| 108 | if (std.mem.eql(u8, found, expected)) return; |
| 109 | raise(line, pos, expected, found); |
| 110 | } |
| 111 | |
| 103 | 112 | fn tryEatSymbol(comptime self: *Parser, comptime needle: string) bool { |
| 104 | 113 | if (self.index >= self.tokens.len) return false; |
| 105 | 114 | switch (self.tokens[self.index].data) { |
| ... | ... | @@ -134,11 +143,13 @@ const Parser = struct { |
| 134 | 143 | } |
| 135 | 144 | |
| 136 | 145 | pub fn eatSymbol(comptime self: *Parser, comptime needle: string) void { |
| 137 | | std.debug.assert(std.mem.eql(u8, self.eat(.symbol), needle)); |
| 146 | const tok = self.tokens[self.index]; |
| 147 | expect(needle, self.eat(.symbol), tok.line, tok.pos); |
| 138 | 148 | } |
| 139 | 149 | |
| 140 | 150 | pub fn eatWord(comptime self: *Parser, comptime needle: string) void { |
| 141 | | std.debug.assert(std.mem.eql(u8, needle, self.eat(.word))); |
| 151 | const tok = self.tokens[self.index]; |
| 152 | expect(needle, self.eat(.word), tok.line, tok.pos); |
| 142 | 153 | } |
| 143 | 154 | |
| 144 | 155 | pub fn doChildren(comptime self: *Parser) []const Value { |
| ... | ... | @@ -257,9 +268,7 @@ const Parser = struct { |
| 257 | 268 | defer self.index += 1; |
| 258 | 269 | const tok = self.tokens[self.index]; |
| 259 | 270 | const tag = std.meta.activeTag(tok.data); |
| 260 | | if (tag != typ) { |
| 261 | | @compileError(std.fmt.comptimePrint("pek: file:{d}:{d}: expected {s}, found {s}", .{ tok.line, tok.pos, @tagName(typ), @tagName(tag) })); |
| 262 | | } |
| 271 | if (tag != typ) raise(tok.line, tok.pos, @tagName(typ), @tagName(tag)); |
| 263 | 272 | return @field(tok.data, @tagName(typ)); |
| 264 | 273 | } |
| 265 | 274 | |