def parse_to_nested_list(target):
  """Parse target to nested lists based on parens."""
  tokens = target.split(" ")
  buffer_stack = [[]]
  for token in tokens:
    if token == ")":
      buffer = buffer_stack.pop()
      if not buffer_stack:
        raise ValueError("Empty buffer stack.\ntarget: `%s`\nbuffer: `%s`" %
                         (target, buffer))
      buffer_stack[-1].append(buffer)
    buffer_stack[-1].append(token)
    if token == "(":
      buffer_stack.append([])
  if len(buffer_stack) != 1:
    raise ValueError("Bad buffer_stack %s for target %s" %
                     (buffer_stack, target))
  return buffer_stack
Parse target to nested lists based on parens.
language/compgen/csl/tasks/smcalflow/tools/generate_target_cfg.py