summaryrefslogtreecommitdiffstats
path: root/src/recite
diff options
context:
space:
mode:
authorRory Dudley2024-03-23 02:45:54 -0600
committerRory Dudley2024-03-23 02:45:54 -0600
commit5a7718698373d07a29fffcb792acdb81aa7712d7 (patch)
treee0147ced4a484e02295cd6a6f0f6dd2250d381c8 /src/recite
parent37e1ae98dc9309715e9415962f21484a807d2c56 (diff)
downloaddwarvish-5a7718698373d07a29fffcb792acdb81aa7712d7.tar.gz
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 (`)
Notes
Notes: This is a huge commit that changes almost the entire program (main.rs is still the same, except for imports). Ideally, huge sweeping changes like this should not occur on the codebase, but since this is still pre-alpha, I guess this is acceptable. This is far from the end of patch set, however, as there is quite a lot of cleanup that needs to be done. For instance, checking for internal poems and environment variables should get split out to their own functions/macros. There is also some defunct code (that's commented out), that is unlikely to be useful in the future.
Diffstat (limited to 'src/recite')
-rw-r--r--src/recite/parse.rs135
-rw-r--r--src/recite/path.rs42
-rw-r--r--src/recite/ps.rs95
3 files changed, 0 insertions, 272 deletions
diff --git a/src/recite/parse.rs b/src/recite/parse.rs
deleted file mode 100644
index 63b16ff..0000000
--- a/src/recite/parse.rs
+++ /dev/null
@@ -1,135 +0,0 @@
-/// Add a Verse to a Poem
-///
-/// Takes the current word stack, and pushes it onto the stanza. Then, takes
-/// the stanza, meter, and some metadata details to create a Verse. That Verse
-/// then gets added to the Poem.
-///
-/// # Arguments
-/// * `$word` - The word stack, used to hold chars dilineated via whitespace
-/// * `$stanza` - The stanza stack, used to hold words when they are popped off
-/// the word stack
-/// * `$cprev` - Indicates this is the second half of a couplet (normally this
-/// is just Verse::couplet(prev), but we may need to set it in
-/// case of Meter::Read), since this basically just tells the
-/// shell to use `$out` in the `task!` macro
-/// * `$prev` - The previous verse (or none if there is no previous verse)
-/// * `$verses` - The list of verses that make up the poem
-/// * `$meter` - The meter corresponding to the verse
-#[macro_export]
-macro_rules! push {
- ($word:expr, $stanza:expr, $cprev:expr, $prev:expr, $verses:expr, $meter:expr) => {
- // If there are chars on the word stack, push that word onto the stanza
- if !$word.is_empty() {
- $stanza.push($word.iter().collect());
- }
-
- // Check if the last verse was a meter of Read, Write, or
- // Addendum, and throw an error if it is
- if Verse::rw($prev) && $stanza.is_empty() {
- let rw = match $prev.unwrap().meter {
- Meter::Read => "read",
- Meter::Write => "write",
- Meter::Addendum => "append",
- _ => "",
- };
- eprintln!("dwvsh: parse error: no {} file(s) specified", rw);
- return None;
- }
-
- // A meter indicates the end of a verse
- if !$stanza.is_empty() {
- $verses.push(Verse::new(
- Stanza::new($stanza.clone()),
- $meter,
- $cprev,
- Verse::rw($prev),
- ));
- }
-
- // Clear the stacks
- $stanza.clear();
- $word.clear();
- };
-}
-
-/// Add a Verse to a Poem, but allow looking ahead by one char
-///
-/// This works the exact same as [[push]], except that it doesn't take
-/// `$cprev` (since there is no need to set it manually right now), and it
-/// takes `$ahead`, which is the next character to look for in the pattern,
-/// along with `$aheadm`, which is the corresponding Meter if that `$ahead` is
-/// found.
-///
-/// # Examples
-/// ```
-/// push1!(word, stanza, prev, verses, Meter::Quiet, '&', Meter::And);
-/// push1!(word, stanza, prev, verses, Meter::Write, '>', Meter::Addendum);
-/// ```
-#[macro_export]
-macro_rules! push1 {
- ($word:expr, $stanza:expr, $chars:expr, $prev:expr, $verses: expr, $meter:expr, $ahead:expr, $aheadm:expr) => {
- // If there are chars on the word stack, push that word
- // onto the stanza
- if !$word.is_empty() {
- $stanza.push($word.iter().collect());
- }
-
- // Check if the last verse was a meter of Read, Write, or
- // Addendum, and throw an error if it is
- if Verse::rw($prev) && $stanza.is_empty() {
- let rw = match $prev.unwrap().meter {
- Meter::Read => "read",
- Meter::Write => "write",
- Meter::Addendum => "append",
- _ => "",
- };
- eprintln!("dwvsh: parse error: no {} file(s) specified", rw);
- return None;
- }
-
- // Need to peek at the next character, since '>' can mean
- // Meter::Write, or '>>' can mean Meter::Addendum
- match $chars.clone().peekable().peek() {
- // Indicated Meter::Addendum
- Some(c) if c == &$ahead => {
- // Pop the next character from the input string
- // (since we know what it is)
- $chars.next();
-
- // A meter indicates the end of a verse
- $verses.push(Verse::new(
- Stanza::new($stanza.clone()),
- $aheadm,
- Verse::couplet($prev),
- Verse::rw($prev),
- ));
- }
-
- // Indicates Meter::Write
- Some(_) => {
- // A meter indicates the end of a verse
- $verses.push(Verse::new(
- Stanza::new($stanza.clone()),
- $meter,
- Verse::couplet($prev),
- Verse::rw($prev),
- ));
- }
-
- // Indicated the end of the input
- None => {
- // A meter indicates the end of a verse
- $verses.push(Verse::new(
- Stanza::new($stanza.clone()),
- $meter,
- Verse::couplet($prev),
- Verse::rw($prev),
- ));
- }
- }
-
- // Clear the stacks
- $stanza.clear();
- $word.clear();
- };
-}
diff --git a/src/recite/path.rs b/src/recite/path.rs
deleted file mode 100644
index 28eb45b..0000000
--- a/src/recite/path.rs
+++ /dev/null
@@ -1,42 +0,0 @@
-use std::fs;
-use std::path::Path;
-
-/// Refresh the shell's $PATH
-///
-/// This function caches all valid paths within within the directories
-/// specified.
-///
-/// # Arguments
-/// * `paths` - A reference to a vector that holds a list to the shell $PATHs
-///
-/// # Returns
-/// * `bins: Vec<String>` - A new cache of all valid file paths in $PATH
-///
-/// # Examples
-/// ```
-/// let path = vec!["/bin"];
-/// let path = path.into_iter().map(Path::new).collect();
-/// let mut bins = prefresh(&path);
-/// ...
-/// // A situation occurs where the $PATH needs to be refreshed
-/// bins = prefresh(&path)
-/// ```
-pub fn prefresh(path: &Vec<&Path>) -> Vec<String> {
- let mut bins: Vec<String> = Vec::new();
-
- for p in path {
- let files = fs::read_dir(p).expect(
- format!(
- "dwvsh: error: unable to read the contents of {}",
- p.display().to_string()
- )
- .as_str(),
- );
-
- for file in files {
- bins.push(file.unwrap().path().display().to_string());
- }
- }
-
- bins
-}
diff --git a/src/recite/ps.rs b/src/recite/ps.rs
deleted file mode 100644
index 0738057..0000000
--- a/src/recite/ps.rs
+++ /dev/null
@@ -1,95 +0,0 @@
-/// Fork into a process from a Verse
-///
-/// Figures out whether or not the given Verse is a couplet. If it is, fork
-/// into a process, and pipe the contents of out `out` into STDIN. If not, then
-/// simply fork into the process.
-///
-/// # Arguments
-/// * `$verse: &Verse` - The verse to fork into
-/// * `$out: &mut String` - If the $verse is a couplet, the contents of STDOUT from the last verse
-#[macro_export]
-macro_rules! task {
- ($verse:expr, $out:expr) => {
- if $verse.couplet {
- let mut child = Command::new($verse.verb())
- .args($verse.clause())
- .stdin(Stdio::piped())
- .spawn()?;
-
- let stdin = child.stdin.as_mut().ok_or(io::ErrorKind::BrokenPipe)?;
- stdin.write_all(&$out.as_bytes())?;
- $out.clear();
-
- child
- } else {
- Command::new($verse.verb()).args($verse.clause()).spawn()?
- }
- };
-}
-
-/// Fork into a process from a Verse, and capture STDOUT
-///
-/// Figures out whether or not the given Verse is a couplet. If it is, fork
-/// into a process, and pipe the contents of out `out` into STDIN. If not, then
-/// simply fork into the process. Additionally, this function will capture
-/// STDOUT of the process specified by the Verse, and store it in `out`.
-///
-/// # Arguments
-/// * `$verse: &Verse` - The verse to fork into
-/// * `$out: &mut String` - If the $verse is a couplet, the contents of STDOUT from the last verse
-#[macro_export]
-macro_rules! ctask {
- ($verse:expr, $out:expr) => {
- if $verse.couplet {
- let mut child = Command::new($verse.verb())
- .args($verse.clause())
- .stdin(Stdio::piped())
- .stdout(Stdio::piped())
- .spawn()?;
-
- let stdin = child.stdin.as_mut().ok_or(io::ErrorKind::BrokenPipe)?;
- stdin.write_all(&$out.as_bytes())?;
- $out.clear();
-
- child
- } else {
- Command::new($verse.verb())
- .args($verse.clause())
- .stdout(Stdio::piped())
- .spawn()?
- }
- };
-}
-
-#[macro_export]
-/// Fork into a background process from a Verse
-///
-/// Figures out whether or not the given Verse is a couplet. If it is, fork
-/// into a backgournd process, and pipe the contents of out `out` into STDIN.
-/// If not, then simply fork into the background process.
-///
-/// # Arguments
-/// * `$verse: &Verse` - The verse to fork into
-/// * `$out: &mut String` - If the $verse is a couplet, the contents of STDOUT from the last verse
-macro_rules! btask {
- ($verse:expr, $out:expr) => {
- if $verse.couplet {
- let mut child = Command::new($verse.verb())
- .args($verse.clause())
- .stdin(Stdio::piped())
- .process_group(0)
- .spawn()?;
-
- let stdin = child.stdin.as_mut().ok_or(io::ErrorKind::BrokenPipe)?;
- stdin.write_all(&$out.as_bytes())?;
- $out.clear();
-
- child
- } else {
- Command::new($verse.verb())
- .args($verse.clause())
- .process_group(0)
- .spawn()?
- }
- };
-}