misc/libfreetype/src/tools/docmaker/sources.py
author koda
Mon, 25 Apr 2011 01:46:54 +0200
changeset 5172 88f2e05288ba
permissions -rw-r--r--
aaand let's add freetype as well while we are at it other smaller changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     1
#  Sources (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     2
#    David Turner <david@freetype.org>
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     3
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     4
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     5
# this file contains definitions of classes needed to decompose
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     6
# C sources files into a series of multi-line "blocks". There are
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     7
# two kinds of blocks:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     8
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     9
#   - normal blocks, which contain source code or ordinary comments
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    10
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    11
#   - documentation blocks, which have restricted formatting, and
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    12
#     whose text always start with a documentation markup tag like
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    13
#     "<Function>", "<Type>", etc..
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    14
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    15
# the routines used to process the content of documentation blocks
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    16
# are not contained here, but in "content.py"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    17
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    18
# the classes and methods found here only deal with text parsing
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    19
# and basic documentation block extraction
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    20
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    21
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    22
import fileinput, re, sys, os, string
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    23
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    24
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    25
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    26
################################################################
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    27
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    28
##  BLOCK FORMAT PATTERN
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    29
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    30
##   A simple class containing compiled regular expressions used
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    31
##   to detect potential documentation format block comments within
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    32
##   C source code
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    33
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    34
##   note that the 'column' pattern must contain a group that will
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    35
##   be used to "unbox" the content of documentation comment blocks
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    36
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    37
class  SourceBlockFormat:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    38
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    39
    def  __init__( self, id, start, column, end ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    40
        """create a block pattern, used to recognize special documentation blocks"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    41
        self.id     = id
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    42
        self.start  = re.compile( start, re.VERBOSE )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    43
        self.column = re.compile( column, re.VERBOSE )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    44
        self.end    = re.compile( end, re.VERBOSE )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    45
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    46
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    47
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    48
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    49
# format 1 documentation comment blocks look like the following:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    50
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    51
#    /************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    52
#    /*                                  */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    53
#    /*                                  */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    54
#    /*                                  */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    55
#    /************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    56
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    57
# we define a few regular expressions here to detect them
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    58
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    59
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    60
start = r'''
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    61
  \s*      # any number of whitespace
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    62
  /\*{2,}/ # followed by '/' and at least two asterisks then '/'
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    63
  \s*$     # probably followed by whitespace
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    64
'''
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    65
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    66
column = r'''
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    67
  \s*      # any number of whitespace
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    68
  /\*{1}   # followed by '/' and precisely one asterisk
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    69
  ([^*].*) # followed by anything (group 1)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    70
  \*{1}/   # followed by one asterisk and a '/'
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    71
  \s*$     # probably followed by whitespace
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    72
'''
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    73
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    74
re_source_block_format1 = SourceBlockFormat( 1, start, column, start )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    75
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    76
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    77
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    78
# format 2 documentation comment blocks look like the following:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    79
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    80
#    /************************************ (at least 2 asterisks)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    81
#     *
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    82
#     *
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    83
#     *
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    84
#     *
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    85
#     **/       (1 or more asterisks at the end)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    86
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    87
# we define a few regular expressions here to detect them
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    88
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    89
start = r'''
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    90
  \s*     # any number of whitespace
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    91
  /\*{2,} # followed by '/' and at least two asterisks
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    92
  \s*$    # probably followed by whitespace
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    93
'''
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    94
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    95
column = r'''
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    96
  \s*        # any number of whitespace
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    97
  \*{1}(?!/) # followed by precisely one asterisk not followed by `/'
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    98
  (.*)       # then anything (group1)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    99
'''
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   100
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   101
end = r'''
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   102
  \s*  # any number of whitespace
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   103
  \*+/ # followed by at least one asterisk, then '/'
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   104
'''
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   105
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   106
re_source_block_format2 = SourceBlockFormat( 2, start, column, end )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   107
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   108
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   109
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   110
# the list of supported documentation block formats, we could add new ones
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   111
# relatively easily
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   112
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   113
re_source_block_formats = [re_source_block_format1, re_source_block_format2]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   114
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   115
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   116
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   117
# the following regular expressions corresponds to markup tags
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   118
# within the documentation comment blocks. they're equivalent
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   119
# despite their different syntax
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   120
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   121
# notice how each markup tag _must_ begin a new line
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   122
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   123
re_markup_tag1 = re.compile( r'''\s*<(\w*)>''' )  # <xxxx> format
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   124
re_markup_tag2 = re.compile( r'''\s*@(\w*):''' )  # @xxxx: format
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   125
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   126
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   127
# the list of supported markup tags, we could add new ones relatively
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   128
# easily
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   129
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   130
re_markup_tags = [re_markup_tag1, re_markup_tag2]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   131
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   132
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   133
# used to detect a cross-reference, after markup tags have been stripped
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   134
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   135
re_crossref = re.compile( r'@(\w*)(.*)' )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   136
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   137
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   138
# used to detect italic and bold styles in paragraph text
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   139
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   140
re_italic = re.compile( r"_(\w(\w|')*)_(.*)" )     #  _italic_
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   141
re_bold   = re.compile( r"\*(\w(\w|')*)\*(.*)" )   #  *bold*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   142
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   143
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   144
# used to detect the end of commented source lines
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   145
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   146
re_source_sep = re.compile( r'\s*/\*\s*\*/' )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   147
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   148
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   149
# used to perform cross-reference within source output
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   150
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   151
re_source_crossref = re.compile( r'(\W*)(\w*)' )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   152
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   153
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   154
# a list of reserved source keywords
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   155
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   156
re_source_keywords = re.compile( '''\\b ( typedef   |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   157
                                          struct    |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   158
                                          enum      |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   159
                                          union     |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   160
                                          const     |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   161
                                          char      |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   162
                                          int       |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   163
                                          short     |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   164
                                          long      |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   165
                                          void      |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   166
                                          signed    |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   167
                                          unsigned  |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   168
                                          \#include |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   169
                                          \#define  |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   170
                                          \#undef   |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   171
                                          \#if      |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   172
                                          \#ifdef   |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   173
                                          \#ifndef  |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   174
                                          \#else    |
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   175
                                          \#endif   ) \\b''', re.VERBOSE )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   176
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   177
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   178
################################################################
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   179
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   180
##  SOURCE BLOCK CLASS
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   181
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   182
##   A SourceProcessor is in charge of reading a C source file
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   183
##   and decomposing it into a series of different "SourceBlocks".
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   184
##   each one of these blocks can be made of the following data:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   185
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   186
##   - A documentation comment block that starts with "/**" and
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   187
##     whose exact format will be discussed later
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   188
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   189
##   - normal sources lines, including comments
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   190
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   191
##   the important fields in a text block are the following ones:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   192
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   193
##     self.lines   : a list of text lines for the corresponding block
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   194
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   195
##     self.content : for documentation comment blocks only, this is the
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   196
##                    block content that has been "unboxed" from its
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   197
##                    decoration. This is None for all other blocks
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   198
##                    (i.e. sources or ordinary comments with no starting
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   199
##                     markup tag)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   200
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   201
class  SourceBlock:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   202
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   203
    def  __init__( self, processor, filename, lineno, lines ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   204
        self.processor = processor
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   205
        self.filename  = filename
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   206
        self.lineno    = lineno
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   207
        self.lines     = lines[:]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   208
        self.format    = processor.format
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   209
        self.content   = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   210
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   211
        if self.format == None:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   212
            return
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   213
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   214
        words = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   215
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   216
        # extract comment lines
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   217
        lines = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   218
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   219
        for line0 in self.lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   220
            m = self.format.column.match( line0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   221
            if m:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   222
                lines.append( m.group( 1 ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   223
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   224
        # now, look for a markup tag
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   225
        for l in lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   226
            l = string.strip( l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   227
            if len( l ) > 0:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   228
                for tag in re_markup_tags:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   229
                    if tag.match( l ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   230
                        self.content = lines
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   231
                        return
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   232
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   233
    def  location( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   234
        return "(" + self.filename + ":" + repr( self.lineno ) + ")"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   235
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   236
    # debugging only - not used in normal operations
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   237
    def  dump( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   238
        if self.content:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   239
            print "{{{content start---"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   240
            for l in self.content:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   241
                print l
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   242
            print "---content end}}}"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   243
            return
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   244
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   245
        fmt = ""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   246
        if self.format:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   247
            fmt = repr( self.format.id ) + " "
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   248
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   249
        for line in self.lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   250
            print line
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   251
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   252
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   253
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   254
################################################################
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   255
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   256
##  SOURCE PROCESSOR CLASS
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   257
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   258
##   The SourceProcessor is in charge of reading a C source file
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   259
##   and decomposing it into a series of different "SourceBlock"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   260
##   objects.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   261
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   262
##   each one of these blocks can be made of the following data:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   263
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   264
##   - A documentation comment block that starts with "/**" and
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   265
##     whose exact format will be discussed later
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   266
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   267
##   - normal sources lines, include comments
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   268
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   269
##
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   270
class  SourceProcessor:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   271
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   272
    def  __init__( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   273
        """initialize a source processor"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   274
        self.blocks   = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   275
        self.filename = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   276
        self.format   = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   277
        self.lines    = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   278
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   279
    def  reset( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   280
        """reset a block processor, clean all its blocks"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   281
        self.blocks = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   282
        self.format = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   283
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   284
    def  parse_file( self, filename ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   285
        """parse a C source file, and add its blocks to the processor's list"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   286
        self.reset()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   287
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   288
        self.filename = filename
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   289
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   290
        fileinput.close()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   291
        self.format = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   292
        self.lineno = 0
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   293
        self.lines  = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   294
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   295
        for line in fileinput.input( filename ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   296
            # strip trailing newlines, important on Windows machines!
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   297
            if line[-1] == '\012':
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   298
                line = line[0:-1]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   299
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   300
            if self.format == None:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   301
                self.process_normal_line( line )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   302
            else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   303
                if self.format.end.match( line ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   304
                    # that's a normal block end, add it to 'lines' and
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   305
                    # create a new block
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   306
                    self.lines.append( line )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   307
                    self.add_block_lines()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   308
                elif self.format.column.match( line ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   309
                    # that's a normal column line, add it to 'lines'
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   310
                    self.lines.append( line )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   311
                else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   312
                    # humm.. this is an unexpected block end,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   313
                    # create a new block, but don't process the line
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   314
                    self.add_block_lines()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   315
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   316
                    # we need to process the line again
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   317
                    self.process_normal_line( line )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   318
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   319
        # record the last lines
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   320
        self.add_block_lines()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   321
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   322
    def  process_normal_line( self, line ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   323
        """process a normal line and check whether it is the start of a new block"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   324
        for f in re_source_block_formats:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   325
            if f.start.match( line ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   326
                self.add_block_lines()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   327
                self.format = f
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   328
                self.lineno = fileinput.filelineno()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   329
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   330
        self.lines.append( line )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   331
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   332
    def  add_block_lines( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   333
        """add the current accumulated lines and create a new block"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   334
        if self.lines != []:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   335
            block = SourceBlock( self, self.filename, self.lineno, self.lines )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   336
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   337
            self.blocks.append( block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   338
            self.format = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   339
            self.lines  = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   340
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   341
    # debugging only, not used in normal operations
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   342
    def  dump( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   343
        """print all blocks in a processor"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   344
        for b in self.blocks:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   345
            b.dump()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   346
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   347
# eof