Skip to content

Commit d951a45

Browse files
authored
fix(awk): treat # inside regex literals as literal, not comment (#840)
## Summary - The awk normalizer now recognizes regex literals (`/pattern/`) in both pattern and action contexts - Previously, regex was only recognized inside `{ }` braces, so `/#/` in a pattern position caused `#` to be treated as a comment start, resulting in "unterminated regex" errors ## Test plan - [x] `awk_regex_hash_pattern` — `/#/` matches lines containing `#` - [x] `awk_regex_hash_caret` — `/^#/` matches comment lines - [x] `awk_regex_hash_no_match` — non-hash regex still works - [x] All 119 awk spec tests pass - [x] All 1748 bash spec tests pass - [x] `cargo fmt --check` and `cargo clippy` clean Closes #835
1 parent e0370ba commit d951a45

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

crates/bashkit/src/builtins/awk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ fn normalize_awk_newlines(input: &str) -> String {
402402
i += 1;
403403
}
404404
}
405-
'/' if brace_depth > 0 => {
406-
// Potential regex literal — pass through unchanged
405+
'/' => {
406+
// Regex literal — pass through unchanged (both pattern and expression context)
407407
result.push('/');
408408
i += 1;
409409
while i < chars.len() && chars[i] != '/' {

crates/bashkit/tests/spec_cases/awk/awk.test.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,24 @@ awk 'BEGIN{print "caf\u00E9"}'
703703
### expect
704704
café
705705
### end
706+
707+
### awk_regex_hash_pattern
708+
# Issue #835: # inside regex pattern should not be treated as comment
709+
echo '#test' | awk '/#/ { print "matched" }'
710+
### expect
711+
matched
712+
### end
713+
714+
### awk_regex_hash_caret
715+
# Issue #835: /^#/ pattern
716+
printf '# heading\nnormal\n' | awk '/^#/ { print "comment:", $0 }'
717+
### expect
718+
comment: # heading
719+
### end
720+
721+
### awk_regex_hash_no_match
722+
# Regex without # still works
723+
echo 'abc' | awk '/abc/ { print "yes" }'
724+
### expect
725+
yes
726+
### end

0 commit comments

Comments
 (0)