From 1415c8f9b89699000ef8d864ff8f0e1bebca4a5f Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Thu, 4 Apr 2024 22:12:14 -0600 Subject: Handle aliases in read() Instead of handling aliases in the recite() function, which requires two loops to handle properly with the current implementation, offload checking for aliases to the read() function. --- src/poem/read/parse.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/poem/read/parse.rs') diff --git a/src/poem/read/parse.rs b/src/poem/read/parse.rs index 0836eea..70367c4 100644 --- a/src/poem/read/parse.rs +++ b/src/poem/read/parse.rs @@ -63,7 +63,7 @@ macro_rules! remark { /// Same as the [string!] macro, but don't `continue` #[macro_export] macro_rules! poem { - ($chars:expr, $j:expr, $i:expr, $c:expr, $verse:expr, $word:expr) => { + ($chars:expr, $j:expr, $i:expr, $c:expr, $verse:expr, $word:expr, $env:expr) => { let token = $c; let mut poetry = Word::new(); loop { @@ -76,7 +76,7 @@ macro_rules! poem { } } } - let sp = Poem::read(poetry.iter().collect()); + let sp = Poem::read(poetry.iter().collect(), $env); let sp = match sp { Ok(sp) => sp, Err(e) => return Err(e), @@ -85,3 +85,31 @@ macro_rules! poem { $word.push('\x0b'); }; } + +/// Append a verse to the poem +/// +/// Append a verse to poem, first checking for aliases in the environment, and +/// processing the alias first, if necessary. +#[macro_export] +macro_rules! append { + ($poem:expr, $last:expr, $meter:expr, $verse:expr, $env:expr) => { + if !$verse.is_empty() { + match $env.aliases.get(&$verse.verb()) { + Some(alias) => { + let alias = alias.to_string(); + let mut poem = Poem::read(alias, $env)?; + let len = poem.len(); + for (i, verse) in poem.iter_mut().enumerate() { + if $verse.clause().is_some() && i + 1 == len { + verse.stanza.append(&mut $verse.clause().unwrap()); + } + $poem.push(verse.clone()); + } + } + None => { + $poem.add(&mut $verse, $last, $meter); + } + } + } + }; +} -- cgit v1.2.3