diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2022-02-10 13:12:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-10 13:12:14 (GMT) |
commit | 390459de6db1e68b79c0897cc88c0d562693ec5c (patch) | |
tree | 797c720a46e689baa63cc29c59a3f41511a0fb6c /Tools | |
parent | 2cea8c29cf975a8ad7d8c3ff19d1e836c2d54707 (diff) | |
download | cpython-390459de6db1e68b79c0897cc88c0d562693ec5c.zip cpython-390459de6db1e68b79c0897cc88c0d562693ec5c.tar.gz cpython-390459de6db1e68b79c0897cc88c0d562693ec5c.tar.bz2 |
Allow the parser to avoid nested processing of invalid rules (GH-31252)
Diffstat (limited to 'Tools')
-rw-r--r-- | Tools/peg_generator/pegen/c_generator.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/Tools/peg_generator/pegen/c_generator.py b/Tools/peg_generator/pegen/c_generator.py index ee255c8..56a1e5a 100644 --- a/Tools/peg_generator/pegen/c_generator.py +++ b/Tools/peg_generator/pegen/c_generator.py @@ -122,6 +122,7 @@ class CCallMakerVisitor(GrammarVisitor): self.exact_tokens = exact_tokens self.non_exact_tokens = non_exact_tokens self.cache: Dict[Any, FunctionCall] = {} + self.cleanup_statements: List[str] = [] def keyword_helper(self, keyword: str) -> FunctionCall: return FunctionCall( @@ -364,6 +365,7 @@ class CParserGenerator(ParserGenerator, GrammarVisitor): self._varname_counter = 0 self.debug = debug self.skip_actions = skip_actions + self.cleanup_statements: List[str] = [] def add_level(self) -> None: self.print("if (p->level++ == MAXSTACK) {") @@ -376,6 +378,8 @@ class CParserGenerator(ParserGenerator, GrammarVisitor): self.print("p->level--;") def add_return(self, ret_val: str) -> None: + for stmt in self.cleanup_statements: + self.print(stmt) self.remove_level() self.print(f"return {ret_val};") @@ -547,9 +551,7 @@ class CParserGenerator(ParserGenerator, GrammarVisitor): f"_PyPegen_update_memo(p, _mark, {node.name}_type, _res)", "_res" ) self.print("p->mark = _mark;") - self.print("p->in_raw_rule++;") self.print(f"void *_raw = {node.name}_raw(p);") - self.print("p->in_raw_rule--;") self.print("if (p->error_indicator) {") with self.indent(): self.add_return("NULL") @@ -663,10 +665,21 @@ class CParserGenerator(ParserGenerator, GrammarVisitor): self._set_up_rule_memoization(node, result_type) self.print("{") + + if node.name.endswith("without_invalid"): + with self.indent(): + self.print("int _prev_call_invalid = p->call_invalid_rules;") + self.print("p->call_invalid_rules = 0;") + self.cleanup_statements.append("p->call_invalid_rules = _prev_call_invalid;") + if is_loop: self._handle_loop_rule_body(node, rhs) else: self._handle_default_rule_body(node, rhs, result_type) + + if node.name.endswith("without_invalid"): + self.cleanup_statements.pop() + self.print("}") def visit_NamedItem(self, node: NamedItem) -> None: |