use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use crate::dialect::TheRefusalOfTheArity;
use crate::runtime::{CaseSense, FloatFormat, IntegerFormat, Value};
pub(crate) fn the_refusal_of_the_arity(name: &str, count: usize) -> Option<TheRefusalOfTheArity> {
let rule = RULES_OF_THE_ARITY
.iter()
.find(|rule| rule.name.eq_ignore_ascii_case(name))?;
if count < rule.generic as usize {
return Some(TheRefusalOfTheArity::AtLeast {
name: rule.spelled.unwrap_or(rule.name),
number: rule.generic,
});
}
if rule.required != 0 && count < rule.required as usize {
return Some(TheRefusalOfTheArity::Required {
number: rule.required,
});
}
if let Some((first, second)) = rule.together {
if count < second as usize {
return Some(TheRefusalOfTheArity::RequiredTogether { first, second });
}
}
None
}
struct Arity {
name: &'static str,
generic: u8,
required: u8,
together: Option<(u8, u8)>,
spelled: Option<&'static str>,
}
const RULES_OF_THE_ARITY: &[Arity] = &[
Arity {
name: "EnvSub",
generic: 1,
required: 2,
together: None,
spelled: None,
},
Arity {
name: "ControlSend",
generic: 0,
required: 2,
together: None,
spelled: None,
},
Arity {
name: "GuiControl",
generic: 0,
required: 2,
together: None,
spelled: None,
},
Arity {
name: "WinMenuSelectItem",
generic: 0,
required: 3,
together: None,
spelled: None,
},
Arity {
name: "SetStoreCapsLockMode",
generic: 1,
required: 0,
together: None,
spelled: Some("SetStoreCapslockMode"),
},
Arity {
name: "MouseClickDrag",
generic: 1,
required: 0,
together: Some((4, 5)),
spelled: None,
},
];
pub(crate) fn the_refusal_of_a_parameter(name: &str, parameters: &[String]) -> Option<String> {
if let Some(rule) = MESSAGES
.iter()
.find(|rule| rule.name.eq_ignore_ascii_case(name))
{
return (rule.message)(parameters);
}
if let Some(refusal) = crate::v1::the_refusal_of_a_sub_command(name, parameters) {
return Some(refusal);
}
let rule = RULES
.iter()
.find(|rule| rule.name.eq_ignore_ascii_case(name))?;
let texts = rule.the_texts(parameters);
let place = (rule.check)(&texts)?;
let text = the_text_at(&texts, place);
Some(format!(
"Parameter #{} invalid.\n Specifically: {text}",
place + 1
))
}
struct Rule {
name: &'static str,
check: fn(&[String]) -> Option<usize>,
}
impl Rule {
fn the_texts(&self, parameters: &[String]) -> Vec<String> {
parameters
.iter()
.map(|parameter| parameter.trim().to_string())
.collect()
}
}
const RULES: &[Rule] = &[
Rule {
name: "AutoTrim",
check: the_switch,
},
Rule {
name: "DetectHiddenText",
check: the_switch,
},
Rule {
name: "DetectHiddenWindows",
check: the_switch,
},
Rule {
name: "SetStoreCapsLockMode",
check: the_switch,
},
Rule {
name: "StringCaseSense",
check: the_case_sense,
},
Rule {
name: "SetTitleMatchMode",
check: the_title_match_mode,
},
Rule {
name: "SendMode",
check: the_send_mode,
},
Rule {
name: "SetRegView",
check: the_reg_view,
},
Rule {
name: "FileEncoding",
check: the_encoding,
},
Rule {
name: "FileAppend",
check: the_encoding_of_the_append,
},
Rule {
name: "SetBatchLines",
check: the_batch_lines,
},
Rule {
name: "SetFormat",
check: the_format,
},
Rule {
name: "Transform",
check: the_transform,
},
Rule {
name: "StringGetPos",
check: the_occurrence,
},
Rule {
name: "FileGetTime",
check: the_time_of_a_file,
},
Rule {
name: "FileGetSize",
check: the_size_of_a_file,
},
Rule {
name: "FileSetAttrib",
check: the_attributes_of_a_file,
},
Rule {
name: "EnvAdd",
check: the_units_of_the_time,
},
Rule {
name: "EnvSub",
check: the_unit_and_the_date_of_the_subtraction,
},
Rule {
name: "FileMoveDir",
check: the_way_of_the_move_of_a_directory,
},
Rule {
name: "GroupActivate",
check: the_mode_of_a_group,
},
Rule {
name: "GroupClose",
check: the_mode_of_a_group,
},
Rule {
name: "GroupDeactivate",
check: the_mode_of_a_group,
},
Rule {
name: "IfMsgBox",
check: the_button_of_a_message_box,
},
Rule {
name: "KeyHistory",
check: the_command_without_a_parameter,
},
Rule {
name: "StringMid",
check: the_letter_of_the_string_mid,
},
Rule {
name: "MouseClickDrag",
check: the_button_of_the_mouse,
},
Rule {
name: "MouseMove",
check: the_speed_and_the_fourth_parameter_of_the_mouse_move,
},
Rule {
name: "GetKeyState",
check: the_name_of_a_key,
},
];
struct Message {
name: &'static str,
message: fn(&[String]) -> Option<String>,
}
const MESSAGES: &[Message] = &[
Message {
name: "EnvDiv",
message: the_division_by_zero,
},
Message {
name: "ImageSearch",
message: the_place_of_the_image,
},
Message {
name: "PixelSearch",
message: the_place_of_the_image,
},
Message {
name: "WinWait",
message: the_window_of_the_win_wait,
},
Message {
name: "WinActivateBottom",
message: the_window_of_the_win_activate_bottom,
},
];
fn the_place_of_the_image(parameters: &[String]) -> Option<String> {
for place in 2..7 {
if the_text_at(parameters, place).trim().is_empty() {
return Some("Parameters 3 through 7 must not be blank.".to_string());
}
}
None
}
fn the_window_of_the_win_wait(parameters: &[String]) -> Option<String> {
the_refusal_of_the_window(parameters, &[0, 1, 3, 4])
}
fn the_window_of_the_win_activate_bottom(parameters: &[String]) -> Option<String> {
the_refusal_of_the_window(parameters, &[0, 1, 2, 3])
}
fn the_refusal_of_the_window(parameters: &[String], places: &[usize]) -> Option<String> {
for place in places {
if !the_text_at(parameters, *place).trim().is_empty() {
return None;
}
}
Some("Requires at least one of its window parameters.".to_string())
}
fn the_division_by_zero(parameters: &[String]) -> Option<String> {
let divisor = parameters.get(1)?;
let number = crate::v1::the_number_at_the_start_of_a_text(divisor);
let zero = match number {
Some(Value::Int(number)) => number == 0,
Some(Value::Float(number)) => number == 0.0,
_ => false,
};
if !zero {
return None;
}
Some(format!("Divide by zero\n Specifically: {divisor}"))
}
pub(crate) fn the_refusal_of_the_percent_sign_of_a_parameter(text: &str) -> Option<String> {
if the_text_is_an_expression_of_the_v1(text) {
return None;
}
let expanded = the_escapes_of_the_v1(text);
let mut rest = text;
while let Some(at) = the_percent_sign_that_opens(rest) {
let after = &rest[at + 1..];
let Some(end) = the_percent_sign_that_opens(after) else {
return Some(format!(
"This parameter contains a variable name missing its ending percent sign.\n Specifically: {expanded}"
));
};
if end == 0 {
return Some(format!(
"Empty variable reference (%%).\n Specifically: {expanded}"
));
}
rest = &after[end + 1..];
}
None
}
pub(crate) fn the_text_is_an_expression_of_the_v1(text: &str) -> bool {
let text = text.trim_start();
let Some(rest) = text.strip_prefix('%') else {
return false;
};
rest.starts_with(' ') || rest.starts_with('\t')
}
pub(crate) fn the_refusal_of_the_percent_sign_of_an_expression(
run: &str,
empty: bool,
) -> Option<String> {
if empty {
return Some(format!(
"Empty variable reference (%%).\n Specifically: {run}"
));
}
Some(format!(
"This parameter contains a variable name missing its ending percent sign.\n Specifically: {run}"
))
}
pub(crate) fn the_refusal_of_the_point_of_an_operand(text: &str) -> Option<String> {
Some(format!(
"Ambiguous or invalid use of \".\"\n Specifically: {text}"
))
}
pub(crate) fn the_refusal_of_the_text_of_a_member(text: &str) -> Option<String> {
Some(format!(
"This character is not allowed here.\n Specifically: {text}"
))
}
pub(crate) fn the_refusal_of_the_name_of_a_variable(text: &str) -> Option<String> {
if let Some(refusal) = the_refusal_of_the_percent_sign_of_a_parameter(text) {
return Some(refusal);
}
if text.contains('%') {
return None;
}
let expanded = the_escapes_of_the_v1(text);
if is_the_name_of_a_variable(&expanded) {
return None;
}
Some(format!(
"The following variable name contains an illegal character:\n\"{expanded}\""
))
}
fn the_percent_sign_that_opens(text: &str) -> Option<usize> {
let mut index = 0;
while index < text.len() {
let character = text[index..].chars().next()?;
if character == '`' {
index += 1;
if let Some(next) = text[index..].chars().next() {
index += next.len_utf8();
}
continue;
}
if character == '%' {
return Some(index);
}
index += character.len_utf8();
}
None
}
pub(crate) fn the_escapes_of_the_v1(text: &str) -> String {
let dialect = crate::dialect::v1();
let mut result = String::with_capacity(text.len());
let mut characters = text.chars();
while let Some(character) = characters.next() {
if character == '`' {
match characters.next() {
Some(next) => result.push(dialect.the_escape_of_the_dialect(next)),
None => result.push('`'),
}
continue;
}
result.push(character);
}
result
}
pub(crate) fn is_the_name_of_a_variable(text: &str) -> bool {
!text.is_empty()
&& text.chars().all(|character| {
character.is_ascii_alphanumeric() || matches!(character, '_' | '#' | '$' | '@')
})
}
pub(crate) fn is_the_name_of_a_variable_that_may_carry_a_point(text: &str) -> bool {
!text.is_empty()
&& text.chars().all(|character| {
character.is_ascii_alphanumeric() || matches!(character, '_' | '#' | '$' | '@' | '.')
})
}
fn the_button_of_a_message_box(texts: &[String]) -> Option<usize> {
let text = the_text_at(texts, 0).trim();
if matches!(
text.to_lowercase().as_str(),
"yes"
| "no"
| "ok"
| "cancel"
| "abort"
| "ignore"
| "retry"
| "continue"
| "tryagain"
| "timeout"
) {
return None;
}
Some(0)
}
fn the_button_of_the_mouse(texts: &[String]) -> Option<usize> {
let text = the_text_at(texts, 0).trim();
if matches!(
text.to_lowercase().as_str(),
"l" | "r" | "m" | "left" | "right" | "middle"
) {
return None;
}
Some(0)
}
fn the_letter_of_the_string_mid(texts: &[String]) -> Option<usize> {
let text = the_text_at(texts, 4).trim();
if text.is_empty() || text.eq_ignore_ascii_case("L") {
return None;
}
Some(4)
}
fn the_mode_of_a_group(texts: &[String]) -> Option<usize> {
let text = the_text_at(texts, 1).trim();
if text.is_empty() || text.eq_ignore_ascii_case("R") {
return None;
}
Some(1)
}
fn the_way_of_the_move_of_a_directory(texts: &[String]) -> Option<usize> {
let text = the_text_at(texts, 2).trim();
if text.is_empty() || matches!(text, "0" | "1" | "2") {
return None;
}
Some(2)
}
fn the_command_without_a_parameter(texts: &[String]) -> Option<usize> {
if texts.is_empty() {
return None;
}
Some(0)
}
fn the_text_at(texts: &[String], place: usize) -> &str {
texts.get(place).map(String::as_str).unwrap_or("")
}
fn the_speed_and_the_fourth_parameter_of_the_mouse_move(texts: &[String]) -> Option<usize> {
if let Some(place) = the_speed_of_the_move_of_the_mouse(texts) {
return Some(place);
}
the_fourth_parameter_of_the_mouse_move(texts)
}
fn the_speed_of_the_move_of_the_mouse(texts: &[String]) -> Option<usize> {
let text = the_text_at(texts, 2).trim();
if text.is_empty() {
return None;
}
let number = crate::v1::the_number_at_the_start_of_a_text(text);
let negative = match number {
Some(Value::Int(number)) => number < 0,
Some(Value::Float(number)) => number < 0.0,
_ => false,
};
if !negative {
return None;
}
Some(2)
}
fn the_fourth_parameter_of_the_mouse_move(texts: &[String]) -> Option<usize> {
if the_text_at(texts, 3).trim().is_empty() {
return None;
}
Some(3)
}
fn the_name_of_a_key(texts: &[String]) -> Option<usize> {
let text = the_text_at(texts, 1);
if text.contains('%') {
return None;
}
if super::keys::is_the_name_of_a_key(text) {
return None;
}
Some(1)
}
fn the_switch(texts: &[String]) -> Option<usize> {
if the_switch_of_the_v1(the_text_at(texts, 0)).is_some() {
return None;
}
Some(0)
}
pub(crate) fn the_switch_of_the_v1(text: &str) -> Option<bool> {
match text.trim().to_lowercase().as_str() {
"on" | "1" => Some(true),
"off" | "0" => Some(false),
_ => None,
}
}
fn the_case_sense(texts: &[String]) -> Option<usize> {
if the_case_sense_of_the_v1(the_text_at(texts, 0)).is_some() {
return None;
}
Some(0)
}
pub(crate) fn the_case_sense_of_the_v1(text: &str) -> Option<CaseSense> {
match text.trim().to_lowercase().as_str() {
"on" | "1" => Some(CaseSense::On),
"off" | "0" => Some(CaseSense::Off),
"locale" => Some(CaseSense::Locale),
_ => None,
}
}
fn the_title_match_mode(texts: &[String]) -> Option<usize> {
match the_text_at(texts, 0).trim().to_lowercase().as_str() {
"1" | "2" | "3" | "regex" | "fast" | "slow" => None,
_ => Some(0),
}
}
fn the_send_mode(texts: &[String]) -> Option<usize> {
match the_text_at(texts, 0).trim().to_lowercase().as_str() {
"event" | "input" | "play" | "inputthenplay" => None,
_ => Some(0),
}
}
fn the_reg_view(texts: &[String]) -> Option<usize> {
match the_text_at(texts, 0).trim().to_lowercase().as_str() {
"32" | "64" | "default" => None,
_ => Some(0),
}
}
fn the_encoding(texts: &[String]) -> Option<usize> {
the_encoding_at(texts, 0)
}
fn the_encoding_at(texts: &[String], place: usize) -> Option<usize> {
let text = the_text_at(texts, place).trim().to_lowercase();
if text.is_empty() {
return None;
}
match text.as_str() {
"utf-8" | "utf-8-raw" | "utf-16" | "utf-16-raw" => None,
_ if crate::settings::is_a_code_page(&text.to_uppercase()) => None,
_ => Some(place),
}
}
fn the_encoding_of_the_append(texts: &[String]) -> Option<usize> {
if texts.len() < 3 {
return None;
}
the_encoding_at(texts, 2)
}
fn the_batch_lines(texts: &[String]) -> Option<usize> {
if is_a_whole_number_of_the_lines(the_text_at(texts, 0)) {
return None;
}
Some(0)
}
pub(crate) fn is_a_whole_number_of_the_lines(text: &str) -> bool {
let text = text.trim();
if text.parse::<i64>().is_ok() {
return true;
}
crate::v1::the_text_is_a_hex(text)
}
fn the_format(texts: &[String]) -> Option<usize> {
match the_kind_of_a_number(the_text_at(texts, 0)) {
Some(TheKindOfANumber::Integer) => {
if the_format_of_the_whole_numbers(the_text_at(texts, 1)).is_some() {
return None;
}
Some(1)
}
Some(TheKindOfANumber::Float) => {
if the_format_of_the_floats(the_text_at(texts, 1)).is_some() {
return None;
}
Some(1)
}
None => Some(0),
}
}
pub(crate) enum TheKindOfANumber {
Integer,
Float,
}
pub(crate) fn the_kind_of_a_number(text: &str) -> Option<TheKindOfANumber> {
let text = text.trim().to_lowercase();
if text.starts_with("integer") {
return Some(TheKindOfANumber::Integer);
}
if text.starts_with("float") {
return Some(TheKindOfANumber::Float);
}
None
}
pub(crate) fn the_format_of_the_whole_numbers(text: &str) -> Option<IntegerFormat> {
match text.trim().chars().next() {
Some('D') | Some('d') => Some(IntegerFormat::Decimal),
Some('H') => Some(IntegerFormat::Hexadecimal),
Some('h') => Some(IntegerFormat::HexadecimalLower),
_ => None,
}
}
pub(crate) fn the_format_of_the_floats(text: &str) -> Option<FloatFormat> {
let text = text.trim();
if text.is_empty() {
return None;
}
let (body, scientific) = match text.strip_suffix('e') {
Some(body) => (body, true),
None => match text.strip_suffix('E') {
Some(body) => (body, true),
None => (text, false),
},
};
let (width, decimals) = body.split_once('.')?;
let (left, width) = match width.strip_prefix('-') {
Some(rest) => (true, rest),
None => (false, width),
};
let (zeroes, width) = match width.strip_prefix('0') {
Some(rest) if !rest.is_empty() => (true, rest),
_ => (false, width),
};
let width: u32 = width.parse().ok()?;
let decimals: u32 = decimals.parse().ok()?;
Some(FloatFormat {
width,
zeroes,
left,
decimals,
scientific,
upper: text.ends_with('E'),
})
}
fn the_transform(texts: &[String]) -> Option<usize> {
if is_a_sub_command_of_the_transform(the_text_at(texts, 1)) {
return None;
}
Some(1)
}
pub(crate) fn is_a_sub_command_of_the_transform(text: &str) -> bool {
matches!(
text.trim().to_lowercase().as_str(),
"asc"
| "chr"
| "mod"
| "exp"
| "sqrt"
| "log"
| "ln"
| "round"
| "ceil"
| "floor"
| "abs"
| "sin"
| "cos"
| "tan"
| "asin"
| "acos"
| "atan"
| "pow"
| "bitnot"
| "bitand"
| "bitor"
| "bitxor"
| "bitshiftleft"
| "bitshiftright"
| "deref"
| "html"
)
}
fn the_occurrence(texts: &[String]) -> Option<usize> {
if is_an_occurrence(the_text_at(texts, 3)) {
return None;
}
Some(3)
}
pub(crate) fn is_an_occurrence(text: &str) -> bool {
let text = text.trim();
let Some(first) = text.chars().next() else {
return true;
};
matches!(first, 'L' | 'l' | 'R' | 'r' | '1')
}
fn the_time_of_a_file(texts: &[String]) -> Option<usize> {
if texts.len() < 3 {
return None;
}
match the_text_at(texts, 2).trim().to_lowercase().as_str() {
"m" | "a" | "c" => None,
_ => Some(2),
}
}
fn the_size_of_a_file(texts: &[String]) -> Option<usize> {
if texts.len() < 3 {
return None;
}
match the_text_at(texts, 2).trim().to_lowercase().as_str() {
"b" | "k" | "m" => None,
_ => Some(2),
}
}
fn the_units_of_the_time(texts: &[String]) -> Option<usize> {
let text = the_text_at(texts, 2);
if text.trim().is_empty() {
return None;
}
if is_a_unit_of_the_time(text) {
return None;
}
Some(2)
}
fn the_unit_and_the_date_of_the_subtraction(texts: &[String]) -> Option<usize> {
if let Some(place) = the_units_of_the_time(texts) {
return Some(place);
}
the_date_of_the_subtraction(texts)
}
fn the_date_of_the_subtraction(texts: &[String]) -> Option<usize> {
if texts.len() < 3 {
return None;
}
let text = the_text_at(texts, 1);
if crate::v1::the_date_of_the_v1(text).is_some() {
return None;
}
if !crate::v1::the_text_is_a_number_of_days(text) {
return None;
}
Some(1)
}
fn is_a_unit_of_the_time(text: &str) -> bool {
let text = text.trim().to_lowercase();
match text.chars().next() {
Some(letter) => matches!(letter, 's' | 'm' | 'h' | 'd'),
None => false,
}
}
fn the_attributes_of_a_file(texts: &[String]) -> Option<usize> {
let text = the_text_at(texts, 0).trim();
let wanted = text.chars().all(|character| {
matches!(
character,
'+' | '-'
| 'R'
| 'r'
| 'A'
| 'a'
| 'H'
| 'h'
| 'S'
| 's'
| 'N'
| 'n'
| 'T'
| 't'
| 'O'
| 'o'
)
});
if wanted {
return None;
}
Some(0)
}