Jump to content

PHP regex to match function declaration in .h file


Donwey

Recommended Posts

Hey all, Ive been lately dealing with a problem how to properly detect c function declarations in .h file (which is C99 standard) and save its info ....

 

 

Suppose we have a .h file consisting of 3 lines:

 

struct SymbolTable;
void initContext(Context *pt);         (<----- function 1)
void deleteContext(Context *pt);

 

 

i need a regular expresions to detect those function declarations and possibly save its info accordingly to this: 

 

$function1type == void
$function1name == initContext
$function1params
== Context *pt

 

Is it possible? Ive been struggling a lot with this but cant get it to work... In a .h file i have already removed blocs and macros and comments...

 

 

Try

// header file
$linesInCode = file('yourheaderfile.h');

// function data will be stored in this array
$functions = array();

// loop over the lines in the file
foreach($linesInCode as $line)
{
    // if a function signature has been found
    if(preg_match('~(\w+)\s+(\w+)\(([^\)]+)\);~i', $line, $match))
    {
        // grab the function type, name and parameters into an array
        $function = array();
        $function['type'] = $match[1];
        $function['name'] = $match[2];
        $function['params'] = explode(',', $match[3]); // split the function parameters by the comma
        
        // append function info to $functions array
        $functions[] = $function;
    }
}
// display structure of $functions array
printf('<pre>%s></pre>', print_r($functions, 1));

Output with your test code

Array
(
    [0] => Array
        (
            [type] => void
            [name] => initContext
            [params] => Array
                (
                    [0] => Context *pt
                )

        )

    [1] => Array
        (
            [type] => void
            [name] => deleteContext
            [params] => Array
                (
                    [0] => Context *pt
                )

        )

)

Wow Thanks for answering so quickly , tryed it, seems to work ... but it should work with all function declarations you can type in C99 standard for example : 

char *stringGet(tString *str);

 

schould return 

 

[type]--> char * 

[name] => stringGet
[params] => Array
(
[0] =>tString *str
)

 
But its not detecting it so it ignores it :/  Is it possible to detect it?
EDIT : 
somehow made it work with : (\w+\s+\W)(\w+)\(([^\)]+)\);       But iam not sure about what other declarations i can get and if the regex  will detect them.  Is it possible to make regex even b

etter to make sure it will detect all possible C99 function declarations?

 

 

But iam not sure about what other declarations i can get and if the regex  will detect them.  Is it possible to make regex even better to make sure it will detect all possible C99 function declarations?

I am not familiar with C so not sure what the C99 standards are. I only wrote the regex so it matched the example function declarations you had posted.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.