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 @@...@@ -1,25 +1,27 @@
1const std = @import("std");1const std = @import("std");
2const Token = @import("./tokenize.zig").Token;2const Token = @import("./tokenize.zig").Token;
33
4const string = []const u8;
5
4//6//
5//7//
68
7pub const Value = union(enum) {9pub 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};
1315
14pub const Element = struct {16pub 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};
1921
20pub const Attr = struct {22pub const Attr = struct {
21 key: []const u8,23 key: string,
22 value: []const u8,24 value: string,
23};25};
2426
25//27//
...@@ -58,7 +60,7 @@ const Parser = struct {...@@ -58,7 +60,7 @@ const Parser = struct {
58 return ret;60 return ret;
59 }61 }
6062
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 }
8587
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 }
8991
...@@ -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 }
112114
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 }
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 {
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 }