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

Rule names can collide with Tatsu internals and raise SemanticError #141

Open
EGrunzke opened this issue Sep 17, 2019 · 4 comments
Open

Comments

@EGrunzke
Copy link

Summary

The generated ModelBuilderSemantics class raises a tatsu.exceptions.SemanticError: Could not call constructor for Sequence: 'closure' object is not callable

Context

Some cases work:

  • tatsu.compile(grammar).parse("(test)")
  • BugParser().parse("(test)")

But BugParser().parse("(test)", semantics=BugModelBuilderSemantics()) fails

The grammar

# bug.peg
@@grammar :: Bug

start = sequence $ ;

sequence::Sequence = children:{term [',']}+ ;

term = parens | word ;

parens = '(' ~ @:sequence ')' ;

word::Word = val:/\w+/ ;

Reproduction

Reproduction

  1. Generate the parser and model:
    python -m tatsu bug.peg -o bug_parser.py -G bug_model.py

  2. Run

parser = BugParser()
semantics = BugModelBuilderSemantics()
parser.parse("(test)", semantics=semantics)

GitHub repo with full code

Full reproduction code is available:
https://github.com/EGrunzke/tatsu-sematic-error-closure-not-callable

@EGrunzkeCvg
Copy link

EGrunzkeCvg commented Sep 17, 2019

I've kept looking at this and I think the problem is a naming collision. My Sequence rule has a name children. This collides with the internal Node object that Sequence extends.

https://github.com/neogeny/TatSu/blob/master/tatsu/objectmodel.py#L145

The solution is to either consider children a reserved word (and append an underscore) or to change the variable name in Node to be something safer, like _children, though that could still collide.

As a workaround, switching from
sequence::Sequence = children:{term [',']}+ ; to
sequence::Sequence = kids:{term [',']}+ ;
works great!

@apalala
Copy link
Collaborator

apalala commented Jan 11, 2020

Ah! Collisions!

I need to think about how to get rid of those in the next version of TatSu. Most of the collisions come from trying to make AST and Node provide access to fields through both __getitem__() and __getattr__(). Most grammars should be able to get away with AST nodes that don't have a base class (@dataclass perhaps?).

@apalala
Copy link
Collaborator

apalala commented Mar 21, 2021

Hello @EGrunzke ! Do we still need to do something about this issue?

@EGrunzke
Copy link
Author

I am still able to reproduce using the grammar from the original post. I used Tatsu 5.6.1

This isn't much of a blocker, as a simple rename of the rule solves the problem. But it does still exist!

@EGrunzke EGrunzke changed the title SemanticError: Could not call constructor for Sequence: 'closure' object is not callable Rule names can collide with Tatsu internals and raise SemanticError Mar 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants