-
Notifications
You must be signed in to change notification settings - Fork 1
Function keyword syntax restriction #21
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
base: master
Are you sure you want to change the base?
Conversation
This seems to be too much of a hack. This code would not be maintainable as it is. What is a good way of figuring out, in the code, what is the right distance between
|
So I guess the issue is that you are not certain that the added if stmt in parser is used for other than function arguments? |
I am pretty certain that the if statement will only catch function arguments (I don't think it would be possible for an My concern is with the way I am currently implementing the change: it is not maintainable, because I hard coded the 15 in, thus creating a strong dependence on the grammar, and one that isn't even made explicit by the code. I need to change this to something that doesn't have this dependence before I make a PR to the main repo. |
Another option:
|
Implemented function keyword argument syntax restriction added with Python 3.8 (listed under issue micropython#7899). This is done by adding a switch in the push_result_rule function in parse.c to turn off the parentheses removal optimization for atom_paren rules encountered while parsing an arglist rule (meaning this atom_paren rule is a function argument) and when the enclosed node is a lone ID. Keeping the atom_paren rule in the parse tree allows the compiler to catch the syntax error when the rule happens to be a keyword argument. In order to determine whether an atom_paren rule is a function argument, a bit is added to the parser structure, which is set in mp_parse when an arglist rule is being parsed (unset once done with it). Other options were considered: hard-coding the distance on the rule stack between an arglist rule and an atom_paren rule in the added switch, using a global boolean variable, using a local boolean variable (by expanding the function signature of the push_result_rule function). Adding a field to the parser structure seemed to provide the best balance between code size increase, code changes, memory usage overhead, code maintainability, etc. Because the compile_atom_paren function did not expect this rule to be a parent for a lone ID, a switch is added in that function to handle this. A new test file fun_kwargs_syntax.py is added to make sure the syntax restriction is in effect, and a test is added to fun2.py to make sure extraneous parenthese around ID function arguments does not cause any issues. Signed-off-by: Rayane Chatrieux <rayane.chatrieux@gmail.com>
d380fd5
to
3f2afe9
Compare
Addresses the function keyword argument syntax restriction issued in Python 3.8. Code such as
f((a) = 2)
now raises a syntax error.