File indexing completed on 2024-05-12 15:50:06

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2020 Jonathan Poelen <jonathan.poelen@gmail.com>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 #ifndef KSYNTAXHIGHLIGHTING_RULE_P_H
0009 #define KSYNTAXHIGHLIGHTING_RULE_P_H
0010 
0011 #include "contextswitch_p.h"
0012 #include "definitionref_p.h"
0013 #include "foldingregion.h"
0014 #include "format.h"
0015 #include "highlightingdata_p.hpp"
0016 #include "keywordlist_p.h"
0017 #include "matchresult_p.h"
0018 #include "worddelimiters_p.h"
0019 
0020 #include <QRegularExpression>
0021 #include <QString>
0022 
0023 #include <memory>
0024 
0025 namespace KSyntaxHighlighting
0026 {
0027 class WordDelimiters;
0028 class DefinitionData;
0029 class IncludeRules;
0030 
0031 class Rule
0032 {
0033 public:
0034     Rule() = default;
0035     virtual ~Rule();
0036 
0037     typedef std::shared_ptr<Rule> Ptr;
0038 
0039     const Format &attributeFormat() const
0040     {
0041         return m_attributeFormat;
0042     }
0043 
0044     const ContextSwitch &context() const
0045     {
0046         return m_context;
0047     }
0048 
0049     bool isLookAhead() const
0050     {
0051         return m_lookAhead;
0052     }
0053 
0054     bool isDynamic() const
0055     {
0056         return m_dynamic;
0057     }
0058 
0059     bool firstNonSpace() const
0060     {
0061         return m_firstNonSpace;
0062     }
0063 
0064     int requiredColumn() const
0065     {
0066         return m_column;
0067     }
0068 
0069     const FoldingRegion &beginRegion() const
0070     {
0071         return m_beginRegion;
0072     }
0073 
0074     const FoldingRegion &endRegion() const
0075     {
0076         return m_endRegion;
0077     }
0078 
0079     const IncludeRules *castToIncludeRules() const;
0080 
0081     bool isLineContinue() const
0082     {
0083         return m_type == Type::LineContinue;
0084     }
0085 
0086     virtual MatchResult doMatch(QStringView text, int offset, const QStringList &captures) const = 0;
0087 
0088     static Rule::Ptr create(DefinitionData &def, const HighlightingContextData::Rule &ruleData, QStringView lookupContextName);
0089 
0090 private:
0091     Q_DISABLE_COPY(Rule)
0092 
0093     bool resolveCommon(DefinitionData &def, const HighlightingContextData::Rule &ruleData, QStringView lookupContextName);
0094 
0095     enum class Type : quint8 {
0096         OtherRule,
0097         LineContinue,
0098         IncludeRules,
0099     };
0100 
0101     Format m_attributeFormat;
0102     ContextSwitch m_context;
0103     int m_column = -1;
0104     FoldingRegion m_beginRegion;
0105     FoldingRegion m_endRegion;
0106     Type m_type;
0107     bool m_firstNonSpace = false;
0108     bool m_lookAhead = false;
0109 
0110 protected:
0111     bool m_dynamic = false;
0112 };
0113 
0114 class AnyChar final : public Rule
0115 {
0116 public:
0117     AnyChar(const HighlightingContextData::Rule::AnyChar &data);
0118 
0119 protected:
0120     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0121 
0122 private:
0123     QString m_chars;
0124 };
0125 
0126 class DetectChar final : public Rule
0127 {
0128 public:
0129     DetectChar(const HighlightingContextData::Rule::DetectChar &data);
0130 
0131 protected:
0132     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0133 
0134 private:
0135     QChar m_char;
0136     int m_captureIndex = 0;
0137 };
0138 
0139 class Detect2Chars final : public Rule
0140 {
0141 public:
0142     Detect2Chars(const HighlightingContextData::Rule::Detect2Chars &data);
0143 
0144 protected:
0145     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0146 
0147 private:
0148     QChar m_char1;
0149     QChar m_char2;
0150 };
0151 
0152 class DetectIdentifier final : public Rule
0153 {
0154 protected:
0155     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0156 };
0157 
0158 class DetectSpaces final : public Rule
0159 {
0160 protected:
0161     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0162 };
0163 
0164 class Float final : public Rule
0165 {
0166 public:
0167     Float(DefinitionData &def, const HighlightingContextData::Rule::Float &data);
0168 
0169 protected:
0170     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0171 
0172 private:
0173     WordDelimiters m_wordDelimiters;
0174 };
0175 
0176 class IncludeRules final : public Rule
0177 {
0178 public:
0179     IncludeRules(const HighlightingContextData::Rule::IncludeRules &data);
0180 
0181     const QString &contextName() const
0182     {
0183         return m_contextName;
0184     }
0185 
0186     bool includeAttribute() const
0187     {
0188         return m_includeAttribute;
0189     }
0190 
0191 protected:
0192     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0193 
0194 private:
0195     QString m_contextName;
0196     bool m_includeAttribute;
0197 };
0198 
0199 class Int final : public Rule
0200 {
0201 public:
0202     Int(DefinitionData &def, const HighlightingContextData::Rule::Int &data);
0203 
0204 protected:
0205     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0206 
0207 private:
0208     WordDelimiters m_wordDelimiters;
0209 };
0210 
0211 class HlCChar final : public Rule
0212 {
0213 protected:
0214     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0215 };
0216 
0217 class HlCHex final : public Rule
0218 {
0219 public:
0220     HlCHex(DefinitionData &def, const HighlightingContextData::Rule::HlCHex &data);
0221 
0222 protected:
0223     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0224 
0225 private:
0226     WordDelimiters m_wordDelimiters;
0227 };
0228 
0229 class HlCOct final : public Rule
0230 {
0231 public:
0232     HlCOct(DefinitionData &def, const HighlightingContextData::Rule::HlCOct &data);
0233 
0234 protected:
0235     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0236 
0237 private:
0238     WordDelimiters m_wordDelimiters;
0239 };
0240 
0241 class HlCStringChar final : public Rule
0242 {
0243 protected:
0244     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0245 };
0246 
0247 class KeywordListRule final : public Rule
0248 {
0249 public:
0250     KeywordListRule(const KeywordList &keywordList, DefinitionData &def, const HighlightingContextData::Rule::Keyword &data);
0251 
0252     static Rule::Ptr create(DefinitionData &def, const HighlightingContextData::Rule::Keyword &data, QStringView lookupContextName);
0253 
0254 protected:
0255     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0256 
0257 private:
0258     WordDelimiters m_wordDelimiters;
0259     const KeywordList &m_keywordList;
0260     Qt::CaseSensitivity m_caseSensitivity;
0261 };
0262 
0263 class LineContinue final : public Rule
0264 {
0265 public:
0266     LineContinue(const HighlightingContextData::Rule::LineContinue &data);
0267 
0268 protected:
0269     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0270 
0271 private:
0272     QChar m_char;
0273 };
0274 
0275 class RangeDetect final : public Rule
0276 {
0277 public:
0278     RangeDetect(const HighlightingContextData::Rule::RangeDetect &data);
0279 
0280 protected:
0281     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0282 
0283 private:
0284     QChar m_begin;
0285     QChar m_end;
0286 };
0287 
0288 class RegExpr final : public Rule
0289 {
0290 public:
0291     RegExpr(const HighlightingContextData::Rule::RegExpr &data);
0292 
0293 protected:
0294     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0295 
0296 private:
0297     void resolve();
0298     QRegularExpression m_regexp;
0299     bool m_isResolved = false;
0300 };
0301 
0302 class DynamicRegExpr final : public Rule
0303 {
0304 public:
0305     DynamicRegExpr(const HighlightingContextData::Rule::RegExpr &data);
0306 
0307 protected:
0308     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0309 
0310 private:
0311     void resolve();
0312     QString m_pattern;
0313     QRegularExpression::PatternOptions m_patternOptions;
0314     bool m_isResolved = false;
0315 };
0316 
0317 class StringDetect final : public Rule
0318 {
0319 public:
0320     StringDetect(const HighlightingContextData::Rule::StringDetect &data);
0321 
0322 protected:
0323     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0324 
0325 private:
0326     QString m_string;
0327     Qt::CaseSensitivity m_caseSensitivity;
0328 };
0329 
0330 class DynamicStringDetect final : public Rule
0331 {
0332 public:
0333     DynamicStringDetect(const HighlightingContextData::Rule::StringDetect &data);
0334 
0335 protected:
0336     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0337 
0338 private:
0339     QString m_string;
0340     Qt::CaseSensitivity m_caseSensitivity;
0341 };
0342 
0343 class WordDetect final : public Rule
0344 {
0345 public:
0346     WordDetect(DefinitionData &def, const HighlightingContextData::Rule::WordDetect &data);
0347 
0348 protected:
0349     MatchResult doMatch(QStringView text, int offset, const QStringList &) const override;
0350 
0351 private:
0352     WordDelimiters m_wordDelimiters;
0353     QString m_word;
0354     Qt::CaseSensitivity m_caseSensitivity;
0355 };
0356 
0357 }
0358 
0359 #endif // KSYNTAXHIGHLIGHTING_RULE_P_H