authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-13 17:52:29 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-13 17:52:29 -07:00
log8e154277a37b31d312596f6108f123eb597846da
tree291559c76af1f87fe8ce59d8ff8a412a0f108b02
parentbcb022592b5751ff9acd5b9ac18d0dfea8fe447f

add line and pos to token struct


2 files changed, 34 insertions(+), 11 deletions(-)

src/astgen.zig+9-4
......@@ -84,7 +84,7 @@ const Parser = struct {
8484 }
8585
8686 fn tryEatSymbol(comptime self: *Parser, comptime needle: string) bool {
87 switch (self.tokens[self.index]) {
87 switch (self.tokens[self.index].data) {
8888 .symbol => |sym| {
8989 if (std.mem.eql(u8, sym, needle)) {
9090 self.index += 1;
......@@ -126,7 +126,7 @@ const Parser = struct {
126126 }
127127
128128 pub fn doValue(comptime self: *Parser) Value {
129 if (self.tokens[self.index] == .string) {
129 if (self.tokens[self.index].data == .string) {
130130 return Value{ .string = self.eat(.string) };
131131 }
132132 if (self.tryEatSymbol("{")) {
......@@ -193,8 +193,13 @@ const Parser = struct {
193193 return ret;
194194 }
195195
196 fn eat(comptime self: *Parser, comptime typ: std.meta.Tag(Token)) string {
196 fn eat(comptime self: *Parser, comptime typ: std.meta.Tag(Token.Data)) string {
197197 defer self.index += 1;
198 return @field(self.tokens[self.index], @tagName(typ));
198 const tok = self.tokens[self.index];
199 const tag = std.meta.activeTag(tok.data);
200 if (tag != typ) {
201 @compileError(std.fmt.comptimePrint("pek: file:{d}:{d}: expected {s}, found {s}", .{ tok.line, tok.pos, @tagName(tag), @tagName(typ) }));
202 }
203 return @field(tok.data, @tagName(typ));
199204 }
200205};
src/tokenize.zig+25-7
......@@ -4,12 +4,18 @@ const string = []const u8;
44//
55//
66
7pub const Token = union(enum) {
8 word: string,
9 symbol: string,
10 string: string,
7pub const Token = struct {
8 data: Data,
9 line: usize,
10 pos: usize,
1111
1212 pub const skippedChars = &[_]u8{ ' ', '\n', '\t', '\r' };
13
14 pub const Data = union(enum) {
15 word: string,
16 symbol: string,
17 string: string,
18 };
1319};
1420
1521//
......@@ -58,7 +64,11 @@ pub fn do(comptime input: string, comptime symbols: []const u8) []const Token {
5864 }
5965 if (mode == 2) {
6066 if (c == input[start]) {
61 ret = ret ++ &[_]Token{.{ .string = input[start .. i + 1] }};
67 ret = ret ++ &[_]Token{.{
68 .data = .{ .string = input[start .. i + 1] },
69 .line = line,
70 .pos = pos,
71 }};
6272 start = i + 1;
6373 end = i;
6474 mode = 0;
......@@ -84,7 +94,11 @@ pub fn do(comptime input: string, comptime symbols: []const u8) []const Token {
8494 if (shouldFlush) {
8595 if (mode == 0) {
8696 if (end - start > 0) {
87 ret = ret ++ &[_]Token{.{ .word = input[start..end] }};
97 ret = ret ++ &[_]Token{.{
98 .data = .{ .word = input[start..end] },
99 .line = line,
100 .pos = pos,
101 }};
88102 start = i;
89103 end = i;
90104 }
......@@ -93,7 +107,11 @@ pub fn do(comptime input: string, comptime symbols: []const u8) []const Token {
93107 end += 1;
94108 }
95109 if (std.mem.indexOfScalar(u8, symbols, c)) |_| {
96 ret = ret ++ &[_]Token{.{ .symbol = s }};
110 ret = ret ++ &[_]Token{.{
111 .data = .{ .symbol = s },
112 .line = line,
113 .pos = pos,
114 }};
97115 start += 1;
98116 end += 1;
99117 }