3 # Transform K&R C function definitions into ANSI equivalent.
5 # Author: Paul Marquess
11 # Asumes no function pointer parameters. unless they are typedefed.
12 # Assumes no literal strings that look like function definitions
13 # Assumes functions start at the beginning of a line
21 my $sp = qr{ \s* (?: /\* .*? \*/ )? \s* }x; # assume no nested comments
23 my $d1 = qr{ $sp (?: [\w\*\s]+ $sp)* $sp \w+ $sp [\[\]\s]* $sp }x ;
24 my $decl = qr{ $sp (?: \w+ $sp )+ $d1 }xo ;
25 my $dList = qr{ $sp $decl (?: $sp , $d1 )* $sp ; $sp }xo ;
31 .*?
# Minimal eat content
32 ( ^ \w
[\w\s\
*]+ ) # $3 -- function name
33 \s
* # optional whitespace
34 ) # $2 - Matched up to before parameter list
36 \
( \s
* # Literal "(" + optional whitespace
37 ( [^\
)]+ ) # $4 - one or more anythings except ")"
38 \s
* \
) # optional whitespace surrounding a Literal ")"
40 ( (?
: $dList )+ ) # $5
42 $sp ^ { # literal "{" at start of line
52 StripComments
($params);
53 StripComments
($param_list);
54 $param_list =~ s/^\s+//;
55 $param_list =~ s/\s+$//;
58 my %pList = map { $_ => $i++ }
59 split /\s*,\s*/, $param_list;
60 my $pMatch = '(\b' . join('|', keys %pList) . '\b)\W*$' ;
62 my @params = split /\s*;\s*/, $params;
64 foreach my $p (@params)
68 my @bits = split /\s*,\s*/, $p;
69 my $first = shift @bits;
71 push @outParams, $first;
72 $first =~ /^(\w+\s*)/;
74 push @outParams, map { $type . $_ } @bits;
84 my %tmp = map { /$pMatch/; $_ => $pList{$1} }
87 @outParams = map { " $_" }
88 sort { $tmp{$a} <=> $tmp{$b} }
92 print "(\n" . join(",\n", @outParams) . ")\n";
97 # Output any trailing code.
107 # Strip C & C++ coments
112 /\* ## Start of /* ... */ comment
113 [^*]*\
*+ ## Non-* followed by 1-or-more *'s
116 )* ## 0-or-more things which don't start with /
117 ## but do end with '*'
118 / ## End of /* ... */ comment
121 // ## Start of C++ comment //
122 [^\n]* ## followed by 0-or-more non end of line characters
124 | ## OR various things which aren't comments:
127 " ## Start of " ... " string
133 " ## End of " ... " string
137 ' ## Start of ' ... ' string
143 ' ## End of ' ... ' string
147 . ## Anything other char
148 [^/"'\\]* ## Chars which doesn't start a comment
, string
or escape