Skip to content

Commit ac7a92c

Browse files
authored
bpo-40334: Avoid collisions between parser variables and grammar variables (GH-19987)
This is for the C generator: - Disallow rule and variable names starting with `_` - Rename most local variable names generated by the parser to start with `_` Exceptions: - Renaming `p` to `_p` will be a separate PR - There are still some names that might clash, e.g. - anything starting with `Py` - C reserved words (`if` etc.) - Macros like `EXTRA` and `CHECK`
1 parent 2c3d508 commit ac7a92c

File tree

5 files changed

+5758
-5716
lines changed

5 files changed

+5758
-5716
lines changed

Lib/test/test_peg_generator/test_pegen.py

+27
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,33 @@ def test_missing_start(self) -> None:
540540
with self.assertRaises(GrammarError):
541541
parser_class = make_parser(grammar)
542542

543+
def test_invalid_rule_name(self) -> None:
544+
grammar = """
545+
start: _a b
546+
_a: 'a'
547+
b: 'b'
548+
"""
549+
with self.assertRaisesRegex(GrammarError, "cannot start with underscore: '_a'"):
550+
parser_class = make_parser(grammar)
551+
552+
def test_invalid_variable_name(self) -> None:
553+
grammar = """
554+
start: a b
555+
a: _x='a'
556+
b: 'b'
557+
"""
558+
with self.assertRaisesRegex(GrammarError, "cannot start with underscore: '_x'"):
559+
parser_class = make_parser(grammar)
560+
561+
def test_invalid_variable_name_in_temporal_rule(self) -> None:
562+
grammar = """
563+
start: a b
564+
a: (_x='a' | 'b') | 'c'
565+
b: 'b'
566+
"""
567+
with self.assertRaisesRegex(GrammarError, "cannot start with underscore: '_x'"):
568+
parser_class = make_parser(grammar)
569+
543570

544571
class TestGrammarVisitor:
545572
class Visitor(GrammarVisitor):

0 commit comments

Comments
 (0)