Allow the parser to avoid nested processing of invalid rules (GH-31252)

This commit is contained in:
Pablo Galindo Salgado 2022-02-10 13:12:14 +00:00 committed by GitHub
parent 2cea8c29cf
commit 390459de6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1998 additions and 1997 deletions

View file

@ -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: