1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

expr: fix escape

This commit is contained in:
Zhuoxun Yang 2023-10-14 23:20:45 +08:00
parent 5b1755387f
commit 4f20773b4f

View file

@ -16,8 +16,6 @@
// spell-checker:ignore (ToDO) paren // spell-checker:ignore (ToDO) paren
use num_bigint::BigInt;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Token { pub enum Token {
Value { Value {
@ -59,11 +57,8 @@ impl Token {
} }
} }
fn is_a_number(&self) -> bool { fn is_a_value(&self) -> bool {
match self { matches!(*self, Self::Value { .. })
Self::Value { value, .. } => value.parse::<BigInt>().is_ok(),
_ => false,
}
} }
fn is_a_close_paren(&self) -> bool { fn is_a_close_paren(&self) -> bool {
@ -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) { 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() { let prev_is_plus = match acc.last() {
None => false, None => false,
Some(t) => t.1.is_infix_plus(), Some(t) => t.1.is_infix_plus(),
}; };
let should_use_as_escaped = if prev_is_plus && acc.len() >= 2 { let should_use_as_escaped = if prev_is_plus && acc.len() >= 2 {
let pre_prev = &acc[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 { } else {
prev_is_plus prev_is_plus
}; };