diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2020-05-10 04:34:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-10 04:34:50 (GMT) |
commit | ac7a92cc0a821699df48bc2e30a02c25d6338f78 (patch) | |
tree | d7f301b66d79b6d3ce497669a99a5f9ce47ba3b5 /Lib/test | |
parent | 2c3d508c5fabe40dac848fb9ae558069f0576879 (diff) | |
download | cpython-ac7a92cc0a821699df48bc2e30a02c25d6338f78.zip cpython-ac7a92cc0a821699df48bc2e30a02c25d6338f78.tar.gz cpython-ac7a92cc0a821699df48bc2e30a02c25d6338f78.tar.bz2 |
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`
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_peg_generator/test_pegen.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_peg_generator/test_pegen.py b/Lib/test/test_peg_generator/test_pegen.py index 0a2a6d4..30e1b67 100644 --- a/Lib/test/test_peg_generator/test_pegen.py +++ b/Lib/test/test_peg_generator/test_pegen.py @@ -540,6 +540,33 @@ class TestPegen(unittest.TestCase): with self.assertRaises(GrammarError): parser_class = make_parser(grammar) + def test_invalid_rule_name(self) -> None: + grammar = """ + start: _a b + _a: 'a' + b: 'b' + """ + with self.assertRaisesRegex(GrammarError, "cannot start with underscore: '_a'"): + parser_class = make_parser(grammar) + + def test_invalid_variable_name(self) -> None: + grammar = """ + start: a b + a: _x='a' + b: 'b' + """ + with self.assertRaisesRegex(GrammarError, "cannot start with underscore: '_x'"): + parser_class = make_parser(grammar) + + def test_invalid_variable_name_in_temporal_rule(self) -> None: + grammar = """ + start: a b + a: (_x='a' | 'b') | 'c' + b: 'b' + """ + with self.assertRaisesRegex(GrammarError, "cannot start with underscore: '_x'"): + parser_class = make_parser(grammar) + class TestGrammarVisitor: class Visitor(GrammarVisitor): |