Hey,

I'm working on a parser to parse at the moment .c files so I can add ND http://www.naturaldocs.org/documenting.html style commenting above the function.

That is, say I have:
static struct super_block* sfs_get_super(struct file_system_type *fst, + int flags, const char *devname, void *data) { struct super_block *sb = 0; struct mounter_data_t *mount_data = sfs_get_mounter_data(dat +a); HIGHPRINT("Calling get_sb_single\n"); sb = get_sb_single(fst, flags, mount_data, sfs_fill_super); HIGHPRINT("Returned from get_sb_single\n"); dealloc_mounter_data(mount_data); return sb; }
I wanna do so this becomes:
/* Function: sfs_get_super(struct file_system_type *fst, int flags, co +nst char *devname, void *data) * * Parameters: * fst - * flags - * devname - * data - * * Returns: * struct super_block* */ static struct super_block* sfs_get_super(struct file_system_type *fst, + int flags, const char *devname, void *data) { struct super_block *sb = 0; struct mounter_data_t *mount_data = sfs_get_mounter_data(dat +a); HIGHPRINT("Calling get_sb_single\n"); sb = get_sb_single(fst, flags, mount_data, sfs_fill_super); HIGHPRINT("Returned from get_sb_single\n"); dealloc_mounter_data(mount_data); return sb; }
That is, takes out the function and its arguments, and creates that skeleton documentation. I'm using Tie::File to read the file, and am currently halted on parsing the code. So far I've done this:
#!/usr/bin/perl # Script to add comments for Natural Doc. http://www.naturaldoc.org use Tie::File; use strict; use warnings; use Data::Dumper; my @FILE_ARRAY; tie @FILE_ARRAY, 'Tie::File', "searchfs.c", recsep => "\n" or die $!; my $found = 0; foreach (@FILE_ARRAY) { #if (/^(sub .+)/) { # Match .c function if (/^(?:([0-9_a-zA-Z*]+) +)?(?:([0-9_a-zA-Z*]+) +)?(?:([0-9_a-zA- +Z*]+) +)?([0-9_a-zA-Z*]+)\((.+?)\)/) { my ($a, $b, $c, $d, $e) = ("") x 5; if (defined($1)) { $a = $1; } if (defined($2)) { $b = $2; } if (defined($3)) { $c = $3; } if (defined($4)) { $d = $4; } if (defined($5)) { $e = $5; } print $a . " " . $b . " " . $c . " " . $d . " ARGS: " . $e . +"\n"; } }
This only works with parsing one row (aka static int sfs_fill_super(struct super_block *sb, void *data, int silent) ), but its a start. { is always on a new line after the function (good old C coding style apparently :) ). I got tips to use C::Scan, but that that lacks documentation, and it seem to need some external application named "cppstdin". (Im using Windows here). So, I was wondering if anyone here had some smart way to solve this problem. I know there are some bright people here! ;)

Later Im gonna add for parsing Perl scripts aswell, but that is alot easier.

Thanks,
Ace

In reply to Parsing C Source File Functions. by Ace128

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.