OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / generichighlighter / rule.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #ifndef RULE_H
34 #define RULE_H
35
36 #include <QtCore/QString>
37 #include <QtCore/QList>
38 #include <QtCore/QSharedPointer>
39
40 namespace TextEditor {
41 namespace Internal {
42
43 class ProgressData;
44 class HighlightDefinition;
45
46 class Rule
47 {
48 public:
49     Rule(bool consumesNonSpace = true);
50     virtual ~Rule();
51
52     void setContext(const QString &context);
53     const QString &context() const;
54
55     void setItemData(const QString &itemData);
56     const QString &itemData() const;
57
58     void setBeginRegion(const QString &begin);
59     const QString &beginRegion() const;
60
61     void setEndRegion(const QString &end);
62     const QString &endRegion() const;
63
64     void setLookAhead(const QString &lookAhead);
65     bool isLookAhead() const;
66
67     void setFirstNonSpace(const QString &firstNonSpace);
68     bool isFirstNonSpace() const;
69
70     void setColumn(const QString &column);
71     int column() const;
72
73     void addChild(const QSharedPointer<Rule> &rule);
74     const QList<QSharedPointer<Rule> > &children() const;
75     bool hasChildren() const;
76
77     void setDefinition(const QSharedPointer<HighlightDefinition> &definition);
78     const QSharedPointer<HighlightDefinition> &definition() const;
79
80     bool matchSucceed(const QString &text, const int length, ProgressData *progress);
81
82     Rule *clone() const;
83
84     void progressFinished();
85
86 protected:
87     bool charPredicateMatchSucceed(const QString &text,
88                                    const int length,
89                                    ProgressData *progress,
90                                    bool (QChar::* predicate)() const) const;
91     bool charPredicateMatchSucceed(const QString &text,
92                                    const int length,
93                                    ProgressData *progress,
94                                    bool (*predicate)(const QChar &)) const;
95
96     bool matchCharacter(const QString &text,
97                         const int length,
98                         ProgressData *progress,
99                         const QChar &c,
100                         bool saveRestoreOffset = true) const;
101     bool matchEscapeSequence(const QString &text,
102                              const int length,
103                              ProgressData *progress,
104                              bool saveRestoreOffset = true) const;
105     bool matchOctalSequence(const QString &text,
106                             const int length,
107                             ProgressData *progress,
108                             bool saveRestoreOffset = true) const;
109     bool matchHexSequence(const QString &text,
110                           const int length,
111                           ProgressData *progress,
112                           bool saveRestoreOffset = true) const;
113
114     static const QLatin1Char kBackSlash;
115     static const QLatin1Char kUnderscore;
116     static const QLatin1Char kDot;
117     static const QLatin1Char kPlus;
118     static const QLatin1Char kMinus;
119     static const QLatin1Char kZero;
120     static const QLatin1Char kQuote;
121     static const QLatin1Char kSingleQuote;
122     static const QLatin1Char kQuestion;
123     static const QLatin1Char kX;
124     static const QLatin1Char kA;
125     static const QLatin1Char kB;
126     static const QLatin1Char kE;
127     static const QLatin1Char kF;
128     static const QLatin1Char kN;
129     static const QLatin1Char kR;
130     static const QLatin1Char kT;
131     static const QLatin1Char kV;
132     static const QLatin1Char kOpeningBrace;
133     static const QLatin1Char kClosingBrace;
134
135 private:
136     virtual bool doMatchSucceed(const QString &text, const int length, ProgressData *progress) = 0;
137
138     virtual Rule *doClone() const = 0;
139
140     virtual void doProgressFinished() {}
141
142     template <class predicate_t>
143     bool predicateMatchSucceed(const QString &text,
144                                const int length,
145                                ProgressData *progress,
146                                const predicate_t &p) const;
147
148     QString m_context;
149     QString m_itemData;
150     QString m_beginRegion;
151     QString m_endRegion;
152     bool m_lookAhead;
153     bool m_firstNonSpace;
154     int m_column;
155     bool m_consumesNonSpace;
156
157     QList<QSharedPointer<Rule> > m_children;
158
159     // Rules are represented within contexts. However, they have their own definition because
160     // of externally included rules.
161     QSharedPointer<HighlightDefinition> m_definition;
162 };
163
164 } // namespace Internal
165 } // namespace TextEditor
166
167 #endif // RULE_H