diff options
author | Daniel Mensinger <daniel@mensinger-ka.de> | 2019-01-20 17:43:09 +0100 |
---|---|---|
committer | Daniel Mensinger <daniel@mensinger-ka.de> | 2019-01-22 16:09:34 +0100 |
commit | 277dc10a5d21eaed884ef8674ca6cc1bec547ec0 (patch) | |
tree | 995e964e6c43c5632f5f39d850cc03dc0f185c51 /mesonbuild | |
parent | 750af9c853423138e0d2812ba538402eedea80f2 (diff) | |
download | meson-277dc10a5d21eaed884ef8674ca6cc1bec547ec0.zip meson-277dc10a5d21eaed884ef8674ca6cc1bec547ec0.tar.gz meson-277dc10a5d21eaed884ef8674ca6cc1bec547ec0.tar.bz2 |
AST post processing
Diffstat (limited to 'mesonbuild')
-rw-r--r-- | mesonbuild/ast/__init__.py | 3 | ||||
-rw-r--r-- | mesonbuild/ast/postprocess.py | 86 | ||||
-rw-r--r-- | mesonbuild/ast/printer.py | 109 | ||||
-rw-r--r-- | mesonbuild/ast/visitor.py | 38 | ||||
-rw-r--r-- | mesonbuild/rewriter.py | 12 |
5 files changed, 172 insertions, 76 deletions
diff --git a/mesonbuild/ast/__init__.py b/mesonbuild/ast/__init__.py index 181c549..8d0a6ec 100644 --- a/mesonbuild/ast/__init__.py +++ b/mesonbuild/ast/__init__.py @@ -18,6 +18,8 @@ __all__ = [ 'AstInterpreter', 'RewriterInterpreter', + 'AstIDGenerator', + 'AstIndentationGenerator', 'AstVisitor', 'AstPrinter', 'IntrospectionInterpreter', @@ -26,4 +28,5 @@ __all__ = [ from .interpreter import (AstInterpreter, RewriterInterpreter) from .introspection import IntrospectionInterpreter from .visitor import AstVisitor +from .postprocess import AstIDGenerator, AstIndentationGenerator from .printer import AstPrinter diff --git a/mesonbuild/ast/postprocess.py b/mesonbuild/ast/postprocess.py new file mode 100644 index 0000000..e913b4f --- /dev/null +++ b/mesonbuild/ast/postprocess.py @@ -0,0 +1,86 @@ +# Copyright 2019 The Meson development team + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This class contains the basic functionality needed to run any interpreter +# or an interpreter-based tool + +from . import AstVisitor +from .. import mparser + +class AstIndentationGenerator(AstVisitor): + def __init__(self): + self.level = 0 + + def visit_default_func(self, node: mparser.BaseNode): + # Store the current level in the node + node.level = self.level + + def visit_ArrayNode(self, node: mparser.ArrayNode): + self.visit_default_func(node) + self.level += 1 + node.args.accept(self) + self.level -= 1 + + def visit_DictNode(self, node: mparser.DictNode): + self.visit_default_func(node) + self.level += 1 + node.args.accept(self) + self.level -= 1 + + def visit_MethodNode(self, node: mparser.MethodNode): + self.visit_default_func(node) + node.source_object.accept(self) + self.level += 1 + node.args.accept(self) + self.level -= 1 + + def visit_FunctionNode(self, node: mparser.FunctionNode): + self.visit_default_func(node) + self.level += 1 + node.args.accept(self) + self.level -= 1 + + def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode): + self.visit_default_func(node) + self.level += 1 + node.items.accept(self) + node.block.accept(self) + self.level -= 1 + + def visit_IfClauseNode(self, node: mparser.IfClauseNode): + self.visit_default_func(node) + for i in node.ifs: + i.accept(self) + if node.elseblock: + self.level += 1 + node.elseblock.accept(self) + self.level -= 1 + + def visit_IfNode(self, node: mparser.IfNode): + self.visit_default_func(node) + self.level += 1 + node.condition.accept(self) + node.block.accept(self) + self.level -= 1 + +class AstIDGenerator(AstVisitor): + def __init__(self): + self.counter = {} + + def visit_default_func(self, node: mparser.BaseNode): + name = type(node).__name__ + if name not in self.counter: + self.counter[name] = 0 + node.ast_id = name + '#' + str(self.counter[name]) + self.counter[name] += 1 diff --git a/mesonbuild/ast/printer.py b/mesonbuild/ast/printer.py index b055480..aab5a30 100644 --- a/mesonbuild/ast/printer.py +++ b/mesonbuild/ast/printer.py @@ -1,4 +1,4 @@ -# Copyright 2018 The Meson development team +# Copyright 2019 The Meson development team # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -31,85 +31,77 @@ class AstPrinter(AstVisitor): self.result = '' self.indent = indent self.arg_newline_cutoff = arg_newline_cutoff - self.level = 0 self.ci = '' self.is_newline = True - def inc_indent(self): - self.level += self.indent - - def dec_indent(self): - self.level -= self.indent - - def append(self, data: str): + def append(self, data: str, node: mparser.BaseNode): + level = 0 + if node and hasattr(node, 'level'): + level = node.level if self.is_newline: - self.result += ' ' * self.level + self.result += ' ' * (level * self.indent) self.result += data self.is_newline = False - def appendS(self, data: str): + def appendS(self, data: str, node: mparser.BaseNode): if self.result[-1] not in [' ', '\n']: data = ' ' + data - self.append(data + ' ') + self.append(data + ' ', node) def newline(self): self.result += '\n' self.is_newline = True def visit_BooleanNode(self, node: mparser.BooleanNode): - self.append('true' if node.value else 'false') + self.append('true' if node.value else 'false', node) def visit_IdNode(self, node: mparser.IdNode): - self.append(node.value) + self.append(node.value, node) def visit_NumberNode(self, node: mparser.NumberNode): - self.append(str(node.value)) + self.append(str(node.value), node) def visit_StringNode(self, node: mparser.StringNode): - self.append("'" + node.value + "'") + self.append("'" + node.value + "'", node) def visit_ContinueNode(self, node: mparser.ContinueNode): - self.append('continue') + self.append('continue', node) def visit_BreakNode(self, node: mparser.BreakNode): - self.append('break') + self.append('break', node) def visit_ArrayNode(self, node: mparser.ArrayNode): - self.append('[') - self.inc_indent() + self.append('[', node) node.args.accept(self) - self.dec_indent() - self.append(']') + self.append(']', node) def visit_DictNode(self, node: mparser.DictNode): - self.append('{') - self.inc_indent() + self.append('{', node) node.args.accept(self) - self.dec_indent() - self.append('}') + self.append('}', node) def visit_OrNode(self, node: mparser.OrNode): node.left.accept(self) - self.appendS('or') + self.appendS('or', node) node.right.accept(self) def visit_AndNode(self, node: mparser.AndNode): node.left.accept(self) - self.appendS('and') + self.appendS('and', node) node.right.accept(self) def visit_ComparisonNode(self, node: mparser.ComparisonNode): node.left.accept(self) - self.appendS(mparser.comparison_map[node.ctype]) + self.appendS(mparser.comparison_map[node.ctype], node) node.right.accept(self) def visit_ArithmeticNode(self, node: mparser.ArithmeticNode): node.left.accept(self) - self.appendS(arithmic_map[node.operation]) + self.appendS(arithmic_map[node.operation], node) node.right.accept(self) def visit_NotNode(self, node: mparser.NotNode): - self.appendS('not') + self.appendS('not', node) node.value.accept(self) def visit_CodeBlockNode(self, node: mparser.CodeBlockNode): @@ -118,75 +110,64 @@ class AstPrinter(AstVisitor): self.newline() def visit_IndexNode(self, node: mparser.IndexNode): - self.append('[') + self.append('[', node) node.index.accept(self) - self.append(']') + self.append(']', node) def visit_MethodNode(self, node: mparser.MethodNode): node.source_object.accept(self) - self.append('.' + node.name + '(') - self.inc_indent() + self.append('.' + node.name + '(', node) node.args.accept(self) - self.dec_indent() - self.append(')') + self.append(')', node) def visit_FunctionNode(self, node: mparser.FunctionNode): - self.append(node.func_name + '(') - self.inc_indent() + self.append(node.func_name + '(', node) node.args.accept(self) - self.dec_indent() - self.append(')') + self.append(')', node) def visit_AssignmentNode(self, node: mparser.AssignmentNode): - self.append(node.var_name + ' = ') + self.append(node.var_name + ' = ', node) node.value.accept(self) def visit_PlusAssignmentNode(self, node: mparser.PlusAssignmentNode): - self.append(node.var_name + ' += ') + self.append(node.var_name + ' += ', node) node.value.accept(self) def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode): varnames = [x.value for x in node.varnames] - self.appendS('foreach') - self.appendS(', '.join(varnames)) - self.appendS(':') + self.appendS('foreach', node) + self.appendS(', '.join(varnames), node) + self.appendS(':', node) node.items.accept(self) self.newline() - self.inc_indent() node.block.accept(self) - self.dec_indent() - self.append('endforeach') + self.append('endforeach', node) def visit_IfClauseNode(self, node: mparser.IfClauseNode): prefix = '' for i in node.ifs: - self.appendS(prefix + 'if') + self.appendS(prefix + 'if', node) prefix = 'el' i.accept(self) if node.elseblock: - self.append('else') - self.indent() - self.inc_indent() + self.append('else', node) node.elseblock.accept(self) - self.dec_indent() - self.append('endif') + self.append('endif', node) def visit_UMinusNode(self, node: mparser.UMinusNode): - self.appendS('-') + self.appendS('-', node) node.value.accept(self) def visit_IfNode(self, node: mparser.IfNode): node.condition.accept(self) self.newline() - self.inc_indent() node.block.accept(self) - self.dec_indent() def visit_TernaryNode(self, node: mparser.TernaryNode): node.condition.accept(self) - self.appendS('?') + self.appendS('?', node) node.trueblock.accept(self) - self.appendS(':') + self.appendS(':', node) node.falseblock.accept(self) def visit_ArgumentNode(self, node: mparser.ArgumentNode): @@ -198,13 +179,13 @@ class AstPrinter(AstVisitor): self.newline() for i in node.arguments: i.accept(self) - self.append(',') + self.append(',', node) if break_args: self.newline() for key, val in node.kwargs.items(): - self.append(key) - self.appendS(':') + self.append(key, node) + self.appendS(':', node) val.accept(self) - self.append(',') + self.append(',', node) if break_args: self.newline() diff --git a/mesonbuild/ast/visitor.py b/mesonbuild/ast/visitor.py index 487cd5b..c8769d4 100644 --- a/mesonbuild/ast/visitor.py +++ b/mesonbuild/ast/visitor.py @@ -1,4 +1,4 @@ -# Copyright 2018 The Meson development team +# Copyright 2019 The Meson development team # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -21,95 +21,117 @@ class AstVisitor: def __init__(self): pass - def visit_BooleanNode(self, node: mparser.BooleanNode): + def visit_default_func(self, node: mparser.BaseNode): pass + def visit_BooleanNode(self, node: mparser.BooleanNode): + self.visit_default_func(node) + def visit_IdNode(self, node: mparser.IdNode): - pass + self.visit_default_func(node) def visit_NumberNode(self, node: mparser.NumberNode): - pass + self.visit_default_func(node) def visit_StringNode(self, node: mparser.StringNode): - pass + self.visit_default_func(node) def visit_ContinueNode(self, node: mparser.ContinueNode): - pass + self.visit_default_func(node) def visit_BreakNode(self, node: mparser.BreakNode): - pass + self.visit_default_func(node) def visit_ArrayNode(self, node: mparser.ArrayNode): + self.visit_default_func(node) node.args.accept(self) def visit_DictNode(self, node: mparser.DictNode): + self.visit_default_func(node) node.args.accept(self) def visit_EmptyNode(self, node: mparser.EmptyNode): - pass + self.visit_default_func(node) def visit_OrNode(self, node: mparser.OrNode): + self.visit_default_func(node) node.left.accept(self) node.right.accept(self) def visit_AndNode(self, node: mparser.AndNode): + self.visit_default_func(node) node.left.accept(self) node.right.accept(self) def visit_ComparisonNode(self, node: mparser.ComparisonNode): + self.visit_default_func(node) node.left.accept(self) node.right.accept(self) def visit_ArithmeticNode(self, node: mparser.ArithmeticNode): + self.visit_default_func(node) node.left.accept(self) node.right.accept(self) def visit_NotNode(self, node: mparser.NotNode): + self.visit_default_func(node) node.value.accept(self) def visit_CodeBlockNode(self, node: mparser.CodeBlockNode): + self.visit_default_func(node) for i in node.lines: i.accept(self) def visit_IndexNode(self, node: mparser.IndexNode): + self.visit_default_func(node) node.index.accept(self) def visit_MethodNode(self, node: mparser.MethodNode): + self.visit_default_func(node) node.source_object.accept(self) node.args.accept(self) def visit_FunctionNode(self, node: mparser.FunctionNode): + self.visit_default_func(node) node.args.accept(self) def visit_AssignmentNode(self, node: mparser.AssignmentNode): + self.visit_default_func(node) node.value.accept(self) def visit_PlusAssignmentNode(self, node: mparser.PlusAssignmentNode): + self.visit_default_func(node) node.value.accept(self) def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode): + self.visit_default_func(node) node.items.accept(self) node.block.accept(self) def visit_IfClauseNode(self, node: mparser.IfClauseNode): + self.visit_default_func(node) for i in node.ifs: i.accept(self) if node.elseblock: node.elseblock.accept(self) def visit_UMinusNode(self, node: mparser.UMinusNode): + self.visit_default_func(node) node.value.accept(self) def visit_IfNode(self, node: mparser.IfNode): + self.visit_default_func(node) node.condition.accept(self) node.block.accept(self) def visit_TernaryNode(self, node: mparser.TernaryNode): + self.visit_default_func(node) node.condition.accept(self) node.trueblock.accept(self) node.falseblock.accept(self) def visit_ArgumentNode(self, node: mparser.ArgumentNode): + self.visit_default_func(node) for i in node.arguments: i.accept(self) for i in node.commas: diff --git a/mesonbuild/rewriter.py b/mesonbuild/rewriter.py index 45504be..cc5d2ab 100644 --- a/mesonbuild/rewriter.py +++ b/mesonbuild/rewriter.py @@ -23,7 +23,7 @@ # - move targets # - reindent? -from .ast import (AstInterpreter, AstPrinter) +from .ast import AstInterpreter, AstVisitor, AstIDGenerator, AstIndentationGenerator, AstPrinter from mesonbuild.mesonlib import MesonException from mesonbuild import mlog import traceback @@ -42,9 +42,13 @@ def run(options): rewriter.parse_project() rewriter.run() - visitor = AstPrinter() - rewriter.ast.accept(visitor) - print(visitor.result) + indentor = AstIndentationGenerator() + idgen = AstIDGenerator() + printer = AstPrinter() + rewriter.ast.accept(indentor) + rewriter.ast.accept(idgen) + rewriter.ast.accept(printer) + print(printer.result) except Exception as e: if isinstance(e, MesonException): mlog.exception(e) |