From 5a7718698373d07a29fffcb792acdb81aa7712d7 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Sat, 23 Mar 2024 02:45:54 -0600 Subject: read() and recite() overhaul Rebuilt the LR parser (i.e. read()) from the ground up. This required that some changes be made to recite(), in order to accomodate the new data structures. These data structures were each split out into their own file, in order to make working with each component a bit easier. In addition to reworking the parts of the parser already present, some new features were also added, such as: - Support for strings (' and ") - Support for environment variables ($) - Support for interpreting tild as $HOME (~) - Support for sub-reading and sub-reciting (`) --- src/poem/read/parse.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/poem/read/parse.rs (limited to 'src/poem/read/parse.rs') diff --git a/src/poem/read/parse.rs b/src/poem/read/parse.rs new file mode 100644 index 0000000..c4d59e6 --- /dev/null +++ b/src/poem/read/parse.rs @@ -0,0 +1,68 @@ +/// Look ahead one character in the input +/// +/// May need to look ahead one character in the input string to determine the +/// proper rune. For instance `&`, vs `&&`. +#[macro_export] +macro_rules! next { + ($chars:expr, $i:expr, $otherwise:expr, $rune:expr, $ahead:expr) => { + match $chars.clone().peekable().peek() { + Some(c) if *c == $ahead => { + $chars.next(); + $i += 1; + $rune + } + Some(_) => $otherwise, + None => $otherwise, + } + }; +} + +/// Keep pushing to the [Word][super::super::elements::word::Word] stack +/// +/// If a [Rune::String][super::super::elements::rune::Rune] character is found, +/// stop interpreting special characters, and push all characters to the +/// [Word][super::super::elements::word::Word] stack, until the corresponding +/// [Rune::String][super::super::elements::rune::Rune] character is found. +#[macro_export] +macro_rules! string { + ($chars:expr, $j:expr, $i:expr, $c:expr, $word:expr) => { + let token = $c; + loop { + match $chars.next() { + None => return Err(Mishap::PartialMishap($j, $i, $c)), + Some(c) if c == token => break, + Some(c) => { + $word.push(c); + $i += 1; + } + } + } + continue; + }; +} + +/// 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) => { + let token = $c; + let mut poetry = Word::new(); + loop { + match $chars.next() { + None => return Err(Mishap::PartialMishap($j, $i, $c)), + Some(c) if c == token => break, + Some(c) => { + poetry.push(c); + $i += 1; + } + } + } + let sp = Poem::read(poetry.iter().collect()); + let sp = match sp { + Ok(sp) => sp, + Err(e) => return Err(e), + }; + $verse.poems.push(sp); + $word.push('\x0b'); + }; +} -- cgit v1.2.3