views:

96

answers:

1

I've been playing around with GEdit's syntax highlighting. I love the way that Visual Studio highlight's user created types. I would like to do this for my user created types in C/C++ (eg. typedef's/classes). For example (in C):

typedef struct Node *pNode;

And an example in C++:

class BigNumber 
{ 
    // Class stuff here.
};

See the way Node is highlighted differntly from typedef struct (keywords) and *pNode isn't highlighted at all. How can I write a regex to detect that, and highlight all occurrences of Node and BigNumber throughout my current document?

+3  A: 

While Regex's will give you good results, they won't ever give you perfect results.

Most regex engines do not support the notion of recursion. That is, it cannot match any type of expression which requires counting (matched braces, parens, etc ...). This means it will not be able to match a typedef which points to a function pointer in a reliable fashion.

To get perfect matches you really need to write a parser.

I think a better approach is to pick the scenarios you care about the most and write regex's which target those specific scenarios.

For instance here is a regex which will match typedefs of structs which point to a single name and may or may not have a pointer.

"^\s*typedef\s+struct\s+\w+\s+((\*?\s*)\w+)\s*;\s*$"
JaredPar
That sounds like a great idea for a GEdit plugin. Thanks!
Lucas McCoy