From 89a533173b69b24430d3e42a0e98de29fce76c33 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Tue, 23 Jul 2024 22:08:20 -0700 Subject: [PATCH] typegen: detect flags in enum namespaces Currently doesn't mark them as flags or work outside of enum namespaces, just a baseline so they show up at all. --- typegen/src/parse.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/typegen/src/parse.rs b/typegen/src/parse.rs index 2a8d7f8..172d7f8 100644 --- a/typegen/src/parse.rs +++ b/typegen/src/parse.rs @@ -154,7 +154,7 @@ impl Parser { fn_param_regex: Regex::new(r#"(const\s+)?(?(\w|::|<|>)+\*?)&?\s+(?\w+)(,|$)"#).unwrap(), signals_regex: Regex::new(r#"signals:(?(\s*(\s*///.*\s*)*void .*;)*)"#).unwrap(), defaultprop_classinfo_regex: Regex::new(r#"^\s*"DefaultProperty", "(?.+)"\s*$"#).unwrap(), - enum_ns_regex: Regex::new(r#"(?(\s*\/\/\/.*\n)+)?\s*namespace (?\w+)\s*\{[\s\S]*?(QML_ELEMENT|QML_NAMED_ELEMENT\((?\w+)\));[\s\S]*?enum\s*(?\w+)\s*\{(?[\s\S]*?)\};[\s\S]*?\}"#).unwrap(), + enum_ns_regex: Regex::new(r#"(?(\s*\/\/\/.*\n)+)?\s*namespace (?\w+)\s*\{(?[\s\S]*?(QML_ELEMENT|QML_NAMED_ELEMENT\((?\w+)\));[\s\S]*?enum\s*(?\w+)\s*\{(?[\s\S]*?)\};[\s\S]*?)\}"#).unwrap(), enum_class_regex: Regex::new(r#"(?(\s*\/\/\/.*\n)+)?\s*enum\s*(?\w+)\s*\{(?[\s\S]*?)\};\s+Q_ENUM\(.+\);"#).unwrap(), enum_variant_regex: Regex::new(r#"(?(\s*\/\/\/.*\n)+)?\s*(?\w+)\s*=\s*.+,"#).unwrap(), } @@ -384,14 +384,41 @@ impl Parser { let comment = enum_.name("comment").map(|m| m.as_str()); let namespace = enum_.name("namespace").unwrap().as_str(); - let enum_name = enum_.name("enum_name").unwrap().as_str(); + let mut enum_name = enum_.name("enum_name").unwrap().as_str(); let qml_name = enum_ .name("qml_name") .map(|m| m.as_str()) .unwrap_or(namespace); + let nsbody = enum_.name("nsbody").unwrap().as_str(); let body = enum_.name("body").unwrap().as_str(); let variants = self.parse_enum_variants(body, ctx)?; + for macro_ in self.macro_regex.captures_iter(nsbody) { + let macro_ = macro_?; + + let type_ = macro_.name("type").unwrap().as_str(); + let args = macro_.name("args").map(|m| m.as_str()); + + (|| { + match type_ { + "Q_DECLARE_FLAGS" => { + enum_name = args + .expect("Q_DECLARE_FLAGS must have arguments") + .split_once(',') + .expect("Q_DECLARE_FLAGS must have two arguments") + .0 + .trim(); + }, + _ => {}, + } + + Ok::<_, anyhow::Error>(()) + })() + .with_context(|| { + format!("while parsing macro `{}`", macro_.get(0).unwrap().as_str()) + })?; + } + ctx.enums.push(EnumInfo { namespace, enum_name,