File: mkcdoc.py

package info (click to toggle)
boolector 3.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 20,744 kB
  • sloc: ansic: 83,136; cpp: 18,159; sh: 3,668; python: 2,889; makefile: 210
file content (201 lines) | stat: -rw-r--r-- 6,396 bytes parent folder | download
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
import sys, re

macros = dict()
typedefs = dict()
functions = dict()
typedefs = dict()
deprecated = dict()
docstrings = dict()

class DocString:

    def __init__(self, string):
        self.docstring = string

    def to_rst(self):
        s = self.docstring

        # reference macros
        for m in macros:
            s = s.replace(m, ":c:macro:`{}`".format(m))

        # reference functions
        for f in functions:
            # exactly match function name (no substrings in other names)
            s = re.sub(r'{}((?![a-z0-9_]))'.format(f),
                       r':c:func:`{}`\1'.format(f), s)
            # all functions with _noref will not be referenced
            s = s.replace("{}_noref".format(f), f)

        return s

    def __str__(self):
        return self.docstring

    def __repr__(self):
        return self.__str__()

class Macro:

    def __init__(self, definition):
        l = definition.split()
        self.name = l[1].strip()
        assert("BOOLECTOR_" in self.name)

    def to_rst(self):
        return ".. c:macro:: {}".format(self.name)

class TypeDef:

    def __init__(self, definition):
        l = definition.replace(';', '').split()
        assert(len(l) == 4)
        self.name = l[-1]

    def to_rst(self):
        return ".. c:type:: {}".format(self.name)


class FunDef:

    def __init__(self, signature):
        p_name = re.compile(r'boolector_([a-z0-9_]+)') 
        m = p_name.search(signature)
        assert(m is not None)
        assert(m.group(0))
        self.name = m.group(0)
        self.signature = signature

        p_params = re.compile(r'\((.*)\)')
        m = p_params.search(signature)
        params = m.group(1).split(',')

        self.params = dict()

        # extract param with type
        if len(params) > 0 and params[0] != "void":
            for param in params:
                p = param.split()
                name = p[-1]
                type = " ".join(p[:-1])
                assert(name not in self.params)
                self.params[name] = type 

    def to_rst(self):
        return ".. c:function:: {}".format(self.signature.replace(';', ''))

    def __str__(self):
        return self.signature

    def __repr__(self):
        return self.__str__()

if __name__ == "__main__":

    if len(sys.argv) != 2:
        raise Exception("Invalid number of arguments")

    with open(sys.argv[1], "r") as file:
        fun_open = False
        doc_open = False
        fun = []
        doc = []
        linenum = 0
        for line in file:
            linenum += 1

            assert(not fun_open or not doc_open)
            assert(not doc_open or not fun_open)

            if line.strip().startswith("/*!"):  # start doc string
                if line.strip() != "/*!":
                    raise Exception(
                        "Invalid start of docstring at line {}. Docstring "\
                        "must start with '/*!' only".format(linenum))
                doc = []
                doc_open = True
            elif doc_open and line.strip().startswith("*/"):  # end doc string
                if line.strip() != "*/":
                    raise Exception(
                        "Invalid end of docstring at line {}. Docstring "\
                        "must end with '*/' only".format(linenum))
                doc_open = False
            elif doc_open: # contents doc string
                doc.append(line)
            elif not doc_open and "boolector_" in line:  # open function
                fun = []
                fun_open = True

            # collect function signature
            if fun_open:
                fun.append(line.strip())

            if fun_open and ");" in line:  # end function
                fun_open = False
                fd = FunDef(" ".join(fun))
                if len(doc) == 0:
                    raise Exception(
                        "No docstring found for function '{}'".format(fd.name))
                ds = DocString("".join(doc))
                if ".. deprecated::" in ds.docstring:
                    deprecated[fd.name] = fd
                else:
                    functions[fd.name] = fd
                docstrings[fd.name] = ds
                fun = []
                doc = []
            elif line.startswith("#define") and "BOOLECTOR_" in line and \
                 len(line.split()) > 2:
                m = Macro(line)
                ds = DocString("".join(doc))
                macros[m.name] = m 
                docstrings[m.name] = ds
                doc = []
            elif "typedef" in line and ("BoolectorNode" in line or \
                                        "BoolectorSort" in line or \
                                        "Btor " in line):
                td = TypeDef(line)
                ds = DocString("".join(doc))
                typedefs[td.name] = td
                docstrings[td.name] = ds
                doc = []

    with open('cboolector_index.rst', 'w') as file:
        file.write("C Interface\n")
        file.write("===========\n\n")

        file.write(".. _macros:\n\n")
        file.write("Macros\n")
        file.write("^^^^^^\n\n")
        for m in sorted(macros.keys()):
            file.write("{}\n\n".format(macros[m].to_rst()))
            for line in docstrings[m].to_rst().split('\n'):
                file.write("{}\n".format(line))
            file.write("\n")

        file.write("Typedefs\n")
        file.write("^^^^^^^^\n\n")
        for td in sorted(typedefs.keys()):
            file.write("{}\n\n".format(typedefs[td].to_rst()))
            for line in docstrings[td].to_rst().split('\n'):
                file.write("{}\n".format(line))
            file.write("\n")

        file.write("Functions\n")
        file.write("^^^^^^^^^\n\n")
        for f in sorted(functions.keys()):
            file.write("{}\n\n".format(functions[f].to_rst()))
            for line in docstrings[f].to_rst().split('\n'):
                file.write("{}\n".format(line))
            file.write("\n")

        file.write("Deprecated\n")
        file.write("^^^^^^^^^^\n\n")
        for f in sorted(deprecated.keys()):
            file.write("{}\n\n".format(deprecated[f].to_rst()))
            for line in docstrings[f].to_rst().split('\n'):
                file.write("{}\n".format(line))
            file.write("\n")
    print("generated cboolector_index.rst")

    sys.exit(0)