GeezerVison is showing github.com — Fix 'gather' rules in the python parser generator (GH-22021) · python/cpython@e55a0e9
File treeExpand file treeCollapse file tree Open diff view settings Lib/test/test_peg_generator Tools/peg_generator/pegen
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_peg_generator/test_pegen.py +15-1Lines changed: 15 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,7 +74,7 @@ def test_typed_rules(self) -> None: | 74 | 74 | "Rule('term', 'int', Rhs([Alt([NamedItem(None, NameLeaf('NUMBER'))])]))" | 75 | 75 | ) | 76 | 76 |
| 77 | | - def test_repeat_with_separator_rules(self) -> None: | | 77 | + def test_gather(self) -> None: | 78 | 78 | grammar = """ | 79 | 79 | start: ','.thing+ NEWLINE | 80 | 80 | thing: NUMBER | @@ -85,6 +85,20 @@ def test_repeat_with_separator_rules(self) -> None: | 85 | 85 | "Rule('start', None, Rhs([Alt([NamedItem(None, Gather(StringLeaf(\"','\"), NameLeaf('thing'" | 86 | 86 | )) | 87 | 87 | 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 | + ] | 88 | 102 |
| 89 | 103 | def test_expr_grammar(self) -> None: | 90 | 104 | grammar = """ | |
Collapse file Copy file name to clipboardExpand all lines: Tools/peg_generator/pegen/python_generator.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -217,6 +217,9 @@ def visit_Alt(self, node: Alt, is_loop: bool, is_gather: bool) -> None: | 217 | 217 | else: | 218 | 218 | self.print("and") | 219 | 219 | self.visit(item) | | 220 | + if is_gather: | | 221 | + self.print("is not None") | | 222 | + | 220 | 223 | self.print("):") | 221 | 224 | with self.indent(): | 222 | 225 | action = node.action | |
|