Skip to content

pygettext: use an AST parser instead of a tokenizer #104400

Closed

Description

@tomasr8

Follow up on this forum discussion

This is a part 1/X of improving pygettext. Replacing the tokenizer that powers the message extraction with a parser will simplify the code (no more counting brackets and f-string madness) and make it much easier to extend it with new features later down the road.

This change should also come with a healthy dose of new tests to verify the implementation.

PR coming shortly ;)

Linked PRs

Activity

  1. terryjreedy commented on May 11, 2023

    @terryjreedy
    Member

    We replaced the custom parser in pyclbr with an ast visitor a couple of years ago. It was shorter and clearer and agreed to be a definite improvement. For this type of application, any possible slowdown is irrelevant.

  2. added a commit that references this issue on Nov 3, 2024
  3. added 2 commits that reference this issue on Nov 3, 2024
  4. added 2 commits that reference this issue on Nov 3, 2024
  5. added a commit that references this issue on Dec 8, 2024
  6. added a commit that references this issue on Jan 12, 2025
  7. added 2 commits that reference this issue on Feb 2, 2025
  8. added 2 commits that reference this issue on Feb 7, 2025
  9. 1 remaining item

  10. added
    triagedThe issue has been accepted as valid by a triager.
    type-featureA feature request or enhancement
    and removed
    type-featureA feature request or enhancement
    triagedThe issue has been accepted as valid by a triager.
    on Feb 10, 2025
  11. added a commit that references this issue on Feb 11, 2025
  12. serhiy-storchaka commented on Feb 11, 2025

    @serhiy-storchaka
    Member

    Thanks, @tomasr8. The AST based implementation is much clearer.

    The drawback is that it prevents implementing the --add-comments option. I think that the right solution of this is to add optional support of comments in AST -- it could also be useful in other applications. But this is a different issue.

    Is it all, or you planned other work on this issue?

  13. AA-Turner commented on Feb 11, 2025

    @AA-Turner
    Member

    Having comments in the AST would also be useful for applications like Sphinx -- is there an open issue to track this, or should we create one

    A

  14. tomasr8 commented on Feb 11, 2025

    @tomasr8
    MemberAuthor

    The drawback is that it prevents implementing the --add-comments option. I think that the right solution of this is to add optional support of comments in AST -- it could also be useful in other applications. But this is a different issue.

    Yes, having the comments in the AST would be great - I can look into that and see if there are any issues open about it!

    There is actually a way to add translator comments even with the AST. You can run the tokenizer separately and collect all comments and their corresponding line numbers. You can then match the comments to gettext calls. I had a working proof of concept implementation of this for babel but I can repurpose it for pygettext!

    Is it all, or you planned other work on this issue?

    Not specifically in this issue, I think we can close it. I have lots of other things I'd like to improve but I think it's better to open separate issues for that (translator comments being one of those)

  15. tomasr8 commented on Feb 11, 2025

    @tomasr8
    MemberAuthor

    is there an open issue to track this, or should we create one

    There's a similar issue about type comments: #101494

  16. sergey-miryanov commented on Feb 12, 2025

    @sergey-miryanov
    Contributor

    Yes, having the comments in the AST would be great - I can look into that and see if there are any issues open about it!

    We didn't fill an issue, 'cause we stick to too old Python version, but in our applications, we need to support comments in AST and now have to do tricky re.subs to preserve them.

  17. serhiy-storchaka commented on Feb 12, 2025

    @serhiy-storchaka
    Member

    There is actually a way to add translator comments even with the AST. You can run the tokenizer separately and collect all comments and their corresponding line numbers. You can then match the comments to gettext calls. I had a working proof of concept implementation of this for babel but I can repurpose it for pygettext!

    This is even more general and flexible way, but it may be hard. With comments in AST we have an issue of representing comments in a way that will not break existing code that uses AST if the tree contains comments. There is a problem of representing comments between string literals which will be concatenated into a single AST node -- it can be ignored for pygettext, but not for other applications. There is a problem of distinguishing comments that precede a node from a comment that follows it on the same line from comments that follow it on the following lines. Different applications may need to distinguish different types of comments and may have different preferences of the representation.

    But there is no urge to add the --add-comments option right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triagedThe issue has been accepted as valid by a triager.type-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions