Skip to content

Commit

Permalink
expr: fix escape
Browse files Browse the repository at this point in the history
  • Loading branch information
Luv-Ray committed Oct 14, 2023
1 parent 5b17553 commit 4f20773
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/uu/expr/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
// spell-checker:ignore (ToDO) paren

use num_bigint::BigInt;

#[derive(Debug, Clone)]
pub enum Token {
Value {
Expand Down Expand Up @@ -59,11 +57,8 @@ impl Token {
}
}

fn is_a_number(&self) -> bool {
match self {
Self::Value { value, .. } => value.parse::<BigInt>().is_ok(),
_ => false,
}
fn is_a_value(&self) -> bool {
matches!(*self, Self::Value { .. })
}

fn is_a_close_paren(&self) -> bool {
Expand Down Expand Up @@ -131,14 +126,14 @@ fn maybe_dump_tokens_acc(tokens_acc: &[(usize, Token)]) {
}

fn push_token_if_not_escaped(acc: &mut Vec<(usize, Token)>, tok_idx: usize, token: Token, s: &str) {
// Smells heuristics... :(
// `+` may escaped such as `expr + 1` and `expr 1 + + 1`
let prev_is_plus = match acc.last() {
None => false,
Some(t) => t.1.is_infix_plus(),
};
let should_use_as_escaped = if prev_is_plus && acc.len() >= 2 {
let pre_prev = &acc[acc.len() - 2];
!(pre_prev.1.is_a_number() || pre_prev.1.is_a_close_paren())
!(pre_prev.1.is_a_value() || pre_prev.1.is_a_close_paren())
} else {
prev_is_plus
};
Expand Down

0 comments on commit 4f20773

Please sign in to comment.