You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
py: Implement decorator syntax relaxation from CPython 3.9.
py/grammar.h: The grammar file hosts the core change associated with
this pull request. The decorator rule is changed to match a
namedexpr_test rule rather than a dotted_name rule. However, since the
compiler currently relies on the dotted_name rule to easily detect
built-in decorators (e.g. @micropython.bytecode), a new way of detecting
these built-ins needed to be found:
1. One possibility is to have the grammar try to match a dotted_name
first and a namedexpr_test second, but since the parser does not
bactrack through lexical tokens, it is not obvious to me how to
accomplish this.
2. Another possiblity is to write code in the compiler to visit the
AST rooted at the namedexpr_test rule of a given decorator and
determine whether said decorator is a built-in that way. This
could potentially be an expensive operation and/or lead to a
significant increase in code size.
3. [SELECTED] A third option is to introduce a grammar rule
built_in_decorator that tries to match a "micropython" string,
and backtracks into a namedexpr_test rule if there is no match.
This relies on the fact that all built-in decorators start with
"micropython". One possible concern is that any decorator
starting with the "micropython" NAME token must match with a
built-in decorator, otherwise a syntax error is issued. This is,
however, congruent with current behavior: this is exactly what
the compile_built_in_decorator function would do (before this
commit).
py/parse.c: Add a check to attempt to match the "micropython"
string to a NAME token when parsing a built_in_decrator rule. The parser
backtracks out of the rule if there is no match.
py/compile.c: Since the compile_built_in_decorator function is not used
anywhere else, I took the liberty to simplify this function to match the
new grammar.
tests/basics: Amend the decorator.py test with additional tests for
syntax, type and name errors. Adds an expected output file so that this
test works out-of-the-box with older versions of CPython than 3.9.
tests/cmdline/cmd_parsetree.py.exp: Update the expected rule IDs in the
expected output since the grammar is changed.
tools/ci.sh: Exclude the decorator_full.py test from the minimal build
since it uses several unsupported features (e.g. decimal/complex numbers).
ports/qemu_arm/Makefile.test: Exclude the decorator_full.py test here
as well.
Signed-off-by: Rayane Chatrieux <rayane.chatrieux@gmail.com>
0 commit comments