2 # this file contains definitions of classes needed to decompose
3 # C sources files into a series of multi-line "blocks". There are
6 # - normal blocks, which contain source code or ordinary comments
8 # - documentation blocks, which have restricted formatting, and
9 # whose text always start with a documentation markup tag like
10 # "<Function>", "<Type>", etc..
12 # the routines used to process the content of documentation blocks
13 # are not contained here, but in "content.py"
15 # the classes and methods found here only deal with text parsing
16 # and basic documentation block extraction
18 import fileinput
, re
, sys
, os
, string
25 ################################################################
27 ## BLOCK FORMAT PATTERN
29 ## A simple class containing compiled regular expressions used
30 ## to detect potential documentation format block comments within
33 ## note that the 'column' pattern must contain a group that will
34 ## be used to "unbox" the content of documentation comment blocks
36 class SourceBlockFormat
:
38 def __init__( self
, id, start
, column
, end
):
39 """create a block pattern, used to recognize special documentation blocks"""
42 self
.start
= re
.compile( start
, re
.VERBOSE
)
43 self
.column
= re
.compile( column
, re
.VERBOSE
)
44 self
.end
= re
.compile( end
, re
.VERBOSE
)
49 # format 1 documentation comment blocks look like the following:
51 # /************************************/
55 # /************************************/
57 # we define a few regular expressions here to detect them
61 \s* # any number of whitespace
62 /\*{2,}/ # followed by '/' and at least two asterisks then '/'
63 \s*$ # eventually followed by whitespace
67 \s* # any number of whitespace
68 /\*{1} # followed by '/' and precisely one asterisk
69 ([^*].*) # followed by anything (group 1)
70 \*{1}/ # followed by one asterisk and a '/'
71 \s*$ # enventually followed by whitespace
74 re_source_block_format1
= SourceBlockFormat( 1, start
, column
, start
)
77 # format 2 documentation comment blocks look like the following:
79 # /************************************ (at least 2 asterisks)
84 # **/ (1 or more asterisks at the end)
86 # we define a few regular expressions here to detect them
89 \s* # any number of whitespace
90 /\*{2,} # followed by '/' and at least two asterisks
91 \s*$ # eventually followed by whitespace
95 \s* # any number of whitespace
96 \*{1}(?!/) # followed by precisely one asterisk not followed by `/'
97 (.*) # then anything (group1)
101 \s* # any number of whitespace
102 \*+/ # followed by at least one asterisk, then '/'
105 re_source_block_format2
= SourceBlockFormat( 2, start
, column
, end
)
108 # the list of supported documentation block formats, we could add new ones
111 re_source_block_formats
= [ re_source_block_format1
, re_source_block_format2
]
115 # the following regular expressions corresponds to markup tags
116 # within the documentation comment blocks. they're equivalent
117 # despite their different syntax
119 # notice how each markup tag _must_ begin a new line
121 re_markup_tag1
= re
.compile( r
'''\s*<(\w*)>''' ) # <xxxx> format
122 re_markup_tag2
= re
.compile( r
'''\s*@(\w*):''' ) # @xxxx: format
125 # the list of supported markup tags, we could add new ones relatively
128 re_markup_tags
= [ re_markup_tag1
, re_markup_tag2
]
131 # used to detect a cross-reference, after markup tags have been stripped
133 re_crossref
= re
.compile( r
'@(\w*)(.*)' )
136 # used to detect italic and bold styles in paragraph text
138 re_italic
= re
.compile( r
"_(\w(\w|')*)_(.*)" ) # _italic_
139 re_bold
= re
.compile( r
"\*(\w(\w|')*)\*(.*)" ) # *bold*
142 # used to detect the end of commented source lines
144 re_source_sep
= re
.compile( r
'\s*/\*\s*\*/' )
147 # used to perform cross-reference within source output
149 re_source_crossref
= re
.compile( r
'(\W*)(\w*)' )
152 # a list of reserved source keywords
154 re_source_keywords
= re
.compile( '''\\b ( typedef |
173 \#endif ) \\b''', re
.VERBOSE
)
175 ################################################################
177 ## SOURCE BLOCK CLASS
179 ## A SourceProcessor is in charge or reading a C source file
180 ## and decomposing it into a series of different "SourceBlocks".
181 ## each one of these blocks can be made of the following data:
183 ## - A documentation comment block that starts with "/**" and
184 ## whose exact format will be discussed later
186 ## - normal sources lines, include comments
188 ## the important fields in a text block are the following ones:
190 ## self.lines : a list of text lines for the corresponding block
192 ## self.content : for documentation comment blocks only, this is the
193 ## block content that has been "unboxed" from its
194 ## decoration. This is None for all other blocks
195 ## (i.e. sources or ordinary comments with no starting
199 def __init__( self
, processor
, filename
, lineno
, lines
):
200 self
.processor
= processor
201 self
.filename
= filename
203 self
.lines
= lines
[:]
204 self
.format
= processor
.format
207 if self
.format
== None:
212 # extract comment lines
215 for line0
in self
.lines
:
216 m
= self
.format
.column
.match( line0
)
218 lines
.append( m
.group(1) )
220 # now, look for a markup tag
224 for tag
in re_markup_tags
:
229 def location( self
):
230 return "(" + self
.filename
+ ":" + repr(self
.lineno
) + ")"
233 # debugging only - not used in normal operations
237 print "{{{content start---"
238 for l
in self
.content
:
240 print "---content end}}}"
245 fmt
= repr(self
.format
.id) + " "
247 for line
in self
.lines
:
251 ################################################################
253 ## SOURCE PROCESSOR CLASS
255 ## The SourceProcessor is in charge or reading a C source file
256 ## and decomposing it into a series of different "SourceBlock"
259 ## each one of these blocks can be made of the following data:
261 ## - A documentation comment block that starts with "/**" and
262 ## whose exact format will be discussed later
264 ## - normal sources lines, include comments
267 class SourceProcessor
:
269 def __init__( self
):
270 """initialize a source processor"""
277 """reset a block processor, clean all its blocks"""
282 def parse_file( self
, filename
):
283 """parse a C source file, and adds its blocks to the processor's list"""
287 self
.filename
= filename
294 for line
in fileinput
.input( filename
):
296 # strip trailing newlines, important on Windows machines !!
297 if line
[-1] == '\012':
300 if self
.format
== None:
301 self
.process_normal_line( line
)
304 if self
.format
.end
.match( line
):
305 # that's a normal block end, add it to lines and
307 self
.lines
.append( line
)
308 self
.add_block_lines()
310 elif self
.format
.column
.match( line
):
311 # that's a normal column line, add it to 'lines'
312 self
.lines
.append( line
)
315 # humm.. this is an unexcepted block end,
316 # create a new block, but don't process the line
317 self
.add_block_lines()
319 # we need to process the line again
320 self
.process_normal_line( line
)
322 # record the last lines
323 self
.add_block_lines()
327 def process_normal_line( self
, line
):
328 """process a normal line and check if it's the start of a new block"""
329 for f
in re_source_block_formats
:
330 if f
.start
.match( line
):
331 self
.add_block_lines()
333 self
.lineno
= fileinput
.filelineno()
335 self
.lines
.append( line
)
339 def add_block_lines( self
):
340 """add the current accumulated lines, and create a new block"""
342 block
= SourceBlock( self
, self
.filename
, self
.lineno
, self
.lines
)
344 self
.blocks
.append( block
)
349 # debugging only, not used in normal operations
351 """print all blocks in a processor"""
352 for b
in self
.blocks
: