[docs]classMacroRules(TupleNewType[MacroRule],Macro):r"""A sequence of ``MacroRule``\ s. :type args: MacroRule """def__call__(self,tokens:Sequence[Token])->Sequence[Token]|None:"""Transform a token sequence."""forruleinself:ifmatch:=rule.matcher.full_match(tokens):returntuple(rule.transcriber.transcribe(match.captures))returnNone
[docs]@dataclass(frozen=True,slots=True)classMacroRulesParserMacro(PartialMatchMacro):"""A macro that parses ``macro_rules`` macro definitions. Parsed macros are added to :attr:`macros`. """macros:dict[str,Macro]=field(default_factory=dict)"""Parsed ``macro_rules`` macros will be added to this dict. It may be shared with other macros, such as a :class:`~macro_polo.macros.function.FunctionMacroInvokerMacro`. """_macro_rules_declaration_matcher=parse_macro_matcher('macro_rules! $name:name: $> $($rules:tt)+ $<')_macro_rules_rules_matcher=parse_macro_matcher('$('' [$($matcher:tt)*]: $['' ($> $($transcriber:tt)* $<)'' |($($[!$^] $transcriber:tt)* $($^)?)'' ]'')+')_raw_rules_transcriber=parse_macro_transcriber('$($rules)*')_raw_matcher_transcriber=parse_macro_transcriber('$($matcher)*')_raw_transcriber_transcriber=parse_macro_transcriber('$($transcriber)*')def__call__(self,tokens:Sequence[Token])->tuple[Sequence[Token],int]:"""Transform the beginning of a token sequence."""matchself._macro_rules_declaration_matcher.match(tokens):caseMacroMatch(size=match_size,captures={'name':Token(string=name)}ascaptures,):rules_tokens=tuple(self._raw_rules_transcriber.transcribe(captures))ifnameinself.macros:raiseMacroError(f'redeclaration of macro {name}')matchself._macro_rules_rules_matcher.full_match(rules_tokens):caseMacroMatch(captures={'matcher':list(matcher_captures),'transcriber':list(transcriber_captures),}):passcase_:raiseMacroError(f'syntax error in macro_rules declaration for {name!r}')matchers=(parse_macro_matcher(self._raw_matcher_transcriber.transcribe({'matcher':matcher}))formatcherinmatcher_captures)transcribers=(parse_macro_transcriber(self._raw_transcriber_transcriber.transcribe({'transcriber':transcriber}))fortranscriberintranscriber_captures)self.macros[name]=MacroRules(*(MacroRule(matcher,transcriber,)formatcher,transcriberinzip(matchers,transcribers)))return(),match_sizereturn(),0