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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
use super::pattern::r#match;
use super::r#macro::MacroSetLike;
use super::token::TokenString;
pub(crate) fn expand_call(name: &str, args: &[TokenString], macros: &impl MacroSetLike) -> String {
match name {
"filter" => {
assert_eq!(args.len(), 2);
text::filter(macros, &args[0], &args[1])
}
"filter-out" => {
assert_eq!(args.len(), 2);
text::filter_out(macros, &args[0], &args[1])
}
"sort" => {
assert_eq!(args.len(), 1);
text::sort(macros, &args[0])
}
"notdir" => {
assert_eq!(args.len(), 1);
file_name::notdir(macros, &args[0])
}
"basename" => {
assert_eq!(args.len(), 1);
file_name::basename(macros, &args[0])
}
"addprefix" => {
assert_eq!(args.len(), 2);
file_name::addprefix(macros, &args[0], &args[1])
}
"wildcard" => {
assert_eq!(args.len(), 1);
file_name::wildcard(macros, &args[0])
}
// foreach
"foreach" => {
assert_eq!(args.len(), 3);
foreach::foreach(macros, &args[0], &args[1], &args[2])
}
// call
"call" => {
assert!(!args.is_empty());
call::call(macros, args.iter())
}
// eval
"eval" => todo!(),
// shell
"shell" => todo!(),
// fallback
_ => panic!("function not implemented: {}", name),
}
}
// Text Functions
mod text {
use super::r#match;
use super::MacroSetLike;
use super::TokenString;
pub(crate) fn filter(
macros: &impl MacroSetLike,
patterns: &TokenString,
text: &TokenString,
) -> String {
let patterns = macros.expand(patterns);
let patterns = patterns.split_whitespace().collect::<Vec<_>>();
let text = macros.expand(text);
let text = text.split_whitespace();
let mut result_pieces = vec![];
for word in text {
if patterns
.iter()
.any(|pattern| r#match(pattern, word).is_some())
{
result_pieces.push(word);
}
}
result_pieces.join(" ")
}
pub(crate) fn filter_out(
macros: &impl MacroSetLike,
patterns: &TokenString,
text: &TokenString,
) -> String {
let patterns = macros.expand(patterns);
let patterns = patterns.split_whitespace().collect::<Vec<_>>();
let text = macros.expand(text);
let text = text.split_whitespace();
let mut result_pieces = vec![];
for word in text {
if patterns
.iter()
.all(|pattern| r#match(pattern, word).is_none())
{
result_pieces.push(word);
}
}
result_pieces.join(" ")
}
pub(crate) fn sort(macros: &impl MacroSetLike, words: &TokenString) -> String {
let words = macros.expand(words);
let mut words = words.split_whitespace().collect::<Vec<_>>();
words.sort_unstable();
words.dedup();
words.join(" ")
}
}
// File Name Functions
mod file_name {
use std::env;
use std::ffi::OsStr;
use std::path::Path;
use super::MacroSetLike;
use super::TokenString;
pub(crate) fn notdir(macros: &impl MacroSetLike, words: &TokenString) -> String {
let words = macros.expand(words);
let words = words
.split_whitespace()
.map(|word| {
Path::new(word)
.file_name()
.and_then(OsStr::to_str)
.unwrap_or("")
})
.collect::<Vec<_>>();
words.join(" ")
}
pub(crate) fn basename(macros: &impl MacroSetLike, words: &TokenString) -> String {
let words = macros.expand(words);
let words = words
.split_whitespace()
.map(|word| {
Path::new(word)
.with_extension("")
.to_str()
.map_or_else(String::new, ToString::to_string)
})
.collect::<Vec<_>>();
words.join(" ")
}
pub(crate) fn addprefix(
macros: &impl MacroSetLike,
prefix: &TokenString,
targets: &TokenString,
) -> String {
let prefix = macros.expand(prefix);
let targets = macros.expand(targets);
let results = targets
.split_whitespace()
.map(|t| format!("{}{}", prefix, t))
.collect::<Vec<_>>();
results.join(" ")
}
pub(crate) fn wildcard(macros: &impl MacroSetLike, pattern: &TokenString) -> String {
let pattern = macros.expand(pattern);
let home_dir = env::var("HOME")
.ok()
.or_else(|| dirs::home_dir().and_then(|p| p.to_str().map(String::from)));
let pattern = if let Some(home_dir) = home_dir {
pattern.replace('~', &home_dir)
} else {
pattern
};
let results = glob::glob(&pattern)
.expect("invalid glob pattern!")
.filter_map(|path| {
path.ok()
.map(|x| x.to_str().map(ToString::to_string).unwrap_or_default())
})
.collect::<Vec<_>>();
results.join(" ")
}
}
// foreach
mod foreach {
use super::MacroSetLike;
use super::TokenString;
pub(crate) fn foreach(
macros: &impl MacroSetLike,
var: &TokenString,
list: &TokenString,
text: &TokenString,
) -> String {
let var = macros.expand(var);
let list = macros.expand(list);
let words = list.split_whitespace();
let mut macros = macros.with_overlay();
let results = words
.map(|word| {
macros.set(var.clone(), TokenString::text(word));
macros.expand(text)
})
.collect::<Vec<_>>();
results.join(" ")
}
}
// call
mod call {
use super::MacroSetLike;
use super::TokenString;
pub(crate) fn call<'a>(
macros: &impl MacroSetLike,
args: impl Iterator<Item = &'a TokenString>,
) -> String {
let args = args.map(|arg| macros.expand(arg)).collect::<Vec<_>>();
let function = args[0].clone();
let mut macros = macros.with_overlay();
for (i, x) in args.into_iter().enumerate() {
macros.set(i.to_string(), TokenString::text(x));
}
macros.expand(&TokenString::r#macro(function))
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::makefile::r#macro::{MacroSet, MacroSource};
fn call(name: &str, args: &[TokenString], macros: &MacroSet) -> String {
super::expand_call(name, args, macros)
}
macro_rules! call {
($func:literal $($arg:literal),+) => {
call($func, &[$(TokenString::text($arg)),+], &MacroSet::new())
};
($func:ident $($arg:literal),+) => {
call(stringify!($func), &[$(TokenString::text($arg)),+], &MacroSet::new())
};
}
#[test]
fn filter() {
let result = call!(filter "word", "this contains a word inside it");
assert_eq!(result, "word");
let result = call!(filter "%.c %.s", "foo.c bar.c baz.s ugh.h");
assert_eq!(result, "foo.c bar.c baz.s");
}
#[test]
fn filter_out() {
let result = call!("filter-out" "main1.o main2.o", "main1.o foo.o main2.o bar.o");
assert_eq!(result, "foo.o bar.o");
}
#[test]
fn sort() {
let result = call!(sort "foo bar lose foo");
assert_eq!(result, "bar foo lose");
}
#[test]
fn notdir() {
let result = call!(notdir "src/foo.c hacks");
assert_eq!(result, "foo.c hacks");
}
#[test]
fn basename() {
let result = call!(basename "src/foo.c src-1.0/bar hacks");
assert_eq!(result, "src/foo src-1.0/bar hacks");
}
#[test]
fn addprefix() {
let result = call!(addprefix "src/", "foo bar");
assert_eq!(result, "src/foo src/bar");
}
#[test]
fn wildcard() {
use std::env::{set_current_dir, set_var};
use std::fs::write;
use std::path::MAIN_SEPARATOR;
let tempdir = tempfile::tempdir().unwrap();
write(tempdir.path().join("foo.c"), "").unwrap();
write(tempdir.path().join("bar.h"), "").unwrap();
write(tempdir.path().join("baz.txt"), "").unwrap();
write(tempdir.path().join("acab.c"), "ACAB").unwrap();
write(tempdir.path().join("based.txt"), "☭").unwrap();
set_current_dir(tempdir.path()).unwrap();
set_var("HOME", tempdir.path().to_str().unwrap());
let sort = |x: String| call("sort", &[TokenString::text(&x)], &MacroSet::new());
assert_eq!(sort(call!(wildcard "*.c")), "acab.c foo.c");
assert_eq!(
sort(call!(wildcard "~/ba?.*")),
format!(
"{0}{1}bar.h {0}{1}baz.txt",
tempdir.path().to_str().unwrap(),
MAIN_SEPARATOR
)
);
}
#[test]
fn foreach() {
let mut macros = MacroSet::new();
macros.set(
"test".to_string(),
MacroSource::File,
"worked for $(item).".parse().unwrap(),
);
assert_eq!(
call(
"foreach",
&[
TokenString::text("item"),
TokenString::text("a b c d"),
TokenString::r#macro("test")
],
¯os,
),
"worked for a. worked for b. worked for c. worked for d."
);
}
#[test]
fn call_test() {
let mut macros = MacroSet::new();
macros.set(
"reverse".to_string(),
MacroSource::File,
"$(2) $(1)".parse().unwrap(),
);
assert_eq!(
call(
"call",
&[
TokenString::text("reverse"),
TokenString::text("a"),
TokenString::text("b")
],
¯os,
),
"b a"
);
}
}
|