-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-143055: Implementation of PEP 798 #143056
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
Changes from 8 commits
0288001
a7ea132
c23496e
49405ac
26841f9
3ebe2f6
0713bbc
0393552
fa16f82
81e86b3
74df6b9
23538a8
fcf129f
acaf3b1
b5ae690
7c16250
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -709,12 +709,17 @@ expressions[expr_ty]: | |||||||||||||||||||||||||||||||||||||||||||||||||||
| | expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| expression[expr_ty] (memo): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_if_expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_legacy_expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | if_expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | disjunction | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | lambdef | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if_expression[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_if_expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure this alternative is never reached, since, in the second pass, it's encountered in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for taking a look! Yep, I think you're right that we can't reach |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| yield_expr[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | 'yield' 'from' a=expression { _PyAST_YieldFrom(a, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | 'yield' a=[star_expressions] { _PyAST_Yield(a, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -731,10 +736,16 @@ star_expression[expr_ty] (memo): | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| star_named_expressions[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression+ [','] { a } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| star_named_expressions_sequence[asdl_expr_seq*]: a[asdl_expr_seq*]=','.star_named_expression_sequence+ [','] { a } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| star_named_expression[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '*' a=bitwise_or { _PyAST_Starred(a, Load, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | named_expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| star_named_expression_sequence[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_starred_expression_unpacking_sequence | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | star_named_expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| assignment_expression[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a=NAME ':=' ~ b=expression { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| CHECK_VERSION(expr_ty, 8, "Assignment expressions are", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -882,9 +893,9 @@ atom[expr_ty]: | |||||||||||||||||||||||||||||||||||||||||||||||||||
| | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | &(STRING|FSTRING_START|TSTRING_START) strings | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | NUMBER | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | &'(' (tuple | group | genexp) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | &'[' (list | listcomp) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | &'{' (dict | set | dictcomp | setcomp) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | &'(' ( genexp | tuple | group) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
adqm marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| | &'[' (listcomp | list) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | &'{' (dictcomp | setcomp | dict | set) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+895
to
+897
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing the order here is probably only about error messages, right? If so, have you checked whether this affects parsing performance at all? To be clear, I'm okay to sacrifice a bit of performance for better error messages.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, the reordering here was purely for error reporting. I hadn't done any performance testing before, but I did a quick, unscientific test this morning, doing some quick timings on As might be expected, compared to the place I branched from, it looks like this branch is, in general, a little bit faster on comprehensions and a little bit slower on literal lists. It's a little hard to say how this works out on the whole, but the time for parsing the full standard library was always within ±2% every time I ran it. I can look more thoroughly if we're concerned about this, but this little test at least suggests that things haven't blown up on average. Sample results:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| group[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -998,13 +1009,13 @@ strings[expr_ty] (memo): | |||||||||||||||||||||||||||||||||||||||||||||||||||
| | a[asdl_expr_seq*]=tstring+ { _PyPegen_concatenate_tstrings(p, a, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| list[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '[' a=[star_named_expressions_sequence] ']' { _PyAST_List(a, Load, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| tuple[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '(' a=[y=star_named_expression ',' z=[star_named_expressions] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '(' a=[y=star_named_expression_sequence ',' z=[star_named_expressions_sequence] { _PyPegen_seq_insert_in_front(p, y, z) } ] ')' { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _PyAST_Tuple(a, Load, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| set[expr_ty]: '{' a=star_named_expressions '}' { _PyAST_Set(a, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| set[expr_ty]: '{' a=star_named_expressions_sequence '}' { _PyAST_Set(a, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Dicts | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ----- | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1040,20 +1051,20 @@ for_if_clause[comprehension_ty]: | |||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_for_target | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| listcomp[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '[' a=named_expression b=for_if_clauses ']' { _PyAST_ListComp(a, b, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '[' a=star_named_expression b=for_if_clauses ']' { _PyAST_ListComp(a, b, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_comprehension | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| setcomp[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '{' a=named_expression b=for_if_clauses '}' { _PyAST_SetComp(a, b, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '{' a=star_named_expression b=for_if_clauses '}' { _PyAST_SetComp(a, b, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_comprehension | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| genexp[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '(' a=( assignment_expression | expression !':=') b=for_if_clauses ')' { _PyAST_GeneratorExp(a, b, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '(' a=( assignment_expression | expression !':=' | starred_expression ) b=for_if_clauses ')' { _PyAST_GeneratorExp(a, b, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_comprehension | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| dictcomp[expr_ty]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '{' a=kvpair b=for_if_clauses '}' { _PyAST_DictComp(a->key, a->value, b, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_dict_comprehension | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '{' '**' a=expression b=for_if_clauses '}' { _PyAST_DictComp(a, NULL, b, EXTRA) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| # FUNCTION CALL ARGUMENTS | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ======================= | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1262,6 +1273,12 @@ invalid_expression: | |||||||||||||||||||||||||||||||||||||||||||||||||||
| | a='lambda' [lambda_params] b=':' &TSTRING_MIDDLE { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "t-string: lambda expressions are not allowed without parentheses") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_if_expression: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | disjunction 'if' b=disjunction 'else' a='*' { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot unpack only part of a conditional expression") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | disjunction 'if' b=disjunction 'else' a='**' { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use dict unpacking on only part of a conditional expression") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_named_expression(memo): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a=expression ':=' expression { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_LOCATION( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1326,16 +1343,15 @@ invalid_assert_stmt: | |||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_block: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_comprehension: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ('[' | '(' | '{') a=starred_expression for_if_clauses { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '[' a='**' b=expression for_if_clauses { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use dict unpacking in list comprehension") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
JelleZijlstra marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '(' a='**' b=expression for_if_clauses { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use dict unpacking in generator expression") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ('[' | '{') a=star_named_expression ',' b=star_named_expressions for_if_clauses { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, PyPegen_last_item(b, expr_ty), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "did you forget parentheses around the comprehension target?") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ('[' | '{') a=star_named_expression b=',' for_if_clauses { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "did you forget parentheses around the comprehension target?") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_dict_comprehension: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '{' a='**' bitwise_or for_if_clauses '}' { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a="/" ',' { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "at least one parameter must precede /") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1530,19 +1546,32 @@ invalid_class_def_raw: | |||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_INDENTATION_ERROR("expected an indented block after class definition on line %d", a->lineno) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_double_starred_kvpairs: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ','.double_starred_kvpair+ ',' invalid_kvpair | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_kvpair_unpacking [','] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ','.double_starred_kvpair+ ',' (invalid_kvpair | invalid_kvpair_unpacking) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_kvpair_unpacking: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a='**' b=if_expression { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid double starred expression. Did you forget to wrap the conditional expression in parentheses?") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a='*' b=bitwise_or ':' expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use a starred expression in a dictionary key") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a='**' b=bitwise_or ':' expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use dict unpacking in a dictionary key") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | expression ':' a='*' b=bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use a starred expression in a dictionary value") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | expression ':' a='**' b=bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot use dict unpacking in a dictionary value") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_kvpair: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a=expression !(':') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | expression ':' a='**' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use dict unpacking in a dictionary value") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_starred_expression_unpacking: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a='*' b=if_expression { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid starred expression. Did you forget to wrap the conditional expression in parentheses?") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a='*' expression '=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to iterable argument unpacking") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_starred_expression_unpacking_sequence: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | a='**' bitwise_or { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use dict unpacking here") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | invalid_starred_expression_unpacking | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_starred_expression: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '*' { RAISE_SYNTAX_ERROR("Invalid star expression") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| invalid_fstring_replacement_field: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '='") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| | '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '!'") } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.