aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/3rdparty/syntax-highlighting/src/lib/rule.cpp
blob: 186ed16120d094c3016645206e1fb90384c2977e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
    SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
    SPDX-FileCopyrightText: 2018 Christoph Cullmann <cullmann@kde.org>
    SPDX-FileCopyrightText: 2020 Jonathan Poelen <jonathan.poelen+kde@gmail.com>

    SPDX-License-Identifier: MIT
*/

#include "context_p.h"
#include "definition_p.h"
#include "dynamicregexpcache_p.h"
#include "ksyntaxhighlighting_logging.h"
#include "rule_p.h"
#include "worddelimiters_p.h"
#include "xml_p.h"

using namespace KSyntaxHighlighting;

// QChar::isDigit() match any digit in unicode (romain numeral, etc)
static bool isDigit(QChar c)
{
    return (c <= QLatin1Char('9') && QLatin1Char('0') <= c);
}

static bool isOctalChar(QChar c)
{
    return (c <= QLatin1Char('7') && QLatin1Char('0') <= c);
}

static bool isHexChar(QChar c)
{
    return isDigit(c) || (c <= QLatin1Char('f') && QLatin1Char('a') <= c) || (c <= QLatin1Char('F') && QLatin1Char('A') <= c);
}

static int matchEscapedChar(QStringView text, int offset)
{
    if (text.at(offset) != QLatin1Char('\\') || text.size() < offset + 2) {
        return offset;
    }

    const auto c = text.at(offset + 1);
    switch (c.unicode()) {
    // control chars
    case 'a':
    case 'b':
    case 'e':
    case 'f':
    case 'n':
    case 'r':
    case 't':
    case 'v':
    case '"':
    case '\'':
    case '?':
    case '\\':
        return offset + 2;

    // hex encoded character
    case 'x':
        if (offset + 2 < text.size() && isHexChar(text.at(offset + 2))) {
            if (offset + 3 < text.size() && isHexChar(text.at(offset + 3))) {
                return offset + 4;
            }
            return offset + 3;
        }
        return offset;

    // octal encoding, simple \0 is OK, too, unlike simple \x above
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
        if (offset + 2 < text.size() && isOctalChar(text.at(offset + 2))) {
            if (offset + 3 < text.size() && isOctalChar(text.at(offset + 3))) {
                return offset + 4;
            }
            return offset + 3;
        }
        return offset + 2;
    }

    return offset;
}

static QString replaceCaptures(const QString &pattern, const QStringList &captures, bool quote)
{
    auto result = pattern;
    for (int i = captures.size(); i >= 1; --i) {
        result.replace(QLatin1Char('%') + QString::number(i), quote ? QRegularExpression::escape(captures.at(i - 1)) : captures.at(i - 1));
    }
    return result;
}

static MatchResult matchString(QStringView pattern, QStringView text, int offset, Qt::CaseSensitivity caseSensitivity)
{
    if (offset + pattern.size() <= text.size() && text.mid(offset, pattern.size()).compare(pattern, caseSensitivity) == 0) {
        return offset + pattern.size();
    }
    return offset;
}

static void resolveAdditionalWordDelimiters(WordDelimiters &wordDelimiters, const HighlightingContextData::Rule::WordDelimiters &delimiters)
{
    // cache for DefinitionData::wordDelimiters, is accessed VERY often
    if (!delimiters.additionalDeliminator.isEmpty() || !delimiters.weakDeliminator.isEmpty()) {
        wordDelimiters.append(QStringView(delimiters.additionalDeliminator));
        wordDelimiters.remove(QStringView(delimiters.weakDeliminator));
    }
}

Rule::~Rule() = default;

const IncludeRules *Rule::castToIncludeRules() const
{
    if (m_type != Type::IncludeRules) {
        return nullptr;
    }
    return static_cast<const IncludeRules *>(this);
}

bool Rule::resolveCommon(DefinitionData &def, const HighlightingContextData::Rule &ruleData, QStringView lookupContextName)
{
    switch (ruleData.type) {
    // IncludeRules uses this with a different semantic
    case HighlightingContextData::Rule::Type::IncludeRules:
        m_type = Type::IncludeRules;
        return true;
    case HighlightingContextData::Rule::Type::LineContinue:
        m_type = Type::LineContinue;
        break;
    default:
        m_type = Type::OtherRule;
        break;
    }

    /**
     * try to get our format from the definition we stem from
     */
    if (!ruleData.common.attributeName.isEmpty()) {
        m_attributeFormat = def.formatByName(ruleData.common.attributeName);
        if (!m_attributeFormat.isValid()) {
            qCWarning(Log) << "Rule: Unknown format" << ruleData.common.attributeName << "in context" << lookupContextName << "of definition" << def.name;
        }
    }

    m_firstNonSpace = ruleData.common.firstNonSpace;
    m_lookAhead = ruleData.common.lookAhead;
    m_column = ruleData.common.column;

    if (!ruleData.common.beginRegionName.isEmpty()) {
        m_beginRegion = FoldingRegion(FoldingRegion::Begin, def.foldingRegionId(ruleData.common.beginRegionName));
    }
    if (!ruleData.common.endRegionName.isEmpty()) {
        m_endRegion = FoldingRegion(FoldingRegion::End, def.foldingRegionId(ruleData.common.endRegionName));
    }

    m_context.resolve(def, ruleData.common.contextName);

    return !(m_lookAhead && m_context.isStay());
}

static Rule::Ptr createRule(DefinitionData &def, const HighlightingContextData::Rule &ruleData, QStringView lookupContextName)
{
    using Type = HighlightingContextData::Rule::Type;

    switch (ruleData.type) {
    case Type::AnyChar:
        return std::make_shared<AnyChar>(ruleData.data.anyChar);
    case Type::DetectChar:
        return std::make_shared<DetectChar>(ruleData.data.detectChar);
    case Type::Detect2Chars:
        return std::make_shared<Detect2Chars>(ruleData.data.detect2Chars);
    case Type::IncludeRules:
        return std::make_shared<IncludeRules>(ruleData.data.includeRules);
    case Type::Int:
        return std::make_shared<Int>(def, ruleData.data.detectInt);
    case Type::Keyword:
        return KeywordListRule::create(def, ruleData.data.keyword, lookupContextName);
    case Type::LineContinue:
        return std::make_shared<LineContinue>(ruleData.data.lineContinue);
    case Type::RangeDetect:
        return std::make_shared<RangeDetect>(ruleData.data.rangeDetect);
    case Type::RegExpr:
        if (!ruleData.data.regExpr.dynamic) {
            return std::make_shared<RegExpr>(ruleData.data.regExpr);
        } else {
            return std::make_shared<DynamicRegExpr>(ruleData.data.regExpr);
        }
    case Type::StringDetect:
        if (ruleData.data.stringDetect.dynamic) {
            return std::make_shared<DynamicStringDetect>(ruleData.data.stringDetect);
        }
        return std::make_shared<StringDetect>(ruleData.data.stringDetect);
    case Type::WordDetect:
        return std::make_shared<WordDetect>(def, ruleData.data.wordDetect);
    case Type::Float:
        return std::make_shared<Float>(def, ruleData.data.detectFloat);
    case Type::HlCOct:
        return std::make_shared<HlCOct>(def, ruleData.data.hlCOct);
    case Type::HlCStringChar:
        return std::make_shared<HlCStringChar>();
    case Type::DetectIdentifier:
        return std::make_shared<DetectIdentifier>();
    case Type::DetectSpaces:
        return std::make_shared<DetectSpaces>();
    case Type::HlCChar:
        return std::make_shared<HlCChar>();
    case Type::HlCHex:
        return std::make_shared<HlCHex>(def, ruleData.data.hlCHex);

    case Type::Unknown:;
    }

    return Rule::Ptr(nullptr);
}

Rule::Ptr Rule::create(DefinitionData &def, const HighlightingContextData::Rule &ruleData, QStringView lookupContextName)
{
    auto rule = createRule(def, ruleData, lookupContextName);
    if (rule && !rule->resolveCommon(def, ruleData, lookupContextName)) {
        rule.reset();
    }
    return rule;
}

AnyChar::AnyChar(const HighlightingContextData::Rule::AnyChar &data)
    : m_chars(data.chars)
{
}

MatchResult AnyChar::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (m_chars.contains(text.at(offset))) {
        return offset + 1;
    }
    return offset;
}

DetectChar::DetectChar(const HighlightingContextData::Rule::DetectChar &data)
    : m_char(data.char1)
    , m_captureIndex((data.dynamic ? data.char1.digitValue() : 0) - 1)
{
    m_dynamic = data.dynamic;
}

MatchResult DetectChar::doMatch(QStringView text, int offset, const QStringList &captures, DynamicRegexpCache &) const
{
    if (m_dynamic) {
        if (m_captureIndex == -1 || captures.size() <= m_captureIndex || captures.at(m_captureIndex).isEmpty()) {
            return offset;
        }
        if (text.at(offset) == captures.at(m_captureIndex).at(0)) {
            return offset + 1;
        }
        return offset;
    }

    if (text.at(offset) == m_char) {
        return offset + 1;
    }
    return offset;
}

Detect2Chars::Detect2Chars(const HighlightingContextData::Rule::Detect2Chars &data)
    : m_char1(data.char1)
    , m_char2(data.char2)
{
}

MatchResult Detect2Chars::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (text.size() - offset < 2) {
        return offset;
    }
    if (text.at(offset) == m_char1 && text.at(offset + 1) == m_char2) {
        return offset + 2;
    }
    return offset;
}

MatchResult DetectIdentifier::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (!text.at(offset).isLetter() && text.at(offset) != QLatin1Char('_')) {
        return offset;
    }

    for (int i = offset + 1; i < text.size(); ++i) {
        const auto c = text.at(i);
        if (!c.isLetterOrNumber() && c != QLatin1Char('_')) {
            return i;
        }
    }

    return text.size();
}

MatchResult DetectSpaces::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    while (offset < text.size() && text.at(offset).isSpace()) {
        ++offset;
    }
    return offset;
}

Float::Float(DefinitionData &def, const HighlightingContextData::Rule::Float &data)
    : m_wordDelimiters(def.wordDelimiters)
{
    resolveAdditionalWordDelimiters(m_wordDelimiters, data.wordDelimiters);
}

MatchResult Float::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (offset > 0 && !m_wordDelimiters.contains(text.at(offset - 1))) {
        return offset;
    }

    auto newOffset = offset;
    while (newOffset < text.size() && isDigit(text.at(newOffset))) {
        ++newOffset;
    }

    if (newOffset >= text.size() || text.at(newOffset) != QLatin1Char('.')) {
        return offset;
    }
    ++newOffset;

    while (newOffset < text.size() && isDigit(text.at(newOffset))) {
        ++newOffset;
    }

    if (newOffset == offset + 1) { // we only found a decimal point
        return offset;
    }

    auto expOffset = newOffset;
    if (expOffset >= text.size() || (text.at(expOffset) != QLatin1Char('e') && text.at(expOffset) != QLatin1Char('E'))) {
        return newOffset;
    }
    ++expOffset;

    if (expOffset < text.size() && (text.at(expOffset) == QLatin1Char('+') || text.at(expOffset) == QLatin1Char('-'))) {
        ++expOffset;
    }
    bool foundExpDigit = false;
    while (expOffset < text.size() && isDigit(text.at(expOffset))) {
        ++expOffset;
        foundExpDigit = true;
    }

    if (!foundExpDigit) {
        return newOffset;
    }
    return expOffset;
}

MatchResult HlCChar::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (text.size() < offset + 3) {
        return offset;
    }

    if (text.at(offset) != QLatin1Char('\'') || text.at(offset + 1) == QLatin1Char('\'')) {
        return offset;
    }

    auto newOffset = matchEscapedChar(text, offset + 1);
    if (newOffset == offset + 1) {
        if (text.at(newOffset) == QLatin1Char('\\')) {
            return offset;
        } else {
            ++newOffset;
        }
    }
    if (newOffset >= text.size()) {
        return offset;
    }

    if (text.at(newOffset) == QLatin1Char('\'')) {
        return newOffset + 1;
    }

    return offset;
}

HlCHex::HlCHex(DefinitionData &def, const HighlightingContextData::Rule::HlCHex &data)
    : m_wordDelimiters(def.wordDelimiters)
{
    resolveAdditionalWordDelimiters(m_wordDelimiters, data.wordDelimiters);
}

MatchResult HlCHex::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (offset > 0 && !m_wordDelimiters.contains(text.at(offset - 1))) {
        return offset;
    }

    if (text.size() < offset + 3) {
        return offset;
    }

    if (text.at(offset) != QLatin1Char('0') || (text.at(offset + 1) != QLatin1Char('x') && text.at(offset + 1) != QLatin1Char('X'))) {
        return offset;
    }

    if (!isHexChar(text.at(offset + 2))) {
        return offset;
    }

    offset += 3;
    while (offset < text.size() && isHexChar(text.at(offset))) {
        ++offset;
    }

    // TODO Kate matches U/L suffix, QtC does not?

    return offset;
}

HlCOct::HlCOct(DefinitionData &def, const HighlightingContextData::Rule::HlCOct &data)
    : m_wordDelimiters(def.wordDelimiters)
{
    resolveAdditionalWordDelimiters(m_wordDelimiters, data.wordDelimiters);
}

MatchResult HlCOct::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (offset > 0 && !m_wordDelimiters.contains(text.at(offset - 1))) {
        return offset;
    }

    if (text.size() < offset + 2) {
        return offset;
    }

    if (text.at(offset) != QLatin1Char('0')) {
        return offset;
    }

    if (!isOctalChar(text.at(offset + 1))) {
        return offset;
    }

    offset += 2;
    while (offset < text.size() && isOctalChar(text.at(offset))) {
        ++offset;
    }

    return offset;
}

MatchResult HlCStringChar::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    return matchEscapedChar(text, offset);
}

IncludeRules::IncludeRules(const HighlightingContextData::Rule::IncludeRules &data)
    : m_contextName(data.contextName)
    , m_includeAttribute(data.includeAttribute)
{
}

MatchResult IncludeRules::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    Q_UNUSED(text);
    qCWarning(Log) << "Unresolved include rule";
    return offset;
}

Int::Int(DefinitionData &def, const HighlightingContextData::Rule::Int &data)
    : m_wordDelimiters(def.wordDelimiters)
{
    resolveAdditionalWordDelimiters(m_wordDelimiters, data.wordDelimiters);
}

MatchResult Int::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (offset > 0 && !m_wordDelimiters.contains(text.at(offset - 1))) {
        return offset;
    }

    while (offset < text.size() && isDigit(text.at(offset))) {
        ++offset;
    }
    return offset;
}

Rule::Ptr KeywordListRule::create(DefinitionData &def, const HighlightingContextData::Rule::Keyword &data, QStringView lookupContextName)
{
    /**
     * get our keyword list, if not found => bail out
     */
    auto *keywordList = def.keywordList(data.name);
    if (!keywordList) {
        qCWarning(Log) << "Rule: Unknown keyword list" << data.name << "in context" << lookupContextName << "of definition" << def.name;
        return Rule::Ptr();
    }

    if (keywordList->isEmpty()) {
        return Rule::Ptr();
    }

    /**
     * we might overwrite the case sensitivity
     * then we need to init the list for lookup of that sensitivity setting
     */
    if (data.hasCaseSensitivityOverride) {
        keywordList->initLookupForCaseSensitivity(data.caseSensitivityOverride);
    }

    return std::make_shared<KeywordListRule>(*keywordList, def, data);
}

KeywordListRule::KeywordListRule(const KeywordList &keywordList, DefinitionData &def, const HighlightingContextData::Rule::Keyword &data)
    : m_wordDelimiters(def.wordDelimiters)
    , m_keywordList(keywordList)
    , m_caseSensitivity(data.hasCaseSensitivityOverride ? data.caseSensitivityOverride : keywordList.caseSensitivity())
{
    resolveAdditionalWordDelimiters(m_wordDelimiters, data.wordDelimiters);
    m_hasSkipOffset = true;
}

MatchResult KeywordListRule::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    auto newOffset = offset;
    while (text.size() > newOffset && !m_wordDelimiters.contains(text.at(newOffset))) {
        ++newOffset;
    }
    if (newOffset == offset) {
        return offset;
    }

    if (m_keywordList.contains(text.mid(offset, newOffset - offset), m_caseSensitivity)) {
        return newOffset;
    }

    // we don't match, but we can skip until newOffset as we can't start a keyword in-between
    return MatchResult(offset, newOffset);
}

LineContinue::LineContinue(const HighlightingContextData::Rule::LineContinue &data)
    : m_char(data.char1)
{
}

MatchResult LineContinue::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (offset == text.size() - 1 && text.at(offset) == m_char) {
        return offset + 1;
    }
    return offset;
}

RangeDetect::RangeDetect(const HighlightingContextData::Rule::RangeDetect &data)
    : m_begin(data.begin)
    , m_end(data.end)
{
}

MatchResult RangeDetect::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (text.size() - offset < 2) {
        return offset;
    }
    if (text.at(offset) != m_begin) {
        return offset;
    }

    auto newOffset = offset + 1;
    while (newOffset < text.size()) {
        if (text.at(newOffset) == m_end) {
            return newOffset + 1;
        }
        ++newOffset;
    }
    return offset;
}

static QRegularExpression::PatternOptions makePattenOptions(const HighlightingContextData::Rule::RegExpr &data)
{
    return (data.isMinimal ? QRegularExpression::InvertedGreedinessOption : QRegularExpression::NoPatternOption)
        | (data.caseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption)
        // DontCaptureOption is removed by resolve() when necessary
        | QRegularExpression::DontCaptureOption
        // ensure Unicode support is enabled
        | QRegularExpression::UseUnicodePropertiesOption;
}

static void resolveRegex(QRegularExpression &regexp, Context *context)
{
    bool enableCapture = context && context->hasDynamicRule();

    // disable DontCaptureOption when reference a context with dynamic rule or
    // with invalid regex because DontCaptureOption with back reference capture is an error
    if (enableCapture || !regexp.isValid()) {
        regexp.setPatternOptions(regexp.patternOptions() & ~QRegularExpression::DontCaptureOption);
    }

    if (!regexp.isValid()) {
        qCDebug(Log) << "Invalid regexp:" << regexp.pattern();
    }
}

static MatchResult regexMatch(const QRegularExpression &regexp, QStringView text, int offset)
{
    /**
     * match the pattern
     */
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
    const auto result = regexp.matchView(text, offset, QRegularExpression::NormalMatch, QRegularExpression::DontCheckSubjectStringMatchOption);
#else
    const auto result = regexp.match(text, offset, QRegularExpression::NormalMatch, QRegularExpression::DontCheckSubjectStringMatchOption);
#endif
    if (result.capturedStart() == offset) {
        /**
         * we only need to compute the captured texts if we have real capture groups
         * highlightings should only address %1..%.., see e.g. replaceCaptures
         * DetectChar ignores %0, too
         */
        int lastCapturedIndex = result.lastCapturedIndex();
        if (lastCapturedIndex > 0) {
            QStringList captures;
            captures.reserve(lastCapturedIndex);
            // ignore the capturing group number 0
            for (int i = 1; i <= lastCapturedIndex; ++i)
                captures.push_back(result.captured(i));
            return MatchResult(offset + result.capturedLength(), std::move(captures));
        }

        /**
         * else: ignore the implicit 0 group we always capture, no need to allocate stuff for that
         */
        return MatchResult(offset + result.capturedLength());
    }

    /**
     * no match
     * we can always compute the skip offset as the highlighter will invalidate the cache for changed captures for dynamic rules!
     */
    return MatchResult(offset, result.capturedStart());
}

RegExpr::RegExpr(const HighlightingContextData::Rule::RegExpr &data)
    : m_regexp(data.pattern, makePattenOptions(data))
{
    m_hasSkipOffset = true;
}

void RegExpr::resolve()
{
    m_isResolved = true;

    resolveRegex(m_regexp, context().context());
}

MatchResult RegExpr::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (Q_UNLIKELY(!m_isResolved)) {
        const_cast<RegExpr *>(this)->resolve();
    }

    return regexMatch(m_regexp, text, offset);
}

DynamicRegExpr::DynamicRegExpr(const HighlightingContextData::Rule::RegExpr &data)
    : m_pattern(data.pattern)
    , m_patternOptions(makePattenOptions(data))
{
    m_dynamic = true;
    m_hasSkipOffset = true;
}

void DynamicRegExpr::resolve()
{
    m_isResolved = true;

    QRegularExpression regexp(m_pattern, m_patternOptions);
    resolveRegex(regexp, context().context());
    m_patternOptions = regexp.patternOptions();
}

MatchResult DynamicRegExpr::doMatch(QStringView text, int offset, const QStringList &captures, DynamicRegexpCache &dynamicRegexpCache) const
{
    if (Q_UNLIKELY(!m_isResolved)) {
        const_cast<DynamicRegExpr *>(this)->resolve();
    }

    /**
     * create new pattern with right instantiation
     */
    auto pattern = replaceCaptures(m_pattern, captures, true);
    auto &regexp = dynamicRegexpCache.compileRegexp(std::move(pattern), m_patternOptions);
    return regexMatch(regexp, text, offset);
}

StringDetect::StringDetect(const HighlightingContextData::Rule::StringDetect &data)
    : m_string(data.string)
    , m_caseSensitivity(data.caseSensitivity)
{
}

MatchResult StringDetect::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    return matchString(m_string, text, offset, m_caseSensitivity);
}

DynamicStringDetect::DynamicStringDetect(const HighlightingContextData::Rule::StringDetect &data)
    : m_string(data.string)
    , m_caseSensitivity(data.caseSensitivity)
{
    m_dynamic = true;
}

MatchResult DynamicStringDetect::doMatch(QStringView text, int offset, const QStringList &captures, DynamicRegexpCache &) const
{
    /**
     * for dynamic case: create new pattern with right instantiation
     */
    const auto pattern = replaceCaptures(m_string, captures, false);
    return matchString(pattern, text, offset, m_caseSensitivity);
}

WordDetect::WordDetect(DefinitionData &def, const HighlightingContextData::Rule::WordDetect &data)
    : m_wordDelimiters(def.wordDelimiters)
    , m_word(data.word)
    , m_caseSensitivity(data.caseSensitivity)
{
    resolveAdditionalWordDelimiters(m_wordDelimiters, data.wordDelimiters);
}

MatchResult WordDetect::doMatch(QStringView text, int offset, const QStringList &, DynamicRegexpCache &) const
{
    if (text.size() - offset < m_word.size()) {
        return offset;
    }

    /**
     * detect delimiter characters on the inner and outer boundaries of the string
     * NOTE: m_word isn't empty
     */
    if (offset > 0 && !m_wordDelimiters.contains(text.at(offset - 1)) && !m_wordDelimiters.contains(text.at(offset))) {
        return offset;
    }

    if (text.mid(offset, m_word.size()).compare(m_word, m_caseSensitivity) != 0) {
        return offset;
    }

    if (text.size() == offset + m_word.size() || m_wordDelimiters.contains(text.at(offset + m_word.size()))
        || m_wordDelimiters.contains(text.at(offset + m_word.size() - 1))) {
        return offset + m_word.size();
    }

    return offset;
}