summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRory Dudley2024-03-28 21:01:49 -0600
committerRory Dudley2024-03-28 21:01:49 -0600
commit527cc04ffb9ea627ca678fe8fb07fe9330420eab (patch)
tree823bdf5d034def38e07c5299a8758ef616bc488a
parent9b2fcdb6bb6598786827e9f211df3db09de54567 (diff)
downloaddwarvish-527cc04ffb9ea627ca678fe8fb07fe9330420eab.tar.gz
Add comments (`#`) to the parser
The parser will now interpret the '#' character as a single-line comment string, which works on it's own line, or at the end of an existing line.
-rw-r--r--src/poem/elements/rune.rs4
-rw-r--r--src/poem/read.rs7
-rw-r--r--src/poem/read/parse.rs15
3 files changed, 24 insertions, 2 deletions
diff --git a/src/poem/elements/rune.rs b/src/poem/elements/rune.rs
index 3f53c67..1742778 100644
--- a/src/poem/elements/rune.rs
+++ b/src/poem/elements/rune.rs
@@ -25,7 +25,7 @@ use std::sync::{Arc, Mutex};
/// * `None` - A shell command with no additional actions (the end of a poem)
/// * `Pause` - The space character, to dilineate words (` `)
/// * `Path` - The forward slash character, to dilineate paths (`/`)
-/// * `Env` - Indicates an environment variable (`$`)
+/// * `Remark` - Indicates a single line comment (`#`)
/// * `String` - Interpret all character as one large
/// [Word][super::word::Word] (`'` or `"`)
/// * `Poem` - A subcommand to run first (`\``)
@@ -43,6 +43,7 @@ pub enum Rune {
None, // No meter (the end of a poem)
Pause, // A space
Path, // A forward slash
+ Remark, // A comment
String, // Interpret the following as one large [Word]
Poem, // Run a sub-poem before the main one
Read, // Read files into STDIN
@@ -65,6 +66,7 @@ impl fmt::Display for Rune {
Rune::None => "",
Rune::Pause => " ",
Rune::Path => "/",
+ Rune::Remark => "#",
Rune::String => "\"",
Rune::Poem => "`",
Rune::Read => "<",
diff --git a/src/poem/read.rs b/src/poem/read.rs
index 0956700..d88cf0d 100644
--- a/src/poem/read.rs
+++ b/src/poem/read.rs
@@ -4,7 +4,7 @@ use super::{
};
use core::fmt;
mod parse;
-use crate::{next, poem, string};
+use crate::{next, poem, remark, string};
#[derive(Debug, PartialEq, Eq)]
pub enum Mishap {
@@ -127,6 +127,7 @@ impl Readable for Poem {
let rune = match c {
' ' => Rune::Pause,
'/' => Rune::Path,
+ '#' => Rune::Remark,
'\'' | '"' => Rune::String,
'`' => Rune::Poem,
'<' => {
@@ -195,6 +196,10 @@ impl Readable for Poem {
verse.add(&mut word);
}
+ Rune::Remark => {
+ remark!(chars);
+ }
+
// Indicates a string (' or ")
Rune::String => {
string!(chars, j, i, c, word);
diff --git a/src/poem/read/parse.rs b/src/poem/read/parse.rs
index c4d59e6..c1dec44 100644
--- a/src/poem/read/parse.rs
+++ b/src/poem/read/parse.rs
@@ -41,6 +41,21 @@ macro_rules! string {
};
}
+/// Same as [string!] macro, but look for newline or EOF
+#[macro_export]
+macro_rules! remark {
+ ($chars:expr) => {
+ loop {
+ match $chars.next() {
+ None => break,
+ Some(c) if c == '\n' => break,
+ Some(_) => {}
+ }
+ }
+ continue;
+ };
+}
+
/// Same as the [string!] macro, but don't `continue`
#[macro_export]
macro_rules! poem {