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...

 

 

Link to comment
Share on other sites

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
                )

        )

)
Link to comment
Share on other sites

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?
Edited by Donwey
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

 

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.