lark-parser / lark

Lark is a parsing toolkit for Python, built with a focus on ergonomics, performance and modularity.
MIT License
4.75k stars 401 forks source link

Req: Option to disable "grammar module name prefixing" #1321

Closed davidmcnabnz closed 2 months ago

davidmcnabnz commented 1 year ago

Suggestion Requesting a constructor option to disable the "double underscore" prefixing of targets based on grammar module names.

Consider grammar file main.lark:

%import foo.target_in_foo
...
start: sometarget

sometarget: othertarget
    | target_in_foo

and grammar file foo.lark

target_in_foo: t1 t2 t3 -> target_in_foo

when target_in_foo is fulfilled, the transformer call doesn't seek the method target_in_foo(). Instead, it seeks foo__target_in_foo(). This means that transformer alias methods need to carry the module name of the matched target.

For refactoring of grammar files ie moving targets between files, this adds the burden of also renaming the transformer target methods.

Describe alternatives you've considered In my transformer class, I have patched around this by adding a __getattr__() shim, which detects the modulename__ prefixes and strips them, returning references for target alias methods that are named without the prefixes.

erezsh commented 1 year ago

Have you tried merge_transformers() for solving this use-case?

Read through this: https://github.com/lark-parser/lark/tree/master/examples/composition