Example #1
0
 def p_script(self, p):
     """script : opt_constdefs opt_compdefs opt_program"""
     s = ast.Node()
     s.add_children(p[1] + p[2] + p[3][0])
     root = ast.Node()
     root.add_child(s.clone())
     d = ast.Node()
     d.add_children(p[1] + p[3][1])
     root.add_child(d)
     p[0] = root
Example #2
0
    def parse(self, source_text, logger=None):
        # return ir (AST) and issuetracker
        self.issuetracker = IssueTracker()
        self.source_text = source_text
        root = None

        try:
            root = self.parser.parse(source_text, debug=logger)
        except SyntaxError as e:
            self.issuetracker.add_error(e.text, {
                'line': e.lineno,
                'col': e.offset
            })
        finally:
            ir, deploy_ir = root.children if root else (ast.Node(), ast.Node())

        return ir, deploy_ir, self.issuetracker
Example #3
0
 def parse(self, source_text):
     # return ir (AST) and issuetracker
     self.issuetracker = IssueTracker()
     self.source_text = source_text
     try:
         ir = self.parser.parse(source_text)
     except SyntaxError as e:
         self.issuetracker.add_error(e.text, {
             'line': e.lineno,
             'col': e.offset
         })
         ir = ast.Node()
     return ir, self.issuetracker
Example #4
0
            self.result.append("{}( {} {}".format(self._indentation(), node,
                                                  node.group.ident))
            self.indent += 1

        def g(node):
            self.indent -= 1
            self.result.append("{})".format(self._indentation()))

        self._visit(node, preorder=f, postorder=g)


class BracePrinter(BraceFormatter):
    """docstring for BracePrinter"""
    def __init__(self):
        super(BracePrinter, self).__init__()

    def process(self, node):
        text = super(BracePrinter, self).process(node)
        print text


if __name__ == '__main__':
    ast.Node._verbose_desc = True

    n = ast.Node()
    bp = BracePrinter()
    bp.process(n)

    # dp = DotPrinter()
    # dp.process(n)
Example #5
0
 def p_script(self, p):
     """script : opt_constdefs opt_compdefs opt_program"""
     root = ast.Node()
     root.add_children(p[1] + p[2] + p[3])
     p[0] = root
Example #6
0
        'RulePredicate': RulePredicate,
        'RuleExpression': RuleExpression,
        'Rule': Rule
    }.get(o['class'])()
    instance.__dict__ = o['data']
    return instance


if __name__ == '__main__':
    import json
    import astprint
    import astnode as ast

    Node._verbose_desc = True

    bp = astprint.BracePrinter()

    root = ast.Node()
    root.add_child(
        ast.Constant(ident=ast.Id(ident="foo"), arg=ast.Value(value=1)))
    bp.visit(root)

    s = json.dumps(root, default=ast.node_encoder, indent=2)

    print
    print s
    print

    tree = json.loads(s, object_hook=ast.node_decoder)
    bp.visit(tree)