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

Run code on every attribute #1512

Open
synalice opened this issue Feb 12, 2025 · 7 comments
Open

Run code on every attribute #1512

synalice opened this issue Feb 12, 2025 · 7 comments
Labels

Comments

@synalice
Copy link

What is your question?

How do I run code inside a Visitor class on every attribute? Something like __default__(tree), but in case the attribute exists.

@synalice
Copy link
Author

As always, the answer comes 10 milliseconds after I post the question. I modified the grammar to group each attribute in another attribute (line in my case). Not sure if this is a way to go, but it works for me.

@synalice
Copy link
Author

No, actually, not fixed. How do I run code before and after each attribute?

@synalice synalice reopened this Feb 12, 2025
@erezsh
Copy link
Member

erezsh commented Feb 12, 2025

Why do you need to run on every attribute?

And maybe you can just solve it with decorators?

@synalice
Copy link
Author

And maybe you can just solve it with decorators?

Most likely. I'll look into that.

Why do you need to run on every attribute?

I parse file line by line. I want to do certain action if previous line was a specific attribute. Setting variable previous_atteibute_was_the_one is not possible since I'd have to set it to False in every tag that's not the one I want..

@erezsh
Copy link
Member

erezsh commented Feb 13, 2025

Then you don't need every attribute, you only need the attribute that marks a new line.

Btw you can use @v_args(wrapper=...) on the class to decorate every method in the class with one line. But maybe it's better to keep it simple.

@synalice
Copy link
Author

Btw you can use @v_args(wrapper=...) on the class to decorate every method in the class with one line.

I tried really hard to make it work, but to no avail. Could you please provide an example? There is currently no one in the documentation.

Then you don't need every attribute, you only need the attribute that marks a new line.

That's really smart. I'd try that, thanks. Thought, I would still like to know how to achieve that with @v_args(wrapper=...).

@erezsh
Copy link
Member

erezsh commented Feb 13, 2025

Sorry, I made a mistake. v_args only applies to Transformer, not Visitor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants