MCPcopy Create free account
hub / github.com/awslabs/gap-text2sql / ASTWrapper

Class ASTWrapper

rat-sql-gap/seq2struct/ast_util.py:69–272  ·  view source on GitHub ↗

Provides helper methods on the ASDL AST.

Source from the content-addressed store, hash-verified

67
68
69class ASTWrapper(object):
70 '''Provides helper methods on the ASDL AST.'''
71
72 default_primitive_type_checkers = {
73 'identifier': lambda x: isinstance(x, str),
74 'int': lambda x: isinstance(x, int),
75 'string': lambda x: isinstance(x, str),
76 'bytes': lambda x: isinstance(x, bytes),
77 'object': lambda x: isinstance(x, object),
78 'singleton': lambda x: x is True or x is False or x is None
79 }
80
81 # pylint: disable=too-few-public-methods
82
83 def __init__(self, ast_def, custom_primitive_type_checkers={}):
84 # type: (asdl.Module, str) -> None
85 self.ast_def = ast_def
86
87 visitor = ASTWrapperVisitor()
88 visitor.visit(ast_def)
89
90 self.constructors = visitor.constructors
91 self.sum_types = visitor.sum_types
92 self.product_types = visitor.product_types
93 self.seq_fragment_constructors = {}
94 self.primitive_type_checkers = {
95 **self.default_primitive_type_checkers,
96 **custom_primitive_type_checkers
97 }
98 self.custom_primitive_types = set(custom_primitive_type_checkers.keys())
99 self.primitive_types = set(self.primitive_type_checkers.keys())
100
101 # Product types and constructors:
102 # no need to decide upon a further type for these.
103 self.singular_types = {} # type: Dict[str, SingularType]
104 self.singular_types.update(self.constructors)
105 self.singular_types.update(self.product_types)
106
107 # IndexedSets for each sum type
108 self.sum_type_vocabs = {
109 name: sorted(t.name for t in sum_type.types)
110 for name, sum_type in self.sum_types.items()
111 }
112 self.constructor_to_sum_type = {
113 constructor.name: name
114 for name, sum_type in self.sum_types.items()
115 for constructor in sum_type.types
116 }
117 self.seq_fragment_constructor_to_sum_type = {
118 constructor.name: name
119 for name, sum_type in self.sum_types.items()
120 for constructor in sum_type.types
121 }
122 self.fieldless_constructors = sorted(
123 visitor.fieldless_constructors.keys())
124
125 @property
126 def types(self):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected