| 1 | #!/usr/bin/env python2
|
| 2 | """pybase.py."""
|
| 3 | from __future__ import print_function
|
| 4 |
|
| 5 | from mycpp import mylib
|
| 6 |
|
| 7 | from typing import TYPE_CHECKING
|
| 8 | if TYPE_CHECKING:
|
| 9 | from _devbuild.gen.hnode_asdl import hnode_t
|
| 10 | from asdl.runtime import TraversalState
|
| 11 |
|
| 12 |
|
| 13 | class Obj(object):
|
| 14 | # NOTE: We're using CAPS for these static fields, since they are constant at
|
| 15 | # runtime after metaprogramming.
|
| 16 | ASDL_TYPE = None # Used for type checking
|
| 17 |
|
| 18 |
|
| 19 | class SimpleObj(int):
|
| 20 | """Base type of simple sum types."""
|
| 21 | # TODO: Get rid of this indirection? Although mycpp might use it.
|
| 22 | pass
|
| 23 |
|
| 24 |
|
| 25 | class CompoundObj(Obj):
|
| 26 | # The tag is set for variant types, which are subclasses of sum
|
| 27 | # types. Never set for product types.
|
| 28 | _type_tag = 0 # Starts at 1. Zero is invalid
|
| 29 |
|
| 30 | def PrettyTree(self, trav=None):
|
| 31 | # type: (TraversalState) -> hnode_t
|
| 32 | raise NotImplementedError(self.__class__.__name__)
|
| 33 |
|
| 34 | def _AbbreviatedTree(self, trav=None):
|
| 35 | # type: (TraversalState) -> hnode_t
|
| 36 | raise NotImplementedError(self.__class__.__name__)
|
| 37 |
|
| 38 | def AbbreviatedTree(self, trav=None):
|
| 39 | # type: (TraversalState) -> hnode_t
|
| 40 | raise NotImplementedError(self.__class__.__name__)
|
| 41 |
|
| 42 | def __repr__(self):
|
| 43 | # type: () -> str
|
| 44 | # TODO: Break this circular dependency.
|
| 45 | from asdl import format as fmt
|
| 46 |
|
| 47 | ast_f = fmt.TextOutput(mylib.BufWriter()) # No color by default.
|
| 48 | tree = self.PrettyTree()
|
| 49 | fmt.PrintTree(tree, ast_f)
|
| 50 | s, _ = ast_f.GetRaw()
|
| 51 | return s
|