-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add parser level hooks support with Parser2 (#108)
This pull request introduces brand new Parser2 class which can be used in cooperation with old Tokenizer class and provides hooks interface based on visitor pattern. > [!NOTE] > It was preferable to introduce new Parser class, as it was not possible to effectively implement hook interface for existing Parser class. Additionally this approach smooths transition between those two for any possible users, as they should be able to keep using old parser. When time will be right we will deprecate and remove old parser. Currently exisiting drawing backend interface is also not compatible with Parser2 and will have to be recreated following similar principles as parser. We can customize parser hooks by creating new hook class inheriting from IHooks or Parser2Hooks and providing it to Parser2 constructor via Parser2Options. Difference between those two is that all IHooks hooks are NOOP while Parser2Hooks actually implement Gerber AST parser logic. For example if we were to implement a parser only to print all comments from Gerber file we could do it following way: ```python from pygerber.gerberx3.parser2.context2 import Parser2Context from pygerber.gerberx3.parser2.ihooks import IHooks from pygerber.gerberx3.parser2.parser2 import Parser2, Parser2Options from pygerber.gerberx3.tokenizer.tokens.g04_comment import Comment class PrintComments(IHooks): class CommentTokenHooks(IHooks.CommentTokenHooks): def on_parser_visit_token(self, token: Comment, context: Parser2Context) -> None: print(token.content) return super().on_parser_visit_token(token, context) ``` All operations except visiting Comment tokens will be NOOP. As you might notice we are creating class within a class body. This nesting mechanism allows us to reduce amount of boilderplate required to define hooks and simplify process of adding new hooks in the future. All hooks specific to some particular token are defined as classes within a class inheriting from IHooks. They must follow particular naming scheme, for now please refer to source code in `pygerber.gerberx3.parser2.ihooks` module. There are also hooks which are not tied to tokens, eg. for error handling, `on_parser_error(self, context: Parser2Context, error: Parser2Error)` which are then defined directly within master hook class. It should be possible to extend hook interface in the future to add tokenizer level hooks and possibly others if needed.
- Loading branch information
Showing
113 changed files
with
9,332 additions
and
517 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Public API Reference | ||
|
||
::: pygerber.gerberx3.api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"semi": true, | ||
"useTabs": false, | ||
"endOfLine": "lf", | ||
"printWidth": 88, | ||
"overrides": [ | ||
{ | ||
"files": ["*.md"], | ||
"options": { | ||
"tabWidth": 4, | ||
"proseWrap": "always" | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.