bpo-41044: Generate valid PEG python parsers for opt+seq rules (GH-20995)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
This commit is contained in:
Batuhan Taskaya 2020-06-20 20:40:06 +03:00 committed by GitHub
parent af157fad28
commit 55460ee6dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -493,6 +493,14 @@ class TestPegen(unittest.TestCase):
# Would assert False without a special case in compute_left_recursives(). # Would assert False without a special case in compute_left_recursives().
make_parser(grammar) make_parser(grammar)
def test_opt_sequence(self) -> None:
grammar = """
start: [NAME*]
"""
# This case was failing because of a double trailing comma at the end
# of a line in the generated source. See bpo-41044
make_parser(grammar)
def test_left_recursion_too_complex(self) -> None: def test_left_recursion_too_complex(self) -> None:
grammar = """ grammar = """
start: foo start: foo

View File

@ -93,7 +93,13 @@ class PythonCallMakerVisitor(GrammarVisitor):
def visit_Opt(self, node: Opt) -> Tuple[str, str]: def visit_Opt(self, node: Opt) -> Tuple[str, str]:
name, call = self.visit(node.node) name, call = self.visit(node.node)
return "opt", f"{call}," # Note trailing comma! # Note trailing comma (the call may already have one comma
# at the end, for example when rules have both repeat0 and optional
# markers, e.g: [rule*])
if call.endswith(","):
return "opt", call
else:
return "opt", f"{call},"
def visit_Repeat0(self, node: Repeat0) -> Tuple[str, str]: def visit_Repeat0(self, node: Repeat0) -> Tuple[str, str]:
if node in self.cache: if node in self.cache: