use super::{UNKNOWN, holds_an_expression, refuse_if};
use crate::pathctx::item_shape::{Binding, binding};
use crate::verdict::Verdict;
pub(crate) fn expression_verdict(text: &str) -> Verdict {
let compared = text
.replace("<<=", "=")
.replace(">>=", "=")
.replace("==", "")
.replace("!=", "")
.replace("<=", "")
.replace(">=", "");
let assigns = compared.contains('=') || compared.contains("++") || compared.contains("--");
let every_argument = ["$@", "$*", "${@", "${*"].iter().any(|all| text.contains(all));
refuse_if(
assigns
|| text.contains(UNKNOWN)
|| reads_through_a_flag(text)
|| builds_an_operator(text)
|| reaches_the_shell_name(text)
|| names(text).iter().any(|name| holds_an_expression(name))
|| (every_argument && any_argument_holds_an_expression()),
)
}
fn any_argument_holds_an_expression() -> bool {
let bound = |i: usize| !matches!(binding(&i.to_string()), Binding::Unbound);
!bound(1) || bound(MAX_ARGUMENTS + 1) || (1..=MAX_ARGUMENTS).take_while(|&i| bound(i)).any(|i| holds_an_expression(&i.to_string()))
}
fn reads_through_a_flag(text: &str) -> bool {
text.match_indices('$').any(|(at, _)| {
let after = &text[at + 1..];
after.starts_with(['~', '^', '=', '+', '-'])
|| after.strip_prefix('{').is_some_and(|b| b.starts_with(['!', '~', '^', '=', '(', '+', '-']))
})
}
const MAX_ARGUMENTS: usize = 64;
pub(super) const SHELL_SET: &[&str] = &[
"_", "REPLY", "reply", "BASH_REMATCH", "MATCH", "match", "MBEGIN", "MEND", "mbegin", "mend", "OPTARG", "MAPFILE", "BASH_COMMAND",
"BASH_ARGV", "FUNCNAME", "BASH_SOURCE", "PWD", "OLDPWD", "BUFFER", "LBUFFER", "RBUFFER",
];
fn names(text: &str) -> Vec<&str> {
let mut out = Vec::new();
let mut rest = text;
while let Some(start) = rest.find(|c: char| c.is_ascii_alphanumeric() || c == '_') {
let run = &rest[start..];
let len = run.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_' || c == '#')).unwrap_or(run.len());
let digits = run.find(|c: char| !c.is_ascii_digit()).unwrap_or(run.len());
let before = &rest[..start];
if before.ends_with("${#") {
rest = &run[len..];
continue;
}
if !run.starts_with(|c: char| c.is_ascii_digit()) {
out.push(run[..len].trim_end_matches('#'));
} else if before.ends_with("${") {
out.push(&run[..digits]);
} else if before.ends_with('$') {
out.extend([&run[..1], &run[..digits]]);
}
rest = &run[len..];
}
out
}
fn builds_an_operator(text: &str) -> bool {
text.match_indices('$').any(|(at, _)| {
let after = text[at + 1..].trim_start_matches('{');
let name = &after[..after.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_')).unwrap_or(after.len())];
let special = name.is_empty() && !after.starts_with(['@', '*']);
text[..at].ends_with(['+', '-'])
&& !special
&& match binding(name) {
Binding::Value(value) => value.trim().starts_with(['+', '-']) || value == crate::cst::scope_budget::UNCERTAIN_VALUE,
Binding::Loop | Binding::Unbound => true,
}
})
}
fn reaches_the_shell_name(text: &str) -> bool {
["${@:", "${*:"].iter().any(|all| {
text.match_indices(all)
.any(|(at, _)| !text[at + all.len()..].starts_with(|c: char| ('1'..='9').contains(&c)))
})
}
pub(super) fn flag_arithmetic(flags: &str) -> Vec<&str> {
let mut out = Vec::new();
let mut rest = flags;
while let Some(c) = rest.chars().next() {
rest = &rest[c.len_utf8()..];
if !"jslrZIg_".contains(c) {
if !(c.is_ascii_alphanumeric() || "@#%*~^=+-!".contains(c)) {
return vec![flags];
}
continue;
}
let Some(open) = rest.chars().next() else { return vec![flags] };
let close = match open {
'(' => ')',
'[' => ']',
'{' => '}',
'<' => '>',
other => other,
};
while let Some(body) = rest.strip_prefix(open) {
let Some(end) = body.find(close) else { return vec![flags] };
if "lrI".contains(c) {
out.push(&body[..end]);
}
rest = &body[end + close.len_utf8()..];
}
}
out
}