summaryrefslogtreecommitdiffstats
path: root/src/poem.rs
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/poem.rs
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/poem.rs')
-rw-r--r--src/poem.rs284
1 files changed, 284 insertions, 0 deletions
diff --git a/src/poem.rs b/src/poem.rs
new file mode 100644
index 0000000..c0e7d6b
--- /dev/null
+++ b/src/poem.rs
@@ -0,0 +1,284 @@
+mod elements;
+use elements::verse::Verse;
+pub mod read;
+pub mod recite;
+
+/// Parse and run shell commands or `dwvsh` files
+///
+/// A [Poem] is the structure that contains a full shell command/program. It
+/// may be composed of one or many [Verse]'s.
+pub type Poem = Vec<Verse>;
+
+#[cfg(test)]
+mod tests {
+ use super::elements::rune::Rune;
+ use super::read::Readable;
+ use super::*;
+
+ #[test]
+ fn it_parses_a_verse_with_no_meter() {
+ let poem = Poem::read("cargo build --release".to_string());
+ assert!(poem.is_ok());
+ let poem = poem.unwrap();
+ assert_eq!(poem.first().unwrap().verb(), "cargo");
+ }
+
+ #[test]
+ fn it_parses_a_verse_with_the_couplet_meter() {
+ let poem = Poem::read("ls -la | lolcat".to_string());
+ assert!(poem.is_ok());
+ let poem = poem.unwrap();
+ assert_eq!(poem.first().unwrap().verb(), "ls");
+ assert_eq!(poem.first().unwrap().meter, Rune::Couplet);
+ }
+
+ #[test]
+ fn it_parses_a_verse_with_the_quiet_meter() {
+ let poem = Poem::read("sleep 20 &".to_string());
+ assert!(poem.is_ok());
+ let poem = poem.unwrap();
+ assert_eq!(poem.first().unwrap().verb(), "sleep");
+ assert_eq!(poem.first().unwrap().meter, Rune::Quiet);
+ }
+
+ #[test]
+ fn it_parses_a_verse_with_the_and_meter() {
+ let poem = Poem::read("sleep 2 && ls -la".to_string());
+ assert!(poem.is_ok());
+ let poem = poem.unwrap();
+ assert_eq!(poem.first().unwrap().verb(), "sleep");
+ assert_eq!(poem.first().unwrap().meter, Rune::And);
+ }
+
+ #[test]
+ fn it_parses_a_verse_with_the_continue_meter() {
+ let poem = Poem::read("sleep 2; ls -la".to_string());
+ assert!(poem.is_ok());
+ let poem = poem.unwrap();
+ assert_eq!(poem.first().unwrap().verb(), "sleep");
+ assert_eq!(poem.first().unwrap().meter, Rune::Continue);
+ }
+
+ // #[test]
+ // fn it_parses_verse_with_the_read_meter() {
+ // let poem = Poem::read("lolcat < src/main.rs".to_string());
+ // assert!(poem.is_ok());
+ // let mut verses = poem.unwrap().into_iter();
+
+ // let verse = verses.next().unwrap();
+ // assert_eq!(verse.verb(), "lolcat");
+ // assert_eq!(verse.meter, Rune::Read);
+
+ // let verse = verses.next().unwrap();
+ // assert_eq!(verse.stanza, vec!["src/main.rs".to_string()]);
+ // }
+
+ // #[test]
+ // fn it_parses_verse_with_the_write_meter() {
+ // let poem = Poem::read("cat src/main.rs > /dev/null".to_string());
+ // assert!(poem.is_ok());
+ // let mut verses = poem.unwrap().into_iter();
+
+ // let verse = verses.next().unwrap();
+ // assert_eq!(verse.verb(), "cat");
+ // assert_eq!(verse.clause().unwrap(), vec!["src/main.rs".to_string()]);
+ // assert_eq!(verse.meter, Rune::Write);
+
+ // let verse = verses.next().unwrap();
+ // assert_eq!(verse.stanza, vec!["/dev/null".to_string()]);
+ // }
+
+ // #[test]
+ // fn it_parses_verse_with_the_addenum_meter() {
+ // let poem = Poem::read("cat src/main.rs >> /dev/null".to_string());
+ // assert!(poem.is_ok());
+ // let mut verses = poem.unwrap().into_iter();
+
+ // let verse = verses.next().unwrap();
+ // assert_eq!(verse.verb(), "cat");
+ // assert_eq!(verse.clause().unwrap(), vec!["src/main.rs".to_string()]);
+ // assert_eq!(verse.meter, Rune::Addendum);
+
+ // let verse = verses.next().unwrap();
+ // assert_eq!(verse.stanza, vec!["/dev/null".to_string()]);
+ // }
+
+ #[test]
+ fn it_throws_a_parse_error_if_no_files_are_specified_for_the_read_meter() {
+ let poem = Poem::read("lolcat <".to_string());
+ assert!(poem.is_err());
+ let poem = Poem::read("lolcat <;".to_string());
+ assert!(poem.is_err());
+ let poem = Poem::read("lolcat < && ls -la".to_string());
+ assert!(poem.is_err());
+ }
+
+ #[test]
+ fn it_throws_a_parse_error_if_no_files_are_specified_for_the_write_meter() {
+ let poem = Poem::read("cat src/main.rs >".to_string());
+ assert!(poem.is_err());
+ let poem = Poem::read("cat src/main.rs >;".to_string());
+ assert!(poem.is_err());
+ let poem = Poem::read("cat > && ls -la".to_string());
+ assert!(poem.is_err());
+ }
+
+ #[test]
+ fn it_throws_a_parse_error_if_no_files_are_specified_for_the_addendum_meter() {
+ let poem = Poem::read("cat src/main.rs >>".to_string());
+ assert!(poem.is_err());
+ let poem = Poem::read("cat src/main.rs >>;".to_string());
+ assert!(poem.is_err());
+ let poem = Poem::read("cat >> && ls -la".to_string());
+ assert!(poem.is_err());
+ }
+
+ #[test]
+ fn it_parses_a_complex_verse_with_lots_of_different_meters() {
+ let poem = Poem::read("ls -la | lolcat && echo hello | lolcat && sleep 2 &".to_string());
+ assert!(poem.is_ok());
+ let mut verses = poem.unwrap().into_iter();
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "ls");
+ assert_eq!(verse.clause().unwrap(), vec!["-la".to_string()]);
+ assert_eq!(verse.meter, Rune::Couplet);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "lolcat");
+ assert_eq!(verse.meter, Rune::And);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "echo");
+ assert_eq!(verse.clause().unwrap(), vec!["hello".to_string()]);
+ assert_eq!(verse.meter, Rune::Couplet);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "lolcat");
+ assert_eq!(verse.meter, Rune::And);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "sleep");
+ assert_eq!(verse.clause().unwrap(), vec!["2".to_string()]);
+ assert_eq!(verse.meter, Rune::Quiet);
+ }
+
+ #[test]
+ fn it_parses_the_continue_meter_without_a_stanza() {
+ let poem = Poem::read(";;;;;;;".to_string());
+ assert!(poem.is_ok());
+ }
+
+ #[test]
+ fn it_errors_if_the_couplet_meter_is_used_without_a_stanza() {
+ let poem = Poem::read("|".to_string());
+ assert!(poem.is_err());
+ }
+
+ #[test]
+ fn it_errors_if_the_quiet_meter_is_used_without_a_stanza() {
+ let poem = Poem::read("&".to_string());
+ assert!(poem.is_err());
+ }
+
+ #[test]
+ fn it_errors_if_the_and_meter_is_used_without_a_stanza() {
+ let poem = Poem::read("&&".to_string());
+ assert!(poem.is_err());
+ }
+
+ #[test]
+ fn it_parses_a_file() {
+ let file = r"
+ ps aux | lolcat
+ sleep 2
+ ";
+
+ let poem = Poem::read(file.to_string());
+ assert!(poem.is_ok());
+
+ let poem = poem.unwrap();
+ assert_eq!(poem.len(), 3);
+
+ let mut verses = poem.into_iter();
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "ps");
+ assert_eq!(verse.clause().unwrap(), vec!["aux".to_string()]);
+ assert_eq!(verse.meter, Rune::Couplet);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "lolcat");
+ assert_eq!(verse.meter, Rune::Continue);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "sleep");
+ assert_eq!(verse.clause().unwrap(), vec!["2".to_string()]);
+ assert_eq!(verse.meter, Rune::Continue);
+ }
+
+ #[test]
+ fn it_parses_a_longer_file() {
+ let file = r"
+ ps aux | lolcat
+ sleep 2
+ ps aux | lolcat
+ sleep 2
+
+ echo hello there
+ export PATH=$PATH:~/.local/bin
+
+ ps aux | lolcat && lolcat src/main.rs
+ fortune | cowsay | lolcat
+
+ wc -l src/**/*.rs | lolcat; ls -la | grep git
+ ";
+
+ let poem = Poem::read(file.to_string());
+ assert!(poem.is_ok());
+
+ let poem = poem.unwrap();
+ assert_eq!(poem.len(), 18);
+ }
+
+ #[test]
+ fn it_catches_parser_errors_related_to_invalid_use_of_special_runes() {
+ let poetry = "cat file.txt &&&".to_string();
+ assert_eq!(Poem::read(poetry).is_err(), true);
+
+ let poetry = "cat file.txt&&|".to_string();
+ assert_eq!(Poem::read(poetry).is_err(), true);
+
+ let poetry = "cat <".to_string();
+ assert_eq!(Poem::read(poetry).is_err(), true);
+ }
+
+ #[test]
+ fn it_catches_parser_errors_related_to_strings() {
+ let poetry = "echo 'hello".to_string();
+ assert_eq!(Poem::read(poetry).is_err(), true);
+
+ let poetry = "echo \"hello".to_string();
+ assert_eq!(Poem::read(poetry).is_err(), true);
+
+ let poetry = "`true".to_string();
+ assert_eq!(Poem::read(poetry).is_err(), true);
+ }
+
+ #[test]
+ fn it_interprets_tilda_as_home() {
+ let poetry = "cd ~".to_string();
+ let poem = Poem::read(poetry).unwrap();
+ assert_eq!(poem[0].verb(), "cd");
+ assert_eq!(poem[0].clause(), Some(vec!["/home/rory".to_string()]));
+
+ let poetry = "cd ~/Code/dwarvish".to_string();
+ let poem = Poem::read(poetry).unwrap();
+ assert_eq!(poem[0].verb(), "cd");
+ assert_eq!(
+ poem[0].clause(),
+ Some(vec!["/home/rory/Code/dwarvish".to_string()])
+ );
+ assert_eq!(poem[0].meter, Rune::None);
+ }
+}