lark icon indicating copy to clipboard operation
lark copied to clipboard

Syntactic sugar: Tree.find_token()

Open makukha opened this issue 1 year ago • 2 comments

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 avatar Sep 11 '24 19:09 makukha