File: class2tex

package info (click to toggle)
mpqc 2.3.1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 39,580 kB
  • ctags: 16,804
  • sloc: cpp: 258,686; sh: 8,584; perl: 6,017; ansic: 5,491; makefile: 2,769; fortran: 1,970; lisp: 1,269; yacc: 313; lex: 177; csh: 45
file content (413 lines) | stat: -rw-r--r-- 11,036 bytes parent folder | download | duplicates (11)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/perl
# Use -*-perl-*- mode in emacs.

@files = ();

$clssection = "section";
$clssubsection = "subsection";

#for $i (@ARGV) {
while ($i = shift) {
    if ($i eq '-clssection') {
        $clssection = shift;
    }
    elsif ($i eq '-clssubsection') {
        $clssubsection = shift;
    }
    else {
        @files = (@files, $i);
    }
}
@ARGV = @files;

$printmember = 0;
$printmembercase = 0;
$template_param = '';
$class = '';
$classdoc = '';
%item = ();
%sectionname = (pri, "Private", pro, "Protected", pub, "Public");
@sectionorder = (pub, pro, pri);
while(<>) {

    while(/^\s*\/\//) {
        if (/^\s*\/\/\.\s+(.*)/) {
            do get_doc($1);
        }
        elsif (/^\s*\/\/\.\s*$/) {
            do get_doc('');
        }
        else {
            last;
        }
    }

    if (/^\s*class\s+([a-zA-Z_][a-zA-Z0-9_]*)/) {
        do new_class($1,'','');
    }
    elsif (/^\s*template <(.*)>\s*class\s+([a-zA-Z_][a-zA-Z0-9_]*)/) {
        do new_class($2,$1,'');
    }
    elsif (/^\s*template <(.*)>\s*$/) {
        $tmp_template_param = $1;
        while (<>) { last; }
        if (/^\s*class\s+([a-zA-Z_][a-zA-Z0-9_]*)/) {
            do new_class($1,$tmp_template_param,'');
        }
    }
    elsif (/^\s*private\s*:/) {
        $interface = 'pri';
    }
    elsif (/^\s*public\s*:/) {
        $interface = 'pub';
    }
    elsif (/^\s*protected\s*:/) {
        $interface = 'pro';
    }
}

do write_doc();

sub new_class {
    # flush out the documentation for the previous class
    do write_doc();
    $class = $_[0];
    $template_param = $_[1];
    $classdoc = $_[2];
    $interface = 'pri';
    $private = '';
    $protected = '';
    $public = '';
}

sub write_doc {
    local($nsection) = keys(%item);
    if ($classdoc ne ''
        || $item{'pri'} ne ''
        || $item{'pro'} ne ''
        || $item{'pub'} ne '') {
        $template_param =~ s/_/\\_/g;
        if (!open(OUTPUT, ">doc/$class.cls.tex")) {
            die "Can't open doc/$class.cls.tex: $!";
        }
        print "writing $class.cls.tex\n";
        if ($template_param eq '') {
            print OUTPUT "\\$clssection\{The \\clsnm{$class} Class}";
            print OUTPUT "\\label{$class}\\index{$class}\n";
            #print OUTPUT "\\clssection{$class}\n";
        }
        else {
            print OUTPUT "\\$clssection\{The \\clsnm{$class";
            print OUTPUT "\$<\$$template_param\$>\$} Class Template}";
            print OUTPUT "\\label{$class}\\index{$class}\n";
            #print OUTPUT "\\tclssection{$class}{$template_param}\n";
        }
        print OUTPUT "\\index{$class}\n";
        if ($classdoc ne '') {
            print OUTPUT "$classdoc\n";
        }
        for (@sectionorder) {
            if ($item{$_} ne '') {
                if ($template_param eq '') {
                    print OUTPUT "\\$clssubsection\{The $sectionname{$_} \\clsnm{$class} Interface}\n";
                }
                else {
                    print OUTPUT "\\$clssubsection\{The \\clsnm{$class";
                    print OUTPUT "\$<\$$template_param\$>\$} Class Template Interface}\n";
                }
                local($memberdoc) = $item{$_};
                # two items in a row shouldn't have an mbox
                $memberdoc =~ s/\\mbox{}\\\\\s*\\item\[/\n\\item\[/g;
                print OUTPUT "\\begin{classinterface}\n";
                print OUTPUT $memberdoc;
                print OUTPUT "\\end{classinterface}\n";
            }
        }
        close(OUTPUT);
    }
    %item = ();
    $classdoc = '';
}

sub beautify_member {
    $m = $_[0];

    print "MEM=$m\n" if ($printmember);

    $_ = $m;

    # remove inline
    s/inline//;

    # remove parent/member constructor calls
    s/::/SAVEDCOLONCOLON/g;
    s/:.*$//;
    s/SAVEDCOLONCOLON/::/g;

    $virtual = 0;
    $static = 0;
    $constreturn = 0;
    # check for a constructor
    if (/^\s*[A-Za-z_][A-Za-z0-9_]*\s*\(/) {
        $typename = 'void';
    }
    # check for a destructor
    elsif (/^\s*(virtual\s)?\s*~\s*[A-Za-z_][A-Za-z0-9_]*\s*\(/) {
        $typename = 'void';
        if (s/^\s*virtual//) { $virtual = 1; }
    }
    else {
        # parse the virtual and static qualifiers
        if (s/^\s*virtual//) {
            $virtual = 1;
        }
        elsif (s/^\s*static//) {
            $static = 1;
        }

        # parse the const qualifier
        if (s/^\s*const//) {
            $constreturn = 1;
        }

        # parse the type name
        /^\s*([A-Za-z_][A-Za-z_0-9<>,]*)(.*)/;
        $typename = $1;
        $_ = $2;

        # parse the reference and pointer and const qualifiers
        while (/^\s*(&)(.*)/ || /^\s*(\*)(.*)/ || /^\s*(const)(.*)/) {
            $typename = "$typename $1";
            $_ = $2;
        }

        $typename =~ s/&/\\&/g;
        $typename =~ s/_/\\_/g;
        $typename =~ s/</\$<\$/g;
        $typename =~ s/>/\$>\$/g;
        $typename = "const $typename" if ($constreturn);
    }

    # parse the member name
    ($destructor, $membername, $rest) =
        /^\s*(~?)\s*([A-Za-z_][A-Za-z0-9_]*)(.*)/;
    $_ = $rest;
    $membername = "$destructor$membername";
    if ($typename eq "operator") {
        # fixup type conversion operators
        $typename = "$membername";
        $membername = "operator $membername";
    }
    elsif ($membername eq 'operator') {
        # operator () is a special case
        if (/^\s*\(\s*\)(.*)/) {
            $membername = "$membername ()";
            $_ = $1;
        }
        else {
            /^\s*([^(]*)\s*(\(.*)/;
            $membername = "$membername $1";
            $_ = $2;
        }
    }
    #print "MN = $membername, _ = $_\n";

    # look for underscores in membername
    #@membername = split(/_/,$membername);
    #$item = "";
    #foreach $i (0 .. $#membername) {
    #    $item = join('$item',"{\\bfseries $membername[$i]}");
    #    if ($i != $#membername) {
    #        $item = "$item\_";
    #    }
    #}
    $item = "{\\bfseries $membername}";
    $item =~ s/_/\\_/g;

    # take care of special characters in item
    $item =~ s/&/\\&/g;
    $item =~ s/</\$<\$/g;
    $item =~ s/>/\$>\$/g;
    $item =~ s/~/\$\\tilde{}\$/;

    # parse the arglist
    /\((.*)\)(.*)/;
    $arglist = $1;
    $_ = $2;

    # take care of special characters in arglist
    $arglist =~ s/&/\\&/g;
    $arglist =~ s/</\$<\$/g;
    $arglist =~ s/>/\$>\$/g;
    $arglist =~ s/_/\\_/g;

    # make sure there is whitespace after commas (for better formatting).
    $arglist =~ s/,\s*/, /g;

    # parse the constness of calling object
    $constobject = 0;
    if (/^[ \t]*const(.*)/) {
        $constobject = 1;
        $_ = $1;
    }

    # parse the pure virtual qualifier
    $pure = 0;
    if (/[ \t]*=[ \t]*0(.*)/) {
        $pure = 1;
        $_ = $1;
    }

    # construct the table entry

    $qualifiers = '';
    if ($constobject) {
        $qualifiers = "$qualifiers const";
    }
    if ($pure) {
        $qualifiers = "$qualifiers pure";
    }
    elsif ($virtual) {
        $qualifiers = "$qualifiers virtual";
    }
    elsif ($static) {
        $qualifiers = "$qualifiers static";
    }
    $_ = $qualifiers;
    /[ \t]*(.*)[ \t]*/;
    $qualifiers = $1;

    #$item = "\@b{$membername}($arglist)";
    $item = "$item($arglist)";
    if ($qualifiers ne '') {
        $item = "$item $qualifiers";
    }
    if ($typename ne 'void') {
        $item = "$item \$\\rightarrow\$ {\\bfseries $typename}";
    }

    return "$item";
}

sub get_doc {
    $doc = $_[0];
    $members = '';
    $indoc = 1;
    while (<>) {
        if ($indoc && /^\s*\/\/(\.)?(\s*.*)/) {
            $doc = "$doc\n$2";
        }
        elsif (/^\s*template <(.*)>\s*class\s+([a-zA-Z_][a-zA-Z0-9_]*)/) {
            do new_class($2,$1,'');
        }
        elsif (/^\s*template <(.*)>\s*$/) {
            $tmp_template_param = $1;
            while (<>) { last; }
            if (/^\s*class\s+([a-zA-Z_][a-zA-Z0-9_]*)/) {
                while(<>) { last; }
                do new_class($1,$tmp_template_param,'');
                last;
            }
        }
        elsif (/^\s*class\s+([a-zA-Z_][a-zA-Z0-9_]*)/) {
            while(<>) { last; }
            do new_class($1,'',$doc);
            last;
        }
        elsif (/^\s*((~\s*)?[A-Za-z_].*,)\s*$/) {
            $indoc = 0;
            $member = $1;
            $remainder = &read_member_continuation;
            $member = "$member$remainder";
            print "CASE 1: MEM=$member\n" if ($printmembercase);
            $member = &beautify_member($member);
            $members = "$members\\item[$member]\\mbox{}\\\\\n";
        }
        elsif (/^\s*((~\s*)?[A-Za-z_].*)\s*{/) {
            $indoc = 0;
            print "CASE 2: MEM=$1\n" if ($printmembercase);
            $member = &beautify_member($1);
            $members = "$members\\item[$member]\\mbox{}\\\\\n";
            # attempt to skip over simple routines
            &skip_inlined_routine("{$'");
        }
        elsif (/^\s*((~\s*)?[A-Za-z_].*);\s*/) {
            $indoc = 0;
            print "CASE 3: MEM=$1\n" if ($printmembercase);
            $member = &beautify_member($1);
            $members = "$members\\item[$member]\\mbox{}\\\\\n";
        }
        elsif (/^\s*((~\s*)?[A-Za-z_].*)\s*/) {
            $indoc = 0;
            $member = $1;
            if (! /\)/) {
                # the member is not complete--finish it
                $remainder = &read_member_continuation;
                $member = "$member$remainder";
            }
            print "CASE 4: MEM=$member\n" if ($printmembercase);
            $member = &beautify_member($member);
            $members = "$members\\item[$member]\\mbox{}\\\\\n";
        }
        elsif (/^\s*{/) {
            $indoc = 0;
            &skip_inlined_routine("{$'");
        }
        else {
            $item{$interface} = "$item{$interface}$members$doc\n\n";
            last;
        }
    }
}

sub skip_inlined_routine {
    local($_) = $_[0];
    local($nbracket) = &count_brackets($_);
    return if ($nbracket == 0);
    while (<>) {
        $nbracket = $nbracket + &count_brackets($_);
        return if ($nbracket == 0);
    }
}

sub count_brackets {
    local($_) = $_[0];
    local($n) = 0;
    s/"[^\"]*"//g;
    local(@chars) = split(//,$_);
    foreach (@chars) {
        if ($_ eq "{") {
            $n++;
        }
        elsif ($_ eq "}") {
            $n--;
        }
    }
    return $n;
}

sub read_member_continuation {
    local($member) = "";
    while (<>) {
        if (/^\s*(.*,)\s*$/) {
            $member = "$member$1";
        }
        elsif (/^\s*(.*){\s*/) {
            $member = "$member$1";
            # attempt to skip over simple routines
            &skip_inlined_routine("{$'");
            last;
        }
        elsif (/^\s*(.*);\s*/) {
            $member = "$member$1";
            last;
        }
        elsif (/};/) {
            last;
        }
        else {
            printf "BAD MEMBER CONTINUATION: $_\n";
        }
    }
    return $member;
}