File: Synopsis/DeclarationSorter.py 1
2
3
4
5
6
7
8from Synopsis.Processor import Parametrized, Parameter
9from Synopsis import ASG
10
11
12_access_specs = {ASG.DEFAULT: '',
13 ASG.PUBLIC: 'Public ',
14 ASG.PROTECTED: 'Protected ',
15 ASG.PRIVATE: 'Private '}
16
17
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
45
46 an = a[len_access:]
47 bn = b[len_access:]
48 break
49 if is_b:
50 return 1
51
52
53 for section in _section_order:
54 if an == section: return -1
55 if bn == section: return 1
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:
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 Wed Mar 19 02:48:16 2008 by
synopsis (version 0.10)