From fafe45cb413f7d76a6f39fece76cbf7df46ee317 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Sun, 15 Aug 2021 20:25:54 +0800 Subject: [PATCH] work save --- RedPandaIDE/parser/cppparser.cpp | 136 ++++++++++++++++++++++++++++++- RedPandaIDE/parser/cppparser.h | 8 +- 2 files changed, 140 insertions(+), 4 deletions(-) diff --git a/RedPandaIDE/parser/cppparser.cpp b/RedPandaIDE/parser/cppparser.cpp index 663b1618..07ff6b58 100644 --- a/RedPandaIDE/parser/cppparser.cpp +++ b/RedPandaIDE/parser/cppparser.cpp @@ -328,6 +328,73 @@ bool CppParser::checkForScope() ); } +void CppParser::checkForSkipStatement() +{ + if ((mSkipList.count()>0) && (mIndex == mSkipList.back())) { // skip to next ';' + do { + mIndex++; + } while ((mIndex < mTokenizer.tokenCount()) && (mTokenizer[mIndex]->text[0] != ';')); + mIndex++; //skip ';' + mSkipList.pop_back(); + } +} + +bool CppParser::CheckForStructs() +{ + int dis = 0; + if ((mTokenizer[mIndex]->text == "friend") + || (mTokenizer[mIndex]->text == "public") + || (mTokenizer[mIndex]->text == "private")) + dis = 1; + if (mIndex >= mTokenizer.tokenCount() - 2 - dis) + return false; + int keyLen = -1; + QString word = mTokenizer[mIndex+dis]->text; + if (word.startsWith("struct")) + keyLen = 6; + else if (word.startsWith("class") + || word.startsWith("union")) + keyLen = 5; + if (keyLen<0) + return false; + bool result = (word.length() == keyLen) || isSpaceChar(word[keyLen] == ' ') + || (word[keyLen] == '['); + + if (result) { + if (mTokenizer[mIndex + 2+dis]->text[0] != ';') { // not: class something; + int i = mIndex+dis +1; + // the check for ']' was added because of this example: + // struct option long_options[] = { + // {"debug", 1, 0, 'D'}, + // {"info", 0, 0, 'i'}, + // ... + // }; + while (i < mTokenizer.tokenCount()) { + QChar ch = mTokenizer[i]->text.back(); + if (ch=='{' || ch == ':') + break; + switch(ch.unicode()) { + case ';': + case '}': + case ',': + case ')': + case ']': + case '=': + case '*': + case '&': + case '%': + case '+': + case '-': + case '~': + return false; + } + i++; + } + } + } + return result; +} + void CppParser::calculateFilesToBeReparsed(const QString &fileName, QStringList &files) { if (fileName.isEmpty()) @@ -410,7 +477,7 @@ QString CppParser::removeArgNames(const QString &args) } break; default: - if (isLetterChar(args[i]) || isDigitChar(args[i])) + if (isWordChar(args[i]) || isDigitChar(args[i])) word+=args[i]; } i++; @@ -431,7 +498,7 @@ bool CppParser::isSpaceChar(const QChar &ch) return ch==' ' || ch =='\t'; } -bool CppParser::isLetterChar(const QChar &ch) +bool CppParser::isWordChar(const QChar &ch) { return (ch>= 'A' && ch<='Z') || (ch>='a' && ch<='z') @@ -440,13 +507,20 @@ bool CppParser::isLetterChar(const QChar &ch) || ch == '&'; } +bool CppParser::isLetterChar(const QChar &ch) +{ + return (ch>= 'A' && ch<='Z') + || (ch>='a' && ch<='z') + || ch == '_'; +} + bool CppParser::isDigitChar(const QChar &ch) { return (ch>='0' && ch<='9'); } bool CppParser::isSeperator(const QChar &ch) { - switch(ch){ + switch(ch.unicode()){ case '(': case ';': case ':': @@ -458,3 +532,59 @@ bool CppParser::isSeperator(const QChar &ch) { return false; } } + +bool CppParser::isLineChar(const QChar &ch) +{ + return ch=='\n' || ch=='\r'; +} + +bool CppParser::isNotFuncArgs(const QString &args) +{ + bool result = true; + int i=1; //skip '(' + int endPos = args.length()-1;//skip ')' + bool lastCharIsId=false; + QString word = ""; + while (iendPos && word.isEmpty()) { + return false; + } + + if (isKeyword(word)) { + return word == "true" || word == "false" || word == "nullptr"; + } + PStatement statement =findStatementOf(mCurrentFile,word,getCurrentScope(),true); + if (statement && + !(statement->kind == StatementKind::skClass + || statement->kind == StatementKind::skTypedef + || statement->kind == StatementKind::skEnum + || statement->kind == StatementKind::skEnumType)) + return true; + return false; +} diff --git a/RedPandaIDE/parser/cppparser.h b/RedPandaIDE/parser/cppparser.h index e66de035..1cd46ad7 100644 --- a/RedPandaIDE/parser/cppparser.h +++ b/RedPandaIDE/parser/cppparser.h @@ -138,7 +138,6 @@ private: bool isInCurrentScopeLevel(const QString& command); void addSoloScopeLevel(PStatement statement, int line); // adds new solo level void removeScopeLevel(int line); // removes level - void checkForSkipStatement(); int skipBraces(int startAt); int skipBracket(int startAt); bool checkForCatchBlock(); @@ -150,6 +149,7 @@ private: bool checkForNamespace(); bool checkForPreprocessor(); bool checkForScope(); + void checkForSkipStatement(); bool CheckForStructs(); bool checkForTypedef(); bool checkForTypedefEnum(); @@ -230,6 +230,8 @@ private: bool isSpaceChar(const QChar& ch); + bool isWordChar(const QChar& ch); + bool isLetterChar(const QChar& ch); bool isDigitChar(const QChar& ch); @@ -237,6 +239,10 @@ private: /*'(', ';', ':', '{', '}', '#' */ bool isSeperator(const QChar& ch); + bool isLineChar(const QChar& ch); + + bool isNotFuncArgs(const QString& args); + void onProgress(const QString& fileName, int total, int current); void onBusy(); void onStartParsing();