summaryrefslogtreecommitdiffstats
path: root/src/poem/read/parse.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/read/parse.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/read/parse.rs')
-rw-r--r--src/poem/read/parse.rs68
1 files changed, 68 insertions, 0 deletions
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');
+ };
+}