Skip to content

Commit

Permalink
Value - try to match common complex expressions to a type (#3573)
Browse files Browse the repository at this point in the history
* Value - try to match common complex expressions to a type

* more matches
  • Loading branch information
PabstMirror authored Feb 11, 2025
1 parent 90c5e69 commit fa45c8d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
27 changes: 26 additions & 1 deletion bin-parse/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, path::Path};

use arma3_wiki::model::{Command, ParseError};
use arma3_wiki::model::{Command, ParseError, Value};
use arma3_wiki_github::report::Report;
use indicatif::ProgressBar;
use regex::Regex;
Expand Down Expand Up @@ -50,8 +50,33 @@ pub async fn list() -> HashMap<String, String> {
pub async fn commands(report: &mut Report, args: &[String], dry_run: bool) {
let commands = if args.is_empty() {
list().await
} else if args.iter().any(|arg| arg == "--bads") {
let mut bads = HashMap::new();
let wiki = arma3_wiki::Wiki::load_dist();
for (_, cmd) in wiki.commands().iter() {
let cmd_name_cased = cmd.name();
if cmd.syntax().iter().any(|syn| {
if syn.ret().0 == Value::Unknown {
println!("cmd {:?} has unknown ret {:?}", cmd_name_cased, syn.ret());
return true;
}
if syn.params().iter().any(|p| *p.typ() == Value::Unknown) {
println!("cmd {:?} has unknown param {:?}", cmd_name_cased, syn.ret());
return true;
}
false
}) {
bads.insert(
cmd_name_cased.to_string(),
format!("https://community.bistudio.com/wiki/{cmd_name_cased}"),
);
}
}
println!("Checking {} bad commands", bads.len());
bads
} else {
args.iter()
.filter(|arg| !arg.starts_with("--"))
.map(|arg| {
(
arg.clone(),
Expand Down
9 changes: 7 additions & 2 deletions clients/rust/src/model/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,13 @@ impl Syntax {
ret = ret_trim.trim().to_string();
}
if ret.contains(" format") {
errors.push(ParseError::Syntax(ret));
(Value::Unknown, None)
Value::match_explicit(&ret).map_or_else(
|| {
errors.push(ParseError::Syntax(ret));
(Value::Unknown, None)
},
|explicit_match| (explicit_match, None),
)
} else {
let (typ, desc) = ret.split_once('-').unwrap_or((&ret, ""));
let typ = typ.trim();
Expand Down
24 changes: 23 additions & 1 deletion clients/rust/src/model/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ impl Value {
/// # Panics
/// Panics if the regex fails to compile.
pub fn from_wiki(source: &str) -> Result<Self, String> {
if let Some(explicit_match) = Self::match_explicit(source) {
return Ok(explicit_match);
}
let regex_type = REGEX_TYPE.get_or_init(|| Regex::new(r"(?m)\[\[([^\[\]]+)\]\]").unwrap());
// let regex_array_in_format = REGEX_ARRAY_IN_FORMAT
// .get_or_init(|| Regex::new(r"(?m)\[\[Array\]\] in format \[\[([^\[\]]+)\]\]").unwrap());
Expand All @@ -107,6 +110,23 @@ impl Value {
Err("Unknown value".to_string())
}

#[must_use]
/// try to match common complex expressions to a value type
pub fn match_explicit(source: &str) -> Option<Self> {
match source.trim() {
"[[Array]] format [[Position]]" => { Some(Self::Position) }
"[[Array]] format [[Position#PositionATL]]" | "[[Array]] format [[Position#PositionATL|PositionATL]]" => { Some(Self::Position3dATL) }
"[[Array]] format [[Position#PositionAGL|PositionAGL]]" |"[[Position#PositionAGL|PositionAGL]]" | "[[Array]] - world position in format [[Position#PositionAGL|PositionAGL]]" | "[[Array]] format [[Position#PositionAGL|PositionAGL]] - translated world position" | "[[Array]] - position in format [[Position#PositionAGL|PositionAGL]]" | "[[Array]] - camera world position, format [[Position#PositionAGL|PositionAGL]]" => { Some(Self::Position3dAGL) }
"[[Array]] format [[Position#PositionAGLS|PositionAGLS]]" | "[[Position#PositionAGLS|PositionAGLS]]" => { Some(Self::Position3dAGLS) }
"[[Array]] format [[Position#PositionASL|PositionASL]]" | "[[Position#PositionASL|PositionASL]]" | "[[Array]] - format [[Position#PositionASL|PositionASL]]" => { Some(Self::Position3dASL) }
"[[Array]] format [[Position#PositionASLW|PositionASLW]]" | "[[Position#PositionASLW|PositionASLW]]" => { Some(Self::Position3DASLW) }
"[[Number]] in range 0..1" | "[[Number]] of control" => { Some(Self::Number) }
"[[Color|Color (RGBA)]]" | "[[Array]] of [[Color|Color (RGB)]]" | "[[Array]] format [[Color|Color (RGB)]]" | "[[Array]] of [[Color|Color (RGBA)]]" | "[[Array]] format [[Color|Color (RGBA)]]" | "[[Array]] in format [[Color|Color (RGBA)]]" | "[[Array]] format [[Color|Color (RGBA)]] - text color" => { Some(Self::ArrayColor) }
"[[Array]] with [[Anything]]" | "[[Array]] of [[Team Member]]s" | "[[Array]] of [[Location]]s" | "[[Array]] of [[Boolean]]s" | "[[Array]] of [[Waypoint]]s" | "[[Array]] of [[Group]]s"| "[[Array]] of [[Object]]s" | "[[Array]] - format [[Vector3D]]" | "[[Array]] format [[Vector3D]]" | "[[Array]] of [[Position]]s" | "[[Array]] format [[Waypoint]]" | "[[Array]] of [[String]] and/or [[Structured Text]]" | "[[Array]] format [[ParticleArray]]" | "[[Array]] of [[Number]]s, where each number represents index of currently active effect layer" | "[[Array]] of [[Number]]s" | "[[Array]] of [[Number]]s of any size" | "[[Array]] of GUI [[Display]]s" | "[[Array]] of [[Control]]s" | "[[Array]] of [[String]]" |"[[Array]] of string"| "[[Array]] of [[String]]s" | "[[Array]] of [[Number]]" | "[[Array]] format [[Turret Path]]" => { Some(Self::ArrayUnknown) }
_ => {None}
}
}

/// Parses a single value from a string.
///
/// # Errors
Expand All @@ -126,6 +146,7 @@ impl Value {
"exception handle" | "exceptionhandle" => Ok(Self::ExceptionHandle),
"for type" | "fortype" => Ok(Self::ForType),
"group" => Ok(Self::Group),
"hashmap" => Ok(Self::HashMapUnknown),
"if type" | "iftype" => Ok(Self::IfType),
"location" => Ok(Self::Location),
"namespace" => Ok(Self::Namespace),
Expand All @@ -135,7 +156,7 @@ impl Value {
"script handle" | "scripthandle" => Ok(Self::ScriptHandle),
"side" => Ok(Self::Side),
"string" => Ok(Self::String),
"structuredtext" => Ok(Self::StructuredText),
"structured text" | "structuredtext" => Ok(Self::StructuredText),
"switch type" | "switchtype" => Ok(Self::SwitchType),
"task" => Ok(Self::Task),
"team member" | "teammember" => Ok(Self::TeamMember),
Expand Down Expand Up @@ -259,5 +280,6 @@ mod tests {
);
assert_eq!(Value::from_wiki("[[Number]]"), Ok(Value::Number));
assert_eq!(Value::from_wiki("[[Object]]"), Ok(Value::Object));
assert_eq!(Value::from_wiki("[[Array]] with [[Anything]]"), Ok(Value::ArrayUnknown));
}
}

0 comments on commit fa45c8d

Please sign in to comment.