关键代码
// 解析为函数
bool FunctionCreator::parse(const QString& lineFunc) {
auto trimFunc = lineFunc.trimmed();
auto list = trimFunc.split(" ");
bool bHasReturn = false;
// 返回值和函数名解析
for (const auto& key : list) {
auto trimKey = key.trimmed();
if(trimKey.isEmpty())
continue;
if("virtual" == trimKey.toLower())
continue;
if("void" == trimKey.toLower()) {
bHasReturn = true;
continue;
}
if(!bHasReturn) {
if(m_returnType.decoration.isEmpty()) {
m_returnType.decoration += trimKey;
continue;
} else {
// 返回值数据类型扩展,首个数据可能是类型修饰符
if(trimKey.endsWith('*')) {
m_returnType.decoration += " " + trimKey;
bHasReturn = true;
continue;
}
if(trimKey.endsWith('&')) {
m_returnType.decoration += " " + trimKey;
bHasReturn = true;
continue;
}
if(trimKey.endsWith(']')) {
m_returnType.decoration += " " + trimKey;
bHasReturn = true;
continue;
}
if(trimKey.startsWith("const")) {
m_returnType.decoration += " " + trimKey;
continue;
}
if(trimKey.startsWith("volatile")) {
m_returnType.decoration += " " + trimKey;
continue;
}
const auto& typeKeys = FunctionCreator::typeKeys();
for (const auto& type : typeKeys) {
bHasReturn = m_returnType.decoration.contains(type);
if(bHasReturn)
break; // 有基础数据类型,返回值有效
}
}
}
// 匹配首个大写字母
auto paramStartPos = trimKey.indexOf('(');
if(-1 == paramStartPos)
break;
QRegularExpression regex("[A-Z]");
QRegularExpressionMatch match = regex.match(trimKey);
if (match.hasMatch() && match.capturedStart() < paramStartPos) {
m_prefixName = trimKey.mid(0, match.capturedStart());
m_name = trimKey.mid(match.capturedStart(), paramStartPos - match.capturedStart());
} else {
m_name = trimKey.mid(0, paramStartPos);
}
break;
}
// 函数参数解析
auto paramStartPos = lineFunc.indexOf('(');
auto paramEndPos = lineFunc.lastIndexOf(')');
if(-1 == paramStartPos || -1 == paramEndPos)
return false;
auto params = lineFunc.mid(paramStartPos + 1, paramEndPos - paramStartPos - 1).split(',');
Param var;
for (const auto& param : params) {
auto trimParam = param.trimmed();
if(trimParam.isEmpty())
continue;
auto varNameStartIndex = trimParam.lastIndexOf(' ');
if(-1 == varNameStartIndex) {
var.type.decoration = trimParam;
continue;
}
var.type.decoration = trimParam.mid(0, varNameStartIndex).trimmed();
var.name = trimParam.mid(varNameStartIndex).trimmed();
addParam(var);
}
// 函数尾部解析
auto lastSuffix = lineFunc.mid(paramEndPos);
auto lastSuffixs = lastSuffix.split(' ');
for (const auto& suffix : lastSuffixs) {
auto trimSuffix = suffix.trimmed();
if(trimSuffix.isEmpty())
continue;
if("const" == trimSuffix.toLower())
m_lastSuffixs.emplace_back(trimSuffix.toLower());
}
return true;
}
解析前
解析后
C++自定义接口类设计器-CSDN博客
创作不易,小小的支持一下吧!