| ... | @@ -100,6 +100,15 @@ const Parser = struct { | ... | @@ -100,6 +100,15 @@ const Parser = struct { |
| 100 | return ret; | 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 | fn tryEatSymbol(comptime self: *Parser, comptime needle: string) bool { | 112 | fn tryEatSymbol(comptime self: *Parser, comptime needle: string) bool { |
| 104 | if (self.index >= self.tokens.len) return false; | 113 | if (self.index >= self.tokens.len) return false; |
| 105 | switch (self.tokens[self.index].data) { | 114 | switch (self.tokens[self.index].data) { |
| ... | @@ -134,11 +143,13 @@ const Parser = struct { | ... | @@ -134,11 +143,13 @@ const Parser = struct { |
| 134 | } | 143 | } |
| 135 | | 144 | |
| 136 | pub fn eatSymbol(comptime self: *Parser, comptime needle: string) void { | 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 | pub fn eatWord(comptime self: *Parser, comptime needle: string) void { | 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 | pub fn doChildren(comptime self: *Parser) []const Value { | 155 | pub fn doChildren(comptime self: *Parser) []const Value { |
| ... | @@ -257,9 +268,7 @@ const Parser = struct { | ... | @@ -257,9 +268,7 @@ const Parser = struct { |
| 257 | defer self.index += 1; | 268 | defer self.index += 1; |
| 258 | const tok = self.tokens[self.index]; | 269 | const tok = self.tokens[self.index]; |
| 259 | const tag = std.meta.activeTag(tok.data); | 270 | const tag = std.meta.activeTag(tok.data); |
| 260 | if (tag != typ) { | 271 | if (tag != typ) raise(tok.line, tok.pos, @tagName(typ), @tagName(tag)); |
| 261 | @compileError(std.fmt.comptimePrint("pek: file:{d}:{d}: expected {s}, found {s}", .{ tok.line, tok.pos, @tagName(typ), @tagName(tag) })); | | |
| 262 | } | | |
| 263 | return @field(tok.data, @tagName(typ)); | 272 | return @field(tok.data, @tagName(typ)); |
| 264 | } | 273 | } |
| 265 | | 274 | |