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:
parent
5b1755387f
commit
4f20773b4f
1 changed files with 4 additions and 9 deletions
|
@ -16,8 +16,6 @@
|
|||
|
||||
// spell-checker:ignore (ToDO) paren
|
||||
|
||||
use num_bigint::BigInt;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Token {
|
||||
Value {
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue