aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-04-05 15:28:37 -0600
committerMelody Horn <melody@boringcactus.com>2021-04-05 15:28:37 -0600
commitd24fa2e2c2309b8e32231364e49edbb5ebd53da4 (patch)
tree40fa6da2a663424ee6f902dcf4b0f509a2350de5 /src
parent726c4eb1f7446d678a5fca1f8ee7ca00fb8fd14d (diff)
downloadmakers-d24fa2e2c2309b8e32231364e49edbb5ebd53da4.tar.gz
makers-d24fa2e2c2309b8e32231364e49edbb5ebd53da4.zip
implement function `word`
Diffstat (limited to 'src')
-rw-r--r--src/makefile/functions.rs13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs
index 712b944..7bb0908 100644
--- a/src/makefile/functions.rs
+++ b/src/makefile/functions.rs
@@ -3,7 +3,7 @@ use std::env;
use std::process::{Command, Stdio};
use std::rc::Rc;
-use eyre::{bail, Result};
+use eyre::{bail, Result, WrapErr};
use super::pattern::r#match;
use super::r#macro::{Set as MacroSet, Source as MacroSource};
@@ -40,6 +40,10 @@ pub fn expand_call(
assert_eq!(args.len(), 1);
text::sort(macros, &args[0])
}
+ "word" => {
+ assert_eq!(args.len(), 2);
+ text::word(macros, &args[0], &args[1])
+ }
"words" => {
assert_eq!(args.len(), 1);
text::words(macros, &args[0])
@@ -211,6 +215,13 @@ mod text {
Ok(words.join(" "))
}
+ pub fn word(macros: &MacroSet, n: &TokenString, text: &TokenString) -> Result<String> {
+ let n = macros.expand(n)?;
+ let n: usize = n.parse().wrap_err("while calling `word`")?;
+ let text = macros.expand(text)?;
+ Ok(text.split_whitespace().nth(n + 1).unwrap_or("").to_owned())
+ }
+
pub fn words(macros: &MacroSet, words: &TokenString) -> Result<String> {
let words = macros.expand(words)?;
Ok(words.split_whitespace().count().to_string())