code: def prepare_predicate(next, token):
      # FIXME: replace with real parser!!! refs:
      # http://javascript.crockford.com/tdop/tdop.html
      signature = []
      predicate = []
      while 1:
          try:
              token = next()
          except StopIteration:
              return
          if token[0] == "]":
              break
          if token == ("", ""):
              # ignore whitespace
              continue
          if token[0] and token[0][:1] in "'\"":
              token = "'", token[0][1:-1]
          signature.append(token[0] or "-")
          predicate.append(token[1])
      signature = "".join(signature)
      # use signature to determine predicate type
      if signature == "@-":
          # [@attribute] predicate
          key = predicate[1]
  
          def select(context, result):
              for elem in result:
                  if elem.get(key) is not None:
                      yield elem
  
          return select
      if signature == "@-='" or signature == "@-!='":
          # [@attribute='value'] or [@attribute!='value']
          key = predicate[1]
          value = predicate[-1]
  
          def select(context, result):
              for elem in result:
                  if elem.get(key) == value:
                      yield elem
  
          def select_negated(context, result):
              for elem in result:
                  if (
                      attr_value := elem.get(key)
                  ) is not None and attr_value != value:
                      yield elem
  
          return select_negated if "!=" in signature else select
      if signature == "-" and not re.match(r"\-?\d+$", predicate[0]):
          # [tag]
          tag = predicate[0]
  
          def select(context, result):
              for elem in result:
                  if elem.find(tag) is not None:
                      yield elem
  
          return select
      if (
          signature == ".='"
          or signature == ".!='"
          or (
              (signature == "-='" or signature == "-!='")
              and not re.match(r"\-?\d+$", predicate[0])
          )
      ):
          # [.='value'] or [tag='value'] or [.!='value'] or [tag!='value']
          tag = predicate[0]
          value = predicate[-1]
          if tag:
  
              def select(context, result):
                  for elem in result:
                      for e in elem.findall(tag):
                          if "".join(e.itertext()) == value:
                              yield elem
                              break
  
              def select_negated(context, result):
                  for elem in result:
                      for e in elem.iterfind(tag):
                          if "".join(e.itertext()) != value:
                              yield elem
                              break
  
          else:
  
              def select(context, result):
                  for elem in result:
                      if "".join(elem.itertext()) == value:
                          yield elem
  
              def select_negated(context, result):
                  for elem in result:
                      if "".join(elem.itertext()) != value:
                          yield elem
  
          return select_negated if "!=" in signature else select
      if signature == "-" or signature == "-()" or signature == "-()-":
          # [index] or [last()] or [last()-index]
          if signature == "-":
              # [index]
              index = int(predicate[0]) - 1
              if index < 0:
                  raise SyntaxError("XPath position >= 1 expected")
          else:
              if predicate[0] != "last":
                  raise SyntaxError("unsupported function")
              if signature == "-()-":
                  try:
                      index = int(predicate[2]) - 1
                  except ValueError:
                      raise SyntaxError("unsupported expression")
                  if index > -2:
                      raise SyntaxError(
                          "XPath offset from last() must be negative"
                      )
              else:
                  index = -1
  
          def select(context, result):
              parent_map = get_parent_map(context)
              for elem in result:
                  try:
                      parent = parent_map[elem]
                      # FIXME: what if the selector is "*" ?
                      elems = list(parent.findall(elem.tag))
                      if elems[index] is elem:
                          yield elem
                  except (IndexError, KeyError):
                      pass
  
          return select
      raise SyntaxError("invalid predicate")
  
file_path: catboost/contrib/tools/python3/src/Lib/xml/etree/ElementPath.py
imports: import re
label_desc: Use image node_id 10 for calling a global function with example usage: prepare_predicate(next, token) and returns: select, unknown, select, unknown, select