| ... | @@ -1,25 +1,27 @@ | ... | @@ -1,25 +1,27 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const Token = @import("./tokenize.zig").Token; | 2 | const Token = @import("./tokenize.zig").Token; |
| 3 | | 3 | |
| | 4 | const string = []const u8; |
| | 5 | |
| 4 | // | 6 | // |
| 5 | // | 7 | // |
| 6 | | 8 | |
| 7 | pub const Value = union(enum) { | 9 | pub const Value = union(enum) { |
| 8 | element: Element, | 10 | element: Element, |
| 9 | attr: Attr, | 11 | attr: Attr, |
| 10 | string: []const u8, | 12 | string: string, |
| 11 | replacement: []const []const u8, | 13 | replacement: []const string, |
| 12 | }; | 14 | }; |
| 13 | | 15 | |
| 14 | pub const Element = struct { | 16 | pub const Element = struct { |
| 15 | name: []const u8, | 17 | name: string, |
| 16 | attrs: []const Attr, | 18 | attrs: []const Attr, |
| 17 | children: []const Value, | 19 | children: []const Value, |
| 18 | }; | 20 | }; |
| 19 | | 21 | |
| 20 | pub const Attr = struct { | 22 | pub const Attr = struct { |
| 21 | key: []const u8, | 23 | key: string, |
| 22 | value: []const u8, | 24 | value: string, |
| 23 | }; | 25 | }; |
| 24 | | 26 | |
| 25 | // | 27 | // |
| ... | @@ -58,7 +60,7 @@ const Parser = struct { | ... | @@ -58,7 +60,7 @@ const Parser = struct { |
| 58 | return ret; | 60 | return ret; |
| 59 | } | 61 | } |
| 60 | | 62 | |
| 61 | fn tryEatSymbol(comptime self: *Parser, comptime needle: []const u8) bool { | 63 | fn tryEatSymbol(comptime self: *Parser, comptime needle: string) bool { |
| 62 | switch (self.tokens[self.index]) { | 64 | switch (self.tokens[self.index]) { |
| 63 | .symbol => |sym| { | 65 | .symbol => |sym| { |
| 64 | if (std.mem.eql(u8, sym, needle)) { | 66 | if (std.mem.eql(u8, sym, needle)) { |
| ... | @@ -83,7 +85,7 @@ const Parser = struct { | ... | @@ -83,7 +85,7 @@ const Parser = struct { |
| 83 | }; | 85 | }; |
| 84 | } | 86 | } |
| 85 | | 87 | |
| 86 | pub fn eatSymbol(comptime self: *Parser, comptime needle: []const u8) void { | 88 | pub fn eatSymbol(comptime self: *Parser, comptime needle: string) void { |
| 87 | std.debug.assert(std.mem.eql(u8, self.eat(.symbol), needle)); | 89 | std.debug.assert(std.mem.eql(u8, self.eat(.symbol), needle)); |
| 88 | } | 90 | } |
| 89 | | 91 | |
| ... | @@ -110,15 +112,15 @@ const Parser = struct { | ... | @@ -110,15 +112,15 @@ const Parser = struct { |
| 110 | return Value{ .element = self.doElement() }; | 112 | return Value{ .element = self.doElement() }; |
| 111 | } | 113 | } |
| 112 | | 114 | |
| 113 | pub fn doReplacement(comptime self: *Parser) []const []const u8 { | 115 | pub fn doReplacement(comptime self: *Parser) []const string { |
| 114 | var ret: []const []const u8 = &.{}; | 116 | var ret: []const string = &.{}; |
| 115 | while (!self.tryEatSymbol("}")) { | 117 | while (!self.tryEatSymbol("}")) { |
| 116 | ret = ret ++ &[_][]const u8{self.eat(.word)}; | 118 | ret = ret ++ &[_]string{self.eat(.word)}; |
| 117 | } | 119 | } |
| 118 | return ret; | 120 | return ret; |
| 119 | } | 121 | } |
| 120 | | 122 | |
| 121 | fn eat(comptime self: *Parser, comptime typ: std.meta.Tag(Token)) []const u8 { | 123 | fn eat(comptime self: *Parser, comptime typ: std.meta.Tag(Token)) string { |
| 122 | defer self.index += 1; | 124 | defer self.index += 1; |
| 123 | return @field(self.tokens[self.index], @tagName(typ)); | 125 | return @field(self.tokens[self.index], @tagName(typ)); |
| 124 | } | 126 | } |