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