简体   繁体   English

正则表达式解析C ++枚举

[英]Regex to parse C++ enum

How can a regular expression be constructed to parse C++ enums? 如何构造正则表达式来解析C ++枚举? 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? 任何可以帮助我的regexp向导?

Edit: to clarify, I want the name and value, eg C and 0. 编辑:澄清,我想要名称和价值,例如C和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: 假设只给出{和}之间的文本,它会捕获所有名称和相应的值:

/(\w+)\s*(?:=\s*(\d+)|)\s*,?\s*(?:(?:\n|$)|\/\/.*?(?:\n|$)|)/

If we use regex to match enum rather than use it to parse enum. 如果我们使用正则表达式匹配枚举而不是用它来解析枚举。 I think it is possible. 我认为这是可能的。 try with these steps: 尝试这些步骤:

step1. 第1步。 make sure the C/C++ source code can be compile successful. 确保C / C ++源代码可以编译成功。
step2. 第2步。 strip all comments from the C/C++ source code. 从C / C ++源代码中删除所有注释。
step3. 第3步。 match enum 匹配枚举

a workable Ruby sample code: 一个可行的Ruby示例代码:

# copy from Mastering Regular Expression 3rd
COMMENT = '/\*[^\*]*\*+(?:[^/*][^*]*\*+)*/'
COMMENT2 = '//[^\n]+'
DOUBLE = '"(?:\\.|[^\\"])*"'
SINGLE = '\'(?:\\.|[^\\\'])*\''
# pattern for match enum
ENUM = '\benum\s*(\w+)\s*\{(\s*\w+(?:\s*=\s*\w+)?(?:\s*,\s*\w+(?:\s*=\s*\w+)?)*)\s*(?:,\s*)?\}\s*\w+\s*;'

foo = File.open("foo.cpp", "r").read()
# strip all comments from foo.cpp
foo.gsub!(/(#{DOUBLE}|#{SINGLE})|#{COMMENT}|#{COMMENT2}/, '\1')
bar = []
# match enum...
foo.scan(/#{ENUM}/) do | m |
    printf("%s: %s\n", m[0], m[1].gsub(/\s/, ''))

end

output: 输出:

Temperature: C=0,F=1,R,K
Depth: m=0,ft=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM