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

Syntactic sugar: Tree.find_token() #1466

Open
makukha opened this issue Sep 11, 2024 · 2 comments · May be fixed by #1467
Open

Syntactic sugar: Tree.find_token() #1466

makukha opened this issue Sep 11, 2024 · 2 comments · May be fixed by #1467

Comments

@makukha
Copy link

makukha commented Sep 11, 2024

Suggestion
Add new method Tree.find_type(typ: str) -> Iterator[Token] as a companion for Tree.find_data():

for term in tree.find_type('TERM'):
    ...

Describe alternatives you've considered
Right now this can be achieved by

for term in tree.find_pred(lambda node: isinstance(node, Token) and node.type == 'TERM'):
    ...

which doesn't fit Black's line width and will look even uglier in real cases.

Additional context
I can provide PR if you find this enhancement reasonable.

The method proposed seems to be a logical extension of existing API:

Tree => find_data()
Token => find_type()
@makukha
Copy link
Author

makukha commented Sep 11, 2024

Sorry for bothering, just started using Lark, and see missing pieces (as of my taste). Handy API already made many packages super-popular (like click, requests etc.)

@erezsh
Copy link
Member

erezsh commented Sep 11, 2024

Actually you need to use scan_values()

But, yeah, I think that's a good one to add. But let's call it find_token

@makukha makukha changed the title Syntactic sugar: Tree.find_type() Syntactic sugar: Tree.find_token() Sep 11, 2024
@makukha makukha linked a pull request Sep 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants