OILS / asdl / pybase.py View on Github | oils.pub

42 lines, 20 significant
1#!/usr/bin/env python2
2"""pybase.py."""
3from __future__ import print_function
4
5from mycpp import mylib
6
7from typing import TYPE_CHECKING
8if TYPE_CHECKING:
9 from _devbuild.gen.hnode_asdl import hnode_t
10 from asdl.runtime import TraversalState
11
12
13class 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
19class 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
25class 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, do_abbrev, trav=None):
31 # type: (bool, TraversalState) -> hnode_t
32 raise NotImplementedError(self.__class__.__name__)
33
34 def __repr__(self):
35 # type: () -> str
36 # TODO: Break this circular dependency.
37 from asdl import format as fmt
38
39 f = mylib.BufWriter()
40 tree = self.PrettyTree(False)
41 fmt.HNodePrettyPrint(tree, f)
42 return f.getvalue()