diff options
author | Rory Dudley | 2024-04-04 22:12:14 -0600 |
---|---|---|
committer | Rory Dudley | 2024-04-04 22:12:14 -0600 |
commit | 1415c8f9b89699000ef8d864ff8f0e1bebca4a5f (patch) | |
tree | 64093c0eded6a6f28105dbd743729b4075d32889 /src/poem/read.rs | |
parent | badbba41476cd6fd424042c48681acde82227ba6 (diff) | |
download | dwarvish-1415c8f9b89699000ef8d864ff8f0e1bebca4a5f.tar.gz |
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.
Diffstat (limited to 'src/poem/read.rs')
-rw-r--r-- | src/poem/read.rs | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/src/poem/read.rs b/src/poem/read.rs index d88cf0d..0af7fec 100644 --- a/src/poem/read.rs +++ b/src/poem/read.rs @@ -4,7 +4,8 @@ use super::{ }; use core::fmt; mod parse; -use crate::{next, poem, remark, string}; +use crate::compose::Environment; +use crate::{append, next, poem, remark, string}; #[derive(Debug, PartialEq, Eq)] pub enum Mishap { @@ -51,20 +52,25 @@ impl Appendable for Poem { /// Push a [Verse] to the [Poem] after checking that the [Verse] is not /// empty. Also sets the meter of the [Verse]. fn add(&mut self, verse: &mut Self::Type, last: Rune, meter: Rune) { - if !verse.is_empty() { - verse.meter = meter; - if last == Rune::Couplet || meter == Rune::Couplet { - verse.couplet = true; - } - self.push(verse.clone()); - verse.clear(); + if verse.is_empty() { + return; + } + + // Check the meter + verse.meter = meter; + if last == Rune::Couplet || meter == Rune::Couplet { + verse.couplet = true; } + + // Push verse(s) and clear the current verse stack + self.push(verse.clone()); + verse.clear(); } } /// A [Poem] can parse poetry pub trait Readable { - fn read(poetry: String) -> Result<Poem, Mishap>; + fn read(poetry: String, env: &Environment) -> Result<Poem, Mishap>; } impl Readable for Poem { @@ -74,7 +80,7 @@ impl Readable for Poem { /// machine-runnable [Poem]. If there is a parse error, [Poem::read] may /// return a [Mishap]. See [Poem::recite][super::recite] for how each /// [Verse] in a [Poem] is called. - fn read(poetry: String) -> Result<Poem, Mishap> { + fn read(poetry: String, env: &Environment) -> Result<Poem, Mishap> { // Get all the characters in the input string as an iterator let mut chars = poetry.chars().into_iter(); @@ -118,7 +124,8 @@ impl Readable for Poem { } // Push the verse and break - poem.add(&mut verse, last, Rune::None); + // poem.add(&mut verse, last, Rune::None); + append!(poem, last, Rune::None, verse, env); break; } }; @@ -207,7 +214,7 @@ impl Readable for Poem { // Indicates a sub-poem Rune::Poem => { - poem!(chars, j, i, c, verse, word); + poem!(chars, j, i, c, verse, word, env); } // Indicates a file operation (<, >, or >>) @@ -221,7 +228,8 @@ impl Readable for Poem { // These meters indicate the end of a verse Rune::Couplet | Rune::Quiet | Rune::And | Rune::Continue => { verse.add(&mut word); - poem.add(&mut verse, last, rune); + // poem.add(&mut verse, last, rune); + append!(poem, last, rune, verse, env); } // Interpret ~ as $HOME |