class_docstr: A data structure to represent a parse tree
class_docstr_tok: ["A","data","structure","to","represent","a","parse","tree"]
docstr: Turn the tree into a string representing the tree
  
  Note that this is not a recursive traversal
  Otherwise, a tree too deep might blow up the call stack
  
  There is a type specific format:
    O       -> one line PTB format, which is the default anyway
    L       -> open and close brackets are labeled, spaces in the tokens are replaced with _
    P       -> pretty print over multiple lines
    V       -> surround lines with <s>...</s>, don't print ROOT, and turn () into L/RBKT
    ?       -> spaces in the tokens are replaced with ? for any value of ? other than OLP
               warning: this may be removed in the future
    ?{OLPV} -> specific format AND a custom space replacement
    Vi      -> add an ID to the <s> in the V format.  Also works with ?Vi
docstr_tok: ["Turn","the","tree","into","a","string","representing","the","tree","Note","that","this","is","not","a","recursive","traversal","Otherwise",",","a","tree","too","deep","might","blow","up","the","call","stack","There","is","a","type","specific","format",":","O","-",">","one","line","PTB","format",",","which","is","the","default","anyway","L","-",">","open","and","close","brackets","are","labeled",",","spaces","in","the","tokens","are","replaced","with","_","P","-",">","pretty","print","over","multiple","lines","V","-",">","surround","lines","with","<","s",">","...","<","\/s",">",",","do","n't","print","ROOT",",","and","turn","(",")","into","L\/RBKT","?","-",">","spaces","in","the","tokens","are","replaced","with","?","for","any","value","of","?","other","than","OLP","warning",":","this","may","be","removed","in","the","future","?","{","OLPV","}","-",">","specific","format","AND","a","custom","space","replacement","Vi","-",">","add","an","ID","to","the","<","s",">","in","the","V","format",".","Also","works","with","?","Vi"]
code: def __format__(self, spec):
      """
      Turn the tree into a string representing the tree
  
      Note that this is not a recursive traversal
      Otherwise, a tree too deep might blow up the call stack
  
      There is a type specific format:
        O       -> one line PTB format, which is the default anyway
        L       -> open and close brackets are labeled, spaces in the tokens are replaced with _
        P       -> pretty print over multiple lines
        V       -> surround lines with <s>...</s>, don't print ROOT, and turn () into L/RBKT
        ?       -> spaces in the tokens are replaced with ? for any value of ? other than OLP
                   warning: this may be removed in the future
        ?{OLPV} -> specific format AND a custom space replacement
        Vi      -> add an ID to the <s> in the V format.  Also works with ?Vi
      """
      space_replacement = " "
      print_format = TreePrintMethod.ONE_LINE
      if spec == "L":
          print_format = TreePrintMethod.LABELED_PARENS
          space_replacement = "_"
      elif spec and spec[-1] == "L":
          print_format = TreePrintMethod.LABELED_PARENS
          space_replacement = spec[0]
      elif spec == "O":
          print_format = TreePrintMethod.ONE_LINE
      elif spec and spec[-1] == "O":
          print_format = TreePrintMethod.ONE_LINE
          space_replacement = spec[0]
      elif spec == "P":
          print_format = TreePrintMethod.PRETTY
      elif spec and spec[-1] == "P":
          print_format = TreePrintMethod.PRETTY
          space_replacement = spec[0]
      elif spec and spec[0] == "V":
          print_format = TreePrintMethod.VLSP
          use_tree_id = spec[-1] == "i"
      elif spec and len(spec) > 1 and spec[1] == "V":
          print_format = TreePrintMethod.VLSP
          space_replacement = spec[0]
          use_tree_id = spec[-1] == "i"
      elif spec:
          space_replacement = spec[0]
          warnings.warn(
              "Use of a custom replacement without a format specifier is deprecated.  Please use {}O instead".format(
                  space_replacement
              ),
              stacklevel=2,
          )
  
      LRB = "LBKT" if print_format == TreePrintMethod.VLSP else "-LRB-"
      RRB = "RBKT" if print_format == TreePrintMethod.VLSP else "-RRB-"
  
      def normalize(text):
          return (
              text.replace(" ", space_replacement)
              .replace("(", LRB)
              .replace(")", RRB)
          )
  
      if print_format is TreePrintMethod.PRETTY:
          return self.pretty_print(normalize)
  
      with StringIO() as buf:
          stack = deque()
          if print_format == TreePrintMethod.VLSP:
              if use_tree_id:
                  buf.write("<s id={}>\n".format(self.tree_id))
              else:
                  buf.write("<s>\n")
              if len(self.children) == 0:
                  raise ValueError(
                      "Cannot print an empty tree with V format"
                  )
              elif len(self.children) > 1:
                  raise ValueError(
                      "Cannot print a tree with %d branches with V format"
                      % len(self.children)
                  )
              stack.append(self.children[0])
          else:
              stack.append(self)
          while len(stack) > 0:
              node = stack.pop()
  
              if isinstance(node, str):
                  buf.write(node)
                  continue
              if len(node.children) == 0:
                  if node.label is not None:
                      buf.write(normalize(node.label))
                  continue
  
              if (
                  print_format is TreePrintMethod.ONE_LINE
                  or print_format is TreePrintMethod.VLSP
              ):
                  buf.write(OPEN_PAREN)
                  if node.label is not None:
                      buf.write(normalize(node.label))
                  stack.append(CLOSE_PAREN)
              elif print_format is TreePrintMethod.LABELED_PARENS:
                  buf.write(
                      "%s_%s" % (OPEN_PAREN, normalize(node.label))
                  )
                  stack.append(
                      CLOSE_PAREN + "_" + normalize(node.label)
                  )
                  stack.append(SPACE_SEPARATOR)
  
              for child in reversed(node.children):
                  stack.append(child)
                  stack.append(SPACE_SEPARATOR)
          if print_format == TreePrintMethod.VLSP:
              buf.write("\n</s>")
          buf.seek(0)
          return buf.read()
  
imports: from collections import deque, Counter
  from enum import Enum
  from io import StringIO
  import itertools
  import re
  import warnings
  from stanza.models.common.stanza_object import StanzaObject
label_desc: Use image node_id 9 for calling the Tree obj's underlying member method code with example usage: obj.__format__(spec) and returns: text, self, buf