RedPanda-CPP/RedPandaIDE/parser/parserutils.h

278 lines
8.5 KiB
C
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-08-05 23:13:21 +08:00
#ifndef PARSER_UTILS_H
#define PARSER_UTILS_H
2021-08-06 11:28:02 +08:00
#include <QMap>
2021-08-05 23:13:21 +08:00
#include <QObject>
2021-08-06 10:58:24 +08:00
#include <QSet>
2022-01-04 16:50:54 +08:00
#include <QVector>
2021-08-05 23:13:21 +08:00
#include <memory>
2021-08-24 15:05:10 +08:00
enum class ParserLanguage {
C,
CPlusPlus
};
struct CodeSnippet {
2021-08-24 15:05:10 +08:00
QString caption; //Name
QString prefix; //Prefix used in code suggestion
QString code; //Code body
QString desc; //Description
int section; //Section in the menu
};
2021-09-30 12:52:22 +08:00
using PCodeSnippet = std::shared_ptr<CodeSnippet>;
2021-08-24 15:05:10 +08:00
2021-08-05 23:13:21 +08:00
// preprocess/ macro define
struct Define {
2021-08-07 18:02:57 +08:00
QString name;
2021-08-05 23:13:21 +08:00
QString args;
QString value;
QString filename;
bool hardCoded;// if true, don't free memory (points to hard defines)
QStringList argList; // args list to format values
2021-08-27 16:38:55 +08:00
QList<bool> argUsed;
2021-08-05 23:13:21 +08:00
QString formatValue; // format template to format values
};
using PDefine = std::shared_ptr<Define>;
2021-08-07 18:02:57 +08:00
using DefineMap = QHash<QString,PDefine>;
using PDefineMap = std::shared_ptr<DefineMap>;
2021-08-05 23:13:21 +08:00
enum class SkipType {
skItself, // skip itself
skToSemicolon, // skip to ;
skToColon, // skip to :
skToRightParenthesis, // skip to )
skToLeftBrace,// Skip to {
skToRightBrace, // skip to }
skNone // It's a keyword but don't process here
};
2021-09-25 23:12:36 +08:00
2021-08-28 09:01:40 +08:00
enum StatementKind {
2021-08-05 23:13:21 +08:00
skUnknown,
skPreprocessor,
skEnumType,
2021-08-18 05:34:04 +08:00
skEnumClassType,
2021-08-05 23:13:21 +08:00
skEnum,
skTypedef,
skClass,
skFunction,
2021-08-07 14:08:51 +08:00
skOperator,
2021-08-05 23:13:21 +08:00
skConstructor,
skDestructor,
skVariable,
skParameter,
skNamespace,
skNamespaceAlias,
skBlock,
2021-09-30 20:10:48 +08:00
skUserCodeSnippet, // user code template
2021-08-05 23:13:21 +08:00
skKeyword, // keywords
skGlobalVariable,
skLocalVariable,
skAlias
};
2021-08-06 10:58:24 +08:00
using StatementKindSet = QSet<StatementKind>;
enum class StatementScope {
ssGlobal,
ssLocal,
ssClassLocal
};
enum class StatementClassScope {
scsNone,
scsPrivate,
scsProtected,
scsPublic
};
enum class MemberOperatorType {
otArrow,
otDot,
otDColon,
otOther
};
struct RemovedStatement{
QString type; // type "int"
QString command; // identifier/name of statement "foo"
int definitionLine; // definition
QString definitionFileName; // definition
QString fullName; // fullname(including class and namespace)
QString noNameArgs; // Args without name
};
2021-12-06 11:37:37 +08:00
enum class EvalStatementKind {
Namespace,
Type,
Variable,
Literal,
Function
};
2021-08-06 10:58:24 +08:00
using PRemovedStatement = std::shared_ptr<RemovedStatement>;
struct StatementMatchPosition{
int start;
int end;
};
using PStatementMathPosition = std::shared_ptr<StatementMatchPosition>;
2021-08-06 10:58:24 +08:00
struct Statement;
using PStatement = std::shared_ptr<Statement>;
2021-08-11 11:47:47 +08:00
using StatementList = QList<PStatement>;
2021-08-07 14:08:51 +08:00
using PStatementList = std::shared_ptr<StatementList>;
2021-08-15 16:49:37 +08:00
using StatementMap = QMultiMap<QString, PStatement>;
2021-08-06 10:58:24 +08:00
struct Statement {
std::weak_ptr<Statement> parentScope; // parent class/struct/namespace scope, don't use auto pointer to prevent circular reference
QString type; // type "int"
QString command; // identifier/name of statement "foo"
QString args; // args "(int a,float b)"
QString value; // Used for macro defines/typedef, "100" in "#defin COUNT 100"
StatementKind kind; // kind of statement class/variable/function/etc
QList<std::weak_ptr<Statement>> inheritanceList; // list of statements this one inherits from, can be nil
StatementScope scope; // global/local/classlocal
StatementClassScope classScope; // protected/private/public
bool hasDefinition; // definiton line/filename is valid
int line; // declaration
int definitionLine; // definition
QString fileName; // declaration
2021-08-06 11:28:02 +08:00
QString definitionFileName; // definition
bool inProject; // statement in project
bool inSystemHeader; // statement in system header (#include <>)
2021-08-07 14:08:51 +08:00
StatementMap children; // functions can be overloaded,so we use list to save children with the same name
2021-08-06 11:28:02 +08:00
QSet<QString> friends; // friend class / functions
bool isStatic; // static function / variable
bool isInherited; // inherted member;
2021-08-07 14:08:51 +08:00
QString fullName; // fullname(including class and namespace), ClassA::foo
2021-08-19 17:08:01 +08:00
QSet<QString> usingList; // using namespaces
2021-08-06 11:28:02 +08:00
QString noNameArgs;// Args without name
// fields for code completion
int usageCount; //Usage Count
int matchPosTotal; // total of matched positions
int matchPosSpan; // distance between the first match pos and the last match pos;
int firstMatchLength; // length of first match;
int caseMatched; // if match with case
QList<PStatementMathPosition> matchPositions;
2021-08-06 11:28:02 +08:00
};
2021-08-07 14:08:51 +08:00
2021-12-06 11:37:37 +08:00
struct EvalStatement;
using PEvalStatement = std::shared_ptr<EvalStatement>;
2021-12-07 08:23:27 +08:00
/**
* @brief Statement for evaluation result
* Ex. (Test*)(y+1)
* it's baseStatement is the statement for y
* it's effetiveTypeStatement is Test
*/
2021-12-06 11:37:37 +08:00
struct EvalStatement {
QString baseType; // type "int"
EvalStatementKind kind; // namespace / type / variable / function / literal
int pointerLevel; // 0 for "int", 1 for "int *", 2 for "int **"...
PStatement baseStatement; // if not literal or primitive type, the base statement
2021-12-07 08:23:27 +08:00
PStatement effectiveTypeStatement;
2021-12-06 11:37:37 +08:00
public:
2021-12-07 14:48:20 +08:00
EvalStatement (const QString& baseType,
2021-12-06 11:37:37 +08:00
EvalStatementKind kind,
const PStatement& baseStatement,
2021-12-07 08:23:27 +08:00
const PStatement& typeStatement,
2021-12-06 11:37:37 +08:00
int pointerLevel = 0);
2021-12-07 08:23:27 +08:00
void assignType(const PEvalStatement& typeStatement);
2021-12-06 11:37:37 +08:00
};
2021-08-07 14:08:51 +08:00
struct UsingNamespace {
QStringList namespaces; // List['std','foo'] for using namespace std::foo;
QString filename;
int line;
bool fromHeader;
};
using PUsingNamespace = std::shared_ptr<UsingNamespace>;
2021-08-22 05:50:26 +08:00
struct CppScope {
int startLine;
PStatement statement;
};
using PCppScope = std::shared_ptr<CppScope>;
class CppScopes {
public:
PStatement findScopeAtLine(int line);
void addScope(int line, PStatement scopeStatement);
PStatement lastScope();
void removeLastScope();
void clear();
private:
QVector<PCppScope> mScopes;
};
2021-08-07 14:08:51 +08:00
struct FileIncludes {
QString baseFile;
2022-06-04 14:44:50 +08:00
QMap<QString, bool> includeFiles; // true means the file is directly included, false means included indirectly
QStringList directIncludes; //
2021-08-07 14:08:51 +08:00
QSet<QString> usings; // namespaces it usings
2021-08-19 23:49:23 +08:00
StatementMap statements; // but we don't save temporary statements (full name as key)
StatementMap declaredStatements; // statements declared in this file (full name as key)
2021-08-22 05:50:26 +08:00
CppScopes scopes; // int is start line of the statement scope
2021-08-07 14:08:51 +08:00
QSet<QString> dependingFiles; // The files I depeneds on
QSet<QString> dependedFiles; // the files depends on me
};
using PFileIncludes = std::shared_ptr<FileIncludes>;
extern QStringList CppDirectives;
extern QStringList JavadocTags;
extern QMap<QString,SkipType> CppKeywords;
extern QSet<QString> CppControlKeyWords;
2021-08-25 00:20:07 +08:00
extern QSet<QString> CKeywords;
2021-08-07 14:08:51 +08:00
extern QSet<QString> CppTypeKeywords;
extern QSet<QString> STLPointers;
extern QSet<QString> STLContainers;
extern QSet<QString> STLElementMethods;
extern QSet<QString> MemberOperators;
extern QSet<QString> IOManipulators;
2021-08-07 14:08:51 +08:00
void initParser();
2021-08-10 21:27:24 +08:00
2021-08-22 21:23:58 +08:00
QString getHeaderFilename(const QString& relativeTo, const QString& line,
2021-10-04 22:32:34 +08:00
const QStringList& includePaths, const QStringList& projectIncludePaths);
2021-08-10 21:27:24 +08:00
2021-08-22 21:23:58 +08:00
QString getLocalHeaderFilename(const QString& relativeTo, const QString& fileName);
2021-08-10 21:27:24 +08:00
2021-10-04 22:32:34 +08:00
QString getSystemHeaderFilename(const QString& fileName, const QStringList& includePaths);
2021-08-19 23:49:23 +08:00
bool isSystemHeaderFile(const QString& fileName, const QSet<QString>& includePaths);
bool isHFile(const QString& filename);
bool isCFile(const QString& filename);
bool isCppFile(const QString& filename);
bool isCppKeyword(const QString& word);
bool isCppControlKeyword(const QString& word);
2021-08-25 00:20:07 +08:00
bool isScopeTypeKind(StatementKind kind);
MemberOperatorType getOperatorType(const QString& phrase, int index);
QStringList getOwnerExpressionAndMember(
const QStringList expression,
QString& memberOperator,
QStringList& memberExpression);
bool isMemberOperator(QString token);
StatementKind getKindOfStatement(const PStatement& statement);
2021-08-24 15:05:10 +08:00
2021-08-05 23:13:21 +08:00
#endif // PARSER_UTILS_H