正则表达式解析 C/C++ 函数声明

Regex to parse C/C++ functions declarations(正则表达式解析 C/C++ 函数声明)
本文介绍了正则表达式解析 C/C++ 函数声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要将 C 和 C++ 函数解析并拆分为主要组件(返回类型、函数名称/类和方法、参数等).

I need to parse and split C and C++ functions into the main components (return type, function name/class and method, parameters, etc).

我正在使用签名采用以下形式的标题或列表:

I'm working from either headers or a list where the signatures take the form:

public: void __thiscall myClass::method(int, class myOtherClass * )

public: void __thiscall myClass::method(int, class myOtherClass * )

我有以下正则表达式,它适用于大多数功能:

I have the following regex, which works for most functions:

(?<expo>public:|protected:|private:) (?<ret>(const )*(void|int|unsigned int|long|unsigned long|float|double|(class .*)|(enum .*))) (?<decl>__thiscall|__cdecl|__stdcall|__fastcall|__clrcall) (?<ns>.*)::(?<class>(.*)((<.*>)*))::(?<method>(.*)((<.*>)*))((?<params>((.*(<.*>)?)(,)?)*))

它不喜欢解析一些函数,但似乎与模式匹配.我现在不担心匹配不是类成员的函数(以后可以处理).该表达式用于 C# 程序中,因此 <label> 用于轻松检索组.

There are a few functions that it doesn't like to parse, but appear to match the pattern. I'm not worried about matching functions that aren't members of a class at the moment (can handle that later). The expression is used in a C# program, so the <label>s are for easily retrieving the groups.

我想知道是否有标准的正则表达式来解析所有函数,或者如何改进我的以处理奇怪的异常?

I'm wondering if there is a standard regex to parse all functions, or how to improve mine to handle the odd exceptions?

推荐答案

众所周知,C++ 很难解析;编写一个捕获所有情况的正则表达式是不可能的.例如,可以有无限数量的嵌套括号,这表明即使是 C++ 语言的这个子集也不是规则的.

C++ is notoriously hard to parse; it is impossible to write a regex that catches all cases. For example, there can be an unlimited number of nested parentheses, which shows that even this subset of the C++ language is not regular.

但您似乎追求的是实用性,而不是理论上的正确性.只需不断改进您的正则表达式,直到它捕获需要捕获的情况,并尝试使其尽可能严格,以免出现任何错误匹配.

But it seems that you're going for practicality, not theoretical correctness. Just keep improving your regex until it catches the cases it needs to catch, and try to make it as stringent as possible so you don't get any false matches.

如果不知道它没有捕获的奇怪的异常",很难说如何改进正则表达式.

Without knowing the "odd exceptions" that it doesn't catch, it's hard to say how to improve the regex.

这篇关于正则表达式解析 C/C++ 函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)