From 789bea635f6b566af6d2793fee484ce129c33caa Mon Sep 17 00:00:00 2001 From: Jonas Maier Date: Sun, 31 May 2026 19:36:01 +0200 Subject: comment parsing --- src/parse/mod.rs | 20 +++++++++++++++++++- test-cases/comment/script.sh | 6 ++++++ test-cases/comment/stdout.txt | 1 + tests/scripts.rs | 5 +++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test-cases/comment/script.sh create mode 100644 test-cases/comment/stdout.txt 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 @@ -6,6 +6,11 @@ fn case0() { common::test_case("case0", include_bytes!("../test-cases/case0/script.sh"), include_bytes!("../test-cases/case0/stdout.txt")); } +#[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")); -- cgit v1.2.3