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
14 changes: 12 additions & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ impl Node {
#[allow(clippy::use_self)]
pub enum NodeKind {
/// A word token, possibly containing expansion parts.
Word { value: String, parts: Vec<Node> },
Word {
value: String,
parts: Vec<Node>,
spans: Vec<crate::lexer::word_builder::WordSpan>,
},

/// A literal text segment within a word's parts list.
WordLiteral { value: String },

/// A simple command: assignments, words, and redirects.
Command {
Expand Down Expand Up @@ -318,7 +325,10 @@ pub enum NodeKind {
CondParen { inner: Box<Node> },

/// A term (word) in a conditional expression.
CondTerm { value: String },
CondTerm {
value: String,
spans: Vec<crate::lexer::word_builder::WordSpan>,
},

// -- Other --
/// Pipeline negation with `!`.
Expand Down
111 changes: 0 additions & 111 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,114 +58,3 @@ impl CaseTracker {
self.depth > 0 && self.in_pattern
}
}

/// Skips a single-quoted region in a char array. Reads from `pos` (which
/// should point AT the opening `'`) through the closing `'`.
/// Pushes all chars (including quotes) into `out`.
pub fn skip_single_quoted(chars: &[char], pos: &mut usize, out: &mut String) {
out.push(chars[*pos]); // opening '
*pos += 1;
while *pos < chars.len() && chars[*pos] != '\'' {
out.push(chars[*pos]);
*pos += 1;
}
if *pos < chars.len() {
out.push(chars[*pos]); // closing '
*pos += 1;
}
}

/// Skips a double-quoted region in a char array. Reads from `pos` (which
/// should point AT the opening `"`) through the closing `"`.
/// Handles backslash escapes inside. Pushes all chars into `out`.
pub fn skip_double_quoted(chars: &[char], pos: &mut usize, out: &mut String) {
out.push(chars[*pos]); // opening "
*pos += 1;
while *pos < chars.len() && chars[*pos] != '"' {
if chars[*pos] == '\\' && *pos + 1 < chars.len() {
out.push(chars[*pos]);
*pos += 1;
}
out.push(chars[*pos]);
*pos += 1;
}
if *pos < chars.len() {
out.push(chars[*pos]); // closing "
*pos += 1;
}
}

/// Skips a backtick region in a char array. Reads from `pos` (which
/// should point AT the opening `` ` ``) through the closing `` ` ``.
/// Handles backslash escapes inside. Pushes all chars into `out`.
pub fn skip_backtick(chars: &[char], pos: &mut usize, out: &mut String) {
out.push(chars[*pos]); // opening `
*pos += 1;
while *pos < chars.len() && chars[*pos] != '`' {
if chars[*pos] == '\\' && *pos + 1 < chars.len() {
out.push(chars[*pos]);
*pos += 1;
}
out.push(chars[*pos]);
*pos += 1;
}
if *pos < chars.len() {
out.push(chars[*pos]); // closing `
*pos += 1;
}
}

/// Reads balanced delimiters (parens, braces, etc.) with quote awareness.
///
/// Starting at depth 1, reads until depth reaches 0. Handles single quotes,
/// double quotes, backticks, and backslash escapes inside.
pub fn read_balanced_delim(
chars: &[char],
pos: &mut usize,
open: char,
close: char,
out: &mut String,
) {
let mut depth = 1;
while *pos < chars.len() && depth > 0 {
match chars[*pos] {
c if c == open => {
depth += 1;
out.push(c);
*pos += 1;
}
c if c == close => {
depth -= 1;
out.push(c);
*pos += 1;
}
'\'' => skip_single_quoted(chars, pos, out),
'"' => skip_double_quoted(chars, pos, out),
'`' => skip_backtick(chars, pos, out),
'\\' if *pos + 1 < chars.len() => {
out.push(chars[*pos]);
*pos += 1;
out.push(chars[*pos]);
*pos += 1;
}
c => {
out.push(c);
*pos += 1;
}
}
}
}

/// Counts consecutive backslashes before a position to determine
/// if a character is escaped.
///
/// Even count → not escaped. Odd count → escaped.
pub fn is_backslash_escaped(chars: &[char], pos: usize) -> bool {
let mut count = 0;
let mut j = pos;
while j > 0 && chars[j - 1] == '\\' {
count += 1;
j -= 1;
}
count % 2 != 0
}
21 changes: 10 additions & 11 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ fn format_command_words(assignments: &[Node], words: &[Node], out: &mut String)
if i > 0 {
out.push(' ');
}
if let NodeKind::Word { value, .. } = &w.kind {
out.push_str(&process_word_value(value));
if let NodeKind::Word { value, spans, .. } = &w.kind {
out.push_str(&process_word_value(value, spans));
} else {
out.push_str(&w.to_string());
}
Expand All @@ -233,8 +233,8 @@ fn format_redirect(node: &Node, out: &mut String) {
if !is_dup {
out.push(' ');
}
if let NodeKind::Word { value, .. } = &target.kind {
out.push_str(&process_word_value(value));
if let NodeKind::Word { value, spans, .. } = &target.kind {
out.push_str(&process_word_value(value, spans));
}
} else if let NodeKind::HereDoc {
delimiter,
Expand Down Expand Up @@ -515,7 +515,7 @@ fn format_cond_node(node: &Node, out: &mut String) {
out.push_str("! ");
format_cond_node(operand, out);
}
NodeKind::CondTerm { value } => {
NodeKind::CondTerm { value, .. } => {
out.push_str(value);
}
NodeKind::CondParen { inner } => {
Expand All @@ -535,13 +535,12 @@ fn indent_str(out: &mut String, n: usize) {
}
}

/// Process a word value for canonical bash output using the same segment
/// pipeline as S-expression formatting. This ensures consistent handling
/// of `$'...'`, `$"..."`, and `$(...)` across both output paths.
fn process_word_value(value: &str) -> String {
use crate::sexp::word::{WordSegment, parse_word_segments};
/// Process a word value for canonical bash output using span-based
/// segment extraction.
fn process_word_value(value: &str, spans: &[crate::lexer::word_builder::WordSpan]) -> String {
use crate::sexp::word::{WordSegment, segments_from_spans};

let segments = parse_word_segments(value);
let segments = segments_from_spans(value, spans);
let mut result = String::with_capacity(value.len());

for seg in &segments {
Expand Down
Loading
Loading