| 1 |
from typing import Any |
| 2 |
|
| 3 |
from pegen import grammar |
| 4 |
from pegen.grammar import Alt, GrammarVisitor, Rhs, Rule |
| 5 |
|
| 6 |
|
| 7 |
class ValidationError(Exception): |
| 8 |
pass |
| 9 |
|
| 10 |
|
| 11 |
class GrammarValidator(GrammarVisitor): |
| 12 |
def __init__(self, grammar: grammar.Grammar) -> None: |
| 13 |
self.grammar = grammar |
| 14 |
self.rulename: str | None = None |
| 15 |
|
| 16 |
def validate_rule(self, rulename: str, node: Rule) -> None: |
| 17 |
self.rulename = rulename |
| 18 |
self.visit(node) |
| 19 |
self.rulename = None |
| 20 |
|
| 21 |
|
| 22 |
class SubRuleValidator(GrammarValidator): |
| 23 |
def visit_Rhs(self, node: Rhs) -> None: |
| 24 |
for index, alt in enumerate(node.alts): |
| 25 |
alts_to_consider = node.alts[index + 1 :] |
| 26 |
for other_alt in alts_to_consider: |
| 27 |
self.check_intersection(alt, other_alt) |
| 28 |
|
| 29 |
def check_intersection(self, first_alt: Alt, second_alt: Alt) -> None: |
| 30 |
if str(second_alt).startswith(str(first_alt)): |
| 31 |
raise ValidationError( |
| 32 |
f"In {self.rulename} there is an alternative that will " |
| 33 |
f"never be visited:\n{second_alt}" |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
class RaiseRuleValidator(GrammarValidator): |
| 38 |
def visit_Alt(self, node: Alt) -> None: |
| 39 |
if self.rulename and self.rulename.startswith('invalid'): |
| 40 |
# raising is allowed in invalid rules |
| 41 |
return |
| 42 |
if node.action and 'RAISE_SYNTAX_ERROR' in node.action: |
| 43 |
raise ValidationError( |
| 44 |
f"In {self.rulename!r} there is an alternative that contains " |
| 45 |
f"RAISE_SYNTAX_ERROR; this is only allowed in invalid_ rules" |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
class CutValidator(GrammarValidator): |
| 50 |
"""Fail if Cut is not directly in a rule. |
| 51 |
|
| 52 |
For simplicity, we currently document that a Cut affects alternatives |
| 53 |
of the *rule* it is in. |
| 54 |
However, the implementation makes cuts local to enclosing Rhs |
| 55 |
(e.g. parenthesized list of choices). |
| 56 |
Additionally, in academic papers about PEG, repeats and optional items |
| 57 |
are "desugared" to choices with an empty alternative, and thus contain |
| 58 |
a Cut's effect. |
| 59 |
|
| 60 |
Please update documentation and tests when adding this cut, |
| 61 |
then get rid of this validator. |
| 62 |
|
| 63 |
See gh-143054. |
| 64 |
""" |
| 65 |
|
| 66 |
def visit(self, node: Any, parents: tuple[Any, ...] = ()) -> None: |
| 67 |
super().visit(node, parents=(*parents, node)) |
| 68 |
|
| 69 |
def visit_Cut(self, node: Alt, parents: tuple[Any, ...] = ()) -> None: |
| 70 |
parent_types = [type(p).__name__ for p in parents] |
| 71 |
if parent_types != ['Rule', 'Rhs', 'Alt', 'NamedItem', 'Cut']: |
| 72 |
raise ValidationError( |
| 73 |
f"Rule {self.rulename!r} contains cut that's not on the " |
| 74 |
"top level. " |
| 75 |
"The intended semantics of such cases need " |
| 76 |
"to be clarified; see the CutValidator docstring." |
| 77 |
f"\nThe cut is inside: {parent_types}" |
| 78 |
) |
| 79 |
|
| 80 |
def validate_grammar(the_grammar: grammar.Grammar) -> None: |
| 81 |
for validator_cls in GrammarValidator.__subclasses__(): |
| 82 |
validator = validator_cls(the_grammar) |
| 83 |
for rule_name, rule in the_grammar.rules.items(): |
| 84 |
validator.validate_rule(rule_name, rule) |