|
7 | 7 |
|
8 | 8 | header_file = "".join(open(header_file_path, "r").readlines())
|
9 | 9 |
|
10 |
| -# https://regex101.com/r/weFtWH/1 |
11 |
| -matches = re.findall(r"([a-zA-Z0-9_]+)\s+([a-zA-Z0-9_]+)\s*\((([a-zA-Z0-9_*\s,]+)*)\);", header_file) |
| 10 | +identifier = r"[a-zA-Z0-9_]+" |
| 11 | +variable = r"(?:(?:const|volatile)\s)*\s*" + identifier + r"\s*\**\s*(?:const\s*)?(" + identifier + r")(?:\[[0-9]*\])*" |
| 12 | +function_declaration = r"(" + variable + r")\s*\(((?:" + variable + r"\s*,\s*)*(?:" + variable + r"|void))\)\s*;" |
| 13 | + |
| 14 | +matches = re.findall(function_declaration, header_file) |
12 | 15 | functions = []
|
13 | 16 | for match in matches:
|
14 |
| - return_type = match[0] |
| 17 | + return_type_with_name = match[0].strip() |
15 | 18 | name = match[1]
|
16 | 19 | args = match[2].split(",")
|
17 |
| - args_with_name = [] |
| 20 | + arg_with_names = [] |
18 | 21 | if not (len(args) == 1 and args[0] == "void"):
|
19 | 22 | for arg in args:
|
20 |
| - # https://regex101.com/r/weFtWH/2 |
21 |
| - arg_match = re.search(r"\s*([a-zA-Z0-9_]+\s+)*([a-zA-Z0-9_]+)\s+\*?([a-zA-Z0-9_]+)", arg) |
22 |
| - args_with_name.append((arg, arg_match.groups()[-1])) |
23 |
| - |
24 |
| - functions.append((return_type, name, args_with_name)) |
| 23 | + arg = arg.strip() |
| 24 | + var_result = re.search(variable, arg) |
| 25 | + arg_with_names.append((arg, var_result.group(1))) |
| 26 | + functions.append((return_type_with_name, name, arg_with_names)) |
25 | 27 |
|
26 | 28 | # https://regex101.com/r/weFtWH/3
|
27 |
| -matches = re.findall(r"extern\s+([a-zA-Z0-9_]+\s+[a-zA-Z0-9_]+\s*(,\s*[a-zA-Z0-9_]+\s*)*;)", header_file) |
| 29 | +matches = re.findall(r"extern\s+("+variable+r";)", header_file) |
28 | 30 |
|
29 | 31 | variables = []
|
30 | 32 | for match in matches:
|
|
52 | 54 | f"#include \"{module_name}.hpp\"\n"
|
53 | 55 | f"namespace mock {{Base<{function_list}> {module_name}; }}\n\n"
|
54 | 56 | )
|
55 |
| -for return_type, name, args_with_name in functions: |
56 |
| - args = ", ".join([arg[0] for arg in args_with_name]) |
| 57 | +for return_type_with_name, name, args_with_name in functions: |
| 58 | + args = ", ".join([f"{arg[0]}" for arg in args_with_name]) |
57 | 59 | args_forward = ", ".join([arg[1] for arg in args_with_name])
|
58 | 60 |
|
59 |
| - return_statement = "return" if return_type != "void" else "" |
| 61 | + return_statement = "return" if return_type_with_name.startswith("void") else "" |
60 | 62 |
|
61 | 63 | mock_src.write(
|
62 |
| - f"{return_type} {name} ({args}) {{\n" |
| 64 | + f"{return_type_with_name}({args}) {{\n" |
63 | 65 | f"\t{return_statement} mock::{module_name}.functionCallDelegate<{name}>({args_forward});\n"
|
64 | 66 | f"}}\n\n"
|
65 | 67 | )
|
|
0 commit comments