File indexing completed on 2024-05-12 04:02:19

0001 /*
0002     SPDX-FileCopyrightText: 2021 Jonathan Poelen <jonathan.poelen@gmail.com>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef KSYNTAXHIGHLIGHTING_HIGHLIGHTING_DATA_P_H
0008 #define KSYNTAXHIGHLIGHTING_HIGHLIGHTING_DATA_P_H
0009 
0010 #include <QString>
0011 #include <QStringList>
0012 
0013 #include <vector>
0014 
0015 QT_BEGIN_NAMESPACE
0016 class QXmlStreamReader;
0017 QT_END_NAMESPACE
0018 
0019 namespace KSyntaxHighlighting
0020 {
0021 /**
0022  * Represents the raw xml data of a context and its rules.
0023  * After resolving contexts, members of this class are no longer
0024  * use and the instance can be freed to recover used memory.
0025  */
0026 class HighlightingContextData
0027 {
0028 public:
0029     void load(const QString &defName, QXmlStreamReader &reader);
0030 
0031     struct ContextSwitch {
0032         ContextSwitch() = default;
0033         ContextSwitch(QStringView str);
0034 
0035         QStringView contextName() const;
0036         QStringView defName() const;
0037 
0038         bool isStay() const;
0039 
0040         int popCount() const
0041         {
0042             return m_popCount;
0043         }
0044 
0045     private:
0046         int m_popCount = 0;
0047         int m_defNameIndex = -1;
0048         QString m_contextAndDefName;
0049     };
0050 
0051     struct Rule {
0052         enum class Type : quint8 {
0053             Unknown,
0054             AnyChar,
0055             Detect2Chars,
0056             DetectChar,
0057             HlCOct,
0058             IncludeRules,
0059             Int,
0060             Keyword,
0061             LineContinue,
0062             RangeDetect,
0063             RegExpr,
0064             StringDetect,
0065             WordDetect,
0066             Float,
0067             HlCStringChar,
0068             DetectIdentifier,
0069             DetectSpaces,
0070             HlCChar,
0071             HlCHex,
0072         };
0073 
0074         struct AnyChar {
0075             QString chars;
0076         };
0077 
0078         struct Detect2Chars {
0079             QChar char1;
0080             QChar char2;
0081         };
0082 
0083         struct DetectChar {
0084             QChar char1;
0085             bool dynamic;
0086         };
0087 
0088         struct WordDelimiters {
0089             QString additionalDeliminator;
0090             QString weakDeliminator;
0091         };
0092 
0093         struct Float {
0094             WordDelimiters wordDelimiters;
0095         };
0096 
0097         struct HlCHex {
0098             WordDelimiters wordDelimiters;
0099         };
0100 
0101         struct HlCOct {
0102             WordDelimiters wordDelimiters;
0103         };
0104 
0105         struct IncludeRules {
0106             QString contextName;
0107             bool includeAttribute;
0108         };
0109 
0110         struct Int {
0111             WordDelimiters wordDelimiters;
0112         };
0113 
0114         struct Keyword {
0115             QString name;
0116             WordDelimiters wordDelimiters;
0117             Qt::CaseSensitivity caseSensitivityOverride;
0118             bool hasCaseSensitivityOverride;
0119         };
0120 
0121         struct LineContinue {
0122             QChar char1;
0123         };
0124 
0125         struct RangeDetect {
0126             QChar begin;
0127             QChar end;
0128         };
0129 
0130         struct RegExpr {
0131             QString pattern;
0132             Qt::CaseSensitivity caseSensitivity;
0133             bool isMinimal;
0134             bool dynamic;
0135         };
0136 
0137         struct StringDetect {
0138             QString string;
0139             Qt::CaseSensitivity caseSensitivity;
0140             bool dynamic;
0141         };
0142 
0143         struct WordDetect {
0144             QString word;
0145             WordDelimiters wordDelimiters;
0146             Qt::CaseSensitivity caseSensitivity;
0147         };
0148 
0149         union Data {
0150             AnyChar anyChar;
0151             Detect2Chars detect2Chars;
0152             DetectChar detectChar;
0153             HlCOct hlCOct;
0154             IncludeRules includeRules;
0155             Int detectInt;
0156             Keyword keyword;
0157             LineContinue lineContinue;
0158             RangeDetect rangeDetect;
0159             RegExpr regExpr;
0160             StringDetect stringDetect;
0161             WordDetect wordDetect;
0162             Float detectFloat;
0163             HlCHex hlCHex;
0164 
0165             Data() noexcept
0166             {
0167             }
0168 
0169             ~Data()
0170             {
0171             }
0172         };
0173 
0174         struct Common {
0175             QString contextName;
0176             QString attributeName;
0177             QString beginRegionName;
0178             QString endRegionName;
0179             int column = -1;
0180             bool firstNonSpace = false;
0181             bool lookAhead = false;
0182         };
0183 
0184         Type type = Type::Unknown;
0185         Common common;
0186         Data data;
0187 
0188         Rule() noexcept;
0189         Rule(Rule &&other) noexcept;
0190         Rule(const Rule &other);
0191         ~Rule();
0192 
0193         // since nothing is deleted in the rules vector, these functions do not need to be implemented
0194         Rule &operator=(Rule &&other) = delete;
0195         Rule &operator=(const Rule &other) = delete;
0196     };
0197 
0198     QString name;
0199 
0200     /**
0201      * attribute name, to lookup our format
0202      */
0203     QString attribute;
0204 
0205     QString lineEndContext;
0206     QString lineEmptyContext;
0207     QString fallthroughContext;
0208 
0209     std::vector<Rule> rules;
0210 
0211     bool stopEmptyLineContextSwitchLoop = false;
0212     bool noIndentationBasedFolding = false;
0213 };
0214 }
0215 
0216 #endif