code: def parse_regex(regex_tokens: list[str]) -> Node:
      """
      Takes a list of tokens from the tokenizer, and returns a parse tree.
      """
      # We add a closing brace because that represents the final pop of the stack.
      tokens: list[str] = [")"] + regex_tokens[::-1]
  
      def wrap(lst: list[Node]) -> Node:
          """Turn list into sequence when it contains several items."""
          if len(lst) == 1:
              return lst[0]
          else:
              return NodeSequence(lst)
  
      def _parse() -> Node:
          or_list: list[list[Node]] = []
          result: list[Node] = []
  
          def wrapped_result() -> Node:
              if or_list == []:
                  return wrap(result)
              else:
                  or_list.append(result)
                  return AnyNode([wrap(i) for i in or_list])
  
          while tokens:
              t = tokens.pop()
  
              if t.startswith("(?P<"):
                  variable = Variable(_parse(), varname=t[4:-1])
                  result.append(variable)
  
              elif t in ("*", "*?"):
                  greedy = t == "*"
                  result[-1] = Repeat(result[-1], greedy=greedy)
  
              elif t in ("+", "+?"):
                  greedy = t == "+"
                  result[-1] = Repeat(
                      result[-1], min_repeat=1, greedy=greedy
                  )
  
              elif t in ("?", "??"):
                  if result == []:
                      raise Exception(
                          "Nothing to repeat." + repr(tokens)
                      )
                  else:
                      greedy = t == "?"
                      result[-1] = Repeat(
                          result[-1],
                          min_repeat=0,
                          max_repeat=1,
                          greedy=greedy,
                      )
  
              elif t == "|":
                  or_list.append(result)
                  result = []
  
              elif t in ("(", "(?:"):
                  result.append(_parse())
  
              elif t == "(?!":
                  result.append(Lookahead(_parse(), negative=True))
  
              elif t == "(?=":
                  result.append(Lookahead(_parse(), negative=False))
  
              elif t == ")":
                  return wrapped_result()
  
              elif t.startswith("#"):
                  pass
  
              elif t.startswith("{"):
                  # TODO: implement!
                  raise Exception(
                      f"{t}-style repetition not yet supported"
                  )
  
              elif t.startswith("(?"):
                  raise Exception("%r not supported" % t)
  
              elif t.isspace():
                  pass
              else:
                  result.append(Regex(t))
  
          raise Exception("Expecting ')' token")
  
      result = _parse()
  
      if len(tokens) != 0:
          raise Exception("Unmatched parentheses.")
      else:
          return result
  
imports: from __future__ import annotations
  import re
label_desc: Use image node_id 2 for calling a global function with example usage: parse_regex(regex_tokens) and returns: result, lst, NodeSequence, wrap, AnyNode, wrapped_result