Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,6 @@ bitwise_and[expr_ty]:
shift_expr[expr_ty]:
| a=shift_expr '<<' b=sum { _PyAST_BinOp(a, LShift, b, EXTRA) }
| a=shift_expr '>>' b=sum { _PyAST_BinOp(a, RShift, b, EXTRA) }
| invalid_arithmetic
| sum

# Arithmetic operators
Expand All @@ -844,6 +843,7 @@ shift_expr[expr_ty]:
sum[expr_ty]:
| a=sum '+' b=term { _PyAST_BinOp(a, Add, b, EXTRA) }
| a=sum '-' b=term { _PyAST_BinOp(a, Sub, b, EXTRA) }
| invalid_arithmetic
| term

term[expr_ty]:
Expand All @@ -852,14 +852,14 @@ term[expr_ty]:
| a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) }
| a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) }
| a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _PyAST_BinOp(a, MatMult, b, EXTRA)) }
| invalid_factor
| factor

factor[expr_ty] (memo):
| '+' a=factor { _PyAST_UnaryOp(UAdd, a, EXTRA) }
| '-' a=factor { _PyAST_UnaryOp(USub, a, EXTRA) }
| '~' a=factor { _PyAST_UnaryOp(Invert, a, EXTRA) }
| power
| invalid_factor

power[expr_ty]:
| a=await_primary '**' b=factor { _PyAST_BinOp(a, Pow, b, EXTRA) }
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,22 @@
Traceback (most recent call last):
SyntaxError: 'not' after an operator must be parenthesized
>>> 1 << 2 + not 3
Traceback (most recent call last):
SyntaxError: 'not' after an operator must be parenthesized
>>> 1 >> 2 * not 3
Traceback (most recent call last):
SyntaxError: 'not' after an operator must be parenthesized
>>> 3 * + not 3
Traceback (most recent call last):
SyntaxError: 'not' after an operator must be parenthesized
>>> 3 ** - not 3
Traceback (most recent call last):
SyntaxError: 'not' after an operator must be parenthesized
# Check that we don't introduce misleading errors
>>> not 1 */ 2
Traceback (most recent call last):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve syntax error messages for misplaced ``not`` after an operator
in contexts where a generic "invalid syntax" error was previously reported,
such as ``1 << 2 + not x`` and ``1 * + not x``.
86 changes: 43 additions & 43 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading