[issue12782] Multiple context expressions do not support parentheses for continuation across lines

Nick Coghlan report at bugs.python.org
Fri Sep 28 10:48:19 EDT 2018


Nick Coghlan <ncoghlan at gmail.com> added the comment:

Especially since the dynamic flexibility of ExitStack comes at a genuine runtime cost when unwinding the resource stack.

I also (very!) belatedly noticed that I never answered Julian's request for clarification about the potential grammar ambiguity, so going into detail about that now:

The first item in the grammar after the 'with' keyword is a 'test' node, which can already start with a parenthesis, which means a naive attempt at allowing grouping parentheses will likely fail to generate a valid LL(1) parser.

That doesn't mean a more sophisticated change isn't possible (and Pablo has apparently implemented one) - it just means that the required grammar update is going to be more complicated than just changing:

    with_stmt: 'with' with_item (',' with_item)*  ':' suite

to be:

    with_stmt: 'with' (with_items | '(' with_items ')') ':' suite
    with_items: with_item (',' with_item)*

(That would need too much lookahead to decide whether an opening parenthesis belongs to the first 'with_item' in 'with_items' or if it's starting the alternative multi-line grouping construct)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue12782>
_______________________________________


More information about the Python-bugs-list mailing list