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-05-14 23:56:43 +08:00
|
|
|
#ifndef SYNHIGHLIGTERBASE_H
|
|
|
|
#define SYNHIGHLIGTERBASE_H
|
|
|
|
|
|
|
|
#include <QColor>
|
|
|
|
#include <QObject>
|
|
|
|
#include <memory>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QSet>
|
|
|
|
#include <QVector>
|
|
|
|
#include <QVector>
|
|
|
|
#include "../Types.h"
|
|
|
|
|
2021-10-30 18:28:40 +08:00
|
|
|
enum SynIndentType {
|
|
|
|
sitBrace = 0,
|
|
|
|
sitParenthesis = 1,
|
|
|
|
sitBracket = 2,
|
|
|
|
sitStatement = 3,
|
|
|
|
};
|
2021-10-28 23:46:54 +08:00
|
|
|
|
2021-09-23 12:06:26 +08:00
|
|
|
struct SynRangeState {
|
2021-10-29 12:25:04 +08:00
|
|
|
int state; // current syntax parsing state
|
|
|
|
int braceLevel; // current braces embedding level (needed by rainbow color)
|
|
|
|
int bracketLevel; // current brackets embedding level (needed by rainbow color)
|
|
|
|
int parenthesisLevel; // current parenthesis embedding level (needed by rainbow color)
|
|
|
|
int leftBraces; // unpairing left braces in the current line ( needed by block folding)
|
|
|
|
int rightBraces; // unparing right braces in the current line (needed by block folding)
|
2021-10-30 18:28:40 +08:00
|
|
|
QVector<int> indents; // indents stack (needed by auto indent)
|
2021-10-29 12:25:04 +08:00
|
|
|
int firstIndentThisLine; /* index of first indent that appended to the indents
|
|
|
|
* stack at this line ( need by auto indent) */
|
2021-10-30 18:28:40 +08:00
|
|
|
QVector<int> matchingIndents; /* the indent matched ( and removed )
|
2021-10-29 12:25:04 +08:00
|
|
|
but not started at this line
|
|
|
|
(need by auto indent) */
|
2021-09-23 12:06:26 +08:00
|
|
|
bool operator==(const SynRangeState& s2);
|
2021-10-30 18:28:40 +08:00
|
|
|
int getLastIndent();
|
2021-09-23 12:06:26 +08:00
|
|
|
};
|
2021-05-14 23:56:43 +08:00
|
|
|
|
|
|
|
typedef int SynTokenKind;
|
|
|
|
|
|
|
|
enum class SynHighlighterTokenType {
|
|
|
|
Default, Space, Comment,
|
|
|
|
PreprocessDirective, String, StringEscapeSequence,
|
|
|
|
Identifier, Symbol,
|
|
|
|
Character, Keyword, Number};
|
|
|
|
|
|
|
|
enum class SynHighlighterClass {
|
|
|
|
Composition,
|
|
|
|
CppHighlighter,
|
2022-01-23 23:27:48 +08:00
|
|
|
AsmHighlighter,
|
|
|
|
GLSLHighlighter
|
2021-05-14 23:56:43 +08:00
|
|
|
};
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
enum class SynHighlighterLanguage {
|
2021-07-01 22:23:24 +08:00
|
|
|
Asssembly,
|
|
|
|
Cpp,
|
2022-01-23 23:27:48 +08:00
|
|
|
GLSL
|
2021-05-26 00:04:20 +08:00
|
|
|
};
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
class SynHighlighterAttribute {
|
|
|
|
public:
|
|
|
|
explicit SynHighlighterAttribute(const QString& name);
|
2021-05-14 23:56:43 +08:00
|
|
|
|
|
|
|
QString name() const;
|
|
|
|
void setName(const QString &name);
|
|
|
|
|
|
|
|
SynFontStyles styles() const;
|
|
|
|
void setStyles(const SynFontStyles &styles);
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
QColor foreground() const;
|
|
|
|
void setForeground(const QColor &color);
|
|
|
|
|
|
|
|
QColor background() const;
|
|
|
|
void setBackground(const QColor &background);
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
private:
|
|
|
|
QColor mForeground;
|
2021-05-26 00:04:20 +08:00
|
|
|
QColor mBackground;
|
2021-05-14 23:56:43 +08:00
|
|
|
QString mName;
|
|
|
|
SynFontStyles mStyles;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::shared_ptr<SynHighlighterAttribute> PSynHighlighterAttribute;
|
|
|
|
using SynHighlighterAttributeList = QVector<PSynHighlighterAttribute>;
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
class SynHighlighter {
|
2021-05-14 23:56:43 +08:00
|
|
|
public:
|
2021-05-26 00:04:20 +08:00
|
|
|
explicit SynHighlighter();
|
2021-05-14 23:56:43 +08:00
|
|
|
|
|
|
|
const QMap<QString, PSynHighlighterAttribute>& attributes() const;
|
|
|
|
|
|
|
|
const QSet<QChar>& wordBreakChars() const;
|
|
|
|
|
|
|
|
|
|
|
|
PSynHighlighterAttribute commentAttribute() const;
|
|
|
|
|
|
|
|
PSynHighlighterAttribute identifierAttribute() const;
|
|
|
|
|
|
|
|
PSynHighlighterAttribute keywordAttribute() const;
|
|
|
|
|
|
|
|
PSynHighlighterAttribute stringAttribute() const;
|
|
|
|
|
|
|
|
PSynHighlighterAttribute whitespaceAttribute() const;
|
|
|
|
|
|
|
|
PSynHighlighterAttribute symbolAttribute() const;
|
|
|
|
|
|
|
|
virtual bool isIdentChar(const QChar& ch) const;
|
|
|
|
|
|
|
|
virtual SynHighlighterClass getClass() const = 0;
|
|
|
|
virtual QString getName() const = 0;
|
|
|
|
|
|
|
|
virtual bool getTokenFinished() const = 0;
|
|
|
|
virtual bool isLastLineCommentNotFinished(int state) const = 0;
|
|
|
|
virtual bool isLastLineStringNotFinished(int state) const = 0;
|
|
|
|
virtual bool eol() const = 0;
|
2021-05-26 18:32:17 +08:00
|
|
|
virtual SynRangeState getRangeState() const = 0;
|
2021-05-14 23:56:43 +08:00
|
|
|
virtual QString getToken() const=0;
|
|
|
|
virtual PSynHighlighterAttribute getTokenAttribute() const=0;
|
|
|
|
virtual SynHighlighterTokenType getTokenType();
|
|
|
|
virtual SynTokenKind getTokenKind() = 0;
|
|
|
|
virtual int getTokenPos() = 0;
|
|
|
|
virtual bool isKeyword(const QString& word);
|
|
|
|
virtual void next() = 0;
|
|
|
|
virtual void nextToEol();
|
2021-09-23 12:06:26 +08:00
|
|
|
virtual void setState(const SynRangeState& rangeState) = 0;
|
2021-05-14 23:56:43 +08:00
|
|
|
virtual void setLine(const QString& newLine, int lineNumber) = 0;
|
|
|
|
virtual void resetState() = 0;
|
2022-01-23 23:27:48 +08:00
|
|
|
virtual QSet<QString> keywords() const;
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2021-05-24 00:41:00 +08:00
|
|
|
virtual QString languageName() = 0;
|
2021-05-26 00:04:20 +08:00
|
|
|
virtual SynHighlighterLanguage language() = 0;
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2021-06-22 23:00:34 +08:00
|
|
|
virtual bool isSpaceChar(const QChar& ch);
|
|
|
|
virtual bool isWordBreakChar(const QChar& ch);
|
2021-05-18 15:49:58 +08:00
|
|
|
bool enabled() const;
|
|
|
|
void setEnabled(bool value);
|
2021-10-03 17:18:43 +08:00
|
|
|
virtual PSynHighlighterAttribute getAttribute(const QString& name) const;
|
2021-05-18 15:49:58 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
protected:
|
|
|
|
PSynHighlighterAttribute mCommentAttribute;
|
|
|
|
PSynHighlighterAttribute mIdentifierAttribute;
|
|
|
|
PSynHighlighterAttribute mKeywordAttribute;
|
|
|
|
PSynHighlighterAttribute mStringAttribute;
|
|
|
|
PSynHighlighterAttribute mWhitespaceAttribute;
|
|
|
|
PSynHighlighterAttribute mSymbolAttribute;
|
|
|
|
|
|
|
|
void addAttribute(PSynHighlighterAttribute attribute);
|
|
|
|
void clearAttributes();
|
|
|
|
virtual int attributesCount() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
QMap<QString,PSynHighlighterAttribute> mAttributes;
|
|
|
|
bool mEnabled;
|
|
|
|
QSet<QChar> mWordBreakChars;
|
|
|
|
};
|
|
|
|
|
|
|
|
using PSynHighlighter = std::shared_ptr<SynHighlighter>;
|
|
|
|
using SynHighlighterList = QVector<PSynHighlighter>;
|
|
|
|
|
|
|
|
#endif // SYNHIGHLIGTERBASE_H
|