1/*
2 SPDX-FileCopyrightText: 2021 Jonathan Poelen <jonathan.poelen@gmail.com>
3
4 SPDX-License-Identifier: MIT
5*/
6
7#ifndef KSYNTAXHIGHLIGHTING_HIGHLIGHTING_DATA_P_H
8#define KSYNTAXHIGHLIGHTING_HIGHLIGHTING_DATA_P_H
9
10#include <QString>
11#include <QStringList>
12
13#include <vector>
14
15QT_BEGIN_NAMESPACE
16class QXmlStreamReader;
17QT_END_NAMESPACE
18
19namespace KSyntaxHighlighting
20{
21/**
22 * Represents the raw xml data of a context and its rules.
23 * After resolving contexts, members of this class are no longer
24 * use and the instance can be freed to recover used memory.
25 */
26class HighlightingContextData
27{
28public:
29 void load(const QString &defName, QXmlStreamReader &reader);
30
31 struct ContextSwitch {
32 ContextSwitch() = default;
33 ContextSwitch(QStringView str);
34
35 QStringView contextName() const;
36 QStringView defName() const;
37
38 bool isStay() const;
39
40 int popCount() const
41 {
42 return m_popCount;
43 }
44
45 private:
46 int m_popCount = 0;
47 int m_defNameIndex = -1;
48 QString m_contextAndDefName;
49 };
50
51 struct Rule {
52 enum class Type : quint8 {
53 Unknown,
54 AnyChar,
55 Detect2Chars,
56 DetectChar,
57 HlCOct,
58 IncludeRules,
59 Int,
60 Keyword,
61 LineContinue,
62 RangeDetect,
63 RegExpr,
64 StringDetect,
65 WordDetect,
66 Float,
67 HlCStringChar,
68 DetectIdentifier,
69 DetectSpaces,
70 HlCChar,
71 HlCHex,
72 };
73
74 struct AnyChar {
75 QString chars;
76 };
77
78 struct Detect2Chars {
79 QChar char1;
80 QChar char2;
81 };
82
83 struct DetectChar {
84 QChar char1;
85 bool dynamic;
86 };
87
88 struct WordDelimiters {
89 QString additionalDeliminator;
90 QString weakDeliminator;
91 };
92
93 struct Float {
94 WordDelimiters wordDelimiters;
95 };
96
97 struct HlCHex {
98 WordDelimiters wordDelimiters;
99 };
100
101 struct HlCOct {
102 WordDelimiters wordDelimiters;
103 };
104
105 struct IncludeRules {
106 QString contextName;
107 bool includeAttribute;
108 };
109
110 struct Int {
111 WordDelimiters wordDelimiters;
112 };
113
114 struct Keyword {
115 QString name;
116 WordDelimiters wordDelimiters;
117 Qt::CaseSensitivity caseSensitivityOverride;
118 bool hasCaseSensitivityOverride;
119 };
120
121 struct LineContinue {
122 QChar char1;
123 };
124
125 struct RangeDetect {
126 QChar begin;
127 QChar end;
128 };
129
130 struct RegExpr {
131 QString pattern;
132 Qt::CaseSensitivity caseSensitivity;
133 bool isMinimal;
134 bool dynamic;
135 };
136
137 struct StringDetect {
138 QString string;
139 Qt::CaseSensitivity caseSensitivity;
140 bool dynamic;
141 };
142
143 struct WordDetect {
144 QString word;
145 WordDelimiters wordDelimiters;
146 Qt::CaseSensitivity caseSensitivity;
147 };
148
149 union Data {
150 AnyChar anyChar;
151 Detect2Chars detect2Chars;
152 DetectChar detectChar;
153 HlCOct hlCOct;
154 IncludeRules includeRules;
155 Int detectInt;
156 Keyword keyword;
157 LineContinue lineContinue;
158 RangeDetect rangeDetect;
159 RegExpr regExpr;
160 StringDetect stringDetect;
161 WordDetect wordDetect;
162 Float detectFloat;
163 HlCHex hlCHex;
164
165 Data() noexcept
166 {
167 }
168
169 ~Data()
170 {
171 }
172 };
173
174 struct Common {
175 QString contextName;
176 QString attributeName;
177 QString beginRegionName;
178 QString endRegionName;
179 int column = -1;
180 bool firstNonSpace = false;
181 bool lookAhead = false;
182 };
183
184 Type type = Type::Unknown;
185 Common common;
186 Data data;
187
188 Rule() noexcept;
189 Rule(Rule &&other) noexcept;
190 Rule(const Rule &other);
191 ~Rule();
192
193 // since nothing is deleted in the rules vector, these functions do not need to be implemented
194 Rule &operator=(Rule &&other) = delete;
195 Rule &operator=(const Rule &other) = delete;
196 };
197
198 QString name;
199
200 /**
201 * attribute name, to lookup our format
202 */
203 QString attribute;
204
205 QString lineEndContext;
206 QString lineEmptyContext;
207 QString fallthroughContext;
208
209 std::vector<Rule> rules;
210
211 bool stopEmptyLineContextSwitchLoop = false;
212 bool noIndentationBasedFolding = false;
213};
214}
215
216#endif
217

source code of syntax-highlighting/src/lib/highlightingdata_p.hpp