implement parsing of enum docstrings
MODIFY
build.rs
build.rs
@@ -77,6 +77,7 @@ fn get_modules(opencv_dir_as_string: &str) -> Result<&'static Vec<(String, Vec<S
"core/fast_math.hpp", // contains functions with Rust native counterparts
"core/private.hpp",
"core/utils/filesystem.hpp", // contains functions with Rust native counterparts
+ "core/utils/logger.defines.hpp", // duplicate definitions of log values
"dnn/blob.hpp",
"ios.h",
"ippasync.hpp",

MODIFY
gen_rust.py
gen_rust.py
@@ -14,6 +14,8 @@ from string import Template
# fixme returning MatAllocator (trait) by reference is bad, check knearestneighbour
# fixme add support for arrays in dnn::DictValue
# fixme VectorOfMat::get allows to mutate?
+# fixme get comments from HOUGH_PROBABILISTIC in imgproc
+# fixme get multiline comments from LSD_REFINE_ADV in imgproc
def template(text):
"""
@@ -1502,9 +1504,9 @@ class ClassInfo(GeneralInfo):
class ConstInfo(GeneralInfo):
TEMPLATES = {
- "rust_string": template("${doccomment}pub const ${name}: &'static str = ${value};\n"),
- "rust_int": template("${doccomment}pub const ${name}: i32 = ${value};\n"),
- "rust_usize": template("${doccomment}pub const ${name}: usize = ${value};\n"),
+ "rust_string": template("${doc_comment}pub const ${name}: &'static str = ${value};\n"),
+ "rust_int": template("${doc_comment}pub const ${name}: i32 = ${value};\n"),
+ "rust_usize": template("${doc_comment}pub const ${name}: usize = ${value};\n"),
"cpp_string": template(""" printf("pub static ${name}: &'static str = \\"%s\\";\\n", ${full_name});\n"""),
"cpp_double": template(""" printf("pub const ${name}: f64 = %f;\\n", ${full_name});\n"""),
"cpp_int": template(""" printf("pub const ${name}: i32 = 0x%x; // %i\\n", ${full_name}, ${full_name});\n"""),
@@ -1521,6 +1523,7 @@ class ConstInfo(GeneralInfo):
self.rustname = self.rustname.replace("::", "_")
self.cname = self.name.replace(".", "::")
self.value = decl[1]
+ self.comment = decl[5] if len(decl) >= 6 else ""
def __repr__(self):
return template("CONST $name=$value").substitute(name=self.name, value=self.value)
@@ -1537,11 +1540,15 @@ class ConstInfo(GeneralInfo):
"value": self.value,
}
while True:
- params["doccomment"] = ""
+ if self.comment:
+ params["doc_comment"] = self.gen.reformat_doc(self.comment)
+ else:
+ params["doc_comment"] = ""
m = re.match(r"^(.+?)\s*(?://\s*(.+)|/\*+\s*(.+?)\s*\*+/)$", params["value"]) # xxx // comment OR xxx /** comment */
if m:
params["value"] = m.group(1)
- params["doccomment"] = "/// {}\n".format(m.group(3) if m.group(2) is None else m.group(2))
+ if not self.comment:
+ params["doc_comment"] = self.gen.reformat_doc(m.group(3) if m.group(2) is None else m.group(2))
if params["value"].startswith('"'):
return ConstInfo.TEMPLATES["rust_string"].substitute(params)
elif self.rustname in ("Mat_AUTO_STEP",):
@@ -1648,10 +1655,10 @@ class EnumInfo(GeneralInfo):
"""),
"rust_const": template("""
- ${name} = ${rustname} as isize,
+ ${doc_comment}${name} = ${rustname} as isize,
"""),
"rust_const_ignored": template("""
- // ${name} = ${rustname} as isize, // duplicate discriminant
+ ${doc_comment}// ${name} = ${rustname} as isize, // duplicate discriminant
"""),
}
@@ -1682,10 +1689,14 @@ class EnumInfo(GeneralInfo):
ignore_discriminants = enum_ignore_discriminant.get(self.fullname, set())
for const in self.consts:
if const.name in ignore_discriminants:
+ doc_comment = self.gen.reformat_doc(const.comment, comment_prefix="//")
tpl = EnumInfo.TEMPLATES["rust_const_ignored"]
else:
+ doc_comment = self.gen.reformat_doc(const.comment)
tpl = EnumInfo.TEMPLATES["rust_const"]
- consts.append(tpl.substitute(const.__dict__))
+ consts.append(tpl.substitute(combine_dicts(const.__dict__, {
+ "doc_comment": doc_comment,
+ })))
return EnumInfo.TEMPLATES["rust"].substitute(combine_dicts(self.__dict__, {
"doc_comment": self.gen.reformat_doc(self.comment),
"consts": indent("".join(consts)),

MODIFY
hdr_parser.py
hdr_parser.py
@@ -238,7 +238,7 @@ class CppHeaderParser(object):
return arg_type, arg_name, modlist, argno
- def parse_enum(self, decl_str):
+ def parse_enum(self, decl_str, enum_docstrings):
l = decl_str
ll = l.split(",")
if ll[-1].strip() == "":
@@ -247,6 +247,7 @@ class CppHeaderParser(object):
prev_val_delta = -1
decl = []
for pair in ll:
+ comment = enum_docstrings.get(pair.strip(), "")
pv = pair.split("=")
if len(pv) == 1:
prev_val_delta += 1
@@ -257,7 +258,7 @@ class CppHeaderParser(object):
else:
prev_val_delta = 0
prev_val = val = pv[1].strip()
- decl.append(["const " + self.get_dotted_name(pv[0].strip()), val, [], [], None, ""])
+ decl.append(["const " + self.get_dotted_name(pv[0].strip()), val, [], [], None, comment])
return decl
def parse_class_decl(self, decl_str):
@@ -694,7 +695,7 @@ class CppHeaderParser(object):
return ["typedef {}".format(self.get_dotted_name(m.group(2))), m.group(1), "", [], None, docstring]
return None
- def parse_stmt(self, stmt, end_token, mat="Mat", docstring=""):
+ def parse_stmt(self, stmt, end_token, mat="Mat", docstring="", enum_docstrings={}):
"""
parses the statement (ending with ';' or '}') or a block head (ending with '{')
@@ -776,7 +777,7 @@ class CppHeaderParser(object):
return "namespace", "", True, None
if end_token == "}" and context.startswith("enum"):
- decl = self.parse_enum(stmt)
+ decl = self.parse_enum(stmt, enum_docstrings)
name = stack_top[self.BLOCK_NAME]
return context, name, False, decl
@@ -855,6 +856,7 @@ class CppHeaderParser(object):
self.block_stack = [["file", hname, True, True, None, True]]
block_head = ""
docstring = ""
+ enum_docstrings = {}
self.lineno = 0
self.wrap_mode = wmode
@@ -923,7 +925,8 @@ class CppHeaderParser(object):
break
if token == "//!":
- block_head += " " + l[:pos]
+ stmt_add = l[:pos]
+ block_head += " " + stmt_add
if self.block_stack[-1][self.PROCESS_FLAG]:
doc_line = l[pos+3:]
if doc_line and doc_line[0] == "<":
@@ -938,6 +941,8 @@ class CppHeaderParser(object):
decl = stack_top[self.CLASS_DECL]
if len(decl[3]) >= 1:
decl[3][-1][2] += doc_line
+ elif ctx_type == "enum":
+ enum_docstrings[" ".join(stmt_add.replace(",", "").split()).strip()] = doc_line + "\n"
else:
docstring += doc_line.lstrip() + "\n"
break
@@ -989,7 +994,8 @@ class CppHeaderParser(object):
# even if stack_top[PUBLIC_SECTION] is False, we still try to process the statement,
# since it can start with "public:"
docstring = docstring.strip()
- stmt_type, name, parse_flag, decl = self.parse_stmt(stmt, token, docstring=docstring)
+ stmt_type, name, parse_flag, decl = self.parse_stmt(stmt, token, docstring=docstring, enum_docstrings=enum_docstrings)
+ enum_docstrings = {}
if decl:
line_stmt_type = stmt_type
if stmt_type.startswith("enum"):

implement parsing of enum docstrings