Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using parsers for transforming #28

Open
grigandal625 opened this issue May 21, 2022 · 6 comments
Open

Using parsers for transforming #28

grigandal625 opened this issue May 21, 2022 · 6 comments

Comments

@grigandal625
Copy link

It would be very useful to implement the search and processing of fragments of transformable code using parsers

For example, using lark-parser, we could make a transformation like this:

# specifying a fragment grammar with lark syntax (EBNF-like)
# such a grammar could be intended for a fragment like:
# 5 $ 10
grammar = """
    new_operation: NUMBER '$' NUMBER 
    %import common.SIGNED_NUMBER -> NUMBER
"""

@detect(grammar)
def transform(parsed_tree, detected_source, full_source, **kwargs):
    # parsed_tree will be like:
    #
    # new_operation:
    # .... 5
    # .... 10

    return do_smth_with_tree(parsed_tree)
@aroberge
Copy link
Owner

There is absolutely nothing that prevents you from doing this.

Some existing examples require the use of other libraries. For example, pythonji requires the use of emoji. easy_units require either pint or astropy.

@grigandal625
Copy link
Author

You are absolutely right, and I plan to do so. I just wanted to say that in solutions related to language processing, an approach using parsers can greatly simplify the processing task. Accordingly, in my opinion, it would be most appropriate to include such functionality in such solutions. Naturally, everything is at your discretion. Thanks

@aroberge
Copy link
Owner

If you do so, would you mind adding a link to this issue so that I could see if I could include a similar example in this project?

@grigandal625
Copy link
Author

Shure

@grigandal625
Copy link
Author

here is an example
but just note that I haven't been careful with the architecture of the project yet, so direct copying can cause problems)

@aroberge
Copy link
Owner

Thanks.

djpohly added a commit to djpohly/python-ideas that referenced this issue Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants