dabeaz / sly

Sly Lex Yacc
Other
816 stars 107 forks source link

what does @_('MINUS expr %prec UMINUS') mean and how it is used internally #97

Closed vnluc closed 2 years ago

vnluc commented 2 years ago

I just start playing with sly and I found this code.

@_('MINUS expr %prec UMINUS') def expr(p): return -p.expr

But unfortunately I can not understand how this @('MINUS expr %prec UMINUS') is used inside framework. From the syntax look like we apply a decorator to this method but where this decorator is defined and how it is used. Can any one please explain this?

jpsnyder commented 2 years ago

It's python black magic using the __prepare__() function to override _ to be a decorator that attaches rules to the function which are later used to generate the grammar.... from my understanding. https://github.com/dabeaz/sly/blob/cd9014eda2c446dbadd370485075a700da92c24c/sly/yacc.py#L1807

But you don't need to know all that to use it (unless you are morbidly curious). Recommend just reading the documentation and trusting it works :)

dabeaz commented 2 years ago

My kids are into these "gamer keyboards" that have backlights which constantly change colors into interesting shades of red, purple, and blue. The @_() is kind of like that--except that it makes the IDE change colors.

vnluc commented 2 years ago

Thank you for your kind response, I started looking at it now with some clue. At least I know what it mean now.