Skip to content

Commit

Permalink
fix: workarounds for edge word parsing cases (reubeno#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno authored and 39555 committed Oct 22, 2024
1 parent 5c8b8cf commit b938586
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 29 deletions.
32 changes: 27 additions & 5 deletions brush-core/src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,37 @@ impl Pattern {
regex_str.push('^');
}

let mut current_pattern = String::new();
for piece in &self.pieces {
let next_part = match piece {
match piece {
PatternPiece::Pattern(s) => {
pattern_to_regex_str(s, false, false, enable_extended_globbing)?
current_pattern.push_str(s);
}
PatternPiece::Literal(s) => escape_for_regex(s),
};
PatternPiece::Literal(s) => {
if !current_pattern.is_empty() {
let regex_piece = pattern_to_regex_str(
current_pattern.as_str(),
false,
false,
enable_extended_globbing,
)?;
regex_str.push_str(regex_piece.as_str());
current_pattern = String::new();
}

regex_str.push_str(escape_for_regex(s).as_str());
}
}
}

regex_str.push_str(next_part.as_str());
if !current_pattern.is_empty() {
let regex_piece = pattern_to_regex_str(
current_pattern.as_str(),
false,
false,
enable_extended_globbing,
)?;
regex_str.push_str(regex_piece.as_str());
}

if strict_suffix_match {
Expand Down
2 changes: 1 addition & 1 deletion brush-interactive/src/reedline/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl ReedlineCompleter {
insertion_index: usize,
delete_count: usize,
) -> reedline::Suggestion {
let mut style = nu_ansi_term::Style::new().dimmed();
let mut style = nu_ansi_term::Style::new();
if candidate.ends_with(std::path::MAIN_SEPARATOR) {
style = style.fg(nu_ansi_term::Color::Green);
}
Expand Down
11 changes: 5 additions & 6 deletions brush-interactive/src/reedline/reedline_shell.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use nu_ansi_term::Color;
use reedline::MenuBuilder;
use std::sync::Arc;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -45,7 +46,9 @@ impl ReedlineShell {
let completion_menu = Box::new(
reedline::ColumnarMenu::default()
.with_name(COMPLETION_MENU_NAME)
.with_marker(""),
.with_marker("")
.with_selected_text_style(Color::Blue.bold().reverse())
.with_selected_match_text_style(Color::Blue.bold().reverse()),
);

// Set up key bindings.
Expand All @@ -54,11 +57,7 @@ impl ReedlineShell {
// Set up default history-based hinter.
let mut hinter = reedline::DefaultHinter::default();
if !options.disable_color {
hinter = hinter.with_style(
nu_ansi_term::Style::new()
.italic()
.fg(nu_ansi_term::Color::DarkGray),
);
hinter = hinter.with_style(nu_ansi_term::Style::new().italic().fg(Color::DarkGray));
}

// Instantiate reedline with some defaults and hand it ownership of
Expand Down
52 changes: 36 additions & 16 deletions brush-parser/src/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ fn cacheable_parse(
let pieces = expansion_parser::unexpanded_word(word.as_str(), &options)
.map_err(|err| error::WordParseError::Word(word.to_owned(), err))?;

tracing::debug!("Parsed word '{}' => {{{:?}}}", word, pieces);
tracing::debug!(target: "parse", "Parsed word '{}' => {{{:?}}}", word, pieces);

Ok(pieces)
}
Expand All @@ -411,7 +411,7 @@ peg::parser! {
pub(crate) rule unexpanded_word() -> Vec<WordPieceWithSource> = word(<![_]>)

rule word<T>(stop_condition: rule<T>) -> Vec<WordPieceWithSource> =
tilde:tilde_prefix_with_source()? pieces:word_piece_with_source(<stop_condition()>)* {
tilde:tilde_prefix_with_source()? pieces:word_piece_with_source(<stop_condition()>, false /*in_command*/)* {
let mut all_pieces = Vec::new();
if let Some(tilde) = tilde {
all_pieces.push(tilde);
Expand All @@ -427,26 +427,26 @@ peg::parser! {

rule arithmetic_word_piece<T>(stop_condition: rule<T>) =
"(" arithmetic_word_plus_right_paren() {} /
word_piece(<param_rule_or_open_paren(<stop_condition()>)>) {}
word_piece(<param_rule_or_open_paren(<stop_condition()>)>, false /*in_command*/) {}

rule param_rule_or_open_paren<T>(stop_condition: rule<T>) -> () =
stop_condition() {} /
"(" {}

rule arithmetic_word_plus_right_paren() =
"(" arithmetic_word_plus_right_paren() ")" /
word_piece(<[')']>)* ")" {}
word_piece(<[')']>, false /*in_command*/)* ")" {}

rule word_piece_with_source<T>(stop_condition: rule<T>) -> WordPieceWithSource =
start_index:position!() piece:word_piece(<stop_condition()>) end_index:position!() {
rule word_piece_with_source<T>(stop_condition: rule<T>, in_command: bool) -> WordPieceWithSource =
start_index:position!() piece:word_piece(<stop_condition()>, in_command) end_index:position!() {
WordPieceWithSource { piece, start_index, end_index }
}

rule word_piece<T>(stop_condition: rule<T>) -> WordPiece =
rule word_piece<T>(stop_condition: rule<T>, in_command: bool) -> WordPiece =
arithmetic_expansion() /
command_substitution() /
parameter_expansion() /
unquoted_text(<stop_condition()>)
unquoted_text(<stop_condition()>, in_command)

rule double_quoted_word_piece() -> WordPiece =
arithmetic_expansion() /
Expand All @@ -455,12 +455,12 @@ peg::parser! {
double_quoted_escape_sequence() /
double_quoted_text()

rule unquoted_text<T>(stop_condition: rule<T>) -> WordPiece =
rule unquoted_text<T>(stop_condition: rule<T>, in_command: bool) -> WordPiece =
s:double_quoted_sequence() { WordPiece::DoubleQuotedSequence(s) } /
s:single_quoted_literal_text() { WordPiece::SingleQuotedText(s.to_owned()) } /
s:ansi_c_quoted_text() { WordPiece::AnsiCQuotedText(s.to_owned()) } /
normal_escape_sequence() /
unquoted_literal_text(<stop_condition()>)
unquoted_literal_text(<stop_condition()>, in_command)

rule double_quoted_sequence() -> Vec<WordPieceWithSource> =
"\"" i:double_quoted_sequence_inner()* "\"" { i }
Expand All @@ -480,18 +480,25 @@ peg::parser! {
rule ansi_c_quoted_text() -> &'input str =
"$\'" inner:$([^'\'']*) "\'" { inner }

rule unquoted_literal_text<T>(stop_condition: rule<T>) -> WordPiece =
s:$(unquoted_literal_text_piece(<stop_condition()>)+) { WordPiece::Text(s.to_owned()) }
rule unquoted_literal_text<T>(stop_condition: rule<T>, in_command: bool) -> WordPiece =
s:$(unquoted_literal_text_piece(<stop_condition()>, in_command)+) { WordPiece::Text(s.to_owned()) }

rule unquoted_literal_text_piece<T>(stop_condition: rule<T>) =
extglob_pattern() /
// TODO: Find a way to remove the special-case logic for extglob + subshell commands
rule unquoted_literal_text_piece<T>(stop_condition: rule<T>, in_command: bool) =
is_true(in_command) extglob_pattern() /
is_true(in_command) subshell_command() /
!stop_condition() !normal_escape_sequence() [^'$' | '\'' | '\"'] {}

rule is_true(value: bool) = &[_] {? if value { Ok(()) } else { Err("not true") } }

rule extglob_pattern() =
("@" / "!" / "?" / "+" / "*") "(" extglob_body_piece()* ")" {}

rule extglob_body_piece() =
word_piece(<[')']>) {}
word_piece(<[')']>, true /*in_command*/) {}

rule subshell_command() =
"(" command() ")" {}

rule double_quoted_text() -> WordPiece =
s:double_quote_body_text() { WordPiece::Text(s.to_owned()) }
Expand Down Expand Up @@ -677,7 +684,7 @@ peg::parser! {
$(command_piece()*)

pub(crate) rule command_piece() -> () =
word_piece(<[')']>) {} /
word_piece(<[')']>, true /*in_command*/) {} /
([' ' | '\t'])+ {}

rule backquoted_command() -> String =
Expand Down Expand Up @@ -767,4 +774,17 @@ mod tests {

Ok(())
}

#[test]
fn parse_extglob_with_embedded_parameter() -> Result<()> {
let parsed = super::parse("+([$var])", &ParserOptions::default())?;
assert_matches!(
&parsed[..],
[WordPieceWithSource { piece: WordPiece::Text(s1), .. },
WordPieceWithSource { piece: WordPiece::ParameterExpansion(ParameterExpr::Parameter { parameter: Parameter::Named(s2), .. }), ..},
WordPieceWithSource { piece: WordPiece::Text(s3), .. }] if s1 == "+([" && s2 == "var" && s3 == "])"
);

Ok(())
}
}
1 change: 0 additions & 1 deletion brush-shell/tests/cases/extended_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ cases:
[[ a* != "abc" ]] && echo "3. Pass"
- name: "Binary string matching with expansion"
known_failure: true
stdin: |
exclude="0123456789"
[[ "clue" == +([$exclude]) ]] && echo "1. Fail"
Expand Down
5 changes: 5 additions & 0 deletions brush-shell/tests/cases/word_expansion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ cases:
echo "$(var="updated"; echo ${var})"
echo "var=${var}"
- name: "Command substitution with embedded parens"
stdin: |
x=$(echo foo | (wc -l; echo hi))
echo "\$x: $x"
- name: "Backtick command substitution"
stdin: |
echo `echo hi`
Expand Down

0 comments on commit b938586

Please sign in to comment.