update: add cycle and firstof
This commit is contained in:
parent
0891dca8bf
commit
510dec9113
2 changed files with 732 additions and 532 deletions
125
src/parser.zig
125
src/parser.zig
|
|
@ -16,6 +16,8 @@ pub const NodeType = enum {
|
|||
autoescape,
|
||||
spaceless,
|
||||
url,
|
||||
cycle,
|
||||
firstof,
|
||||
};
|
||||
|
||||
pub const AutoescapeNode = struct {
|
||||
|
|
@ -25,6 +27,14 @@ pub const AutoescapeNode = struct {
|
|||
enabled: bool,
|
||||
};
|
||||
|
||||
pub const FirstOfNode = struct {
|
||||
values: []const []const u8,
|
||||
};
|
||||
|
||||
pub const CycleNode = struct {
|
||||
values: []const []const u8,
|
||||
};
|
||||
|
||||
pub const UrlNode = struct {
|
||||
name: []const u8,
|
||||
args: []const []const u8,
|
||||
|
|
@ -129,6 +139,8 @@ pub const Node = struct {
|
|||
autoescape: ?AutoescapeNode = null,
|
||||
spaceless: ?SpacelessNode = null,
|
||||
url: ?UrlNode = null,
|
||||
cycle: ?CycleNode = null,
|
||||
firstof: ?FirstOfNode = null,
|
||||
|
||||
pub fn deinit(self: Node, allocator: std.mem.Allocator) void {
|
||||
switch (self.type) {
|
||||
|
|
@ -205,6 +217,14 @@ pub const Node = struct {
|
|||
for (u.args) |a| allocator.free(a);
|
||||
allocator.free(u.args);
|
||||
},
|
||||
.cycle => if (self.cycle) |c| {
|
||||
for (c.values) |v| allocator.free(v);
|
||||
allocator.free(c.values);
|
||||
},
|
||||
.firstof => if (self.firstof) |fo| {
|
||||
for (fo.values) |v| allocator.free(v);
|
||||
allocator.free(fo.values);
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -1289,6 +1309,111 @@ pub const Parser = struct {
|
|||
continue;
|
||||
}
|
||||
|
||||
if (std.mem.eql(u8, tag_name, "cycle")) {
|
||||
const args = node.tag.?.args;
|
||||
|
||||
var values = std.ArrayList([]const u8){};
|
||||
defer values.deinit(allocator);
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < args.len) {
|
||||
while (i < args.len and std.ascii.isWhitespace(args[i])) : (i += 1) {}
|
||||
|
||||
if (i >= args.len) break;
|
||||
|
||||
const start = i;
|
||||
var in_quote = false;
|
||||
var quote_char: u8 = 0;
|
||||
if (args[i] == '"' or args[i] == '\'') {
|
||||
in_quote = true;
|
||||
quote_char = args[i];
|
||||
i += 1;
|
||||
}
|
||||
|
||||
while (i < args.len) {
|
||||
if (in_quote) {
|
||||
if (args[i] == quote_char) {
|
||||
i += 1;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (std.ascii.isWhitespace(args[i])) break;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
const value = std.mem.trim(u8, args[start..i], " \t\r\n\"'");
|
||||
try values.append(allocator, try allocator.dupe(u8, value));
|
||||
}
|
||||
|
||||
allocator.free(node.tag.?.name);
|
||||
allocator.free(node.tag.?.args);
|
||||
|
||||
std.debug.print("3.0 - na real sou um cycle\n", .{});
|
||||
std.debug.print("===================================\n", .{});
|
||||
|
||||
try list.append(allocator, Node{
|
||||
.type = .cycle,
|
||||
.cycle = .{
|
||||
.values = try values.toOwnedSlice(allocator),
|
||||
},
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
if (std.mem.eql(u8, tag_name, "firstof")) {
|
||||
const args = node.tag.?.args;
|
||||
|
||||
var values = std.ArrayList([]const u8){};
|
||||
defer values.deinit(allocator);
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < args.len) {
|
||||
while (i < args.len and std.ascii.isWhitespace(args[i])) : (i += 1) {}
|
||||
|
||||
if (i >= args.len) break;
|
||||
|
||||
const start = i;
|
||||
var in_quote = false;
|
||||
var quote_char: u8 = 0;
|
||||
if (args[i] == '"' or args[i] == '\'') {
|
||||
in_quote = true;
|
||||
quote_char = args[i];
|
||||
i += 1;
|
||||
}
|
||||
|
||||
while (i < args.len) {
|
||||
if (in_quote) {
|
||||
if (args[i] == quote_char) {
|
||||
i += 1;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (std.ascii.isWhitespace(args[i])) break;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
const value = std.mem.trim(u8, args[start..i], " \t\r\n\"'");
|
||||
try values.append(allocator, try allocator.dupe(u8, value));
|
||||
|
||||
}
|
||||
|
||||
allocator.free(node.tag.?.name);
|
||||
allocator.free(node.tag.?.args);
|
||||
|
||||
std.debug.print("3.0 - na real sou um firstof\n", .{});
|
||||
std.debug.print("===================================\n", .{});
|
||||
|
||||
try list.append(allocator, Node{
|
||||
.type = .firstof,
|
||||
.firstof = .{
|
||||
.values = try values.toOwnedSlice(allocator),
|
||||
},
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// Para tags normais
|
||||
std.debug.print("===================================\n", .{});
|
||||
try list.append(allocator, node);
|
||||
|
|
|
|||
1139
src/parser_test.zig
1139
src/parser_test.zig
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue