Synopsis - Cross-Reference
File: Synopsis/DeclarationSorter.py1# 2# Copyright (C) 2007 Stefan Seefeld 3# All rights reserved. 4# Licensed to the public under the terms of the GNU LGPL (>= 2), 5# see the file COPYING for details. 6# 7 8from Synopsis.Processor import Parametrized, Parameter 9from Synopsis import ASG 10 11# The names of the access specifiers 12_access_specs = {ASG.DEFAULT: '', 13 ASG.PUBLIC: 'Public ', 14 ASG.PROTECTED: 'Protected ', 15 ASG.PRIVATE: 'Private '} 16 17# The predefined order for section names 18_section_order = ('Packages', 19 'Modules', 20 'Namespaces', 21 'Class templates', 22 'Classes', 23 'Interfaces', 24 'Typedefs', 25 'Structs', 26 'Enums', 27 'Unions', 28 'Member function templates', 29 'Member functions', 30 'Function templates', 31 'Functions') 32 33def _compare(a, b): 34 """Compare two section names.""" 35 36 ai, bi = 0, 0 37 an, bn = a, b 38 for i in range(1,4): 39 access = _access_specs[i] 40 len_access = len(access) 41 is_a = (a[:len_access] == access) 42 is_b = (b[:len_access] == access) 43 if is_a: 44 if not is_b: return -1 # a before b 45 # Both matched 46 an = a[len_access:] 47 bn = b[len_access:] 48 break 49 if is_b: 50 return 1 # b before a 51 # Neither matched 52 # Same access, sort by predefined order 53 for section in _section_order: 54 if an == section: return -1 # a before b 55 if bn == section: return 1 # b before a 56 return 0 57 58 59class DeclarationSorter(Parametrized, ASG.Visitor): 60 """Sort declarations by type and accessibility.""" 61 62 struct_as_class = Parameter(False, 'Fuse structs and classes into the same section.') 63 group_as_section = Parameter(True, 'Map group to section, instead of keeping it as a single declaration.') 64 65 def __init__(self, declarations = None, **args): 66 67 super(DeclarationSorter, self).__init__(**args) 68 self.__sections = {} 69 self.__sorted_keys = [] 70 if not declarations: # This is a prototype 71 return 72 for d in declarations: 73 d.accept(self) 74 self.__sorted_keys = self.__sections.keys() 75 self.__sorted_keys.sort(_compare) 76 77 def __iter__(self): return iter(self.__sorted_keys) 78 def __getitem__(self, i): return self.__sections[i] 79 def get(self, *args): return self.__sections.get(*args) 80 def has_key(self, k): return self.__sections.haskey(k) 81 def keys(self): return self.__sorted_keys 82 def values(self): return self.__sections.values() 83 84 def _section_of(self, decl, name = None): 85 """Generate a section name for the given declaration.""" 86 87 section = name or decl.type.capitalize() 88 if self.struct_as_class and section == 'Struct': 89 section = 'Class' 90 if section[-1] == 's': section += 'es' 91 else: section += 's' 92 if decl.accessibility != ASG.DEFAULT: 93 section = _access_specs[decl.accessibility] + section 94 return section 95 96 97 def _add_declaration(self, decl, section): 98 "Adds the given declaration with given name and section." 99 100 if section not in self.__sections: 101 self.__sections[section] = [decl] 102 else: 103 self.__sections[section].append(decl) 104 105 106 def visit_declaration(self, decl): 107 self._add_declaration(decl, self._section_of(decl)) 108 109 def visit_builtin(self, decl): pass 110 def visit_macro(self, decl): pass 111 def visit_forward(self, decl): 112 if decl.template: 113 self.visit_class_template(decl) 114 else: 115 self.visit_declaration(decl) 116 def visit_group(self, group): 117 if self.group_as_section: 118 section = group.name[-1] 119 for d in group.declarations: 120 self._add_declaration(d, section) 121 else: 122 self.visit_declaration(group) 123 def visit_scope(self, decl): 124 self.visit_declaration(decl) 125 def visit_class_template(self, decl): 126 self._add_declaration(decl, self._section_of(decl, 'Class template')) 127 def visit_function(self, decl): 128 self.visit_declaration(decl) 129 def visit_function_template(self, decl): 130 self._add_declaration(decl, self._section_of(decl, 'Function template')) 131
Generated on Tue May 13 02:39:13 2008 by
synopsis (version 0.10)
synopsis (version 0.10)