From d24fa2e2c2309b8e32231364e49edbb5ebd53da4 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Mon, 5 Apr 2021 15:28:37 -0600 Subject: implement function `word` --- src/makefile/functions.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 { + 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 { let words = macros.expand(words)?; Ok(words.split_whitespace().count().to_string()) -- cgit v1.2.3