1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "highlighter.h"
52
53//! [0]
54Highlighter::Highlighter(QTextDocument *parent)
55 : QSyntaxHighlighter(parent)
56{
57 HighlightingRule rule;
58
59 keywordFormat.setForeground(Qt::darkBlue);
60 keywordFormat.setFontWeight(QFont::Bold);
61 const QString keywordPatterns[] = {
62 QStringLiteral("\\bchar\\b"), QStringLiteral("\\bclass\\b"), QStringLiteral("\\bconst\\b"),
63 QStringLiteral("\\bdouble\\b"), QStringLiteral("\\benum\\b"), QStringLiteral("\\bexplicit\\b"),
64 QStringLiteral("\\bfriend\\b"), QStringLiteral("\\binline\\b"), QStringLiteral("\\bint\\b"),
65 QStringLiteral("\\blong\\b"), QStringLiteral("\\bnamespace\\b"), QStringLiteral("\\boperator\\b"),
66 QStringLiteral("\\bprivate\\b"), QStringLiteral("\\bprotected\\b"), QStringLiteral("\\bpublic\\b"),
67 QStringLiteral("\\bshort\\b"), QStringLiteral("\\bsignals\\b"), QStringLiteral("\\bsigned\\b"),
68 QStringLiteral("\\bslots\\b"), QStringLiteral("\\bstatic\\b"), QStringLiteral("\\bstruct\\b"),
69 QStringLiteral("\\btemplate\\b"), QStringLiteral("\\btypedef\\b"), QStringLiteral("\\btypename\\b"),
70 QStringLiteral("\\bunion\\b"), QStringLiteral("\\bunsigned\\b"), QStringLiteral("\\bvirtual\\b"),
71 QStringLiteral("\\bvoid\\b"), QStringLiteral("\\bvolatile\\b"), QStringLiteral("\\bbool\\b")
72 };
73 for (const QString &pattern : keywordPatterns) {
74 rule.pattern = QRegularExpression(pattern);
75 rule.format = keywordFormat;
76 highlightingRules.append(rule);
77//! [0] //! [1]
78 }
79//! [1]
80
81//! [2]
82 classFormat.setFontWeight(QFont::Bold);
83 classFormat.setForeground(Qt::darkMagenta);
84 rule.pattern = QRegularExpression(QStringLiteral("\\bQ[A-Za-z]+\\b"));
85 rule.format = classFormat;
86 highlightingRules.append(rule);
87//! [2]
88
89//! [3]
90 singleLineCommentFormat.setForeground(Qt::red);
91 rule.pattern = QRegularExpression(QStringLiteral("//[^\n]*"));
92 rule.format = singleLineCommentFormat;
93 highlightingRules.append(rule);
94
95 multiLineCommentFormat.setForeground(Qt::red);
96//! [3]
97
98//! [4]
99 quotationFormat.setForeground(Qt::darkGreen);
100 rule.pattern = QRegularExpression(QStringLiteral("\".*\""));
101 rule.format = quotationFormat;
102 highlightingRules.append(rule);
103//! [4]
104
105//! [5]
106 functionFormat.setFontItalic(true);
107 functionFormat.setForeground(Qt::blue);
108 rule.pattern = QRegularExpression(QStringLiteral("\\b[A-Za-z0-9_]+(?=\\()"));
109 rule.format = functionFormat;
110 highlightingRules.append(rule);
111//! [5]
112
113//! [6]
114 commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
115 commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
116}
117//! [6]
118
119//! [7]
120void Highlighter::highlightBlock(const QString &text)
121{
122 for (const HighlightingRule &rule : qAsConst(highlightingRules)) {
123 QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
124 while (matchIterator.hasNext()) {
125 QRegularExpressionMatch match = matchIterator.next();
126 setFormat(match.capturedStart(), match.capturedLength(), rule.format);
127 }
128 }
129//! [7] //! [8]
130 setCurrentBlockState(0);
131//! [8]
132
133//! [9]
134 int startIndex = 0;
135 if (previousBlockState() != 1)
136 startIndex = text.indexOf(commentStartExpression);
137
138//! [9] //! [10]
139 while (startIndex >= 0) {
140//! [10] //! [11]
141 QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
142 int endIndex = match.capturedStart();
143 int commentLength = 0;
144 if (endIndex == -1) {
145 setCurrentBlockState(1);
146 commentLength = text.length() - startIndex;
147 } else {
148 commentLength = endIndex - startIndex
149 + match.capturedLength();
150 }
151 setFormat(startIndex, commentLength, multiLineCommentFormat);
152 startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
153 }
154}
155//! [11]
156