From 86ca50592e67ff8902e3e223a1841d270de917cd Mon Sep 17 00:00:00 2001 From: Martin Taibr Date: Sun, 22 Oct 2017 02:17:56 +0200 Subject: [PATCH] find all chained ifs... and do precisely nothing with them... for now --- qcsrc/tools/unc-chained-if-fix.py | 84 +++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100755 qcsrc/tools/unc-chained-if-fix.py diff --git a/qcsrc/tools/unc-chained-if-fix.py b/qcsrc/tools/unc-chained-if-fix.py new file mode 100755 index 000000000..8186372ee --- /dev/null +++ b/qcsrc/tools/unc-chained-if-fix.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 + +import regex # needs regex instead of re for multiple captures of a group +import glob + +# this is soooooo not regular ;) +CHAINED_IF_REGEX = regex.compile(r""" +(?:^|\n) +(? + [\t ]* +) +if[\t ]*\( # the first condition + (? + (? + [^()\n]* + | + [^()\n]*\( + (?&expr1) # this is to match nested parentheses correctly + \)[^()\n]* + )+ + ) +\)[\t ]* +(?://.*|/\*.*\*/)? # comments +\n +(?: # all subsequent conditions with the same indentation + \1 + if[\t ]*\( + (? + (? + [^()\n]* + | + [^()\n]*\( + (?&expr2) + \)[^()\n]* + )+ + ) + \)[\t ]* + (?://.*|/\*.*\*/)? + \n +)+ +""", regex.VERBOSE) + +all_files = set(glob.glob('**/*.qc', recursive=True) + glob.glob('**/*.qh', recursive=True)) +all_files = sorted({f for f in all_files if not f.startswith('qcsrc/dpdefs')}) + +if 0: # for debugging + all_files = ["qcsrc/common/mutators/mutator/campcheck/sv_campcheck.qc", "qcsrc/common/weapons/weapon/minelayer.qc", "qcsrc/server/bot/default/navigation.qc"] + +total = 0 +for file_name in all_files: + with open(file_name, 'r+') as f: + print() + print(file_name) + print("=================") + print() + + def repl(match): + global total + total += 1 + + print("whole match:") + print(match.group(0)) + print("lines:", len(match.group(0).split('\n'))) + print("captures 1:", match.captures('indent')) + print("captures 2:", match.captures('cond')) + print("captures 3:", match.captures('conds')) + if '||' in match.group(0) or '^' in match.group(0): + print("WARNING - logical ops") + + print() + print() + + return str(match.group(0)) + + old_code = f.read() + new_code = CHAINED_IF_REGEX.sub(repl, old_code) + #with open('new-code', 'w') as f: + # f.write(new_code) + + f.seek(0) + f.truncate() + f.write(new_code) + +print("total if chains:", total) -- 2.39.2