code: def _translate(pat, STAR, QUESTION_MARK):
      res = []
      add = res.append
      i, n = 0, len(pat)
      while i < n:
          c = pat[i]
          i = i + 1
          if c == "*":
              # compress consecutive `*` into one
              if (not res) or res[-1] is not STAR:
                  add(STAR)
          elif c == "?":
              add(QUESTION_MARK)
          elif c == "[":
              j = i
              if j < n and pat[j] == "!":
                  j = j + 1
              if j < n and pat[j] == "]":
                  j = j + 1
              while j < n and pat[j] != "]":
                  j = j + 1
              if j >= n:
                  add("\\[")
              else:
                  stuff = pat[i:j]
                  if "-" not in stuff:
                      stuff = stuff.replace("\\", r"\\")
                  else:
                      chunks = []
                      k = i + 2 if pat[i] == "!" else i + 1
                      while True:
                          k = pat.find("-", k, j)
                          if k < 0:
                              break
                          chunks.append(pat[i:k])
                          i = k + 1
                          k = k + 3
                      chunk = pat[i:j]
                      if chunk:
                          chunks.append(chunk)
                      else:
                          chunks[-1] += "-"
                      # Remove empty ranges -- invalid in RE.
                      for k in range(len(chunks) - 1, 0, -1):
                          if chunks[k - 1][-1] > chunks[k][0]:
                              chunks[k - 1] = (
                                  chunks[k - 1][:-1] + chunks[k][1:]
                              )
                              del chunks[k]
                      # Escape backslashes and hyphens for set difference (--).
                      # Hyphens that create ranges shouldn't be escaped.
                      stuff = "-".join(
                          s.replace("\\", r"\\").replace("-", r"\-")
                          for s in chunks
                      )
                  # Escape set operations (&&, ~~ and ||).
                  stuff = re.sub(r"([&~|])", r"\\\1", stuff)
                  i = j + 1
                  if not stuff:
                      # Empty range: never match.
                      add("(?!)")
                  elif stuff == "!":
                      # Negated empty range: match any character.
                      add(".")
                  else:
                      if stuff[0] == "!":
                          stuff = "^" + stuff[1:]
                      elif stuff[0] in ("^", "["):
                          stuff = "\\" + stuff
                      add(f"[{stuff}]")
          else:
              add(re.escape(c))
      assert i == n
      return res
  
file_path: cpython/Lib/fnmatch.py
imports: import os
  import posixpath
  import re
  import functools
label_desc: Use image node_id 6 for calling a global function with example usage: _translate(pat, STAR, QUESTION_MARK) and returns: res