Modules | Files | Inheritance Tree | Inheritance Graph | Name Index | Config
File: Synopsis/Linker/EmptyNS.py
    1| # $Id: EmptyNS.py,v 1.1 2002/08/23 04:37:26 chalky Exp $
    2| #
    3| # This file is a part of Synopsis.
    4| # Copyright (C) 2000, 2001 Stefan Seefeld
    5| # Copyright (C) 2000, 2001 Stephen Davies
    6| #
    7| # Synopsis is free software; you can redistribute it and/or modify it
    8| # under the terms of the GNU General Public License as published by
    9| # the Free Software Foundation; either version 2 of the License, or
   10| # (at your option) any later version.
   11| #
   12| # This program is distributed in the hope that it will be useful,
   13| # but WITHOUT ANY WARRANTY; without even the implied warranty of
   14| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15| # General Public License for more details.
   16| #
   17| # You should have received a copy of the GNU General Public License
   18| # along with this program; if not, write to the Free Software
   19| # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   20| # 02111-1307, USA.
   21| #
   22| # $Log: EmptyNS.py,v $
   23| # Revision 1.1  2002/08/23 04:37:26  chalky
   24| # Huge refactoring of Linker to make it modular, and use a config system similar
   25| # to the HTML package
   26| #
   27| 
   28| import string
   29| 
   30| from Synopsis.Core import AST, Type, Util
   31| 
   32| from Linker import config, Operation
   33| 
   34| class EmptyNS (Operation, AST.Visitor):
   35|     """A class that removes empty namespaces"""
   36|     def __init__(self):
   37|         """Overrides the default processAll() to setup the stack"""
   38|         self.__scopestack = []
   39|         self.__currscope = []
   40|     def execute(self, ast):
   41|         declarations = ast.declarations()
   42|         for decl in declarations:
   43|             decl.accept(self)
   44|         declarations[:] = self.__currscope
   45|     def push(self):
   46|         """Pushes the current scope onto the stack and starts a new one"""
   47|         self.__scopestack.append(self.__currscope)
   48|         self.__currscope = []
   49|     def pop(self, decl):
   50|         """Pops the current scope from the stack, and appends the given
   51|         declaration to it"""
   52|         self.__currscope = self.__scopestack.pop()
   53|         self.__currscope.append(decl)
   54|     def pop_only(self):
   55|         """Only pops, doesn't append to scope"""
   56|         self.__currscope = self.__scopestack.pop()
   57|     def add(self, decl):
   58|         """Adds the given decl to the current scope"""
   59|         self.__currscope.append(decl)
   60|     def currscope(self):
   61|         """Returns the current scope: a list of declarations"""
   62|         return self.__currscope
   63|     def visitDeclaration(self, decl):
   64|         """Adds declaration to scope"""
   65|         self.add(decl)
   66|     def visitGroup(self, group):
   67|         """Overrides recursive behaviour to just add the group"""
   68|         self.add(group)
   69|     def visitEnum(self, enum):
   70|         """Overrides recursive behaviour to just add the enum"""
   71|         self.add(enum)
   72|     def visitModule(self, module):
   73|         """Visits all children of the module, and if there are no declarations
   74|         after that removes the module"""
   75|         self.push()
   76|         for decl in module.declarations(): decl.accept(self)
   77|         module.declarations()[:] = self.currscope()
   78|         count = self._count_not_forwards(self.currscope())
   79|         #print string.join(module.name(),'::'),"%d (%d)"%(len(self.currscope()),count),"children"
   80|         if count: self.pop(module)
   81|         else: self.pop_only()
   82|     def _count_not_forwards(self, decls):
   83|         """Returns the number of declarations not instances of AST.Forward"""
   84|         count = 0
   85|         for decl in decls:
   86|             if not isinstance(decl, AST.Forward): count = count+1
   87|         return count
   88| 
   89| linkerOperation = EmptyNS