How can a regular expression be constructed to parse C++ enums? The enums I tried on looked like
enum Temperature
{
C = 0,
F=1, // some elements are commented
R, // most elements are not gived a value
K // sometimes the last element is succeeded by a comma
} temperature;
// different indent style is used
enum Depth {
m = 0,
ft = 1,
} depth;
I tried several simple patterns but none is general enough to catch all cases above.
Any regexp wizard who can help me?
Edit: to clarify, I want the name and value, e.g. C and 0.
That was challenging :) Below is the best I could come up with. Assuming it is given just the text between { and } it captures all names and corresponding values:
If we use regex to match enum rather than use it to parse enum. I think it is possible. try with these steps:
step1. make sure the C/C++ source code can be compile successful.
step2. strip all comments from the C/C++ source code.
step3. match enum
a workable Ruby sample code:
output: