summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBatuhan Taskaya <isidentical@gmail.com>2020-06-20 17:40:06 (GMT)
committerGitHub <noreply@github.com>2020-06-20 17:40:06 (GMT)
commit55460ee6dc9a4f16bd68d6b6be3a8398c7d4a596 (patch)
tree2be1b8e48d5d63eb03eea14257faba853b475354
parentaf157fad286c00ff204e86d8556648cbb53ba99e (diff)
downloadcpython-55460ee6dc9a4f16bd68d6b6be3a8398c7d4a596.zip
cpython-55460ee6dc9a4f16bd68d6b6be3a8398c7d4a596.tar.gz
cpython-55460ee6dc9a4f16bd68d6b6be3a8398c7d4a596.tar.bz2
bpo-41044: Generate valid PEG python parsers for opt+seq rules (GH-20995)
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
-rw-r--r--Lib/test/test_peg_generator/test_pegen.py8
-rw-r--r--Tools/peg_generator/pegen/python_generator.py8
2 files changed, 15 insertions, 1 deletions
diff --git a/Lib/test/test_peg_generator/test_pegen.py b/Lib/test/test_peg_generator/test_pegen.py
index 30e1b67..5b4e964 100644
--- a/Lib/test/test_peg_generator/test_pegen.py
+++ b/Lib/test/test_peg_generator/test_pegen.py
@@ -493,6 +493,14 @@ class TestPegen(unittest.TestCase):
# 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 6433655..45a7597 100644
--- a/Tools/peg_generator/pegen/python_generator.py
+++ b/Tools/peg_generator/pegen/python_generator.py
@@ -93,7 +93,13 @@ class PythonCallMakerVisitor(GrammarVisitor):
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: