refactor check for struct/class/union defines in the parser

This commit is contained in:
Roy Qu 2023-03-12 10:50:47 +08:00
parent 16258cc015
commit b246e3d145
4 changed files with 37 additions and 26 deletions

View File

@ -1620,6 +1620,7 @@ bool CppParser::checkForKeyword(KeywordType& keywordType)
case KeywordType::For:
case KeywordType::Public:
case KeywordType::Private:
case KeywordType::Struct:
case KeywordType::Enum:
case KeywordType::Inline:
case KeywordType::Namespace:
@ -1674,16 +1675,21 @@ bool CppParser::checkForStructs(KeywordType keywordType)
|| keywordType == KeywordType::Public
|| keywordType == KeywordType::Private)
dis = 1;
if (mIndex >= mTokenizer.tokenCount() - 2 - dis)
return false;
QString word = mTokenizer[mIndex+dis]->text;
int keyLen = calcKeyLenForStruct(word);
if (keyLen<0)
return false;
bool result = (word.length() == keyLen) || isSpaceChar(word[keyLen])
|| (word[keyLen] == '[');
// int keyLen = calcKeyLenForStruct(word);
// if (keyLen<0)
// return false;
// bool result = (word.length() == keyLen) || isSpaceChar(word[keyLen])
// || (word[keyLen] == '[');
bool result;
if (dis!=0) {
result = (mCppKeywords.value(mTokenizer[mIndex+dis]->text,KeywordType::None)==KeywordType::Struct);
} else {
result = (keywordType==KeywordType::Struct);
}
if (result) {
if (mIndex >= mTokenizer.tokenCount() - 2 - dis)
return false;
if (mTokenizer[mIndex + 2+dis]->text[0] != ';') { // not: class something;
int i = mIndex+dis +1;
// the check for ']' was added because of this example:
@ -1735,11 +1741,13 @@ bool CppParser::checkForTypedefStruct()
//should call CheckForTypedef first!!!
if (mIndex+1 >= mTokenizer.tokenCount())
return false;
QString word = mTokenizer[mIndex + 1]->text;
int keyLen = calcKeyLenForStruct(word);
if (keyLen<0)
return false;
return (word.length() == keyLen) || isSpaceChar(word[keyLen]) || word[keyLen]=='[';
return (mCppKeywords.value(mTokenizer[mIndex+1]->text,KeywordType::None)==KeywordType::Struct);
// QString word = mTokenizer[mIndex + 1]->text;
// int keyLen = calcKeyLenForStruct(word);
// if (keyLen<0)
// return false;
// return (word.length() == keyLen) || isSpaceChar(word[keyLen]) || word[keyLen]=='[';
}
bool CppParser::checkForUsing(KeywordType keywordType)
@ -5260,15 +5268,15 @@ QSet<QString> CppParser::calculateFilesToBeReparsed(const QString &fileName)
return result;
}
int CppParser::calcKeyLenForStruct(const QString &word)
{
if (word.startsWith("struct"))
return 6;
else if (word.startsWith("class")
|| word.startsWith("union"))
return 5;
return -1;
}
//int CppParser::calcKeyLenForStruct(const QString &word)
//{
// if (word.startsWith("struct"))
// return 6;
// else if (word.startsWith("class")
// || word.startsWith("union"))
// return 5;
// return -1;
//}
void CppParser::scanMethodArgs(const PStatement& functionStatement, int argStart)
{

View File

@ -501,7 +501,7 @@ private:
void internalInvalidateFile(const QString& fileName);
void internalInvalidateFiles(const QSet<QString>& files);
QSet<QString> calculateFilesToBeReparsed(const QString& fileName);
int calcKeyLenForStruct(const QString& word);
// int calcKeyLenForStruct(const QString& word);
// {
// function GetClass(const Phrase: AnsiString): AnsiString;
// function GetMember(const Phrase: AnsiString): AnsiString;

View File

@ -185,11 +185,13 @@ void initParser()
CppKeywords.insert("extern",KeywordType::None);
// handled elsewhere
CppKeywords.insert("class",KeywordType::None);
CppKeywords.insert("operator",KeywordType::None);
CppKeywords.insert("static",KeywordType::None);
CppKeywords.insert("struct",KeywordType::None);
CppKeywords.insert("union",KeywordType::None);
//struct/class/union
CppKeywords.insert("class",KeywordType::Struct);
CppKeywords.insert("struct",KeywordType::Struct);
CppKeywords.insert("union",KeywordType::Struct);
CppKeywords.insert("for",KeywordType::For);

View File

@ -69,6 +69,7 @@ enum class KeywordType {
Private,
Protected,
Friend,
Struct, // struct/class/enum
Enum, //enum
Inline, // inline
Namespace, //namespace