aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/parse/mod.rs20
-rw-r--r--test-cases/comment/script.sh6
-rw-r--r--test-cases/comment/stdout.txt1
-rw-r--r--tests/scripts.rs5
4 files changed, 31 insertions, 1 deletions
diff --git a/src/parse/mod.rs b/src/parse/mod.rs
index 5815730..b475943 100644
--- a/src/parse/mod.rs
+++ b/src/parse/mod.rs
@@ -1537,8 +1537,26 @@ impl<'a> Cursor<'a> {
matches!(self.buf[0], b' ' | b'\t' | b'\n' | b'\r')
}
+ fn peek_comment(&self) -> bool {
+ self.has() && self.peek() == b'#'
+ }
+
+ fn consume_comment(&mut self) {
+ assert_eq!(self.adv(), b'#');
+ while self.has() && self.peek() != b'\n' {
+ self.adv();
+ }
+ }
+
fn spaces(&mut self) {
- while self.peek_space() {
+ while {
+ if self.peek_comment() {
+ self.consume_comment();
+ true
+ } else {
+ self.peek_space()
+ }
+ } {
self.adv();
self.spaced = true;
}
diff --git a/test-cases/comment/script.sh b/test-cases/comment/script.sh
new file mode 100644
index 0000000..5f51002
--- /dev/null
+++ b/test-cases/comment/script.sh
@@ -0,0 +1,6 @@
+set X = good
+fun mutate {
+ set X = bad
+}
+# if this were not a comment, command interpolations would be executed: $(mutate)
+echo $X
diff --git a/test-cases/comment/stdout.txt b/test-cases/comment/stdout.txt
new file mode 100644
index 0000000..12799cc
--- /dev/null
+++ b/test-cases/comment/stdout.txt
@@ -0,0 +1 @@
+good
diff --git a/tests/scripts.rs b/tests/scripts.rs
index 9503d8c..93034c4 100644
--- a/tests/scripts.rs
+++ b/tests/scripts.rs
@@ -7,6 +7,11 @@ fn case0() {
}
#[test]
+fn comment() {
+ common::test_case("comment", include_bytes!("../test-cases/comment/script.sh"), include_bytes!("../test-cases/comment/stdout.txt"));
+}
+
+#[test]
fn fun0() {
common::test_case("fun0", include_bytes!("../test-cases/fun0/script.sh"), include_bytes!("../test-cases/fun0/stdout.txt"));
}