-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.zig
188 lines (160 loc) · 6.33 KB
/
pattern.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const std = @import("std");
const Expr = @import("expr.zig").Expr;
const Instr = @import("instr.zig").Instr;
pub const Pattern = struct {
data: Data,
location: Instr.Location,
pub const Data = union(enum) {
name: []const u8,
ignore,
expr: *const Expr,
guard: *const Guard,
list: []const Pattern,
lists: []const Pattern,
dict: []const Pattern.Pair,
dicts: []const Pattern,
str: *const Str,
};
pub fn deinit(self: Pattern, allocator: std.mem.Allocator) void {
switch (self.data) {
.name, .ignore => {},
inline .list, .lists, .dict, .dicts => |items| {
for (items) |item| item.deinit(allocator);
allocator.free(items);
},
inline else => |value| {
value.deinit(allocator);
allocator.destroy(value);
},
}
}
pub fn clone(self: Pattern, allocator: std.mem.Allocator) std.mem.Allocator.Error!Pattern {
var copy = self;
copy.data = switch (self.data) {
.name, .ignore => self.data,
inline .list, .lists, .dict, .dicts => |items, tag| data: {
const copies = try allocator.alloc(@TypeOf(items[0]), items.len);
var i: usize = 0;
errdefer {
allocator.free(copies);
for (copies[0..i]) |item| item.deinit(allocator);
}
while (i < items.len) : (i += 1) {
copies[i] = try items[i].clone(allocator);
}
break :data @unionInit(Pattern.Data, @tagName(tag), copies);
},
inline else => |value, tag| data: {
const copy_ = try allocator.create(@TypeOf(value.*));
errdefer allocator.destroy(copy_);
copy_.* = try value.clone(allocator);
break :data @unionInit(Pattern.Data, @tagName(tag), copy_);
},
};
return copy;
}
pub fn usesName(self: Pattern, name: []const u8) ?bool {
return switch (self.data) {
.name => |name_| if (std.mem.eql(u8, name_, name)) false else null,
.ignore => null,
inline .list, .lists, .dict, .dicts => |items| for (items) |item| {
if (item.usesName(name)) |uses| break uses;
} else null,
.expr => |expr| if (expr.usesName(name)) true else null,
inline else => |value| value.usesName(name),
};
}
pub const Guard = struct {
pattern: Pattern,
cond: Expr,
pub fn deinit(self: Guard, allocator: std.mem.Allocator) void {
self.pattern.deinit(allocator);
self.cond.deinit(allocator);
}
pub fn clone(self: Guard, allocator: std.mem.Allocator) std.mem.Allocator.Error!Guard {
var copy: Guard = undefined;
copy.pattern = try self.pattern.clone(allocator);
errdefer copy.pattern.deinit(allocator);
copy.cond = try self.cond.clone(allocator);
return copy;
}
pub fn usesName(self: Guard, name: []const u8) ?bool {
if (self.pattern.usesName(name)) |uses| return uses;
if (self.cond.usesName(name)) return true;
return null;
}
};
pub const Str = struct {
prefix: []const u8,
splits: []const StrSplit,
last_pattern: Pattern,
suffix: []const u8,
pub fn deinit(self: Str, allocator: std.mem.Allocator) void {
allocator.free(self.prefix);
for (self.splits) |split| split.deinit(allocator);
allocator.free(self.splits);
self.last_pattern.deinit(allocator);
allocator.free(self.suffix);
}
pub fn clone(self: Str, allocator: std.mem.Allocator) std.mem.Allocator.Error!Str {
var copy: Str = undefined;
copy.prefix = try allocator.dupe(u8, self.prefix);
errdefer allocator.free(copy.prefix);
const splits = try allocator.alloc(StrSplit, self.splits.len);
var i: usize = 0;
errdefer {
allocator.free(splits);
for (splits[0..i]) |item| item.deinit(allocator);
}
while (i < splits.len) : (i += 1) {
splits[i] = try self.splits[i].clone(allocator);
}
copy.splits = splits;
copy.last_pattern = try self.last_pattern.clone(allocator);
errdefer copy.last_pattern.deinit(allocator);
copy.suffix = try allocator.dupe(u8, self.suffix);
return copy;
}
pub fn usesName(self: Str, name: []const u8) ?bool {
for (self.splits) |split| if (split.usesName(name)) |uses| return uses;
return self.last_pattern.usesName(name);
}
};
pub const StrSplit = struct {
pattern: Pattern,
separator: []const u8,
pub fn deinit(self: StrSplit, allocator: std.mem.Allocator) void {
self.pattern.deinit(allocator);
allocator.free(self.separator);
}
pub fn clone(self: StrSplit, allocator: std.mem.Allocator) std.mem.Allocator.Error!StrSplit {
var copy: StrSplit = undefined;
copy.pattern = try self.pattern.clone(allocator);
errdefer copy.pattern.deinit(allocator);
copy.separator = try allocator.dupe(u8, self.separator);
return copy;
}
pub fn usesName(self: StrSplit, name: []const u8) ?bool {
return self.pattern.usesName(name);
}
};
pub const Pair = struct {
key: Expr,
value: Pattern,
pub fn deinit(self: Pair, allocator: std.mem.Allocator) void {
self.key.deinit(allocator);
self.value.deinit(allocator);
}
pub fn clone(self: Pair, allocator: std.mem.Allocator) std.mem.Allocator.Error!Pair {
var copy: Pair = undefined;
copy.key = try self.key.clone(allocator);
errdefer copy.key.deinit(allocator);
copy.value = try self.value.clone(allocator);
return copy;
}
pub fn usesName(self: Pair, name: []const u8) ?bool {
if (self.key.usesName(name)) return true;
return self.value.usesName(name);
}
};
};