qscripthighlighter.cpp 22.4 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
#include "qscripthighlighter.h"

#include <QtCore/QSet>
#include <QtCore/QtAlgorithms>
34
#include <QtCore/QDebug>
con's avatar
con committed
35

hjk's avatar
hjk committed
36
namespace SharedTools {
con's avatar
con committed
37
38

QScriptHighlighter::QScriptHighlighter(QTextDocument *parent)
39
40
    : QSyntaxHighlighter(parent),
      m_duiEnabled(false)
con's avatar
con committed
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
    qscriptKeywords.insert(QLatin1String("Infinity"));
    qscriptKeywords.insert(QLatin1String("NaN"));
    qscriptKeywords.insert(QLatin1String("abstract"));
    qscriptKeywords.insert(QLatin1String("boolean"));
    qscriptKeywords.insert(QLatin1String("break"));
    qscriptKeywords.insert(QLatin1String("byte"));
    qscriptKeywords.insert(QLatin1String("case"));
    qscriptKeywords.insert(QLatin1String("catch"));
    qscriptKeywords.insert(QLatin1String("char"));
    qscriptKeywords.insert(QLatin1String("class"));
    qscriptKeywords.insert(QLatin1String("const"));
    qscriptKeywords.insert(QLatin1String("constructor"));
    qscriptKeywords.insert(QLatin1String("continue"));
    qscriptKeywords.insert(QLatin1String("debugger"));
    qscriptKeywords.insert(QLatin1String("default"));
    qscriptKeywords.insert(QLatin1String("delete"));
    qscriptKeywords.insert(QLatin1String("do"));
    qscriptKeywords.insert(QLatin1String("double"));
    qscriptKeywords.insert(QLatin1String("else"));
    qscriptKeywords.insert(QLatin1String("enum"));
    qscriptKeywords.insert(QLatin1String("export"));
    qscriptKeywords.insert(QLatin1String("extends"));
    qscriptKeywords.insert(QLatin1String("false"));
    qscriptKeywords.insert(QLatin1String("final"));
    qscriptKeywords.insert(QLatin1String("finally"));
    qscriptKeywords.insert(QLatin1String("float"));
    qscriptKeywords.insert(QLatin1String("for"));
    qscriptKeywords.insert(QLatin1String("function"));
    qscriptKeywords.insert(QLatin1String("goto"));
    qscriptKeywords.insert(QLatin1String("if"));
    qscriptKeywords.insert(QLatin1String("implements"));
    qscriptKeywords.insert(QLatin1String("import"));
    qscriptKeywords.insert(QLatin1String("in"));
    qscriptKeywords.insert(QLatin1String("instanceof"));
    qscriptKeywords.insert(QLatin1String("int"));
    qscriptKeywords.insert(QLatin1String("interface"));
    qscriptKeywords.insert(QLatin1String("long"));
    qscriptKeywords.insert(QLatin1String("native"));
    qscriptKeywords.insert(QLatin1String("new"));
    qscriptKeywords.insert(QLatin1String("package"));
    qscriptKeywords.insert(QLatin1String("private"));
    qscriptKeywords.insert(QLatin1String("protected"));
    qscriptKeywords.insert(QLatin1String("public"));
    qscriptKeywords.insert(QLatin1String("return"));
    qscriptKeywords.insert(QLatin1String("short"));
    qscriptKeywords.insert(QLatin1String("static"));
    qscriptKeywords.insert(QLatin1String("super"));
    qscriptKeywords.insert(QLatin1String("switch"));
    qscriptKeywords.insert(QLatin1String("synchronized"));
    qscriptKeywords.insert(QLatin1String("this"));
    qscriptKeywords.insert(QLatin1String("throw"));
    qscriptKeywords.insert(QLatin1String("throws"));
    qscriptKeywords.insert(QLatin1String("transient"));
    qscriptKeywords.insert(QLatin1String("true"));
    qscriptKeywords.insert(QLatin1String("try"));
    qscriptKeywords.insert(QLatin1String("typeof"));
    qscriptKeywords.insert(QLatin1String("undefined"));
    qscriptKeywords.insert(QLatin1String("var"));
    qscriptKeywords.insert(QLatin1String("void"));
    qscriptKeywords.insert(QLatin1String("volatile"));
    qscriptKeywords.insert(QLatin1String("while"));
    qscriptKeywords.insert(QLatin1String("with"));    // end

con's avatar
con committed
105
106
107
    setFormats(defaultFormats());
}

108
109
110
111
112
113
bool QScriptHighlighter::isDuiEnabled() const
{ return m_duiEnabled; }

void QScriptHighlighter::setDuiEnabled(bool enabled)
{ m_duiEnabled = enabled; }

con's avatar
con committed
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
void QScriptHighlighter::highlightBlock(const QString &text)
{
    // states
    enum {
        StateStandard,
        StateCommentStart1,
        StateCCommentStart2,
        StateCppCommentStart2,
        StateCComment,
        StateCppComment,
        StateCCommentEnd1,
        StateCCommentEnd2,
        StateStringStart,
        StateString,
        StateStringEnd,
        StateString2Start,
        StateString2,
        StateString2End,
        StateNumber,
        StatePreProcessor,
        NumStates
    };
    // tokens
    enum {
        InputAlpha,
        InputNumber,
        InputAsterix,
        InputSlash,
        InputParen,
        InputSpace,
        InputHash,
        InputQuotation,
        InputApostrophe,
        InputSep,
        NumInputs
    };

    static const uchar table[NumStates][NumInputs] = {
        { StateStandard,      StateNumber,     StateStandard,       StateCommentStart1,    StateStandard,   StateStandard,   StatePreProcessor, StateStringStart, StateString2Start, StateStandard }, // StateStandard
        { StateStandard,      StateNumber,   StateCCommentStart2, StateCppCommentStart2, StateStandard,   StateStandard,   StatePreProcessor, StateStringStart, StateString2Start, StateStandard }, // StateCommentStart1
        { StateCComment,      StateCComment,   StateCCommentEnd1,   StateCComment,         StateCComment,   StateCComment,   StateCComment,     StateCComment,    StateCComment,     StateCComment }, // StateCCommentStart2
        { StateCppComment,    StateCppComment, StateCppComment,     StateCppComment,       StateCppComment, StateCppComment, StateCppComment,   StateCppComment,  StateCppComment,   StateCppComment }, // CppCommentStart2
        { StateCComment,      StateCComment,   StateCCommentEnd1,   StateCComment,         StateCComment,   StateCComment,   StateCComment,     StateCComment,    StateCComment,     StateCComment }, // StateCComment
        { StateCppComment,    StateCppComment, StateCppComment,     StateCppComment,       StateCppComment, StateCppComment, StateCppComment,   StateCppComment,  StateCppComment,   StateCppComment }, // StateCppComment
        { StateCComment,      StateCComment,   StateCCommentEnd1,   StateCCommentEnd2,     StateCComment,   StateCComment,   StateCComment,     StateCComment,    StateCComment,     StateCComment }, // StateCCommentEnd1
        { StateStandard,      StateNumber,     StateStandard,       StateCommentStart1,    StateStandard,   StateStandard,   StatePreProcessor, StateStringStart, StateString2Start, StateStandard }, // StateCCommentEnd2
        { StateString,        StateString,     StateString,         StateString,           StateString,     StateString,     StateString,       StateStringEnd,   StateString,       StateString }, // StateStringStart
        { StateString,        StateString,     StateString,         StateString,           StateString,     StateString,     StateString,       StateStringEnd,   StateString,       StateString }, // StateString
        { StateStandard,      StateStandard,   StateStandard,       StateCommentStart1,    StateStandard,   StateStandard,   StatePreProcessor, StateStringStart, StateString2Start, StateStandard }, // StateStringEnd
        { StateString2,       StateString2,    StateString2,        StateString2,          StateString2,    StateString2,    StateString2,      StateString2,     StateString2End,   StateString2 }, // StateString2Start
        { StateString2,       StateString2,    StateString2,        StateString2,          StateString2,    StateString2,    StateString2,      StateString2,     StateString2End,   StateString2 }, // StateString2
        { StateStandard,      StateStandard,   StateStandard,       StateCommentStart1,    StateStandard,   StateStandard,   StatePreProcessor, StateStringStart, StateString2Start, StateStandard }, // StateString2End
        { StateNumber,        StateNumber,     StateStandard,       StateCommentStart1,    StateStandard,   StateStandard,   StatePreProcessor, StateStringStart, StateString2Start, StateStandard }, // StateNumber
        { StatePreProcessor,  StateStandard,   StateStandard,       StateCommentStart1,    StateStandard,   StateStandard,   StatePreProcessor, StateStringStart, StateString2Start, StateStandard } // StatePreProcessor
    };

    QString buffer;
    buffer.reserve(text.length());
    QTextCharFormat emptyFormat;

    int state = onBlockStart();
    if (text.isEmpty()) {
        onBlockEnd(state, 0);
        return;
    }

    int input = -1;
    int i = 0;
    bool lastWasBackSlash = false;
    bool makeLastStandard = false;

    static const QString alphabeth = QLatin1String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
    static const QString mathChars = QString::fromLatin1("xXeE");
    static const QString numbers = QString::fromLatin1("0123456789");
    bool questionMark = false;
    QChar lastChar;

    int firstNonSpace = -1;
    int lastNonSpace = -1;

    forever {
        const QChar c = text.at(i);

197
198
        bool lookAtBinding = false;

con's avatar
con committed
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
        if (lastWasBackSlash) {
            input = InputSep;
        } else {
            switch (c.toLatin1()) {
                case '*':
                    input = InputAsterix;
                    break;
                case '/':
                    input = InputSlash;
                    break;
                case '(': case '[': case '{':
                    input = InputParen;
                    if (state == StateStandard
                        || state == StateNumber
                        || state == StatePreProcessor
                        || state == StateCCommentEnd2
                        || state == StateCCommentEnd1
                        || state == StateString2End
                        || state == StateStringEnd
                       )
                    onOpeningParenthesis(c, i);
                    break;
                case ')': case ']': case '}':
                    input = InputParen;
                    if (state == StateStandard
                        || state == StateNumber
                        || state == StatePreProcessor
                        || state == StateCCommentEnd2
                        || state == StateCCommentEnd1
                        || state == StateString2End
                        || state == StateStringEnd
                       ) {
                        onClosingParenthesis(c, i);
                    }
                    break;
                case '#':
                    input = InputHash;
                    break;
                case '"':
                    input = InputQuotation;
                    break;
                case '\'':
                    input = InputApostrophe;
                    break;
                case ' ':
                    input = InputSpace;
                    break;
                case '1': case '2': case '3': case '4': case '5':
                case '6': case '7': case '8': case '9': case '0':
                    if (alphabeth.contains(lastChar)
                        && (!mathChars.contains(lastChar) || !numbers.contains(text.at(i - 1)))) {
                        input = InputAlpha;
                    } else {
                        if (input == InputAlpha && numbers.contains(lastChar))
                            input = InputAlpha;
                        else
                            input = InputNumber;
                    }
                    break;
                case ':': {
259
                              input = InputSep;
con's avatar
con committed
260
261
262
                              QChar nextChar = ' ';
                              if (i < text.length() - 1)
                                  nextChar = text.at(i + 1);
263

con's avatar
con committed
264
265
                              if (state == StateStandard && !questionMark &&
                                  lastChar != ':' && nextChar != ':') {
266
                                  int start = i - 1;
267
268
269
270
271
272
273
274
275

                                  // skip white spaces
                                  for (; start != -1; --start) {
                                      if (! text.at(start).isSpace())
                                          break;
                                  }

                                  int lastNonSpace = start + 1;

276
                                  for (; start != -1; --start) {
277
278
                                      const QChar ch = text.at(start);
                                      if (! (ch.isLetterOrNumber() || ch == QLatin1Char('_') || ch == QLatin1Char('.')))
279
                                          break;
con's avatar
con committed
280
                                  }
281

282
                                  ++start;
283

284
285
                                  lookAtBinding = true;

286
287
288
289
290
                                  if (m_duiEnabled && text.midRef(start, lastNonSpace - start) == QLatin1String("id")) {
                                      setFormat(start, i - start, m_formats[KeywordFormat]);
                                  } else {
                                      setFormat(start, i - start, m_formats[LabelFormat]);
                                  }
con's avatar
con committed
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
                              }
                              break;
                          }
                default: {
                             if (!questionMark && c == QLatin1Char('?'))
                                 questionMark = true;
                             if (c.isLetter() || c == QLatin1Char('_'))
                                 input = InputAlpha;
                             else
                                 input = InputSep;
                         } break;
            }
        }

        if (input != InputSpace) {
            if (firstNonSpace < 0)
                firstNonSpace = i;
            lastNonSpace = i;
        }

        lastWasBackSlash = !lastWasBackSlash && c == QLatin1Char('\\');

        if (input == InputAlpha)
            buffer += c;

        state = table[state][input];

        switch (state) {
            case StateStandard: {
                                    setFormat(i, 1, emptyFormat);
                                    if (makeLastStandard)
                                        setFormat(i - 1, 1, emptyFormat);
                                    makeLastStandard = false;
                                    if (!buffer.isEmpty() && input != InputAlpha ) {
325
326
                                        if (! lookAtBinding)
                                            highlightKeyword(i, buffer);
con's avatar
con committed
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
                                        buffer.clear();
                                    }
                                } break;
            case StateCommentStart1:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = true;
                                buffer.resize(0);
                                break;
            case StateCCommentStart2:
                                setFormat(i - 1, 2, m_formats[CommentFormat]);
                                makeLastStandard = false;
                                buffer.resize(0);
                                break;
            case StateCppCommentStart2:
                                setFormat(i - 1, 2, m_formats[CommentFormat]);
                                makeLastStandard = false;
                                buffer.resize(0);
                                break;
            case StateCComment:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, m_formats[CommentFormat]);
                                buffer.resize(0);
                                break;
            case StateCppComment:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, m_formats[CommentFormat]);
                                buffer.resize(0);
                                break;
            case StateCCommentEnd1:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, m_formats[CommentFormat]);
                                buffer.resize(0);
                                break;
            case StateCCommentEnd2:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, m_formats[CommentFormat]);
                                buffer.resize(0);
                                break;
            case StateStringStart:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, emptyFormat);
                                buffer.resize(0);
                                break;
            case StateString:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, m_formats[StringFormat]);
                                buffer.resize(0);
                                break;
            case StateStringEnd:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, emptyFormat);
                                buffer.resize(0);
                                break;
            case StateString2Start:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, emptyFormat);
                                buffer.resize(0);
                                break;
            case StateString2:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, m_formats[StringFormat]);
                                buffer.resize(0);
                                break;
            case StateString2End:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, emptyFormat);
                                buffer.resize(0);
                                break;
            case StateNumber:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat( i, 1, m_formats[NumberFormat]);
                                buffer.resize(0);
                                break;
            case StatePreProcessor:
                                if (makeLastStandard)
                                    setFormat(i - 1, 1, emptyFormat);
                                makeLastStandard = false;
                                setFormat(i, 1, m_formats[PreProcessorFormat]);
                                buffer.resize(0);
                                break;
        }

        lastChar = c;
        i++;
        if (i >= text.length())
            break;
    }

    highlightKeyword(text.length(), buffer);

    if (state == StateCComment
        || state == StateCCommentEnd1
        || state == StateCCommentStart2
       ) {
        state = StateCComment;
    } else {
        state = StateStandard;
    }

    onBlockEnd(state, firstNonSpace);
}

void QScriptHighlighter::highlightKeyword(int currentPos, const QString &buffer)
{
    if (buffer.isEmpty())
        return;

457
    if (m_duiEnabled && buffer.at(0).isUpper() || (! m_duiEnabled && buffer.at(0) == QLatin1Char('Q'))) {
con's avatar
con committed
458
459
        setFormat(currentPos - buffer.length(), buffer.length(), m_formats[TypeFormat]);
    } else {
460
        if (qscriptKeywords.contains(buffer))
con's avatar
con committed
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
            setFormat(currentPos - buffer.length(), buffer.length(), m_formats[KeywordFormat]);
    }
}

const QVector<QTextCharFormat> &QScriptHighlighter::defaultFormats()
{
    static QVector<QTextCharFormat> rc;
    if (rc.empty()) {
        rc.resize(NumFormats);
        rc[NumberFormat].setForeground(Qt::blue);
        rc[StringFormat].setForeground(Qt::darkGreen);
        rc[TypeFormat].setForeground(Qt::darkMagenta);
        rc[KeywordFormat].setForeground(Qt::darkYellow);
        rc[LabelFormat].setForeground(Qt::darkRed);
        rc[CommentFormat].setForeground(Qt::red);
        rc[CommentFormat].setFontItalic(true);
        rc[PreProcessorFormat].setForeground(Qt::darkBlue);
    }
    return rc;
}

void QScriptHighlighter::setFormats(const QVector<QTextCharFormat> &s)
{
    qCopy(s.constBegin(), s.constEnd(), m_formats);
}

hjk's avatar
hjk committed
487
488
int QScriptHighlighter::onBlockStart()
{
con's avatar
con committed
489
490
491
492
493
494
495
496
497
    int state = 0;
    int previousState = previousBlockState();
    if (previousState != -1)
        state = previousState;
    return state;
}
void QScriptHighlighter::onOpeningParenthesis(QChar, int) {}
void QScriptHighlighter::onClosingParenthesis(QChar, int) {}
void QScriptHighlighter::onBlockEnd(int state, int) { return setCurrentBlockState(state); }
hjk's avatar
hjk committed
498
499

} // namespace SharedTools
con's avatar
con committed
500