Fix parsing of nested parenthesis around Number nodes

The parser would lose track of nested parenthesis, because it was
reading the wrong lpar/rpar value.

Thanks for reporting this, @rayjzeng!
This commit is contained in:
Benjamin Woodruff 2019-07-16 14:45:50 -07:00
parent a8ca94691e
commit 11349acdea
2 changed files with 30 additions and 2 deletions

View file

@ -61,7 +61,34 @@ class NumberTest(CSTNodeTest):
parse_expression,
CodeRange.create((1, 0), (1, 3)),
),
# TODO: add test cases for "((5))" and "(+((5)))"
# multiple nested parenthesis
(
cst.Number(
cst.Integer(
"5",
lpar=(cst.LeftParen(), cst.LeftParen()),
rpar=(cst.RightParen(), cst.RightParen()),
)
),
"((5))",
parse_expression,
CodeRange.create((1, 0), (1, 5)),
),
(
cst.Number(
lpar=(cst.LeftParen(),),
operator=cst.Plus(),
number=cst.Integer(
"5",
lpar=(cst.LeftParen(), cst.LeftParen()),
rpar=(cst.RightParen(), cst.RightParen()),
),
rpar=(cst.RightParen(),),
),
"(+((5)))",
parse_expression,
CodeRange.create((1, 1), (1, 7)),
),
)
)
def test_valid(

View file

@ -790,7 +790,8 @@ def convert_atom_parens(config: ParserConfig, children: Sequence[Any]) -> Any:
return WithLeadingWhitespace(
inner_atom.with_changes(
number=inner_atom.number.with_changes(
lpar=(lpar, *inner_atom.lpar), rpar=(*inner_atom.rpar, rpar)
lpar=(lpar, *inner_atom.number.lpar),
rpar=(*inner_atom.number.rpar, rpar),
)
),
lpar_tok.whitespace_before,