summaryrefslogtreecommitdiff
path: root/src/plugins/texteditor/generichighlighter/rule.cpp
blob: bba073972194c81db2f76595a3cd4b9465c68ede (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
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "rule.h"
#include "progressdata.h"
#include "highlightdefinition.h"
#include "reuse.h"

#include <functional>

using namespace TextEditor;
using namespace Internal;

const QLatin1Char Rule::kBackSlash('\\');
const QLatin1Char Rule::kUnderscore('_');
const QLatin1Char Rule::kDot('.');
const QLatin1Char Rule::kPlus('+');
const QLatin1Char Rule::kMinus('-');
const QLatin1Char Rule::kZero('0');
const QLatin1Char Rule::kQuote('\"');
const QLatin1Char Rule::kSingleQuote('\'');
const QLatin1Char Rule::kQuestion('?');
const QLatin1Char Rule::kX('x');
const QLatin1Char Rule::kA('a');
const QLatin1Char Rule::kB('b');
const QLatin1Char Rule::kE('e');
const QLatin1Char Rule::kF('f');
const QLatin1Char Rule::kN('n');
const QLatin1Char Rule::kR('r');
const QLatin1Char Rule::kT('t');
const QLatin1Char Rule::kV('v');
const QLatin1Char Rule::kOpeningBrace('{');
const QLatin1Char Rule::kClosingBrace('}');

Rule::Rule(bool consumesNonSpace) :
    m_lookAhead(false), m_firstNonSpace(false), m_column(-1), m_consumesNonSpace(consumesNonSpace)
{}

Rule::~Rule()
{}

void Rule::setContext(const QString &context)
{ m_context = context; }

const QString &Rule::context() const
{ return m_context; }

void Rule::setItemData(const QString &itemData)
{ m_itemData = itemData; }

const QString &Rule::itemData() const
{ return m_itemData; }

void Rule::setBeginRegion(const QString &begin)
{ m_beginRegion = begin; }

const QString &Rule::beginRegion() const
{ return m_beginRegion; }

void Rule::setEndRegion(const QString &end)
{ m_endRegion = end; }

const QString &Rule::endRegion() const
{ return m_endRegion; }

void Rule::setLookAhead(const QString &lookAhead)
{ m_lookAhead = toBool(lookAhead); }

bool Rule::isLookAhead() const
{ return m_lookAhead; }

void Rule::setFirstNonSpace(const QString &firstNonSpace)
{ m_firstNonSpace = toBool(firstNonSpace); }

bool Rule::isFirstNonSpace() const
{ return m_firstNonSpace; }

void Rule::setColumn(const QString &column)
{
    bool ok;
    m_column = column.toInt(&ok);
    if (!ok)
        m_column = -1;
}

int Rule::column() const
{ return m_column; }

void Rule::addChild(const QSharedPointer<Rule> &rule)
{ m_children.append(rule); }

bool Rule::hasChildren() const
{ return !m_children.isEmpty(); }

const QList<QSharedPointer<Rule> > &Rule::children() const
{ return m_children; }

void Rule::setDefinition(const QSharedPointer<HighlightDefinition> &definition)
{ m_definition = definition; }

const QSharedPointer<HighlightDefinition> &Rule::definition() const
{ return m_definition; }

template <class predicate_t>
bool Rule::predicateMatchSucceed(const QString &text,
                                 const int length,
                                 ProgressData *progress,
                                 const predicate_t &p) const
{
    int original = progress->offset();
    while (progress->offset() < length && p(text.at(progress->offset())))
        progress->incrementOffset();

    if (original != progress->offset())
        return true;

    return false;
}

bool Rule::charPredicateMatchSucceed(const QString &text,
                                     const int length,
                                     ProgressData *progress,
                                     bool (QChar::* predicate)() const) const
{
    return predicateMatchSucceed(text, length, progress, std::mem_fun_ref(predicate));
}

bool Rule::charPredicateMatchSucceed(const QString &text,
                                     const int length,
                                     ProgressData *progress,
                                     bool (*predicate)(const QChar &)) const
{
    return predicateMatchSucceed(text, length, progress, std::ptr_fun(predicate));
}

bool Rule::matchSucceed(const QString &text, const int length, ProgressData *progress)
{
    if (m_firstNonSpace && !progress->isOnlySpacesSoFar())
        return false;

    if (m_column != -1 && m_column != progress->offset())
        return false;

    int original = progress->offset();
    if (doMatchSucceed(text, length, progress)) {
        if (progress->isOnlySpacesSoFar() && !m_lookAhead && m_consumesNonSpace)
            progress->setOnlySpacesSoFar(false);

        if (m_lookAhead)
            progress->setOffset(original);

        return true;
    }

    return false;
}

Rule *Rule::clone() const
{ return doClone(); }

void Rule::progressFinished()
{ doProgressFinished(); }

bool Rule::matchCharacter(const QString &text,
                          const int length,
                          ProgressData *progress,
                          const QChar &c,
                          bool saveRestoreOffset) const
{
    Q_UNUSED(length)
    Q_ASSERT(progress->offset() < length);

    if (text.at(progress->offset()) == c) {
        if (saveRestoreOffset)
            progress->saveOffset();
        progress->incrementOffset();
        return true;
    }

    return false;
}

bool Rule::matchEscapeSequence(const QString &text,
                               const int length,
                               ProgressData *progress,
                               bool saveRestoreOffset) const
{
    if (matchCharacter(text, length, progress, kBackSlash, saveRestoreOffset)) {

        if (progress->offset() < length) {
            const QChar &c = text.at(progress->offset());
            if (c == kA || c == kB || c == kE || c == kF || c == kN || c == kR || c == kT ||
                c == kV || c == kQuestion || c == kSingleQuote || c == kQuote || c == kBackSlash) {
                progress->incrementOffset();
                return true;
            } else if (saveRestoreOffset) {
                    progress->restoreOffset();
            }
        } else if (saveRestoreOffset) {
            progress->restoreOffset();
        }
    }

    return false;
}

bool Rule::matchOctalSequence(const QString &text,
                              const int length,
                              ProgressData *progress,
                              bool saveRestoreOffset) const
{
    // An octal sequence is identified as in the C++ Standard.
    // octal-escape-sequence:
    //   \ octal-digit
    //   \ octal-digit octal-digit
    //   \ octal-digit octal-digit octal-digit

    if (matchCharacter(text, length, progress, kBackSlash, saveRestoreOffset)) {

        int count = 0;
        while (progress->offset() < length &&
               count < 3 &&
               isOctalDigit(text.at(progress->offset()))) {
            ++count;
            progress->incrementOffset();
        }

        if (count > 0)
            return true;
        else if (saveRestoreOffset)
            progress->restoreOffset();
    }

    return false;
}

bool Rule::matchHexSequence(const QString &text,
                            const int length,
                            ProgressData *progress,
                            bool saveRestoreOffset) const
{
    // An hex sequence is identified as in the C++ Standard.
    // hexadecimal-escape-sequence:
    //   \x hexadecimal-digit
    //   hexadecimal-escape-sequence hexadecimal-digit

    if (matchCharacter(text, length, progress, kBackSlash, saveRestoreOffset)) {

        if (progress->offset() < length && matchCharacter(text, length, progress, kX, false)) {
            bool found = false;
            while (progress->offset() < length && isHexDigit(text.at(progress->offset()))) {
                if (!found)
                    found = true;
                progress->incrementOffset();
            }

            if (found)
                return true;
            else if (saveRestoreOffset)
                progress->restoreOffset();
        } else if (saveRestoreOffset) {
            progress->restoreOffset();
        }
    }

    return false;
}