diff options
author | Daniel Mensinger <daniel@mensinger-ka.de> | 2022-04-23 12:56:52 +0200 |
---|---|---|
committer | Eli Schwartz <eschwartz93@gmail.com> | 2022-05-24 18:00:49 -0400 |
commit | 7ef73e8f6a2555ffcbf6c10db4085236a64514a5 (patch) | |
tree | b43ac98ceaf6a6b1cf331e6bd68ff2b5c8b86400 /mesonbuild/ast/printer.py | |
parent | c88c1e7dba168e62fe52f677717ec75ceaa84d5c (diff) | |
download | meson-7ef73e8f6a2555ffcbf6c10db4085236a64514a5.zip meson-7ef73e8f6a2555ffcbf6c10db4085236a64514a5.tar.gz meson-7ef73e8f6a2555ffcbf6c10db4085236a64514a5.tar.bz2 |
ast: cmake: Generate line numbers while printing the AST for better debugging
Diffstat (limited to 'mesonbuild/ast/printer.py')
-rw-r--r-- | mesonbuild/ast/printer.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/mesonbuild/ast/printer.py b/mesonbuild/ast/printer.py index f185449..a0b92bb 100644 --- a/mesonbuild/ast/printer.py +++ b/mesonbuild/ast/printer.py @@ -29,13 +29,14 @@ arithmic_map = { } class AstPrinter(AstVisitor): - def __init__(self, indent: int = 2, arg_newline_cutoff: int = 5): + def __init__(self, indent: int = 2, arg_newline_cutoff: int = 5, update_ast_line_nos: bool = False): self.result = '' self.indent = indent self.arg_newline_cutoff = arg_newline_cutoff self.ci = '' self.is_newline = True self.last_level = 0 + self.curr_line = 1 if update_ast_line_nos else None def post_process(self) -> None: self.result = re.sub(r'\s+\n', '\n', self.result) @@ -55,37 +56,48 @@ class AstPrinter(AstVisitor): def newline(self) -> None: self.result += '\n' self.is_newline = True + if self.curr_line is not None: + self.curr_line += 1 def visit_BooleanNode(self, node: mparser.BooleanNode) -> None: self.append('true' if node.value else 'false', node) + node.lineno = self.curr_line or node.lineno def visit_IdNode(self, node: mparser.IdNode) -> None: assert isinstance(node.value, str) self.append(node.value, node) + node.lineno = self.curr_line or node.lineno def visit_NumberNode(self, node: mparser.NumberNode) -> None: self.append(str(node.value), node) + node.lineno = self.curr_line or node.lineno def visit_StringNode(self, node: mparser.StringNode) -> None: assert isinstance(node.value, str) self.append("'" + node.value + "'", node) + node.lineno = self.curr_line or node.lineno def visit_FormatStringNode(self, node: mparser.FormatStringNode) -> None: assert isinstance(node.value, str) self.append("f'" + node.value + "'", node) + node.lineno = self.curr_line or node.lineno def visit_ContinueNode(self, node: mparser.ContinueNode) -> None: self.append('continue', node) + node.lineno = self.curr_line or node.lineno def visit_BreakNode(self, node: mparser.BreakNode) -> None: self.append('break', node) + node.lineno = self.curr_line or node.lineno def visit_ArrayNode(self, node: mparser.ArrayNode) -> None: + node.lineno = self.curr_line or node.lineno self.append('[', node) node.args.accept(self) self.append(']', node) def visit_DictNode(self, node: mparser.DictNode) -> None: + node.lineno = self.curr_line or node.lineno self.append('{', node) node.args.accept(self) self.append('}', node) @@ -93,58 +105,70 @@ class AstPrinter(AstVisitor): def visit_OrNode(self, node: mparser.OrNode) -> None: node.left.accept(self) self.append_padded('or', node) + node.lineno = self.curr_line or node.lineno node.right.accept(self) def visit_AndNode(self, node: mparser.AndNode) -> None: node.left.accept(self) self.append_padded('and', node) + node.lineno = self.curr_line or node.lineno node.right.accept(self) def visit_ComparisonNode(self, node: mparser.ComparisonNode) -> None: node.left.accept(self) self.append_padded(node.ctype, node) + node.lineno = self.curr_line or node.lineno node.right.accept(self) def visit_ArithmeticNode(self, node: mparser.ArithmeticNode) -> None: node.left.accept(self) self.append_padded(arithmic_map[node.operation], node) + node.lineno = self.curr_line or node.lineno node.right.accept(self) def visit_NotNode(self, node: mparser.NotNode) -> None: + node.lineno = self.curr_line or node.lineno self.append_padded('not', node) node.value.accept(self) def visit_CodeBlockNode(self, node: mparser.CodeBlockNode) -> None: + node.lineno = self.curr_line or node.lineno for i in node.lines: i.accept(self) self.newline() def visit_IndexNode(self, node: mparser.IndexNode) -> None: node.iobject.accept(self) + node.lineno = self.curr_line or node.lineno self.append('[', node) node.index.accept(self) self.append(']', node) def visit_MethodNode(self, node: mparser.MethodNode) -> None: + node.lineno = self.curr_line or node.lineno node.source_object.accept(self) self.append('.' + node.name + '(', node) node.args.accept(self) self.append(')', node) def visit_FunctionNode(self, node: mparser.FunctionNode) -> None: + node.lineno = self.curr_line or node.lineno self.append(node.func_name + '(', node) node.args.accept(self) self.append(')', node) def visit_AssignmentNode(self, node: mparser.AssignmentNode) -> None: + node.lineno = self.curr_line or node.lineno self.append(node.var_name + ' = ', node) node.value.accept(self) def visit_PlusAssignmentNode(self, node: mparser.PlusAssignmentNode) -> None: + node.lineno = self.curr_line or node.lineno self.append(node.var_name + ' += ', node) node.value.accept(self) def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None: + node.lineno = self.curr_line or node.lineno varnames = [x for x in node.varnames] self.append_padded('foreach', node) self.append_padded(', '.join(varnames), node) @@ -155,6 +179,7 @@ class AstPrinter(AstVisitor): self.append('endforeach', node) def visit_IfClauseNode(self, node: mparser.IfClauseNode) -> None: + node.lineno = self.curr_line or node.lineno prefix = '' for i in node.ifs: self.append_padded(prefix + 'if', node) @@ -166,15 +191,18 @@ class AstPrinter(AstVisitor): self.append('endif', node) def visit_UMinusNode(self, node: mparser.UMinusNode) -> None: + node.lineno = self.curr_line or node.lineno self.append_padded('-', node) node.value.accept(self) def visit_IfNode(self, node: mparser.IfNode) -> None: + node.lineno = self.curr_line or node.lineno node.condition.accept(self) self.newline() node.block.accept(self) def visit_TernaryNode(self, node: mparser.TernaryNode) -> None: + node.lineno = self.curr_line or node.lineno node.condition.accept(self) self.append_padded('?', node) node.trueblock.accept(self) @@ -182,6 +210,7 @@ class AstPrinter(AstVisitor): node.falseblock.accept(self) def visit_ArgumentNode(self, node: mparser.ArgumentNode) -> None: + node.lineno = self.curr_line or node.lineno break_args = (len(node.arguments) + len(node.kwargs)) > self.arg_newline_cutoff for i in node.arguments + list(node.kwargs.values()): if not isinstance(i, (mparser.ElementaryNode, mparser.IndexNode)): |