I was going through the python grammer specification and find the following statement
for_stmt: | 'for' star_targets 'in' ~ star_expressions ':' [TYPE_COMMENT] block [else_block]
What does ~
means in this grammar rule?. The other symbols used in the grammer(like &
, !
, |
) are already documented but not ~
.
The notation is a mixture of
EBNF
andPEG
. In particular,&
followed by a symbol, token or parenthesized group indicates a positive lookahead (i.e., is required to match but not consumed), while!
indicates a negative lookahead (i.e., is required not to match). We use the|
separator to mean PEG’s “ordered choice” (written as/
in traditional PEG grammars)
It's documented in PEP 617 under Grammar Expressions:
~
Commit to the current alternative, even if it fails to parse.
rule_name: '(' ~ some_rule ')' | some_alt
In this example, if a left parenthesis is parsed, then the other alternative won’t be considered, even if some_rule or ‘)’ fail to be parsed.
The ~
basically indicates that once you reach it, you're locked into the particular rule and cannot move onto the next rule if the parse fails.
PEP 617 mentions earlier that | some_alt
can be written in the next line.