How to search for (void) from the line.
[snip]
and be mixed with comments. A better solution is to get a C++ parser somewhere.
But, if this isn't real source code text and all you want to do is parse anything
in (void) "function(anything)" on a line by line basis, I guess you could adopt a quick and
dirty approach, something like this:
while (<DATA>) # no caching of lines, raw per-line basis
{
chomp;
if (/(\s*void\s*)\s*([a-z][\w-]*?\s*\(.*\)\s*)/ { # greedy () in liue of balanced parenthesis
^^^
I forgot to mention this is untested. After looking at it a bit, this may be better:
/\(\s*void\s*\)\s*([a-zA-Z][\w-]*\s*\(.*\))/
Gee, there are alot of paren's in this one..
sln- Hide quoted text -
- Show quoted text -
HI
yes this is not a real example.
the acually the pattern will be of type
1. (void) function(....)
2. function(....)
3. return (function(....)........)
I want to extract the function(....) from the above type of lines.
Thanks & Regards
Gururaja
Oh, I needed something to do for a few hours.
I guess the below should parse C/C++ functions any way desired.
sln
---------------------------------------------------------
## Idea - To parse out C/C++ style functions
## that have parenthetical closures (some don't)
## ===============================================
use strict;
use warnings;
my $Source = join '', <DATA>;
my @Funct = (); # Holds sequential functions info;
# ie: substr indexes to 'name', '(', and ')' segment positions
my @Ndx = (); # Index stack of current @Funct elements in find recursion
my $Preamble = "\\s*\\("; # constant
my $Compliance = "_*[a-zA-Z][\\w]*"; # constant
my $FName = $Compliance; # Gets all functions, uses the compliant pattern
#my $FName = "\\(\\s*void\\s*\\)\\s*function"; # Extended, non-compliant function pattern
# ***************************************
# Or, uncomment these to search for a specific function name that is compliant.
# (Warning! FName should resolve to a compliant function name)
# If you wish to use a custom regex for FName (non-compliant), skip this block and enter FName above.
# -------------------------
# my $FName = "atoi";
# die "Function name pattern: '$FName' is non-compliant!\n" if ($FName !~ /$Compliance/);
# ***************************************
# The main pre-compiled parser regular expression
my $FxParse = qr/(\/{2}.*?\n)|(\/\*.*?\*\/)|($FName$Preamble)|(\()|(\))/s;
# 1 1|2 2|3 3|4 4|5 5
# Parse out some functions
Find_Function(\$Source);
# Print functions found
if (!@Funct) { print "Function name pattern: '$FName' not found!\n" }
else { print "\nFound ".@Funct." matches.\nFunction pattern: '$FName' \n" }
for my $ref (@Funct)
{
# print "@{$ref}\n";
print "\n\@: ";
print substr($Source,$ref->[0],$ref->[2]-$ref->[0]),"\n";
# Here, each function name and/or parameter can be printed
# and/or examined separately.
}
# Finished!
#
# ---------------------------------------------------------
# Recursive procedure that finds C/C++ style functions
# (the engine)
#
sub Find_Function
{
my ($src,$spos,$closure) = @_;
$spos = 0 if (!defined $spos);
$closure = 0 if (!defined $closure);
pos($$src) = $spos;
while ($$src =~ /$FxParse/g)
{
# $1/$2 - comments
if (defined $3) # 'function name'
{
push @Ndx, scalar(@Funct);
# positions: function ( )
push @Funct , [$-[0], pos($$src), 0];
# recurse this procedure
pos($$src) = Find_Function ( $src, pos($$src), 1 );
}
elsif (defined $4) # '('
{
++$closure;
}
elsif (defined $5) # ')'
{
--$closure;
if ($closure <= 0)
{
if (@Ndx)
{
$Funct[pop @Ndx]->[2] = pos($$src);
return pos($$src);
}
$closure = 0;
}
}
}
}
__DATA__
1. (void) function(....)
2. function(....)
3. return ( // some comments
function(..(atoi("398"), (5 * x)..)..,
/* ) <-forget this parenth */......)
)
---------------------------------------------
Do not copy from here down...
---------------------------------------------
Some various input/output:
c:\temp>perl c_functionparser.pl
Found 5 matches.
Function pattern: '_*[a-zA-Z][\w]*'
@: function(....)
@: function(....)
@: return ( // some comments
function(..(atoi("398"), (5 * x)..)..,
/* ) <-forget this parenth */......)
)
@: function(..(atoi("398"), (5 * x)..)..,
/* ) <-forget this parenth */......)
@: atoi("398")
c:\temp>
----------------------------
c:\temp>perl c_functionparser.pl
Found 3 matches.
Function pattern: 'function'
@: function(....)
@: function(....)
@: function(..(atoi("398"), (5 * x)..)..,
/* ) <-forget this parenth */......)
c:\temp>
---------------------------
c:\temp>perl c_functionparser.pl
Found 1 matches.
Function pattern: 'atoi'
@: atoi("398")
c:\temp>
---------------------------
c:\temp>perl c_functionparser.pl
Found 1 matches.
Function pattern: '\(\s*void\s*\)\s*function'
@: (void) function(....)
c:\temp>