pygerber
pygerber copied to clipboard
Unify visitor implementation for Parser2 with ASTVisitor
After careful consideration I have came to conclusion that current introspection API based on Parser2Hooks introduces too much unnecessary complexity in form of nested classes. I can't change it now, as it will break API compatibility in awful way. Therefore I will schedule this change for PyGerber 3.0.0. I will also provide some kind of adapter for current API to future API, allowing for transition by changing just inheritance or by some decorator, it was not decided yet.
Current approach:
class CustomHooks(Parser2Hooks):
def __init__(self) -> None:
super().__init__()
self.comments: list[str] = []
class CommentTokenHooks(Parser2Hooks.CommentTokenHooks):
hooks: CustomHooks
def on_parser_visit_token(
self,
token: Comment,
context: Parser2Context,
) -> None:
self.hooks.comments.append(token.content)
return super().on_parser_visit_token(token, context)
Future approach:
class CustomHooks(Parser2Hooks):
def __init__(self) -> None:
super().__init__()
self.comments: list[str] = []
def visit_comment_token(self, token: Comment) -> None:
self.hooks.comments.append(token.content)
return super().visit_comment_token(token, context)
Actually, it may be a better approach to just move hooks into Parser2 class.