class_docstr: For `Perl 6 <http://www.perl6.org>`_ source code.
  
  .. versionadded:: 2.0
code: def brackets_callback(token_class):
      def callback(lexer, match, context):
          groups = match.groupdict()
          opening_chars = groups["delimiter"]
          n_chars = len(opening_chars)
          adverbs = groups.get("adverbs")
  
          closer = Perl6Lexer.PERL6_BRACKETS.get(opening_chars[0])
          text = context.text
  
          if (
              closer is None
          ):  # it's not a mirrored character, which means we
              # just need to look for the next occurrence
  
              end_pos = text.find(
                  opening_chars, match.start("delimiter") + n_chars
              )
          else:  # we need to look for the corresponding closing character,
              # keep nesting in mind
              closing_chars = closer * n_chars
              nesting_level = 1
  
              search_pos = match.start("delimiter")
  
              while nesting_level > 0:
                  next_open_pos = text.find(
                      opening_chars, search_pos + n_chars
                  )
                  next_close_pos = text.find(
                      closing_chars, search_pos + n_chars
                  )
  
                  if next_close_pos == -1:
                      next_close_pos = len(text)
                      nesting_level = 0
                  elif (
                      next_open_pos != -1
                      and next_open_pos < next_close_pos
                  ):
                      nesting_level += 1
                      search_pos = next_open_pos
                  else:  # next_close_pos < next_open_pos
                      nesting_level -= 1
                      search_pos = next_close_pos
  
              end_pos = next_close_pos
  
          if (
              end_pos < 0
          ):  # if we didn't find a closer, just highlight the
              # rest of the text in this class
              end_pos = len(text)
  
          if adverbs is not None and re.search(r":to\b", adverbs):
              heredoc_terminator = text[
                  match.start("delimiter") + n_chars : end_pos
              ]
              end_heredoc = re.search(
                  r"^\s*" + re.escape(heredoc_terminator) + r"\s*$",
                  text[end_pos:],
                  re.MULTILINE,
              )
  
              if end_heredoc:
                  end_pos += end_heredoc.end()
              else:
                  end_pos = len(text)
  
          yield match.start(), token_class, text[
              match.start() : end_pos + n_chars
          ]
          context.pos = end_pos + n_chars
  
      return callback
  
file_path: catboost/contrib/python/Pygments/py2/pygments/lexers/perl.py
imports: import re
  from pygments.lexer import RegexLexer, ExtendedRegexLexer, include, bygroups, using, this, default, words
  from pygments.token import Text, Comment, Operator, Keyword, Name, String, Number, Punctuation
  from pygments.util import shebang_matches
label_desc: Use image node_id 2 for calling the Perl6Lexer obj's underlying member method code with example usage: obj.brackets_callback(token_class) and returns: callback