Used by ASTWrapper to collect information. - put constructors in one place. - checks that all fields have names. - get all optional fields.
| 27 | |
| 28 | |
| 29 | class ASTWrapperVisitor(asdl.VisitorBase): |
| 30 | """Used by ASTWrapper to collect information. |
| 31 | |
| 32 | - put constructors in one place. |
| 33 | - checks that all fields have names. |
| 34 | - get all optional fields. |
| 35 | """ |
| 36 | |
| 37 | def __init__(self): |
| 38 | # type: () -> None |
| 39 | super(ASTWrapperVisitor, self).__init__() |
| 40 | self.constructors = {} # type: Dict[str, asdl.Constructor] |
| 41 | self.sum_types = {} # type: Dict[str, asdl.Sum] |
| 42 | self.product_types = {} # type: Dict[str, asdl.Product] |
| 43 | self.fieldless_constructors = {} # type: Dict[str, asdl.Constructor] |
| 44 | |
| 45 | def visitModule(self, mod): |
| 46 | # type: (asdl.Module) -> None |
| 47 | for dfn in mod.dfns: |
| 48 | self.visit(dfn) |
| 49 | |
| 50 | def visitType(self, type_): |
| 51 | # type: (asdl.Type) -> None |
| 52 | self.visit(type_.value, str(type_.name)) |
| 53 | |
| 54 | def visitSum(self, sum_, name): |
| 55 | # type: (asdl.Sum, str) -> None |
| 56 | self.sum_types[name] = sum_ |
| 57 | for t in sum_.types: |
| 58 | self.visit(t, name) |
| 59 | |
| 60 | def visitConstructor(self, cons, _name): |
| 61 | # type: (asdl.Constructor, str) -> None |
| 62 | assert cons.name not in self.constructors |
| 63 | self.constructors[cons.name] = cons |
| 64 | if not cons.fields: |
| 65 | self.fieldless_constructors[cons.name] = cons |
| 66 | for f in cons.fields: |
| 67 | self.visit(f, cons.name) |
| 68 | |
| 69 | def visitField(self, field, name): |
| 70 | # type: (asdl.Field, str) -> None |
| 71 | # pylint: disable=no-self-use |
| 72 | if field.name is None: |
| 73 | raise ValueError(f'Field of type {field.type} in {name} lacks name') |
| 74 | |
| 75 | def visitProduct(self, prod, name): |
| 76 | # type: (asdl.Product, str) -> None |
| 77 | self.product_types[name] = prod |
| 78 | for f in prod.fields: |
| 79 | self.visit(f, name) |
| 80 | |
| 81 | |
| 82 | SingularType = Union[asdl.Constructor, asdl.Product] |