Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-40334: Avoid collisions between parser variables and grammar variables #19987

Merged
merged 2 commits into from
May 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Address feedback
  • Loading branch information
pablogsal committed May 10, 2020
commit 9a5559dd31a0bb4a78cf73e16623a99d2902f427
8 changes: 4 additions & 4 deletions Lib/test/test_peg_generator/test_pegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,11 @@ def test_missing_start(self) -> None:

def test_invalid_rule_name(self) -> None:
grammar = """
start: a b
start: _a b
_a: 'a'
b: 'b'
"""
with self.assertRaises(GrammarError):
with self.assertRaisesRegex(GrammarError, "cannot start with underscore: '_a'"):
parser_class = make_parser(grammar)

def test_invalid_variable_name(self) -> None:
Expand All @@ -555,7 +555,7 @@ def test_invalid_variable_name(self) -> None:
a: _x='a'
b: 'b'
"""
with self.assertRaises(GrammarError):
with self.assertRaisesRegex(GrammarError, "cannot start with underscore: '_x'"):
parser_class = make_parser(grammar)

def test_invalid_variable_name_in_temporal_rule(self) -> None:
Expand All @@ -564,7 +564,7 @@ def test_invalid_variable_name_in_temporal_rule(self) -> None:
a: (_x='a' | 'b') | 'c'
b: 'b'
"""
with self.assertRaises(GrammarError):
with self.assertRaisesRegex(GrammarError, "cannot start with underscore: '_x'"):
parser_class = make_parser(grammar)


Expand Down
6 changes: 3 additions & 3 deletions Tools/peg_generator/pegen/parser_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def visit_NameLeaf(self, node: NameLeaf) -> None:

def visit_NamedItem(self, node: NameLeaf) -> None:
if node.name and node.name.startswith("_"):
raise GrammarError(f"Variable name '{node.name}' collides with parser reserved names")
raise GrammarError(f"Variable names cannot start with underscore: '{node.name}'")
self.visit(node.item)


Expand Down Expand Up @@ -59,8 +59,8 @@ def __init__(self, grammar: Grammar, tokens: Dict[int, str], file: Optional[IO[T

def validate_rule_names(self):
for rule in self.rules:
if rule.startswith("__"):
raise GrammarError(f"Rule '{rule}' collides with parser reserved names")
if rule.startswith("_"):
raise GrammarError(f"Rule names cannot start with underscore: '{rule}'")

@contextlib.contextmanager
def local_variable_context(self) -> Iterator[None]:
Expand Down