[Python-checkins] bpo-41044: Generate valid PEG python parsers for opt+seq rules (GH-20995)
Batuhan Taskaya
webhook-mailer at python.org
Sat Jun 20 13:40:11 EDT 2020
https://github.com/python/cpython/commit/55460ee6dc9a4f16bd68d6b6be3a8398c7d4a596
commit: 55460ee6dc9a4f16bd68d6b6be3a8398c7d4a596
branch: master
author: Batuhan Taskaya <isidentical at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-06-20T18:40:06+01:00
summary:
bpo-41044: Generate valid PEG python parsers for opt+seq rules (GH-20995)
Co-authored-by: Pablo Galindo <Pablogsal at gmail.com>
files:
M Lib/test/test_peg_generator/test_pegen.py
M Tools/peg_generator/pegen/python_generator.py
diff --git a/Lib/test/test_peg_generator/test_pegen.py b/Lib/test/test_peg_generator/test_pegen.py
index 30e1b675643b2..5b4e964d698ad 100644
--- a/Lib/test/test_peg_generator/test_pegen.py
+++ b/Lib/test/test_peg_generator/test_pegen.py
@@ -493,6 +493,14 @@ def test_start_leader(self) -> None:
# Would assert False without a special case in compute_left_recursives().
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:
grammar = """
start: foo
diff --git a/Tools/peg_generator/pegen/python_generator.py b/Tools/peg_generator/pegen/python_generator.py
index 64336552f24f6..45a75975dbf5e 100644
--- a/Tools/peg_generator/pegen/python_generator.py
+++ b/Tools/peg_generator/pegen/python_generator.py
@@ -93,7 +93,13 @@ def visit_NegativeLookahead(self, node: NegativeLookahead) -> Tuple[None, str]:
def visit_Opt(self, node: Opt) -> Tuple[str, str]:
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]:
if node in self.cache:
More information about the Python-checkins
mailing list