Skip to content

py: Decorator syntax relaxation. #25

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RayaneCTX
Copy link

Signed-off-by: Rayane Chatrieux rayane.chatrieux@gmail.com

@RayaneCTX RayaneCTX force-pushed the decorator-syntax-relaxation branch 9 times, most recently from b55e6a3 to 1a41e2f Compare April 7, 2022 23:53
@codecov-commenter
Copy link

codecov-commenter commented Apr 11, 2022

Codecov Report

Merging #25 (51f36ba) into master (71344c1) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #25      +/-   ##
==========================================
- Coverage   98.25%   98.25%   -0.01%     
==========================================
  Files         154      154              
  Lines       20287    20286       -1     
==========================================
- Hits        19932    19931       -1     
  Misses        355      355              
Impacted Files Coverage Δ
py/compile.c 99.93% <100.00%> (-0.01%) ⬇️
py/parse.c 99.15% <100.00%> (+<0.01%) ⬆️
py/persistentcode.c 97.17% <0.00%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 71344c1...51f36ba. Read the comment docs.

@RayaneCTX RayaneCTX force-pushed the decorator-syntax-relaxation branch 3 times, most recently from e3b7d1d to be8df3d Compare April 12, 2022 15:11
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.

Signed-off-by: Rayane Chatrieux <rayane.chatrieux@gmail.com>
@RayaneCTX RayaneCTX force-pushed the decorator-syntax-relaxation branch from be8df3d to 808fdbb Compare April 12, 2022 19:22
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

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy