Parse the given rule into rhs -> production mappings. As a bit of a terrible design, ignore token lists will be parsed and added to RuleParser.ignore. But the rhs -> production mappings will still be created and returned.
(self, rule: str)
| 112 | return rule_grammar.parseString(rule) |
| 113 | |
| 114 | def parse(self, rule: str) -> Tuple[Token, RuleMapping]: |
| 115 | """Parse the given rule into rhs -> production mappings. |
| 116 | |
| 117 | As a bit of a terrible design, ignore token lists will be parsed and added to |
| 118 | RuleParser.ignore. But the rhs -> production mappings will still be created and returned. |
| 119 | """ |
| 120 | results = self._parse(rule) |
| 121 | |
| 122 | if "ignore" in results: |
| 123 | for tok in results["ignore"]: |
| 124 | self.ignore.add(tok) |
| 125 | return None |
| 126 | |
| 127 | if "lhs" not in results or "rhs" not in results: |
| 128 | raise ValueError("Something went horribly wrong") |
| 129 | |
| 130 | probability = results.get("probability", None) |
| 131 | left_context = results.get("left_context", None) |
| 132 | right_context = results.get("right_context", None) |
| 133 | |
| 134 | if left_context is not None: |
| 135 | left_context = Token(left_context) |
| 136 | |
| 137 | if right_context is not None: |
| 138 | right_context = Token(right_context) |
| 139 | |
| 140 | lhs = Token(results["lhs"]) |
| 141 | rhs = tuple(Token(r) for r in results["rhs"]) |
| 142 | |
| 143 | return lhs, RuleMapping(rhs, probability, left_context, right_context) |
| 144 | |
| 145 | def add_rule(self, rule: str): |
| 146 | """Add the given rule to the parser. |