Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 204 additions & 1 deletion crates/bashkit/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,101 @@ impl<'a> Lexer<'a> {
continue;
} else if ch == '$' {
// Handle variable references and command substitution
word.push(ch);
self.advance();

// $'...' — ANSI-C quoting: resolve escapes at parse time
if self.peek_char() == Some('\'') {
self.advance(); // consume opening '
word.push_str(&self.read_dollar_single_quoted_content());
continue;
}

// $"..." — locale translation synonym, treated like "..."
if self.peek_char() == Some('"') {
self.advance(); // consume opening "
while let Some(c) = self.peek_char() {
if c == '"' {
self.advance();
break;
}
if c == '\\' {
self.advance();
if let Some(next) = self.peek_char() {
match next {
'\n' => {
self.advance();
}
'"' | '\\' | '$' | '`' => {
word.push(next);
self.advance();
}
_ => {
word.push('\\');
word.push(next);
self.advance();
}
}
continue;
}
}
if c == '$' {
word.push(c);
self.advance();
if let Some(nc) = self.peek_char() {
if nc == '{' {
word.push(nc);
self.advance();
while let Some(bc) = self.peek_char() {
word.push(bc);
self.advance();
if bc == '}' {
break;
}
}
} else if nc == '(' {
word.push(nc);
self.advance();
let mut depth = 1;
while let Some(pc) = self.peek_char() {
word.push(pc);
self.advance();
if pc == '(' {
depth += 1;
} else if pc == ')' {
depth -= 1;
if depth == 0 {
break;
}
}
}
} else if nc.is_ascii_alphanumeric()
|| nc == '_'
|| matches!(nc, '?' | '#' | '@' | '*' | '!' | '$' | '-')
{
word.push(nc);
self.advance();
if nc.is_ascii_alphabetic() || nc == '_' {
while let Some(vc) = self.peek_char() {
if vc.is_ascii_alphanumeric() || vc == '_' {
word.push(vc);
self.advance();
} else {
break;
}
}
}
}
}
continue;
}
word.push(c);
self.advance();
}
continue;
}

word.push(ch); // push the '$'

// Check for $( - command substitution or arithmetic
if self.peek_char() == Some('(') {
word.push('(');
Expand Down Expand Up @@ -616,6 +708,117 @@ impl<'a> Lexer<'a> {
Some(Token::LiteralWord(content))
}

/// Read ANSI-C quoted content ($'...').
/// Opening $' already consumed. Returns the resolved string.
fn read_dollar_single_quoted_content(&mut self) -> String {
let mut out = String::new();
while let Some(ch) = self.peek_char() {
if ch == '\'' {
self.advance();
break;
}
if ch == '\\' {
self.advance();
if let Some(esc) = self.peek_char() {
self.advance();
match esc {
'n' => out.push('\n'),
't' => out.push('\t'),
'r' => out.push('\r'),
'a' => out.push('\x07'),
'b' => out.push('\x08'),
'f' => out.push('\x0C'),
'v' => out.push('\x0B'),
'e' | 'E' => out.push('\x1B'),
'\\' => out.push('\\'),
'\'' => out.push('\''),
'"' => out.push('"'),
'?' => out.push('?'),
'x' => {
let mut hex = String::new();
for _ in 0..2 {
if let Some(h) = self.peek_char() {
if h.is_ascii_hexdigit() {
hex.push(h);
self.advance();
} else {
break;
}
}
}
if let Ok(val) = u8::from_str_radix(&hex, 16) {
out.push(val as char);
}
}
'u' => {
let mut hex = String::new();
for _ in 0..4 {
if let Some(h) = self.peek_char() {
if h.is_ascii_hexdigit() {
hex.push(h);
self.advance();
} else {
break;
}
}
}
if let Ok(val) = u32::from_str_radix(&hex, 16) {
if let Some(c) = char::from_u32(val) {
out.push(c);
}
}
}
'U' => {
let mut hex = String::new();
for _ in 0..8 {
if let Some(h) = self.peek_char() {
if h.is_ascii_hexdigit() {
hex.push(h);
self.advance();
} else {
break;
}
}
}
if let Ok(val) = u32::from_str_radix(&hex, 16) {
if let Some(c) = char::from_u32(val) {
out.push(c);
}
}
}
'0'..='7' => {
let mut oct = String::new();
oct.push(esc);
for _ in 0..2 {
if let Some(o) = self.peek_char() {
if o.is_ascii_digit() && o < '8' {
oct.push(o);
self.advance();
} else {
break;
}
}
}
if let Ok(val) = u8::from_str_radix(&oct, 8) {
out.push(val as char);
}
}
_ => {
out.push('\\');
out.push(esc);
}
}
} else {
out.push('\\');
}
continue;
}
out.push(ch);
self.advance();
}
out
}

fn read_double_quoted_string(&mut self) -> Option<Token> {
self.advance(); // consume opening "
let mut content = String::new();
Expand Down
9 changes: 0 additions & 9 deletions crates/bashkit/tests/spec_cases/bash/quote.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,20 @@ a\tb

### quote_dollar_single_basic
# $'' basic
### skip: TODO $'' (dollar single quote) not implemented
echo $'foo'
### expect
foo
### end

### quote_dollar_single_quotes
# $'' with quotes
### skip: TODO $'' (dollar single quote) not implemented
echo $'single \' double \"'
### expect
single ' double "
### end

### quote_dollar_single_newlines
# $'' with newlines
### skip: TODO $'' (dollar single quote) not implemented
echo $'col1\ncol2\ncol3'
### expect
col1
Expand All @@ -189,7 +186,6 @@ col3

### quote_dollar_double_synonym
# $"" is a synonym for ""
### skip: TODO $"" (dollar double quote) not implemented
echo $"foo"
x=x
echo $"foo $x"
Expand All @@ -200,39 +196,34 @@ foo x

### quote_dollar_single_hex
# $'' with hex escapes
### skip: TODO $'' (dollar single quote) not implemented
echo $'\x41\x42\x43'
### expect
ABC
### end

### quote_dollar_single_octal
# $'' with octal escapes
### skip: TODO $'' (dollar single quote) not implemented
echo $'\101\102\103'
### expect
ABC
### end

### quote_dollar_single_unicode_u
# $'' with \u unicode escape
### skip: TODO $'' (dollar single quote) not implemented
echo $'\u0041\u0042'
### expect
AB
### end

### quote_dollar_single_unicode_U
# $'' with \U unicode escape
### skip: TODO $'' (dollar single quote) not implemented
echo $'\U00000041\U00000042'
### expect
AB
### end

### quote_dollar_single_special
# $'' with special escapes
### skip: TODO $'' (dollar single quote) not implemented
printf '%s' $'\a' | od -A n -t x1 | tr -d ' \n'
echo
printf '%s' $'\b' | od -A n -t x1 | tr -d ' \n'
Expand Down
6 changes: 3 additions & 3 deletions crates/bashkit/tests/spec_cases/bash/unicode.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ echo "μ"

### unicode_dollar_single
# Unicode in $'' via \u escape
### skip: TODO $'' (dollar single quote) not implemented
### bash_diff: system bash may not support \u in $''
echo $'\u03bc'
### expect
μ
### end

### unicode_dollar_single_U
# Unicode in $'' via \U escape
### skip: TODO $'' (dollar single quote) not implemented
### bash_diff: system bash may not support \U in $''
echo $'\U000003bc'
### expect
μ
Expand Down Expand Up @@ -136,7 +136,7 @@ hello 🌍

### unicode_dollar_single_ascii
# $'' with unicode for ASCII range
### skip: TODO $'' (dollar single quote) not implemented
### bash_diff: system bash may not support \u in $''
echo $'\u0041\u0042\u0043'
### expect
ABC
Expand Down
Loading