File: Synopsis/Processors/Comments/Previous.py
 1#
 2# Copyright (C) 2005 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 Previous(Processor, ASG.Visitor):
12     """A class that maps comments that begin with '<' to the previous
13     declaration"""
14
15     def process(self, ir, **kwds):
16          """decorates process() to initialise last and laststack"""
17
18          self.set_parameters(kwds)
19          self.ir = self.merge_input(ir)
20
21          self.last = None
22          self.__laststack = []
23          for d in ir.asg.declarations:
24               d.accept(self)
25               self.last = d
26
27          return self.output_and_return_ir()
28
29     def push(self):
30          """decorates push() to also push 'last' onto 'laststack'"""
31
32          self.__laststack.append(self.last)
33          self.last = None
34
35     def pop(self):
36          """decorates pop() to also pop 'last' from 'laststack'"""
37
38          self.last = self.__laststack.pop()
39
40     def visit_scope(self, scope):
41          """overrides visit_scope() to set 'last' after each declaration"""
42
43          self.push()
44          for d in scope.declarations:
45               d.accept(self)
46               self.last = d
47          self.pop()
48
49     def visit_declaration(self, decl):
50          self.process_comments(decl)
51
52     def visit_builtin(self, decl):
53          if decl.type == 'EOS': # treat it if it is an 'end of scope' marker
54               self.process_comments(decl)
55
56     def visit_enum(self, enum):
57          """Does the same as visit_scope but for enum and enumerators"""
58
59          self.push()
60          for enumor in enum.enumerators:
61               enumor.accept(self)
62               self.last = enumor
63          if enum.eos: enum.eos.accept(self)
64          self.pop()
65
66     def visit_enumerator(self, enumor):
67          """Checks previous comment and removes dummies"""
68
69          self.process_comments(enumor)
70
71     def process_comments(self, decl):
72          """Checks a decl to see if the comment should be moved. If the comment
73          begins with a less-than sign, then it is moved to the 'last'
74          declaration"""
75
76          comments = decl.annotations.get('comments', [])
77          if comments and self.last:
78               first = comments[0]
79               if first and first[0] == "<" and self.last:
80                    if self.debug:
81                         print 'found comment for previous in', decl.name
82                    comments = self.last.annotations.get('comments', [])
83                    comments.append(first[1:]) # Remove '<'
84                    self.last.annotations['comments'] = comments
85                    del comments[0]
86
87