def name_gather(self, node: Gather) -> str: self.counter += 1 name = f"_gather_{self.counter}" self.counter += 1 extra_function_name = f"_loop0_{self.counter}" extra_function_alt = Alt( [ NamedItem(None, node.separator), NamedItem("elem", node.node), ], action="elem", ) self.todo[extra_function_name] = Rule( extra_function_name, None, Rhs([extra_function_alt]), ) alt = Alt([ NamedItem("elem", node.node), NamedItem("seq", NameLeaf(extra_function_name)), ], ) self.todo[name] = Rule( name, None, Rhs([alt]), ) return name
def name_loop(self, node: Plain, is_repeat1: bool) -> str: self.counter += 1 if is_repeat1: prefix = '_loop1_' else: prefix = '_loop0_' name = f'{prefix}{self.counter}' # TODO: It's ugly to signal via the name. self.todo[name] = Rule(name, None, Rhs([Alt([NamedItem(None, node)])])) return name
def artificial_rule_from_repeat(self, node: Plain, is_repeat1: bool) -> str: self.counter += 1 if is_repeat1: prefix = "_loop1_" else: prefix = "_loop0_" name = f"{prefix}{self.counter}" self.all_rules[name] = Rule(name, None, Rhs([Alt([NamedItem(None, node)])])) return name
def __init__( self, grammar: grammar.Grammar, file: Optional[IO[Text]], *, tokens: Dict[int, str] = token.tok_name, skip_actions: bool = False, ): keywords = grammar.metas.get("keywords") self.use_reserved_words = self.parse_bool(keywords, "keywords", True) if skip_actions and ("start" not in grammar.rules and "trailer" not in grammar.metas): first_rule = next(iter(grammar.rules)) grammar.rules["start"] = Rule( "start", None, Rhs([Alt([NamedItem(None, NameLeaf(first_rule))])])) super().__init__(grammar, tokens, file) self.skip_actions = skip_actions self.callmakervisitor: PythonCallMakerVisitor = PythonCallMakerVisitor( self)
cut = False if ((literal := self.expect("|")) and (alts := self.alts()) and (newline_ := self.expect('NEWLINE'))): return Rhs(alts.alts) self.reset(mark) if cut: return None return None @memoize def alt(self) -> Optional[Alt]: # alt: items '$' action | items '$' | items action | items mark = self.mark() cut = False if ((items := self.items()) and (literal := self.expect('$')) and (action := self.action())): return Alt(items + [NamedItem(None, NameLeaf('ENDMARKER'))], action=action) self.reset(mark) if cut: return None cut = False if ((items := self.items()) and (literal := self.expect('$'))): return Alt(items + [NamedItem(None, NameLeaf('ENDMARKER'))], action=None) self.reset(mark) if cut: return None cut = False if ((items := self.items()) and (action := self.action())): return Alt(items, action=action) self.reset(mark) if cut: return None cut = False if ((items := self.items())):