parameters: (file_lines)
return_statement: return num_namespaces_deleted
docstring: Delete namespaces with nothing in them.
  
    Empty namespaces could be caused by transformations that removed
    forward-declarations:
          namespace foo {
          class Myclass;
          }
       ->
          namespace foo {
          }
    We want to get rid of the 'empty' namespace in this case.
  
    This routine 'deletes' lines by setting their 'deleted' field to True.
  
    Arguments:
      file_lines: an array of LineInfo objects with .type fields filled in.
  
    Returns:
      The number of namespaces deleted.
docstring_summary: Delete namespaces with nothing in them.
docstring_tokens: Delete namespaces with nothing in them .
function: def _DeleteEmptyNamespaces(file_lines):
    """Delete namespaces with nothing in them.
  
    Empty namespaces could be caused by transformations that removed
    forward-declarations:
          namespace foo {
          class Myclass;
          }
       ->
          namespace foo {
          }
    We want to get rid of the 'empty' namespace in this case.
  
    This routine 'deletes' lines by setting their 'deleted' field to True.
  
    Arguments:
      file_lines: an array of LineInfo objects with .type fields filled in.
  
    Returns:
      The number of namespaces deleted.
    """
    num_namespaces_deleted = 0
    start_line = 0
    while start_line < len(file_lines):
      line_info = file_lines[start_line]
      if (line_info.deleted or
          (line_info.type != _NAMESPACE_START_RE and
           line_info.type != _NAMESPACE_START_ALLMAN_RE and
           line_info.type != _NAMESPACE_START_MIXED_RE)):
        start_line += 1
        continue
      if line_info.type in (_NAMESPACE_START_RE, _NAMESPACE_START_MIXED_RE):
        # Because multiple namespaces can be on one line
        # ("namespace foo { namespace bar { ..."), we need to count.
        # We use the max because line may have 0 '{'s if it's a macro.
        # TODO(csilvers): ignore { in comments.
        namespace_depth = max(line_info.line.count('{'), 1)
      elif line_info.type == _NAMESPACE_START_ALLMAN_RE:
        # For Allman namespaces, keep the start line and increment
        # the namespace depths when the actual brace is encountered.
        namespace_depth = 0
      else:
        # We should have handled all the namespace styles above!
        assert False, ('unknown namespace type',
                       _LINE_TYPES.index(line_info.type))
      end_line = start_line + 1
      while end_line < len(file_lines):
        line_info = file_lines[end_line]
        if line_info.deleted:
          end_line += 1
        elif line_info.type in (_COMMENT_LINE_RE, _BLANK_LINE_RE):
          end_line += 1                # ignore blank lines
        elif line_info.type == _NAMESPACE_CONTINUE_ALLMAN_MIXED_RE:
          namespace_depth += 1
          end_line += 1
        elif line_info.type in (_NAMESPACE_START_RE, _NAMESPACE_START_MIXED_RE):
          # nested namespace
          namespace_depth += max(line_info.line.count('{'), 1)
          end_line += 1
        elif line_info.type == _NAMESPACE_START_ALLMAN_RE:
          # nested Allman namespace
          end_line += 1
        elif line_info.type == _NAMESPACE_END_RE:
          namespace_depth -= max(line_info.line.count('}'), 1)
          end_line += 1
          if namespace_depth <= 0:
            # Delete any comments preceding this namespace as well.
            start_line = _LineNumberStartingPrecedingComments(file_lines,
                                                              start_line)
            # And also blank lines.
            while (start_line > 0 and
                   file_lines[start_line-1].type == _BLANK_LINE_RE):
              start_line -= 1
            for line_number in range(start_line, end_line):
              file_lines[line_number].deleted = True
            num_namespaces_deleted += 1
            break
        else:   # bail: we're at a line indicating this isn't an empty namespace
          end_line = start_line + 1  # rewind to try again with nested namespaces
          break
      start_line = end_line
  
    return num_namespaces_deleted
function_tokens: def _DeleteEmptyNamespaces ( file_lines ) : num_namespaces_deleted = 0 start_line = 0 while start_line < len ( file_lines ) : line_info = file_lines [ start_line ] if ( line_info . deleted or ( line_info . type != _NAMESPACE_START_RE and line_info . type != _NAMESPACE_START_ALLMAN_RE and line_info . type != _NAMESPACE_START_MIXED_RE ) ) : start_line += 1 continue if line_info . type in ( _NAMESPACE_START_RE , _NAMESPACE_START_MIXED_RE ) : # Because multiple namespaces can be on one line # ("namespace foo { namespace bar { ..."), we need to count. # We use the max because line may have 0 '{'s if it's a macro. # TODO(csilvers): ignore { in comments. namespace_depth = max ( line_info . line . count ( '{' ) , 1 ) elif line_info . type == _NAMESPACE_START_ALLMAN_RE : # For Allman namespaces, keep the start line and increment # the namespace depths when the actual brace is encountered. namespace_depth = 0 else : # We should have handled all the namespace styles above! assert False , ( 'unknown namespace type' , _LINE_TYPES . index ( line_info . type ) ) end_line = start_line + 1 while end_line < len ( file_lines ) : line_info = file_lines [ end_line ] if line_info . deleted : end_line += 1 elif line_info . type in ( _COMMENT_LINE_RE , _BLANK_LINE_RE ) : end_line += 1 # ignore blank lines elif line_info . type == _NAMESPACE_CONTINUE_ALLMAN_MIXED_RE : namespace_depth += 1 end_line += 1 elif line_info . type in ( _NAMESPACE_START_RE , _NAMESPACE_START_MIXED_RE ) : # nested namespace namespace_depth += max ( line_info . line . count ( '{' ) , 1 ) end_line += 1 elif line_info . type == _NAMESPACE_START_ALLMAN_RE : # nested Allman namespace end_line += 1 elif line_info . type == _NAMESPACE_END_RE : namespace_depth -= max ( line_info . line . count ( '}' ) , 1 ) end_line += 1 if namespace_depth <= 0 : # Delete any comments preceding this namespace as well. start_line = _LineNumberStartingPrecedingComments ( file_lines , start_line ) # And also blank lines. while ( start_line > 0 and file_lines [ start_line - 1 ] . type == _BLANK_LINE_RE ) : start_line -= 1 for line_number in range ( start_line , end_line ) : file_lines [ line_number ] . deleted = True num_namespaces_deleted += 1 break else : # bail: we're at a line indicating this isn't an empty namespace end_line = start_line + 1 # rewind to try again with nested namespaces break start_line = end_line return num_namespaces_deleted