authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-05 17:51:47 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-05 17:51:47 -07:00
log595471f38abf012c68d0cfe4b8667d904b6f920a
tree7f5186147081c64be124e73be81b0ecf777b69aa
parent27b00a8788aec21df582dcae38a976edb90bb818

astgen- alias `[]const u8` to `string`


1 files changed, 13 insertions(+), 11 deletions(-)

src/astgen.zig+13-11
......@@ -1,25 +1,27 @@
11const std = @import("std");
22const Token = @import("./tokenize.zig").Token;
33
4const string = []const u8;
5
46//
57//
68
79pub const Value = union(enum) {
810 element: Element,
911 attr: Attr,
10 string: []const u8,
11 replacement: []const []const u8,
12 string: string,
13 replacement: []const string,
1214};
1315
1416pub const Element = struct {
15 name: []const u8,
17 name: string,
1618 attrs: []const Attr,
1719 children: []const Value,
1820};
1921
2022pub const Attr = struct {
21 key: []const u8,
22 value: []const u8,
23 key: string,
24 value: string,
2325};
2426
2527//
......@@ -58,7 +60,7 @@ const Parser = struct {
5860 return ret;
5961 }
6062
61 fn tryEatSymbol(comptime self: *Parser, comptime needle: []const u8) bool {
63 fn tryEatSymbol(comptime self: *Parser, comptime needle: string) bool {
6264 switch (self.tokens[self.index]) {
6365 .symbol => |sym| {
6466 if (std.mem.eql(u8, sym, needle)) {
......@@ -83,7 +85,7 @@ const Parser = struct {
8385 };
8486 }
8587
86 pub fn eatSymbol(comptime self: *Parser, comptime needle: []const u8) void {
88 pub fn eatSymbol(comptime self: *Parser, comptime needle: string) void {
8789 std.debug.assert(std.mem.eql(u8, self.eat(.symbol), needle));
8890 }
8991
......@@ -110,15 +112,15 @@ const Parser = struct {
110112 return Value{ .element = self.doElement() };
111113 }
112114
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 = &.{};
115117 while (!self.tryEatSymbol("}")) {
116 ret = ret ++ &[_][]const u8{self.eat(.word)};
118 ret = ret ++ &[_]string{self.eat(.word)};
117119 }
118120 return ret;
119121 }
120122
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 {
122124 defer self.index += 1;
123125 return @field(self.tokens[self.index], @tagName(typ));
124126 }