MCPcopy Index your code
hub / github.com/python/cpython / visit_Alt

Method visit_Alt

Tools/peg_generator/pegen/c_generator.py:835–878  ·  view source on GitHub ↗
(
        self, node: Alt, is_loop: bool, is_gather: bool, rulename: str | None
    )

Source from the content-addressed store, hash-verified

833 self.print("}")
834
835 def visit_Alt(
836 self, node: Alt, is_loop: bool, is_gather: bool, rulename: str | None
837 ) -> None:
838 if len(node.items) == 1 and str(node.items[0]).startswith("invalid_"):
839 self.print(f"if (p->call_invalid_rules) {{ // {node}")
840 else:
841 self.print(f"{{ // {node}")
842 with self.indent():
843 self._check_for_errors()
844 node_str = str(node).replace('"', '\\"')
845 self.print(
846 f'D(fprintf(stderr, "%*c> {rulename}[%d-%d]: %s\\n", p->level, \' \', _mark, p->mark, "{node_str}"));'
847 )
848 # Prepare variable declarations for the alternative
849 vars = self.collect_vars(node)
850 for v, var_type in sorted(item for item in vars.items() if item[0] is not None):
851 if not var_type:
852 var_type = "void *"
853 else:
854 var_type += " "
855 if v == "_cut_var":
856 v += " = 0" # cut_var must be initialized
857 self.print(f"{var_type}{v};")
858 if v and v.startswith("_opt_var"):
859 self.print(f"UNUSED({v}); // Silence compiler warnings")
860
861 with self.local_variable_context():
862 if is_loop:
863 self.handle_alt_loop(node, is_gather, rulename)
864 else:
865 self.handle_alt_normal(node, is_gather, rulename)
866
867 self.print("p->mark = _mark;")
868 node_str = str(node).replace('"', '\\"')
869 self.print(
870 f"D(fprintf(stderr, \"%*c%s {rulename}[%d-%d]: %s failed!\\n\", p->level, ' ',\n"
871 f' p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "{node_str}"));'
872 )
873 if "_cut_var" in vars:
874 self.print("if (_cut_var) {")
875 with self.indent():
876 self.add_return("NULL")
877 self.print("}")
878 self.print("}")
879
880 def collect_vars(self, node: Alt) -> dict[str | None, str | None]:
881 types = {}

Callers

nothing calls this directly

Calls 12

_check_for_errorsMethod · 0.95
collect_varsMethod · 0.95
handle_alt_loopMethod · 0.95
handle_alt_normalMethod · 0.95
add_returnMethod · 0.95
strFunction · 0.85
startswithMethod · 0.45
printMethod · 0.45
indentMethod · 0.45
replaceMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected