Used by ASTWrapper to collect information. - put constructors in one place. - checks that all fields have names. - get all optional fields.
| 10 | |
| 11 | |
| 12 | class ASTWrapperVisitor(asdl.VisitorBase): |
| 13 | '''Used by ASTWrapper to collect information. |
| 14 | |
| 15 | - put constructors in one place. |
| 16 | - checks that all fields have names. |
| 17 | - get all optional fields. |
| 18 | ''' |
| 19 | |
| 20 | def __init__(self): |
| 21 | # type: () -> None |
| 22 | super(ASTWrapperVisitor, self).__init__() |
| 23 | self.constructors = {} # type: Dict[str, asdl.Constructor] |
| 24 | self.sum_types = {} # type: Dict[str, asdl.Sum] |
| 25 | self.product_types = {} # type: Dict[str, asdl.Product] |
| 26 | self.fieldless_constructors = {} # type: Dict[str, asdl.Constructor] |
| 27 | |
| 28 | def visitModule(self, mod): |
| 29 | # type: (asdl.Module) -> None |
| 30 | for dfn in mod.dfns: |
| 31 | self.visit(dfn) |
| 32 | |
| 33 | def visitType(self, type_): |
| 34 | # type: (asdl.Type) -> None |
| 35 | self.visit(type_.value, str(type_.name)) |
| 36 | |
| 37 | def visitSum(self, sum_, name): |
| 38 | # type: (asdl.Sum, str) -> None |
| 39 | self.sum_types[name] = sum_ |
| 40 | for t in sum_.types: |
| 41 | self.visit(t, name) |
| 42 | |
| 43 | def visitConstructor(self, cons, _name): |
| 44 | # type: (asdl.Constructor, str) -> None |
| 45 | assert cons.name not in self.constructors |
| 46 | self.constructors[cons.name] = cons |
| 47 | if not cons.fields: |
| 48 | self.fieldless_constructors[cons.name] = cons |
| 49 | for f in cons.fields: |
| 50 | self.visit(f, cons.name) |
| 51 | |
| 52 | def visitField(self, field, name): |
| 53 | # type: (asdl.Field, str) -> None |
| 54 | # pylint: disable=no-self-use |
| 55 | if field.name is None: |
| 56 | raise ValueError('Field of type {} in {} lacks name'.format( |
| 57 | field.type, name)) |
| 58 | |
| 59 | def visitProduct(self, prod, name): |
| 60 | # type: (asdl.Product, str) -> None |
| 61 | self.product_types[name] = prod |
| 62 | for f in prod.fields: |
| 63 | self.visit(f, name) |
| 64 | |
| 65 | |
| 66 | SingularType = Union[asdl.Constructor, asdl.Product] |