diff options
| author | Jonas Maier <jonas@x77.dev> | 2026-05-31 19:36:01 +0200 |
|---|---|---|
| committer | Jonas Maier <jonas@x77.dev> | 2026-05-31 19:36:01 +0200 |
| commit | 789bea635f6b566af6d2793fee484ce129c33caa (patch) | |
| tree | 585f323b909f90fade205f61bdbd36f9d8c16c40 | |
| parent | 81759dd51eb1f6f9a7dc8af9b2b8126ff7dfab24 (diff) | |
| download | pish-789bea635f6b566af6d2793fee484ce129c33caa.tar.gz | |
comment parsing
| -rw-r--r-- | src/parse/mod.rs | 20 | ||||
| -rw-r--r-- | test-cases/comment/script.sh | 6 | ||||
| -rw-r--r-- | test-cases/comment/stdout.txt | 1 | ||||
| -rw-r--r-- | tests/scripts.rs | 5 |
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")); } |
