From 07c3bd51410449b4e143e1c7b75a4e8d3d31f6b1 Mon Sep 17 00:00:00 2001 From: Martin Taibr Date: Sun, 22 Oct 2017 22:55:06 +0200 Subject: [PATCH] do nothing in a more complicated way ... for now --- qcsrc/tools/unc-chained-if-fix.py | 76 +++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/qcsrc/tools/unc-chained-if-fix.py b/qcsrc/tools/unc-chained-if-fix.py index 8186372ee..994815c0f 100755 --- a/qcsrc/tools/unc-chained-if-fix.py +++ b/qcsrc/tools/unc-chained-if-fix.py @@ -9,9 +9,10 @@ CHAINED_IF_REGEX = regex.compile(r""" (? [\t ]* ) -if[\t ]*\( # the first condition - (? - (? +if(?[\t ]*) # the first if +\( + (? # the condition itself + (? # various parts of the condition, not exactly an expression but anything separated by parens [^()\n]* | [^()\n]*\( @@ -19,12 +20,16 @@ if[\t ]*\( # the first condition \)[^()\n]* )+ ) -\)[\t ]* -(?://.*|/\*.*\*/)? # comments +\) +(? # trailing comments + [\t ]* + (?://.*|/\*.*\*/)? +) \n -(?: # all subsequent conditions with the same indentation +(?: # all subsequent ifs with the same indentation \1 - if[\t ]*\( + if[\t ]* + \( (? (? [^()\n]* @@ -34,8 +39,11 @@ if[\t ]*\( # the first condition \)[^()\n]* )+ ) - \)[\t ]* - (?://.*|/\*.*\*/)? + \) + (? + [\t ]* + (?://.*|/\*.*\*/)? + ) \n )+ """, regex.VERBOSE) @@ -58,19 +66,57 @@ for file_name in all_files: global total total += 1 + indent = match.group('indent') + whitespace = match.group('ws') + first_cond = match.group('cond') + first_comment = match.group('comm') + + other_conds = match.captures('conds') + other_comments = match.captures('comms') + assert len(middle_conds) == len(middle_comments) + + middle_conds = other_conds[0:-1] + middle_comments = other_comments[0:-1] + + last_cond = other_conds[-1] + last_comment = other_comments[-1] + + #logical_ops = '||' in match.group(0) or '^' in match.group(0) + 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("indent:", indent) + print("first cond:", first_cond) + print("first comm:", first_comment) + print("other conds:", other_conds) + print("other comms:", other_comments) + #print("captures expr1:", match.captures('expr1')) + #print("captures expr2:", match.captures('expr2')) + #if logical_ops: # TODO per cond + # print("WARNING - logical ops") print() print() - return str(match.group(0)) + def fix_logical_ops(cond): + if '||' in cond or '^' in cond: + return '(' + cond + ')' + else: + return cond + + replacement = "\n{}if{}({}){}\n".format(indent, whitespace, fix_logical_ops(first_cond), first_comment) + for cond, comm in zip(middle_conds, middle_comments): + replacement += "{}if{}({}){}\n".format(indent, whitespace, fix_logical_ops(cond), comm) + replacement += "{}if{}({}){}\n".format(indent, whitespace, fix_logical_ops(last_cond), last_comment) + + #replacement = "\n{}if{}({}{}\n".format(indent, whitespace, fix_logical_ops(first_cond), first_comment) + #for cond, comm in zip(middle_conds, middle_comments): + # replacement += "{}\t&& {}{}\n".format(indent, fix_logical_ops(cond), comm) + #replacement += "{}\t&& {}){}\n".format(indent, fix_logical_ops(last_cond), last_comment) + + #return str(match.group(0)) + return replacement old_code = f.read() new_code = CHAINED_IF_REGEX.sub(repl, old_code) -- 2.39.2