2 # Copyright 1999, 2000, 2001 Patrik Stridvall
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 package winapi_parser
;
23 use output
qw($output);
24 use options qw($options);
28 my $callbacks = shift;
30 my $empty_callback = sub { };
32 my $c_comment_found_callback = $$callbacks{c_comment_found} || $empty_callback;
33 my $cplusplus_comment_found_callback = $$callbacks{cplusplus_comment_found} || $empty_callback;
34 my $function_create_callback = $$callbacks{function_create} || $empty_callback;
35 my $function_found_callback = $$callbacks{function_found} || $empty_callback;
36 my $type_create_callback = $$callbacks{type_create} || $empty_callback;
37 my $type_found_callback = $$callbacks{type_found} || $empty_callback;
38 my $preprocessor_found_callback = $$callbacks{preprocessor_found} || $empty_callback;
41 my $debug_channels = [];
47 my $documentation_line;
52 my $calling_convention;
53 my $internal_name = "";
56 my $argument_documentations;
60 $function_begin = sub {
61 $documentation_line = shift;
62 $documentation = shift;
63 $function_line = shift;
66 $calling_convention = shift;
67 $internal_name = shift;
68 $argument_types = shift;
69 $argument_names = shift;
70 $argument_documentations = shift;
72 if(defined($argument_names) && defined($argument_types) &&
73 $#$argument_names == -1)
75 foreach my $n (0..$#$argument_types) {
76 push @$argument_names, "";
80 if(defined($argument_documentations) &&
81 $#$argument_documentations == -1)
83 foreach my $n (0..$#$argument_documentations) {
84 push @$argument_documentations, "";
92 $statements_line = shift;
95 my $function = &$function_create_callback();
97 if(!defined($documentation_line)) {
98 $documentation_line = 0;
101 $function->file($file);
102 $function->debug_channels([@$debug_channels]);
103 $function->documentation_line($documentation_line);
104 $function->documentation($documentation);
105 $function->function_line($function_line);
106 $function->linkage($linkage);
107 $function->return_type($return_type);
108 $function->calling_convention($calling_convention);
109 $function->internal_name($internal_name);
110 if(defined($argument_types)) {
111 $function->argument_types([@$argument_types]);
113 if(defined($argument_names)) {
114 $function->argument_names([@$argument_names]);
116 if(defined($argument_documentations)) {
117 $function->argument_documentations([@$argument_documentations]);
119 $function->statements_line($statements_line);
120 $function->statements($statements);
122 &$function_found_callback($function);
142 foreach my $name (@$names) {
143 if($type =~ /^(?:struct|enum)/) {
144 # $output->write("typedef $type {\n");
145 # $output->write("} $name;\n");
147 # $output->write("typedef $type $name;\n");
154 my %regs_entrypoints;
155 my @comment_lines = ();
163 my $lookahead_count = 0;
165 print STDERR "Processing file '$file' ... " if $options->verbose;
166 open(IN, "< $file") || die "<internal>: $file: $!\n";
168 while($again || defined(my $line = <IN>)) {
169 $_ = "" if !defined($_);
179 $lookahead_count = 0;
181 $output->write(" $level($lookahead_count): $line\n") if $options->debug >= 2;
182 $output->write("*** $_\n") if $options->debug >= 3;
184 $lookahead_count = 0;
188 # CVS merge conflicts in file?
189 if(/^(<<<<<<<|=======|>>>>>>>)/) {
190 $output->write("$file: merge conflicts in file\n");
195 if(s/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)(?=\/\*)//s) {
197 if(s/^(\/\*.*?\*\/)//s) {
198 my @lines = split(/\n/, $1);
199 push @comment_lines, $.;
201 &$c_comment_found_callback($. - $#lines, $., $1);
205 $_ = $prefix . ("\n" x $#lines) . $_;
215 # remove C++ comments
216 while(s/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)(\/\/.*?)$/$1/s) {
217 &$cplusplus_comment_found_callback($., $2);
222 # remove preprocessor directives
227 } elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
228 my @lines = split(/\n/, $2);
233 &$preprocessor_found_callback($1, $3);
235 &$preprocessor_found_callback($1, "");
243 if(s/^\s*extern\s+"C"\s+\{//m) {
249 my $documentation_line;
251 my @argument_documentations = ();
254 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
255 $comments[$n] =~ /^\/\*\*+\/$/))
260 if(defined($comments[$n]) && $n >= 0) {
261 my @lines = split(/\n/, $comments[$n]);
263 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
264 $documentation = $comments[$n];
266 for(my $m=$n+1; $m <= $#comments; $m++) {
267 if($comments[$m] =~ /^\/\*\*+\/$/ ||
268 $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
270 @argument_documentations = ();
273 push @argument_documentations, $comments[$m];
284 s/^([^\{\}\'\"]*)//s;
288 while(/^./ && !s/^\'//) {
305 while(/^./ && !s/^\"//) {
326 print "+1: \{$_\n" if $options->debug >= 2;
328 $statements .= $line;
331 $line .= "}" if $level > 1;
332 print "-1: \}$_\n" if $options->debug >= 2;
334 if($level == -1 && $extern_c) {
338 $statements .= $line;
340 $statements .= "$line\n";
345 &$function_end($statements_line, $statements);
348 if(/^\s*(?:WINE_PACKED\s+)?((?:\*\s*)?\w+\s*(?:\s*,\s*(?:\*+\s*)?\w+)*\s*);/s) {
349 my @parts = split(/\s*,\s*/, $1);
350 &$type_end([@parts]);
352 die "$file: $.: syntax error: '$_'\n";
359 } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
360 ((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
361 (\w+(\(\w+\))?)\s*\(([^\)]*)\)\s*(\{|\;)/sx)
363 my @lines = split(/\n/, $&);
364 my $function_line = $. - scalar(@lines) + 1;
373 my $return_type = $2;
374 my $calling_convention = $7;
378 if(!defined($linkage)) {
382 if(!defined($calling_convention)) {
383 $calling_convention = "";
386 $linkage =~ s/\s*$//;
388 $return_type =~ s/\s*$//;
389 $return_type =~ s/\s*\*\s*/*/g;
390 $return_type =~ s/(\*+)/ $1/g;
392 if($regs_entrypoints{$name}) {
393 $name = $regs_entrypoints{$name};
396 $arguments =~ y/\t\n/ /;
397 $arguments =~ s/^\s*(.*?)\s*$/$1/;
398 if($arguments eq "") { $arguments = "..." }
402 my @arguments = split(/,/, $arguments);
403 foreach my $n (0..$#arguments) {
404 my $argument_type = "";
405 my $argument_name = "";
406 my $argument = $arguments[$n];
407 $argument =~ s/^\s*(.*?)\s*$/$1/;
408 # print " " . ($n + 1) . ": '$argument'\n";
409 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
410 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
411 if($argument =~ /^\.\.\.$/) {
412 $argument_type = "...";
413 $argument_name = "...";
414 } elsif($argument =~ /^
415 ((?:struct\s+|union\s+|enum\s+|(?:signed\s+|unsigned\s+)
416 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
417 ((?:const)?\s*(?:\*\s*?)*)\s*
418 (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL)?/x)
420 $argument_type = "$1";
422 $argument_type .= " $2";
426 $argument_type =~ s/\s*const\s*/ /;
427 $argument_type =~ s/^\s*(.*?)\s*$/$1/;
429 $argument_name =~ s/^\s*(.*?)\s*$/$1/;
431 die "$file: $.: syntax error: '$argument'\n";
433 $argument_types[$n] = $argument_type;
434 $argument_names[$n] = $argument_name;
435 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
437 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
438 $#argument_types = -1;
439 $#argument_names = -1;
442 if($options->debug) {
443 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
446 &$function_begin($documentation_line, $documentation,
447 $function_line, $linkage, $return_type, $calling_convention, $name,
448 \@argument_types,\@argument_names,\@argument_documentations);
450 &$function_end(undef, undef);
452 $statements_line = $.;
454 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
455 my @lines = split(/\n/, $&);
456 my $function_line = $. - scalar(@lines) + 1;
460 &$function_begin($documentation_line, $documentation,
461 $function_line, "", "void", "__asm", $1);
462 &$function_end($., "");
463 } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
464 my @lines = split(/\n/, $&);
465 my $function_line = $. - scalar(@lines) + 1;
468 my @arguments16 = ("HWAVEIN16");
469 my @arguments32 = ("HWAVEIN");
470 &$function_begin($documentation_line, $documentation,
471 $function_line, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
472 &$function_end($., "");
473 &$function_begin($documentation_line, $documentation,
474 $function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
475 &$function_end($., "");
476 } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
477 my @lines = split(/\n/, $&);
478 my $function_line = $. - scalar(@lines) + 1;
482 my @arguments16 = ("HWAVEOUT16");
483 my @arguments32 = ("HWAVEOUT");
484 &$function_begin($documentation_line, $documentation,
485 $function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
486 &$function_end($., "");
487 &$function_begin($documentation_line, $documentation,
488 $function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
489 &$function_end($., "");
490 } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
491 my @lines = split(/\n/, $&);
492 my $function_line = $. - scalar(@lines) + 1;
497 my @arguments16 = ("HWAVEOUT16", $4);
498 my @arguments32 = ("HWAVEOUT", $4);
499 &$function_begin($documentation_line, $documentation,
500 $function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
501 &$function_end($., "");
502 &$function_begin($documentation_line, $documentation,
503 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
504 &$function_end($., "");
506 my @arguments16 = ("UINT16", $4);
507 my @arguments32 = ("UINT", $4);
508 &$function_begin($documentation_line, $documentation,
509 $function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
510 &$function_end($., "");
511 &$function_begin($documentation_line, $documentation,
512 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
513 &$function_end($., "");
515 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
517 $regs_entrypoints{$2} = $1;
518 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
520 unshift @$debug_channels, $1;
521 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
523 push @$debug_channels, $1;
524 } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
533 ((?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
536 ((?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?
537 (?:\s*,\s*(?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
545 my @parts = split(/\s*,\s*/, $2);
546 foreach my $part (@parts) {
547 if($part =~ /(?:\s*(\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
559 &$type_end([@names]);
561 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
562 (\w+(?:\s*\*+\s*)?)\s+
564 \((?:(\w+)\s*)?\s*\*\s*(\w+)\s*\)\s*
565 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
569 if(defined($2) || defined($3)) {
572 $type = "$1 ($cc *)($5)";
574 $type = "$1 ($cc *)[$6]";
578 $type = "$1 (*)($5)";
580 $type = "$1 (*)[$6]";
586 } elsif(/typedef[^\{;]*;/s) {
588 $output->write("$file: $.: can't parse: '$&'\n");
589 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
591 $output->write("$file: $.: can't parse: '$&'\n");
592 } elsif(/\'[^\']*\'/s) {
594 } elsif(/\"[^\"]*\"/s) {
598 } elsif(/extern\s+"C"\s+{/s) {
602 print "+1: $_\n" if $options->debug >= 2;
609 print STDERR "done\n" if $options->verbose;
610 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;