gh-98831: Modernize FORMAT_VALUE (#101628)

Generator update: support balanced parentheses and brackets in conditions and size expressions.
This commit is contained in:
Guido van Rossum 2023-02-07 17:35:55 -08:00 committed by GitHub
parent aacbdb0c65
commit b2b85b5db9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 35 deletions

View file

@ -263,7 +263,14 @@ class Parser(PLexer):
@contextual
def expression(self) -> Expression | None:
tokens: list[lx.Token] = []
while (tkn := self.peek()) and tkn.kind not in (lx.RBRACKET, lx.RPAREN):
level = 1
while tkn := self.peek():
if tkn.kind in (lx.LBRACKET, lx.LPAREN):
level += 1
elif tkn.kind in (lx.RBRACKET, lx.RPAREN):
level -= 1
if level == 0:
break
tokens.append(tkn)
self.next()
if not tokens: