File: Synopsis/SXR.py
 1#
 2# Copyright (C) 2008 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
 8class Entry(object):
 9
10    def __init__(self):
11        """Represents a set of references found for a given symbol."""
12
13        self.definitions = []
14        "List of (file, line, scope) tuples."
15        self.calls = []
16        "List of (file, line, scope) tuples."
17        self.references = []
18        "List of (file, line, scope) tuples."
19
20
21class SXR(dict):
22    """Symboltable containing source code locations of symbol definitions,
23    as well as different types of references."""
24
25    def __init__(self):
26
27        self._index = {}
28
29    def index(self):
30
31        return self._index
32
33    def generate_index(self):
34        """(Re-)generate an index after entries have been added."""
35
36        # Sort the data
37        for target, entry in self.items():
38            entry.calls.sort()
39            entry.references.sort()
40
41            name = target[-1]
42            self._index.setdefault(name,[]).append(target)
43            # If it's a function name, also add without the parameters
44            paren = name.find('(')
45            if paren != -1:
46                self._index.setdefault(name[:paren],[]).append(target)
47
48    def merge(self, other):
49
50        for k in other:
51            e = other[k]
52            entry = self.setdefault(k, Entry())
53            # TODO: Should we try to eliminate duplicates here ?
54            entry.definitions += e.definitions
55            entry.calls += e.calls
56            entry.references += e.references
57
58        self.generate_index()
59
60