summaryrefslogtreecommitdiffstats
path: root/src/poem/read.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/poem/read.rs')
-rw-r--r--src/poem/read.rs124
1 files changed, 90 insertions, 34 deletions
diff --git a/src/poem/read.rs b/src/poem/read.rs
index 35ca2e1..da13f76 100644
--- a/src/poem/read.rs
+++ b/src/poem/read.rs
@@ -42,13 +42,8 @@ impl fmt::Display for Mishap {
/// A [Poem] can add more [Verse]s to itself
trait Appendable {
type Type;
- fn add(
- &mut self,
- verse: &mut Self::Type,
- meter: Rune,
- last: Rune,
- env: &Environment,
- ) -> Result<(), Mishap>;
+ fn add(&mut self, verse: &mut Self::Type, meter: Rune, env: &Environment)
+ -> Result<(), Mishap>;
}
impl Appendable for Poem {
@@ -61,7 +56,6 @@ impl Appendable for Poem {
fn add(
&mut self,
verse: &mut Self::Type,
- last: Rune,
meter: Rune,
env: &Environment,
) -> Result<(), Mishap> {
@@ -69,10 +63,20 @@ impl Appendable for Poem {
return Ok(());
}
+ // Get meter of the last verse
+ let last = match self.last() {
+ Some(last) => last.meter,
+ None => Rune::Else,
+ };
+
// Check the meter
verse.meter = meter;
- if last == Rune::Couplet || meter == Rune::Couplet {
- verse.couplet = true;
+ if last == Rune::Couplet && meter == Rune::Couplet {
+ verse.couplet = 3;
+ } else if last == Rune::Couplet {
+ verse.couplet = 2;
+ } else if meter == Rune::Couplet {
+ verse.couplet = 1;
}
// Check for aliases
@@ -88,10 +92,10 @@ impl Appendable for Poem {
};
// The last verse inherits the traits from the original
- if verse.couplet {
+ if verse.couplet > 0 {
lv.couplet = verse.couplet;
}
- lv.io = verse.io;
+ lv.io = verse.io.clone();
lv.poems = verse.poems.clone();
lv.meter = verse.meter;
if verse.clause().is_some() {
@@ -147,6 +151,9 @@ impl Readable for Poem {
// Keep track of the last rune
let mut last = Rune::None;
+ // Keep track of the channel
+ let mut channel: Option<Rune> = None;
+
// Keep track of the line
let mut j = 0;
@@ -167,7 +174,7 @@ impl Readable for Poem {
// If c is none, it indicates the end of a poem, so wrap up and
// then break from the loop
- verse.add(&mut word);
+ verse.add(&mut word, channel);
// Throw an error if the verse is empty
if verse.is_empty() && (last == Rune::Couplet || last == Rune::And) {
@@ -175,7 +182,7 @@ impl Readable for Poem {
}
// Push the verse and break
- poem.add(&mut verse, last, Rune::None, env)?;
+ poem.add(&mut verse, Rune::None, env)?;
// append!(poem, last, Rune::None, verse, env);
break;
}
@@ -188,13 +195,31 @@ impl Readable for Poem {
'#' => Rune::Remark,
'\'' | '"' => Rune::String,
'`' => Rune::Poem,
- '<' => {
- verse.couplet = true;
- Rune::Read
- }
- '>' => next(&mut chars, &mut i, Rune::Write, vec![('>', Rune::Addendum)]),
+ '<' => Rune::Read,
+ '>' => next(&mut chars, &mut i, Rune::Write, vec![(">", Rune::Addendum)]),
+ '1' => next(
+ &mut chars,
+ &mut i,
+ Rune::Else,
+ vec![(">", Rune::Write), (">>", Rune::Addendum)],
+ ),
+ '2' => next(
+ &mut chars,
+ &mut i,
+ Rune::Else,
+ vec![(">", Rune::Write2), (">>", Rune::Addendum2)],
+ ),
'|' => Rune::Couplet,
- '&' => next(&mut chars, &mut i, Rune::Quiet, vec![('&', Rune::And)]),
+ '&' => next(
+ &mut chars,
+ &mut i,
+ Rune::Quiet,
+ vec![
+ ("&", Rune::And),
+ (">", Rune::WriteAll),
+ (">>", Rune::AddendumAll),
+ ],
+ ),
';' => Rune::Continue,
'\n' => {
j += 1;
@@ -212,13 +237,21 @@ impl Readable for Poem {
| Rune::And
| Rune::Read
| Rune::Write
- | Rune::Addendum => {
+ | Rune::Write2
+ | Rune::WriteAll
+ | Rune::Addendum
+ | Rune::Addendum2
+ | Rune::AddendumAll => {
if (last == Rune::Couplet
|| last == Rune::Quiet
|| last == Rune::And
|| last == Rune::Read
|| last == Rune::Write
- || last == Rune::Addendum)
+ || last == Rune::Write2
+ || last == Rune::WriteAll
+ || last == Rune::Addendum
+ || last == Rune::Addendum2
+ || last == Rune::AddendumAll)
|| verse.is_empty()
{
return Err(Mishap::ParseMishap(j, i, c));
@@ -226,17 +259,34 @@ impl Readable for Poem {
}
Rune::Continue => {
- if last == Rune::Read || last == Rune::Write || last == Rune::Addendum {
+ if last == Rune::Read
+ || last == Rune::Write
+ || last == Rune::Write2
+ || last == Rune::WriteAll
+ || last == Rune::Addendum
+ || last == Rune::Addendum2
+ || last == Rune::AddendumAll
+ {
return Err(Mishap::ParseMishap(j, i, c));
}
}
_ => {
- if (last == Rune::Read || last == Rune::Write || last == Rune::Addendum)
+ if (last == Rune::Read
+ || last == Rune::Write
+ || last == Rune::Write2
+ || last == Rune::WriteAll
+ || last == Rune::Addendum
+ || last == Rune::Addendum2
+ || last == Rune::AddendumAll)
&& rune == Rune::None
&& rune == Rune::Read
&& rune == Rune::Write
+ && rune == Rune::Write2
+ && rune == Rune::WriteAll
&& rune == Rune::Addendum
+ && rune == Rune::Addendum2
+ && rune == Rune::AddendumAll
&& rune == Rune::Couplet
&& rune == Rune::Quiet
&& rune == Rune::And
@@ -251,7 +301,7 @@ impl Readable for Poem {
match rune {
// Indicates the end of a word (space dilineated)
Rune::Pause => {
- verse.add(&mut word);
+ verse.add(&mut word, channel);
}
Rune::Remark => {
@@ -261,7 +311,7 @@ impl Readable for Poem {
// Indicates a string (' or ")
Rune::String => {
string!(chars, j, i, c, word);
- verse.add(&mut word);
+ verse.add(&mut word, channel);
}
// Indicates a sub-poem
@@ -270,17 +320,23 @@ impl Readable for Poem {
}
// Indicates a file operation (<, >, or >>)
- Rune::Read | Rune::Write | Rune::Addendum => {
- verse.add(&mut word);
- word.push('<');
- verse.add(&mut word);
- verse.io = rune;
+ Rune::Read
+ | Rune::Write
+ | Rune::Write2
+ | Rune::WriteAll
+ | Rune::Addendum
+ | Rune::Addendum2
+ | Rune::AddendumAll => {
+ verse.add(&mut word, channel);
+ channel = Some(rune);
+ verse.io.push(rune);
}
// 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, env)?;
+ channel = None;
+ verse.add(&mut word, channel);
+ poem.add(&mut verse, rune, env)?;
// append!(poem, last, rune, verse, env);
}
@@ -290,7 +346,7 @@ impl Readable for Poem {
word.append(&mut chars);
}
- // Any other char i.e. Meter::Else
+ // Any other char i.e. Rune::Else
_ => {
word.push(c);
}