diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2020-12-26 19:11:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-26 19:11:29 (GMT) |
commit | 3bcc4ead3f66a58604b7ce87d14e909406c3b364 (patch) | |
tree | ab8cf61a0a589e5935f080a861b7b7866a6da7e7 /Lib/test/test_peg_generator | |
parent | 7865f516f313bd31ca48ee1fdae2a80add2293b6 (diff) | |
download | cpython-3bcc4ead3f66a58604b7ce87d14e909406c3b364.zip cpython-3bcc4ead3f66a58604b7ce87d14e909406c3b364.tar.gz cpython-3bcc4ead3f66a58604b7ce87d14e909406c3b364.tar.bz2 |
Add small validator utility for PEG grammars (GH-23519)
Diffstat (limited to 'Lib/test/test_peg_generator')
-rw-r--r-- | Lib/test/test_peg_generator/test_grammar_validator.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/Lib/test/test_peg_generator/test_grammar_validator.py b/Lib/test/test_peg_generator/test_grammar_validator.py new file mode 100644 index 0000000..2e72ff8 --- /dev/null +++ b/Lib/test/test_peg_generator/test_grammar_validator.py @@ -0,0 +1,51 @@ +import unittest +from test import test_tools + +test_tools.skip_if_missing('peg_generator') +with test_tools.imports_under_tool('peg_generator'): + from pegen.grammar_parser import GeneratedParser as GrammarParser + from pegen.validator import SubRuleValidator, ValidationError + from pegen.testutil import parse_string + from pegen.grammar import Grammar + + +class TestPegen(unittest.TestCase): + def test_rule_with_no_collision(self) -> None: + grammar_source = """ + start: bad_rule + sum: + | NAME '-' NAME + | NAME '+' NAME + """ + grammar: Grammar = parse_string(grammar_source, GrammarParser) + validator = SubRuleValidator(grammar) + for rule_name, rule in grammar.rules.items(): + validator.validate_rule(rule_name, rule) + + def test_rule_with_simple_collision(self) -> None: + grammar_source = """ + start: bad_rule + sum: + | NAME '+' NAME + | NAME '+' NAME ';' + """ + grammar: Grammar = parse_string(grammar_source, GrammarParser) + validator = SubRuleValidator(grammar) + with self.assertRaises(ValidationError): + for rule_name, rule in grammar.rules.items(): + validator.validate_rule(rule_name, rule) + + def test_rule_with_collision_after_some_other_rules(self) -> None: + grammar_source = """ + start: bad_rule + sum: + | NAME '+' NAME + | NAME '*' NAME ';' + | NAME '-' NAME + | NAME '+' NAME ';' + """ + grammar: Grammar = parse_string(grammar_source, GrammarParser) + validator = SubRuleValidator(grammar) + with self.assertRaises(ValidationError): + for rule_name, rule in grammar.rules.items(): + validator.validate_rule(rule_name, rule) |