已合并
fix(merge_proto): 修复带注释/预处理守卫的proto解析,避免误吞额外内容 #1095
liu-wei创建于 7月7日
fix(merge_proto): 修复带注释/预处理守卫的proto解析,避免误吞额外内容 #1095
已合并
共 1 个文件变更+18-9
| @@ -16,16 +16,26 @@ import re | |||
| 16 | import argparse | 16 | import argparse |
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | +OP_DEF_PATTERN = re.compile( | ||
| 20 | + r"(?:[ \t]*/\*\*?(?:[^*]|\*(?!/))*?\*/[ \t]*\n\s*)?" | ||
| 21 | + r"(?P<guard>[ \t]*#\s*ifndef\s+\w+[^\n]*\n" | ||
| 22 | + r"[ \t]*#\s*define\s+\w+[^\n]*\n\s*)?" | ||
| 23 | + r"(?P<opdef>REG_OP\((?P<opname>.+?)\)" | ||
| 24 | + r".*?OP_END_FACTORY_REG\((?P=opname)\))" | ||
| 25 | + r"(?(guard)[^\n]*\n[ \t]*#\s*endif[^\n]*)", | ||
| 26 | + re.DOTALL, | ||
| 27 | +) | ||
| 28 | + | ||
| 29 | + | ||
| 19 | def match_op_proto(file_path): | 30 | def match_op_proto(file_path): |
| 20 | with open(file_path, 'r', encoding='utf-8') as f: | 31 | with open(file_path, 'r', encoding='utf-8') as f: |
| 21 | content = f.read() | 32 | content = f.read() |
| 22 | 33 | ||
| 23 | - op_def_pattern = re.compile(r"REG_OP\((.+)\).*OP_END_FACTORY_REG\(\1\)", re.DOTALL) | 34 | + match = OP_DEF_PATTERN.search(content) |
| 24 | - match = op_def_pattern.search(content) | ||
| 25 | 35 | ||
| 26 | if match: | 36 | if match: |
| 27 | - op_name = match.group(1) | 37 | + op_name = match.group("opname") |
| 28 | - op_def = match.group(0) | 38 | + op_def = match.group("opdef") |
| 29 | return op_name, op_def | 39 | return op_name, op_def |
| 30 | else: | 40 | else: |
| 31 | return None, None | 41 | return None, None |
| @@ -36,13 +46,12 @@ def match_op_proto_extend(file_path): | |||
| 36 | with open(file_path, 'r', encoding='utf-8') as f: | 46 | with open(file_path, 'r', encoding='utf-8') as f: |
| 37 | content = f.read() | 47 | content = f.read() |
| 38 | 48 | ||
| 39 | - op_def_pattern = re.compile(r"REG_OP\((.+)\).*OP_END_FACTORY_REG\(\1\)", re.DOTALL) | 49 | + matches = OP_DEF_PATTERN.finditer(content) |
| 40 | - matches = op_def_pattern.finditer(content) | ||
| 41 | 50 | ||
| 42 | results = [] | 51 | results = [] |
| 43 | for match in matches: | 52 | for match in matches: |
| 44 | - op_name = match.group(1) | 53 | + op_name = match.group("opname") |
| 45 | - op_def = match.group(0) | 54 | + op_def = match.group("opdef") |
| 46 | results.append((op_name, op_def)) | 55 | results.append((op_name, op_def)) |
| 47 | return results | 56 | return results |
| 48 | 57 | ||
| @@ -93,4 +102,4 @@ if __name__ == "__main__": | |||
| 93 | 102 | ||
| 94 | protos_path = args.protos[1:] | 103 | protos_path = args.protos[1:] |
| 95 | output_file = args.output_file[0] | 104 | output_file = args.output_file[0] |
| 96 | - merge_op_proto(protos_path, output_file) | 105 | + merge_op_proto(protos_path, output_file) |