def METHOD_NAME(
    self,
    grammar: Grammar,
    repository: FChainMap[str, _Rule],
    s: str,
) -> tuple[list[str], tuple[_Rule, ...]]:
    if s == "$self":
        return self._patterns(grammar, grammar.patterns)
    elif s == "$base":
        grammar = self._grammars.grammar_for_scope(self._root_scope)
        return self.METHOD_NAME(grammar, grammar.repository, "$self")
    elif s.startswith("#"):
        return self._patterns(grammar, (repository[s[1:]],))
    elif "#" not in s:
        grammar = self._grammars.grammar_for_scope(s)
        return self.METHOD_NAME(grammar, grammar.repository, "$self")
    else:
        scope, _, s = s.partition("#")
        grammar = self._grammars.grammar_for_scope(scope)
        return self.METHOD_NAME(grammar, grammar.repository, f"#{s}")