aroberge / ideas

Easy creation of custom import hooks to experiment on alternatives to Python's syntax; see https://aroberge.github.io/ideas/docs/html/
Other
76 stars 5 forks source link

Using parsers for transforming #28

Open grigandal625 opened 2 years ago

grigandal625 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Shure

grigandal625 commented 2 years ago

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 commented 2 years ago

Thanks.