code: def textproto_split(input_lines, json_encoder):
      """When given e.g. the output of "bazel aquery --output=textproto",
      yields each top-level item as a string formatted as JSON (if an encoder is
      given) or Python AST.
      The input MUST be formatted neatly line-by-line, as follows:
      actions {
          mnemonic: "Genrule"
          environment_variables {
              key: "CC"
              value: "clang"
          }
          ...
      }
      targets {
          id: "0"
          label: "//:target"
          rule_class_id: "0"
      }
      """
      outputs = []
      re_flags = re.M
      pat_open = re.compile(
          b"^(\\s*)([-\\w:]+)(\\s*){$", flags=re_flags
      )
      pat_line = re.compile(b"^(\\s*)([-\\w]+): (.*)$", flags=re_flags)
      pat_close = re.compile(b"}$", flags=re_flags)
      prev_comma = False
      prev_tail = b""
      for full_line in input_lines:
          pieces = re.split(b"(\\r|\\n)", full_line, 1)
          pieces[1:] = [b"".join(pieces[1:])]
          [line, tail] = pieces
          next_line = pat_open.sub(b'\\1["\\2",\\3[', line)
          outputs.append(
              b""
              if not prev_comma
              else b"]"
              if next_line.endswith(b"}")
              else b","
          )
          next_line = pat_close.sub(b"]", next_line)
          next_line = pat_line.sub(
              lambda m: textproto_format(
                  *(m.groups() + (json_encoder,))
              ),
              next_line,
          )
          outputs.append(prev_tail + next_line)
          if line == b"}":
              yield b"".join(outputs)
              del outputs[:]
          prev_comma = line != b"}" and (
              next_line.endswith(b"]") or next_line.endswith(b'"')
          )
          prev_tail = tail
      if len(outputs) > 0:
          yield b"".join(outputs)
          del outputs[:]
  
imports: import ast
  import errno
  import json
  import os
  import re
  import subprocess
  import stat
  import sys
  from collections import defaultdict, OrderedDict
label_desc: Use image node_id 2 for calling a global function with example usage: textproto_split(input_lines, json_encoder) without return types