Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e55a0e9

Browse files
authored
Fix 'gather' rules in the python parser generator (GH-22021)
Currently, empty sequences in gather rules make the conditional for gather rules fail as empty sequences evaluate as "False". We need to explicitly check for "None" (the failure condition) to avoid false negatives.
1 parent 315a61f commit e55a0e9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Lib/test/test_peg_generator/test_pegen.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_typed_rules(self) -> None:
7474
"Rule('term', 'int', Rhs([Alt([NamedItem(None, NameLeaf('NUMBER'))])]))"
7575
)
7676

77-
def test_repeat_with_separator_rules(self) -> None:
77+
def test_gather(self) -> None:
7878
grammar = """
7979
start: ','.thing+ NEWLINE
8080
thing: NUMBER
@@ -85,6 +85,20 @@ def test_repeat_with_separator_rules(self) -> None:
8585
"Rule('start', None, Rhs([Alt([NamedItem(None, Gather(StringLeaf(\"','\"), NameLeaf('thing'"
8686
))
8787
self.assertEqual(str(rules["thing"]), "thing: NUMBER")
88+
parser_class = make_parser(grammar)
89+
node = parse_string("42\n", parser_class)
90+
assert node == [
91+
[[TokenInfo(NUMBER, string="42", start=(1, 0), end=(1, 2), line="42\n")]],
92+
TokenInfo(NEWLINE, string="\n", start=(1, 2), end=(1, 3), line="42\n"),
93+
]
94+
node = parse_string("1, 2\n", parser_class)
95+
assert node == [
96+
[
97+
[TokenInfo(NUMBER, string="1", start=(1, 0), end=(1, 1), line="1, 2\n")],
98+
[TokenInfo(NUMBER, string="2", start=(1, 3), end=(1, 4), line="1, 2\n")],
99+
],
100+
TokenInfo(NEWLINE, string="\n", start=(1, 4), end=(1, 5), line="1, 2\n"),
101+
]
88102

89103
def test_expr_grammar(self) -> None:
90104
grammar = """

Tools/peg_generator/pegen/python_generator.py

+3
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ def visit_Alt(self, node: Alt, is_loop: bool, is_gather: bool) -> None:
217217
else:
218218
self.print("and")
219219
self.visit(item)
220+
if is_gather:
221+
self.print("is not None")
222+
220223
self.print("):")
221224
with self.indent():
222225
action = node.action

0 commit comments

Comments
 (0)