File: Synopsis/Formatters/List.py
 1#
 2# Copyright (C) 2006 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 Processor, Parameter
 9from Synopsis import ASG
10
11class Formatter(Processor, ASG.Visitor):
12    """Generate a high-level list of the content of a syn file.
13    This formatter can lists source files (by name), as well as
14    declarations (by name, type) contained in a given scope."""
15
16    show_files = Parameter(False, 'list files')
17    show_scope = Parameter(None, 'list declarations in the given scope')
18
19    def process(self, ir, **kwds):
20
21        self.set_parameters(kwds)
22        self.ir = self.merge_input(ir)
23
24        if self.show_files:
25            for f in self.ir.files.values():
26                print '%s (language=%s, primary=%d)'%(f.name, f.annotations['language'],
27                                                      f.annotations['primary'])
28
29        if self.show_scope is not None:
30            if '.' in self.show_scope:
31                self.scope = tuple(self.show_scope.split('.'))
32            elif '::' in self.show_scope:
33                self.scope = tuple(self.show_scope.split('::'))
34            else:
35                self.scope = (self.show_scope,)
36
37            for d in self.ir.asg.declarations:
38                d.accept(self)
39
40        return self.ir
41
42
43    def visit_scope(self, node):
44
45        if self.scope == node.name:
46
47            # We found the right scope.
48            # List all declarations directly contained in it.
49            declarations = node.declarations[:]
50            declarations.sort(lambda x, y : cmp(x.name, y.name))
51            for d in declarations:
52                if isinstance(d, ASG.Builtin): continue
53                print '%s : %s'%(d.name[-1], d.type)
54        elif (len(node.name) < self.scope and
55              self.scope[0:len(node.name)] == node.name):
56
57            # We found a parent scope.
58            # Visit child scopes.
59            for d in node.declarations:
60                d.accept(self)
61