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

发布于 2024-11-30 17:25:39 字数 453 浏览 2 评论 0原文

如何构造正则表达式来解析 C++ 枚举? 我尝试过的枚举看起来像是

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;

我尝试了几种简单的模式,但没有一个足够通用来捕获上述所有情况。

任何正则表达式向导可以帮助我吗?

编辑:澄清一下,我想要名称和值,例如 C 和 0。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熟人话多 2024-12-07 17:25:39

这很有挑战性:)以下是我能想到的最好的。假设只给出了 { 和 } 之间的文本,它会捕获所有名称和相应的值:

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

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|$)|)/
嘦怹 2024-12-07 17:25:39

如果我们使用正则表达式来匹配枚举而不是使用它来解析枚举。我认为这是可能的。尝试执行以下步骤:

步骤 1。确保C/C++源代码能够编译成功。
步骤2。从 C/C++ 源代码中删除所有注释。
步骤3。匹配枚举

一个可行的 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

输出:

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

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:

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文