From a3bc9bb32b3c3ef98c581a8e95d13a9cd1ee0317 Mon Sep 17 00:00:00 2001 From: rocky Date: Thu, 23 Dec 2021 16:11:25 -0500 Subject: [PATCH] Add operator precedence to -T output --- uncompyle6/semantics/pysource.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index e2701842..b1fa68aa 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -223,7 +223,7 @@ class SourceWalker(GenericASTTraversal, object): If `showast' is True, we print the syntax tree. - `compile_mode' is is either 'exec' or 'single'. It isthe compile + `compile_mode' is is either 'exec' or 'single'. It is the compile mode that was used to create the Syntax Tree and specifies a gramar variant within a Python version to use. @@ -288,6 +288,7 @@ class SourceWalker(GenericASTTraversal, object): # An example is: # __module__ = __name__ self.hide_internal = True + self.compile_mode = "exec" self.name = None self.version = version self.is_pypy = is_pypy @@ -308,21 +309,24 @@ class SourceWalker(GenericASTTraversal, object): if isinstance(self.showast, dict) and self.showast.get: maybe_show_tree(self, ast) - def str_with_template(self, ast): + def str_with_template(self, ast) -> str: stream = sys.stdout stream.write(self.str_with_template1(ast, "", None)) stream.write("\n") - def str_with_template1(self, ast, indent, sibNum=None): + def str_with_template1(self, ast, indent, sibNum=None) -> str: rv = str(ast.kind) if sibNum is not None: rv = "%2d. %s" % (sibNum, rv) enumerate_children = False if len(ast) > 1: - rv += " (%d)" % (len(ast)) + rv += f" ({len(ast)})" enumerate_children = True + if ast in PRECEDENCE: + rv += f", precedence {PRECEDENCE[ast]}" + mapping = self._get_mapping(ast) table = mapping[0] key = ast @@ -2075,6 +2079,7 @@ class SourceWalker(GenericASTTraversal, object): arg += 1 elif typ == "p": p = self.prec + # entry[arg] tup = entry[arg] assert isinstance(tup, tuple) if len(tup) == 3: @@ -2443,7 +2448,7 @@ class SourceWalker(GenericASTTraversal, object): returnNone=False, debug_opts=DEFAULT_DEBUG_OPTS, ): - """convert SyntaxTree to Python source code""" + """convert parse tree to Python source code""" rn = self.return_none self.return_none = returnNone