code: def parse_new_descriptor(tokens, pos, prev_names):
      size = len(tokens)
      first_token = tokens[pos]
      pos += 1
      d = Descriptor()
  
      # when reading this function, be careful to note the indent level,
      # there is an if-statement within an if-statement.
      if first_token in [
          "Offset",
          "Round",
          "ReplaceIndex",
          "Append",
          "Sum",
          "Switch",
          "Failover",
          "IfDefined",
      ]:
          expect_token("(", tokens[pos], first_token + "()")
          pos += 1
          d.operator = first_token
          # the 1st argument of all these operators is a Descriptor.
          (desc, pos) = parse_new_descriptor(tokens, pos, prev_names)
          d.items = [desc]
  
          if first_token == "Offset":
              expect_token(",", tokens[pos], "Offset()")
              pos += 1
              try:
                  t_offset = int(tokens[pos])
                  pos += 1
                  d.items.append(t_offset)
              except:
                  raise RuntimeError(
                      "Parsing Offset(), expected integer, got "
                      + tokens[pos]
                  )
              if tokens[pos] == ")":
                  return (d, pos + 1)
              elif tokens[pos] != ",":
                  raise RuntimeError(
                      "Parsing Offset(), expected ')' or ',', got "
                      + tokens[pos]
                  )
              pos += 1
              try:
                  x_offset = int(tokens[pos])
                  pos += 1
                  d.items.append(x_offset)
              except:
                  raise RuntimeError(
                      "Parsing Offset(), expected integer, got "
                      + tokens[pos]
                  )
              expect_token(")", tokens[pos], "Offset()")
              pos += 1
          elif first_token in [
              "Append",
              "Sum",
              "Switch",
              "Failover",
              "IfDefined",
          ]:
              while True:
                  if tokens[pos] == ")":
                      # check num-items is correct for some special cases.
                      if (
                          first_token == "Failover"
                          and len(d.items) != 2
                      ):
                          raise RuntimeError(
                              "Parsing Failover(), expected 2 items but got {0}".format(
                                  len(d.items)
                              )
                          )
                      if (
                          first_token == "IfDefined"
                          and len(d.items) != 1
                      ):
                          raise RuntimeError(
                              "Parsing IfDefined(), expected 1 item but got {0}".format(
                                  len(d.items)
                              )
                          )
                      pos += 1
                      break
                  elif tokens[pos] == ",":
                      pos += 1  # consume the comma.
                  else:
                      raise RuntimeError(
                          "Parsing Append(), expected ')' or ',', got "
                          + tokens[pos]
                      )
  
                  (desc, pos) = parse_new_descriptor(
                      tokens, pos, prev_names
                  )
                  d.items.append(desc)
          elif first_token == "Round":
              expect_token(",", tokens[pos], "Round()")
              pos += 1
              try:
                  t_modulus = int(tokens[pos])
                  assert t_modulus > 0
                  pos += 1
                  d.items.append(t_modulus)
              except:
                  raise RuntimeError(
                      "Parsing Offset(), expected integer, got "
                      + tokens[pos]
                  )
              expect_token(")", tokens[pos], "Round()")
              pos += 1
          elif first_token == "ReplaceIndex":
              expect_token(",", tokens[pos], "ReplaceIndex()")
              pos += 1
              if tokens[pos] in ["x", "t"]:
                  d.items.append(tokens[pos])
                  pos += 1
              else:
                  raise RuntimeError(
                      "Parsing ReplaceIndex(), expected 'x' or 't', got "
                      + tokens[pos]
                  )
              expect_token(",", tokens[pos], "ReplaceIndex()")
              pos += 1
              try:
                  new_value = int(tokens[pos])
                  pos += 1
                  d.items.append(new_value)
              except:
                  raise RuntimeError(
                      "Parsing Offset(), expected integer, got "
                      + tokens[pos]
                  )
              expect_token(")", tokens[pos], "ReplaceIndex()")
              pos += 1
          else:
              raise RuntimeError("code error")
      elif first_token in ["Scale", "Const"]:
          # Parsing something like 'Scale(2.0, lstm1)' or 'Const(1.0, 512)'
          expect_token("(", tokens[pos], first_token + "()")
          pos += 1
          d.operator = first_token
          # First arg of Scale() and Const() is a float: the scale or value,
          # respectively.
          try:
              value = float(tokens[pos])
              pos += 1
              d.items = [value]
          except:
              raise RuntimeError(
                  "Parsing {0}, expected float, got {1}".format(
                      first_token, tokens[pos]
                  )
              )
          # Consume the comma.
          expect_token(",", tokens[pos], first_token + "()")
          pos += 1
          if first_token == "Scale":
              # Second arg of Scale() is a Descriptor.
              (desc, pos) = parse_new_descriptor(
                  tokens, pos, prev_names
              )
              d.items.append(desc)
          else:
              assert first_token == "Const"
              try:
                  dim = int(tokens[pos])
                  pos += 1
                  d.items.append(dim)
              except:
                  raise RuntimeError(
                      "Parsing Const() expression, expected int, got {0}".format(
                          tokens[pos]
                      )
                  )
          expect_token(")", tokens[pos], first_token)
          pos += 1
      elif first_token in ["end of string", "(", ")", ",", "@"]:
          raise RuntimeError("Expected descriptor, got " + first_token)
      elif is_valid_line_name(first_token) or first_token == "[":
          # This section parses a raw input/layer/output name, e.g. "affine2"
          # (which must start with an alphabetic character or underscore),
          # optionally followed by an offset like '@-3'.
  
          d.operator = None
          d.items = [first_token]
  
          # If the layer-name o is followed by '@', then
          # we're parsing something like 'affine1@-3' which
          # is syntactic sugar for 'Offset(affine1, 3)'.
          if tokens[pos] == "@":
              pos += 1
              try:
                  offset_t = int(tokens[pos])
                  pos += 1
              except:
                  raise RuntimeError(
                      "Parse error parsing {0}@{1}".format(
                          first_token, tokens[pos]
                      )
                  )
              if offset_t != 0:
                  inner_d = d
                  d = Descriptor()
                  # e.g. foo@3 is equivalent to 'Offset(foo, 3)'.
                  d.operator = "Offset"
                  d.items = [inner_d, offset_t]
      else:
          # the last possible case is that 'first_token' is just an integer i,
          # which can appear in things like Append(-3, 0, 3).
          # See if the token is an integer.
          # In this case, it's interpreted as the name of previous layer
          # (with that time offset applied).
          try:
              offset_t = int(first_token)
          except:
              raise RuntimeError(
                  "Parsing descriptor, expected descriptor but got "
                  + first_token
              )
          assert isinstance(prev_names, list)
          if len(prev_names) < 1:
              raise RuntimeError(
                  "Parsing descriptor, could not interpret '{0}' because "
                  "there is no previous layer".format(first_token)
              )
          d.operator = None
          # the layer name is the name of the most recent layer.
          d.items = [prev_names[-1]]
          if offset_t != 0:
              inner_d = d
              d = Descriptor()
              d.operator = "Offset"
              d.items = [inner_d, offset_t]
      return (d, pos)
  
imports: from __future__ import print_function
  from __future__ import division
  import re
  import sys
label_desc: Use image node_id 8 for calling a global function with example usage: parse_new_descriptor(tokens, pos, prev_names) and returns: d,  pos, d,  unknown