From 70d6284bd93fa0e3c6c2310e5e62f1a36535b6ca Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Wed, 21 Jun 2023 18:28:43 -0700 Subject: [PATCH] add number literal arguments to custom functions --- src/astgen.zig | 9 ++++++++- src/lib.zig | 2 ++ zig.mod | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/astgen.zig b/src/astgen.zig index 54e149942fc175a4ba510339ab4fe020ec5d46e0..5a6b1aab89ce374ac77057b24cf2b3472d6e7380 100644 --- a/src/astgen.zig +++ b/src/astgen.zig @@ -1,5 +1,6 @@ const std = @import("std"); const Token = @import("./tokenize.zig").Token; +const extras = @import("extras"); const string = []const u8; @@ -59,6 +60,7 @@ pub const Fn = struct { pub const Arg = union(enum) { lookup: []const string, plain: string, + int: u64, }; // @@ -209,7 +211,12 @@ const Parser = struct { continue; } if (temp.len == 0 and self.nextIs(.word)) { - temp = temp ++ &[_]string{self.eat(.word)}; + const next_word = self.eat(.word); + if (extras.matchesAll(u8, next_word, std.ascii.isDigit)) { + ret = ret ++ &[_]Arg{.{ .int = std.fmt.parseUnsigned(u64, next_word, 10) catch unreachable }}; + continue; + } + temp = temp ++ &[_]string{next_word}; } if (self.tryEatSymbol(".")) { temp = temp ++ &[_]string{self.eat(.word)}; diff --git a/src/lib.zig b/src/lib.zig index 6efea24aff7d020a3b69853e620941ebb209b380..d56b44e8b7a8df53535736ac9220308a92797c17 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -251,6 +251,7 @@ fn resolveArg(comptime arg: astgen.Arg, data: anytype, ctx: anytype) ResolveArg( return switch (arg) { .plain => |av| av[1 .. av.len - 1], .lookup => |av| if (comptime std.mem.eql(u8, av[0], "this")) search(av[1..], data) else search(av, ctx), + .int => |av| av, }; } @@ -258,6 +259,7 @@ fn ResolveArg(comptime arg: astgen.Arg, comptime This: type, comptime Ctx: type) return switch (arg) { .plain => string, .lookup => |av| if (comptime std.mem.eql(u8, av[0], "this")) FieldSearch(This, av[1..]) else FieldSearch(Ctx, av), + .int => u64, }; } diff --git a/zig.mod b/zig.mod index 48a6dd3c5631bc89e49a995ad2dca6ec14edfe98..99df6e038a5d483d14e8990d08d0b975db28321c 100644 --- a/zig.mod +++ b/zig.mod @@ -5,3 +5,4 @@ license: AGPL-3.0 description: An HTML preprocessor with a builtin template engine. dependencies: - src: git https://github.com/kivikakk/htmlentities.zig + - src: git https://github.com/nektro/zig-extras -- 2.54.0