Modules |
Files |
Inheritance Tree |
Inheritance Graph |
Name Index |
Config
File: Synopsis/Linker/NameMapper.py
1| # $Id: NameMapper.py,v 1.3 2002/12/23 12:17:02 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: NameMapper.py,v $
23| # Revision 1.3 2002/12/23 12:17:02 chalky
24| # Add types for parent scopes
25| #
26| # Revision 1.2 2002/10/20 02:22:38 chalky
27| # Fix reference to verbose flag
28| #
29| # Revision 1.1 2002/08/23 04:37:26 chalky
30| # Huge refactoring of Linker to make it modular, and use a config system similar
31| # to the HTML package
32| #
33|
34| import string
35|
36| from Synopsis.Core import AST, Type, Util
37|
38| from Linker import config, Operation
39|
40| class NameMapper (Operation, AST.Visitor):
41| """This class adds a prefix to all declaration and type names."""
42|
43| def visitDeclaration(self, decl):
44| """Changes the name of this declaration and its associated type"""
45| # Change the name of the decl
46| name = decl.name()
47| newname = tuple(config.map_declaration_names + name)
48| decl.set_name(newname)
49| # Change the name of the associated type
50| try:
51| type = config.types[name]
52| del config.types[name]
53| type.set_name(newname)
54| config.types[newname] = type
55| except KeyError, msg:
56| if config.verbose: print "Warning: Unable to map name of type:",msg
57| def visitGroup(self, node):
58| """Recursively visits declarations under this group/scope/etc"""
59| self.visitDeclaration(node)
60| for declaration in node.declarations():
61| declaration.accept(self)
62|
63|
64| def execute(self, ast):
65| if not config.map_declaration_names: return
66| declarations = ast.declarations()
67| types = ast.types()
68| # Map the names of declarations and their types
69| for decl in declarations:
70| decl.accept(self)
71| # Now we need to put the declarations in actual nested MetaModules
72| lang, type = '', config.map_declaration_type
73| names = config.map_declaration_names
74| for index in range(len(names),0, -1):
75| module = AST.MetaModule(lang, type, names[:index])
76| module.declarations().extend(declarations)
77| types[module.name()] = Type.Declared(lang, module.name(), module)
78| declarations[:] = [module]
79|
80| linkerOperation = NameMapper