- fix: Parser can't correctly differentiate function and var initialization.

This commit is contained in:
Roy Qu 2023-02-06 19:37:01 +08:00
parent 6081054f89
commit 1b15f3eb1d
2 changed files with 42 additions and 21 deletions

View File

@ -4,6 +4,7 @@ Red Panda C++ Version 2.11
- enhancement: Auto suggest keyword "operator" when define functions. - enhancement: Auto suggest keyword "operator" when define functions.
- enhancement: Differentiate class and constructors in syntax color and jupming to declarations. - enhancement: Differentiate class and constructors in syntax color and jupming to declarations.
- enhancement: Improve parsing for operator overloading. - enhancement: Improve parsing for operator overloading.
- fix: Parser can't correctly differentiate function and var initialization.
Red Panda C++ Version 2.10 Red Panda C++ Version 2.10

View File

@ -1463,8 +1463,8 @@ void CppParser::addSoloScopeLevel(PStatement& statement, int line, bool shouldRe
mClassScope = StatementClassScope::Public; // structs are public by default mClassScope = StatementClassScope::Public; // structs are public by default
mCurrentClassScope.append(mClassScope); mCurrentClassScope.append(mClassScope);
#ifdef QT_DEBUG #ifdef QT_DEBUG
if (mCurrentClassScope.count()<=2) // if (mCurrentClassScope.count()<=2)
qDebug()<<"++add scope"<<mCurrentFile<<line<<mCurrentClassScope.count(); // qDebug()<<"++add scope"<<mCurrentFile<<line<<mCurrentClassScope.count();
#endif #endif
} }
@ -1474,8 +1474,8 @@ void CppParser::removeScopeLevel(int line)
if (mCurrentScope.isEmpty()) if (mCurrentScope.isEmpty())
return; // TODO: should be an exception return; // TODO: should be an exception
#ifdef QT_DEBUG #ifdef QT_DEBUG
if (mCurrentClassScope.count()<=2) // if (mCurrentClassScope.count()<=2)
qDebug()<<"--remove scope"<<mCurrentFile<<line<<mCurrentClassScope.count(); // qDebug()<<"--remove scope"<<mCurrentFile<<line<<mCurrentClassScope.count();
#endif #endif
PStatement currentScope = getCurrentScope(); PStatement currentScope = getCurrentScope();
PFileIncludes fileIncludes = mPreprocessor.includesList().value(mCurrentFile); PFileIncludes fileIncludes = mPreprocessor.includesList().value(mCurrentFile);
@ -1802,7 +1802,7 @@ void CppParser::checkAndHandleMethodOrVar(KeywordType keywordType)
} }
} else if (mTokenizer[mIndex]->text.startsWith('*') } else if (mTokenizer[mIndex]->text.startsWith('*')
|| mTokenizer[mIndex]->text.startsWith('&') || mTokenizer[mIndex]->text.startsWith('&')
|| mTokenizer[mIndex]->text.startsWith("::") || mTokenizer[mIndex]->text=="::"
|| tokenIsIdentifier(mTokenizer[mIndex]->text) || tokenIsIdentifier(mTokenizer[mIndex]->text)
) { ) {
// it should be function/var // it should be function/var
@ -1858,18 +1858,23 @@ void CppParser::checkAndHandleMethodOrVar(KeywordType keywordType)
handleVar(sType+" "+sName,isExtern,isStatic); handleVar(sType+" "+sName,isExtern,isStatic);
return; return;
} }
if (mTokenizer[indexAfter]->text[0] == ';') { if (mTokenizer[indexAfter]->text[0] == ';' && sType!="void") {
//function can only be defined in global/namespaces/classes //function can only be defined in global/namespaces/classes
PStatement currentScope=getCurrentScope(); PStatement currentScope=getCurrentScope();
if (currentScope) { if (currentScope) {
// if (mTokenizer[mIndex]->text=="upper_bound"
// && this->mCurrentFile.endsWith("algorithmfwd.h")){
// qDebug()<<"!!!!"<<isNotFuncArgs(mIndex + 1);
// }
//in namespace, it might be function or object initilization //in namespace, it might be function or object initilization
if (currentScope->kind == StatementKind::skNamespace if (currentScope->kind == StatementKind::skNamespace) {
&& isNotFuncArgs(mIndex + 1)) { if (isNotFuncArgs(mIndex + 1)) {
// var decl with init // var decl with init
handleVar(sType+" "+sName,isExtern,isStatic); handleVar(sType+" "+sName,isExtern,isStatic);
return; return;
//not in class, it can't be a valid function definition }
} else if (currentScope->kind != StatementKind::skClass) { } else if (currentScope->kind != StatementKind::skClass) {
//not in class, it can't be a valid function definition
// var decl with init // var decl with init
handleVar(sType+" "+sName,isExtern,isStatic); handleVar(sType+" "+sName,isExtern,isStatic);
return; return;
@ -1881,7 +1886,15 @@ void CppParser::checkAndHandleMethodOrVar(KeywordType keywordType)
return; return;
} }
} }
sName = sName+mTokenizer[mIndex]->text; if (!sName.isEmpty()) {
if (sName.endsWith("::"))
sName+=mTokenizer[mIndex]->text;
else {
sType += " "+sName;
sName = mTokenizer[mIndex]->text;
}
} else
sName = mTokenizer[mIndex]->text;
mIndex++; mIndex++;
handleMethod(StatementKind::skFunction,sType, handleMethod(StatementKind::skFunction,sType,
@ -1897,11 +1910,17 @@ void CppParser::checkAndHandleMethodOrVar(KeywordType keywordType)
handleVar(sType+" "+sName,isExtern,isStatic); handleVar(sType+" "+sName,isExtern,isStatic);
return; return;
} else if ( mTokenizer[mIndex + 1]->text == "::") { } else if ( mTokenizer[mIndex + 1]->text == "::") {
sName = sName + mTokenizer[mIndex]->text+mTokenizer[mIndex+1]->text; sName = sName + mTokenizer[mIndex]->text+ "::";
mIndex+=2; mIndex+=2;
} else { } else {
QString s = mTokenizer[mIndex]->text; QString s = mTokenizer[mIndex]->text;
if (sName.isEmpty()) { if (sName.endsWith("::")) {
sName+=s;
} else {
if (!sName.isEmpty()) {
sType = sType+" "+sName;
sName = "";
}
if (s == "static") if (s == "static")
isStatic = true; isStatic = true;
else if (s == "friend") else if (s == "friend")
@ -1910,8 +1929,6 @@ void CppParser::checkAndHandleMethodOrVar(KeywordType keywordType)
isExtern = true; isExtern = true;
if (!s.isEmpty() && !(s=="extern")) if (!s.isEmpty() && !(s=="extern"))
sType = sType + ' '+ s; sType = sType + ' '+ s;
} else {
sName += s;
} }
mIndex++; mIndex++;
} }
@ -5270,10 +5287,10 @@ bool CppParser::isNotFuncArgs(int startIndex)
return true; return true;
if (isIdentChar(ch)) { if (isIdentChar(ch)) {
QString currentText=mTokenizer[i]->text; QString currentText=mTokenizer[i]->text;
if (mTokenizer[i]->text.endsWith('.')) // if (mTokenizer[i]->text.endsWith('.'))
return true; // return true;
if (mTokenizer[i]->text.endsWith("->")) // if (mTokenizer[i]->text.endsWith("->"))
return true; // return true;
if (!mCppTypeKeywords.contains(currentText)) { if (!mCppTypeKeywords.contains(currentText)) {
if (currentText=="true" || currentText=="false" || currentText=="nullptr" || if (currentText=="true" || currentText=="false" || currentText=="nullptr" ||
currentText=="this") currentText=="this")
@ -5285,6 +5302,9 @@ bool CppParser::isNotFuncArgs(int startIndex)
return false; return false;
PStatement statement =findStatementOf(mCurrentFile,word,getCurrentScope(),true); PStatement statement =findStatementOf(mCurrentFile,word,getCurrentScope(),true);
//template arguments
if (!statement)
return false;
if (statement && isTypeStatement(statement->kind)) if (statement && isTypeStatement(statement->kind))
return false; return false;
} else { } else {