Modules | Files | Inheritance Tree | Inheritance Graph | Name Index | Config
File: Synopsis/Formatter/TOC.py
    1| # $Id: TOC.py,v 1.3 2002/11/01 04:05:06 chalky Exp $
    2| #
    3| # This file is a part of Synopsis.
    4| # Copyright (C) 2000, 2001 Stephen Davies
    5| # Copyright (C) 2000, 2001 Stefan Seefeld
    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: TOC.py,v $
   23| # Revision 1.3  2002/11/01 04:05:06  chalky
   24| # Don't let comma replacement screw up ampersands
   25| #
   26| # Revision 1.2  2001/02/13 05:20:04  chalky
   27| # Made C++ parser mangle functions by formatting their parameter types
   28| #
   29| # Revision 1.1  2001/02/01 18:37:25  chalky
   30| # moved TOC out to here
   31| #
   32| #
   33| 
   34| """Table of Contents classes"""
   35| 
   36| import string, re
   37| 
   38| from Synopsis.Core import AST, Util
   39| 
   40| # Not sure how this should be set..
   41| verbose = 0
   42| 
   43| class Linker:
   44|     """Abstract class for linking declarations. This class has only one
   45|     method, link(decl), which returns the link for the given declaration. This
   46|     is dependant on the type of formatter used, and so a Linker derivative
   47|     must be passed to the TOC upon creation."""
   48|     def link(decl): pass
   49| 
   50| class TocEntry:
   51|     """Struct for an entry in the table of contents.
   52|     Vars: link, lang, type (all strings)
   53|     Also: name (scoped)"""
   54|     def __init__(self, name, link, lang, type):
   55|         self.name = name
   56|         self.link = link
   57|         self.lang = lang
   58|         self.type = type
   59| 
   60| class TableOfContents(AST.Visitor):
   61|     """
   62|     Maintains a dictionary of all declarations which can be looked up to create
   63|     cross references. Names are fully scoped.
   64|     """
   65|     def __init__(self, linker):
   66|         """linker is an instance that implements the Linker interface and is
   67|         used to generate the links from declarations."""
   68|         self.__toc = {}
   69|         self.linker = linker
   70|     
   71|     def lookup(self, name):
   72|         name = tuple(name)
   73|         if self.__toc.has_key(name): return self.__toc[name]
   74|         if verbose and len(name) > 1:
   75|             print "Warning: TOC lookup of",name,"failed!"
   76|         return None
   77| 
   78|     # def referenceName(self, name, scope, label=None, **keys):
   79|     #     """Same as reference but takes a tuple name"""
   80|     #     if not label: label = Util.ccolonName(name, scope)
   81|     #     entry = self[name]
   82|     #     if entry: return apply(href, (entry.link, label), keys)
   83|     #     return label or ''
   84| 
   85| 
   86|     def size(self): return len(self.__toc)
   87|     
   88|     __getitem__ = lookup
   89|     
   90|     def insert(self, entry): self.__toc[tuple(entry.name)] = entry
   91|     
   92|     def store(self, file):
   93|         """store the table of contents into a file, such that it can be used later when cross referencing"""
   94|         fout = open(file, 'w')
   95|         nocomma = lambda str: str.replace("&","&").replace(",","&2c;")
   96|         for name in self.__toc.keys():
   97|             scopedname = nocomma(string.join(name, "::"))
   98|             lang = self.__toc[tuple(name)].lang
   99|             link = nocomma(self.__toc[tuple(name)].link)
  100|             fout.write(scopedname + "," + lang + "," + link + "\n")
  101|             
  102|     def load(self, resource):
  103|         args = string.split(resource, "|")
  104|         file = args[0]
  105|         if len(args) > 1: url = args[1]
  106|         else: url = ""
  107|         fin = open(file, 'r')
  108|         line = fin.readline()
  109|         recomma = lambda str: re.sub("&2c;",",",str)
  110|         while line:
  111|             if line[-1] == '\n': line = line[:-1]
  112|             scopedname, lang, link = string.split(line, ",")
  113|             scopedname, link = recomma(scopedname), recomma(link)
  114|             param_index = string.find(scopedname, '(')
  115|             if param_index >= 0:
  116|                name = string.split(scopedname[:param_index], "::")
  117|                name = name[:-1] + [name[-1]+scopedname[param_index:]]
  118|          else:
  119|                name = string.split(scopedname, "::")
  120|             if len(url): link = string.join([url, link], "/")
  121|             entry = TocEntry(name, link, lang, "decl")
  122|             self.insert(entry)
  123|             line = fin.readline()
  124|     
  125|     def visitAST(self, ast):
  126|         for decl in ast.declarations():
  127|             decl.accept(self)
  128|     def visitDeclaration(self, decl):
  129|         entry = TocEntry(decl.name(), self.linker.link(decl), decl.language(), "decl")
  130|         self.insert(entry)
  131|     def visitForward(self, decl):
  132|         pass
  133| 
  134|