lark-parser / lark

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

Exclude classes in create_tranformer by user provided pedicate #1378

Open vityaman opened 7 months ago

vityaman commented 7 months ago

Suggestion

I suggest to add an option for user to exclude classes from being handled by transformer produced by ast_utils.create_transformer not only by underscoring class name, but with custom predicate. Here in lark/ast_utils.py we have hardcoded condition for exclusion.

Why

It seems to me that its quite unpleasant to do such things as we may wish to use such underscored classes as normal in our program, so why we has to make their name ugly in some way.

I faced with this fact during the development of such AST hierarchy: https://github.com/vityaman-edu/sleepy/pull/8/files#diff-62348556570d26da9f0e70903e81ee752f97a3ea6cf48bbfe9c138f080a8d329R15

There I don't really want to have names like _Expression, so my only option is to do something like

Expression = _Expression
Expression.__name__ = "Expression"

after calling to create_transformer. Other way is converting this like LarkAST to MyOwnAST after parsing looks like overengineering. But it seems to me that the solution of this problem is nearly trivial.

Proposal

My proposal is

  1. Add another parameter predicate excluded to create_transformer. Then the signature would be
    def create_transformer(
    ast_module: types.ModuleType,
    transformer: Optional[Transformer]=None,
    decorator_factory: Callable=v_args,
    excluded: Callable[[str], bool]=lambda name: name.startswith('_'),
    ) -> Transformer:

Then add it to that condition.

for name, obj in inspect.getmembers(ast_module):
  if not name.startswith('_') and not excluded(name) and inspect.isclass(obj):
    if issubclass(obj, Ast):

I left not name.startswith('_') to ensure that the statement "Classes starting with an underscore (_) will be skipped." remains true in general. But maybe it can be removed, so it needs to be discussed.

Alternatives

User effect

Actions

  1. Edit ast_utils.py:create_transformer, update docstring.
  2. Update project docs?
  3. Make an MR linked to this issue.

I can make an MR and solve this issue myself if you let me.

Discuss