File: BoostBook.py

package info (click to toggle)
synopsis 0.8.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,112 kB
  • ctags: 12,996
  • sloc: cpp: 34,254; ansic: 33,620; python: 10,975; sh: 7,261; xml: 6,369; makefile: 773; asm: 445
file content (280 lines) | stat: -rw-r--r-- 9,117 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# $Id: BoostBook.py,v 1.3 2003/11/13 17:21:03 stefan Exp $
#
# Copyright (C) 2000 Stefan Seefeld
# Copyright (C) 2000 Stephen Davies
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

"""a BoostBook formatter"""

from Synopsis.Processor import Processor, Parameter
from Synopsis import AST, Type, Util

import sys, getopt, os, os.path, string, re

class Formatter(Processor, Type.Visitor, AST.Visitor):
   """
   The type visitors should generate names relative to the current scope.
   The generated references however are fully scoped names
   """

   def process(self, ast, **kwds):
      
      self.set_parameters(kwds)
      self.ast = self.merge_input(ast)

      self.__os = open(self.output, 'w')
      self.__scope = ()
      self.__scopestack = []
      self.__indent = 0
      self.__type_label = ''
      
      for d in ast.declarations():
         d.accept(self)

      self.__os.close()
      
      return self.ast

   def scope(self): return self.__scope
   def push_scope(self, newscope):

      self.__scopestack.append(self.__scope)
      self.__scope = newscope

   def pop_scope(self):

      self.__scope = self.__scopestack[-1]
      del self.__scopestack[-1]

   def write(self, text):
      """Write some text to the output stream, replacing \n's with \n's and
      indents."""

      indent = ' ' * self.__indent
      self.__os.write(text.replace('\n', '\n'+indent))

   def start_entity(self, __type, **__params):
      """Write the start of an entity, ending with a newline"""

      param_text = ""
      if __params: param_text = " " + string.join(map(lambda p:'%s="%s"'%(p[0].lower(), p[1]), __params.items()))
      self.write("<" + __type + param_text + ">")
      self.__indent = self.__indent + 2
      self.write("\n")

   def end_entity(self, type):
      """Write the end of an entity, starting with a newline"""

      self.__indent = self.__indent - 2
      self.write("\n</" + type + ">")

   def write_entity(self, __type, __body, **__params):
      """Write a single entity on one line (though body may contain
      newlines)"""

      param_text = ""
      if __params: param_text = " " + string.join(map(lambda p:'%s="%s"'%(p[0].lower(), p[1]), __params.items()))
      self.write("<" + __type + param_text + ">")
      self.__indent = self.__indent + 2
      self.write(__body)
      self.__indent = self.__indent - 2
      self.write("</" + __type + ">")

   def entity(self, __type, __body, **__params):
      """Return but do not write the text for an entity on one line"""

      param_text = ""
      if __params: param_text = " " + string.join(map(lambda p:'%s="%s"'%(p[0].lower(), p[1]), __params.items()))
      return "<%s%s>%s</%s>"%(__type, param_text, __body, __type)

   def reference(self, ref, label):
      """reference takes two strings, a reference (used to look up the symbol and generated the reference),
      and the label (used to actually write it)"""

      location = self.__toc.lookup(ref)
      if location != "": return href("#" + location, label)
      else: return span("type", str(label))

   def label(self, ref):

      location = self.__toc.lookup(Util.ccolonName(ref))
      ref = Util.ccolonName(ref, self.scope())
      if location != "": return name("\"" + location + "\"", ref)
      else: return ref

   def type_label(self): return self.__type_label
   #################### Type Visitor ##########################################

   def visitBaseType(self, type):

      self.__type_ref = Util.ccolonName(type.name())
      self.__type_label = Util.ccolonName(type.name())
        
   def visitUnknown(self, type):

      self.__type_ref = Util.ccolonName(type.name())
      self.__type_label = Util.ccolonName(type.name(), self.scope())

   def visitDeclared(self, type):

      self.__type_label = Util.ccolonName(type.name(), self.scope())
      self.__type_ref = Util.ccolonName(type.name())

   def visitModifier(self, type):

      type.alias().accept(self)
      self.__type_ref = string.join(type.premod()) + " " + self.__type_ref + " " + string.join(type.postmod())
      self.__type_label = string.join(type.premod()) + " " + self.__type_label + " " + string.join(type.postmod())

   def visitParametrized(self, type):

      type.template().accept(self)
      type_label = self.__type_label + "&lt;"
      parameters_label = []
      for p in type.parameters():
         p.accept(self)
         parameters_label.append(self.__type_label)
      self.__type_label = type_label + string.join(parameters_label, ", ") + "&gt;"

   def format_type(self, type):

      type.accept(self)
      return self.__type_label

   def visitFunctionType(self, type):

      # TODO: this needs to be implemented
      self.__type_ref = 'function_type'
      self.__type_label = 'function_type'

   def visitComment(self, comment):

      text = comment.text()
      text = text.replace('\n\n', '</para><para>')
      self.write(self.entity("para", text)+'\n')

   #################### AST Visitor ###########################################
         
   def visitDeclarator(self, node):
      self.__declarator = node.name()
      for i in node.sizes():
         self.__declarator[-1] = self.__declarator[-1] + "[" + str(i) + "]"

   def visitTypedef(self, typedef):

      self.start_entity("typedef", name=Util.ccolonName(self.scope(), typedef.name()))
      self.write_entity("type", self.format_type(typedef.alias()))
      self.end_entity("typedef")

   def visitVariable(self, variable):

      self.start_entity("fieldsynopsis")
      variable.vtype().accept(self)
      self.entity("type", self.type_label())
      self.entity("varname", variable.name()[-1])
      self.end_entity("fieldsynopsis")

   def visitConst(self, const):

      print "sorry, <const> not implemented"

   def visitModule(self, module):

      self.start_entity("namespace", name=Util.ccolonName(self.scope(), module.name()))
      self.write("\n")
      map(self.visitComment, module.comments())
      self.push_scope(module.name())
      for declaration in module.declarations(): declaration.accept(self)
      self.pop_scope()
      self.end_entity("namespace")

   def visitClass(self, clas):

      self.start_entity("class", name=Util.ccolonName(self.scope(), clas.name()))
      # clas.type()
      if len(clas.parents()):
         for parent in clas.parents(): parent.accept(self)
      self.push_scope(clas.name())
      if clas.comments():
         self.start_entity("purpose")
         map(self.visitComment, clas.comments())
         self.end_entity("purpose")
      for declaration in clas.declarations():
         declaration.accept(self)
      self.pop_scope()
      self.end_entity("class")

   def visitInheritance(self, inheritance):

      if len(inheritance.attributes()):
         self.start_entity("inherit", access=inheritance.attributes()[0])
      else:
         self.start_entity("inherit")
      self.write_entity("classname", self.format_type(inheritance.parent()))
      self.end_entity("inherit")

   def visitParameter(self, parameter):

      self.start_entity("parameter", name=parameter.identifier())
      self.write_entity("param_type", self.format_type(parameter.type()))
      #map(lambda m, this=self: this.write_entity("modifier", m), parameter.premodifier())
      #map(lambda m, this=self: this.write_entity("modifier", m), parameter.postmodifier())
      self.end_entity("parameter")
      self.write("\n")

   def visitFunction(self, function):

      self.start_entity("function", name=Util.ccolonName(self.scope(), function.realname()))
      self.do_function(function)
      self.end_entity("function")
      self.write("\n")

   def visitOperation(self, operation):

      name = operation.name()
      tag = None
      if len(name) > 1:
         if name[-1] == name[-2]:
            tag = "constructor"
            self.start_entity(tag)
         elif name[-1] == "~"+name[-2]:
            tag = "destructor"
            self.start_entity(tag)
      if tag is None:
         tag = "method"
         self.start_entity(tag, name=Util.ccolonName(self.scope(), operation.realname()))
      self.do_function(operation)
      self.end_entity(tag)
      self.write("\n")

   def do_function(self, func):
      """Stuff common to functions and methods, contructors, destructors"""

      for parameter in func.parameters(): parameter.accept(self)
      if func.returnType():
         self.write_entity("type", self.format_type(func.returnType()))
         self.write("\n")
      if func.comments():
         self.start_entity("purpose")
         map(self.visitComment, func.comments())
         self.end_entity("purpose")
         self.write("\n")
	
      if func.exceptions():
         self.start_entity("throws")
         for ex in func.exceptions():
            self.write_entity("simpara", ex)
         self.end_entity("throws")
         self.write("\n")

   def visitEnumerator(self, enumerator):

      print "sorry, <enumerator> not implemented"

   def visitEnum(self, enum):

      print "sorry, <enum> not implemented"