in reply to Parsing C Source File Functions.

Giving up isn't my deal :)
#!/usr/bin/perl # Script to add comments for Natural Doc. http://www.naturaldoc.org use strict; use warnings; use Tie::File; use Data::Dumper; if (@ARGV < 1) { print "Usage: app file"; exit(0); } my $file; open $file, $ARGV[0] or die $!; my $data = join("", <$file>); close $file; my $data_with_comment = ""; my @matches; while ( $data =~ /\n((?:\w+\*?\*?\s+)+) (\*?\w+\s*) \s* \((.*?) \) (\s +*\{)/gsx ) { push(@matches, { 'fullmatch' => $&, 'functype' => $1, 'funcname' = +> $2, 'funcargs' => $3 }); } foreach (@matches) { my $fullmatch = $_->{'fullmatch'}; my $functype = $_->{'functype'}; my $funcname = $_->{'funcname'}; my $funcargs = $_->{'funcargs'}; my $comment = ""; my @funcargs = split /,\s*/, $funcargs; my $args = join( ", ", @funcargs ); my $args2 = join( "\n * ", @funcargs ); $comment .= qq { /* Function: $funcname($args) * * * * Arguments: * $args2 * * Returns: * $functype * */ }; $data =~ s/\Q$fullmatch\E/$comment$fullmatch/; } print $data;
This works for me atleast.

Replies are listed 'Best First'.
Re^2: Parsing C Source File Functions.
by Anonymous Monk on Jan 10, 2014 at 10:49 UTC
    How can this be exteneded for parsing the CPP functions : static gboolean g::ber_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)

      Change the regex to accept the namespace prefix of the function name.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        I am though not very good at the regex, I tired with the following, but it is not working. The regex is as below. Could any one help with this.

        $data =~ /(.*?)\n((?:\w+\*?\s+)+)(\*?\:|\w+\s*)\s*\((.*?)\)(\s*\{)/gsx