authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-06-21 18:28:43 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-06-21 18:28:43 -07:00
log70d6284bd93fa0e3c6c2310e5e62f1a36535b6ca
tree6682f3f307df172e2d7ea4a35a3293b6d3fc64e8
parent8401a1944fff76034468cbc9daf74f19aabfe8fb

add number literal arguments to custom functions


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

src/astgen.zig+8-1
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const Token = @import("./tokenize.zig").Token;2const Token = @import("./tokenize.zig").Token;
3const extras = @import("extras");
34
4const string = []const u8;5const string = []const u8;
56
...@@ -59,6 +60,7 @@ pub const Fn = struct {...@@ -59,6 +60,7 @@ pub const Fn = struct {
59pub const Arg = union(enum) {60pub const Arg = union(enum) {
60 lookup: []const string,61 lookup: []const string,
61 plain: string,62 plain: string,
63 int: u64,
62};64};
6365
64//66//
...@@ -209,7 +211,12 @@ const Parser = struct {...@@ -209,7 +211,12 @@ const Parser = struct {
209 continue;211 continue;
210 }212 }
211 if (temp.len == 0 and self.nextIs(.word)) {213 if (temp.len == 0 and self.nextIs(.word)) {
212 temp = temp ++ &[_]string{self.eat(.word)};214 const next_word = self.eat(.word);
215 if (extras.matchesAll(u8, next_word, std.ascii.isDigit)) {
216 ret = ret ++ &[_]Arg{.{ .int = std.fmt.parseUnsigned(u64, next_word, 10) catch unreachable }};
217 continue;
218 }
219 temp = temp ++ &[_]string{next_word};
213 }220 }
214 if (self.tryEatSymbol(".")) {221 if (self.tryEatSymbol(".")) {
215 temp = temp ++ &[_]string{self.eat(.word)};222 temp = temp ++ &[_]string{self.eat(.word)};
src/lib.zig+2
...@@ -251,6 +251,7 @@ fn resolveArg(comptime arg: astgen.Arg, data: anytype, ctx: anytype) ResolveArg(...@@ -251,6 +251,7 @@ fn resolveArg(comptime arg: astgen.Arg, data: anytype, ctx: anytype) ResolveArg(
251 return switch (arg) {251 return switch (arg) {
252 .plain => |av| av[1 .. av.len - 1],252 .plain => |av| av[1 .. av.len - 1],
253 .lookup => |av| if (comptime std.mem.eql(u8, av[0], "this")) search(av[1..], data) else search(av, ctx),253 .lookup => |av| if (comptime std.mem.eql(u8, av[0], "this")) search(av[1..], data) else search(av, ctx),
254 .int => |av| av,
254 };255 };
255}256}
256257
...@@ -258,6 +259,7 @@ fn ResolveArg(comptime arg: astgen.Arg, comptime This: type, comptime Ctx: type)...@@ -258,6 +259,7 @@ fn ResolveArg(comptime arg: astgen.Arg, comptime This: type, comptime Ctx: type)
258 return switch (arg) {259 return switch (arg) {
259 .plain => string,260 .plain => string,
260 .lookup => |av| if (comptime std.mem.eql(u8, av[0], "this")) FieldSearch(This, av[1..]) else FieldSearch(Ctx, av),261 .lookup => |av| if (comptime std.mem.eql(u8, av[0], "this")) FieldSearch(This, av[1..]) else FieldSearch(Ctx, av),
262 .int => u64,
261 };263 };
262}264}
263265
zig.mod+1
...@@ -5,3 +5,4 @@ license: AGPL-3.0...@@ -5,3 +5,4 @@ license: AGPL-3.0
5description: An HTML preprocessor with a builtin template engine.5description: An HTML preprocessor with a builtin template engine.
6dependencies:6dependencies:
7 - src: git https://github.com/kivikakk/htmlentities.zig7 - src: git https://github.com/kivikakk/htmlentities.zig
8 - src: git https://github.com/nektro/zig-extras