OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / shared / proparser / proitems.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 PROITEMS_H
34 #define PROITEMS_H
35
36 #include <QtCore/QString>
37 #include <QtCore/QVector>
38
39 QT_BEGIN_NAMESPACE
40
41 #ifdef PROPARSER_THREAD_SAFE
42 typedef QAtomicInt ProItemRefCount;
43 #else
44 class ProItemRefCount {
45 public:
46     ProItemRefCount(int cnt = 0) : m_cnt(cnt) {}
47     bool ref() { return ++m_cnt != 0; }
48     bool deref() { return --m_cnt != 0; }
49     ProItemRefCount &operator=(int value) { m_cnt = value; return *this; }
50 private:
51     int m_cnt;
52 };
53 #endif
54
55 namespace ProStringConstants {
56 enum OmitPreHashing { NoHash };
57 }
58
59 class ProStringList;
60
61 class ProString {
62 public:
63     ProString();
64     ProString(const ProString &other);
65     ProString(const ProString &other, ProStringConstants::OmitPreHashing);
66     explicit ProString(const QString &str);
67     ProString(const QString &str, ProStringConstants::OmitPreHashing);
68     explicit ProString(const char *str);
69     ProString(const char *str, ProStringConstants::OmitPreHashing);
70     ProString(const QString &str, int offset, int length);
71     ProString(const QString &str, int offset, int length, uint hash);
72     ProString(const QString &str, int offset, int length, ProStringConstants::OmitPreHashing);
73     QString toQString() const;
74     QString &toQString(QString &tmp) const;
75     ProString &operator+=(const ProString &other);
76     ProString &append(const ProString &other, bool *pending = 0);
77     ProString &append(const ProStringList &other, bool *pending = 0, bool skipEmpty1st = false);
78     bool operator==(const ProString &other) const;
79     bool operator==(const QString &other) const;
80     bool operator==(const QLatin1String &other) const;
81     bool operator!=(const ProString &other) const { return !(*this == other); }
82     bool operator!=(const QString &other) const { return !(*this == other); }
83     bool operator!=(const QLatin1String &other) const { return !(*this == other); }
84     bool isEmpty() const { return !m_length; }
85     int size() const { return m_length; }
86     const QChar *constData() const { return m_string.constData() + m_offset; }
87     ProString mid(int off, int len = -1) const;
88     ProString left(int len) const { return mid(0, len); }
89     ProString right(int len) const { return mid(qMax(0, size() - len)); }
90     ProString trimmed() const;
91     void clear() { m_string.clear(); m_length = 0; }
92
93     static uint hash(const QChar *p, int n);
94
95 private:
96     QString m_string;
97     int m_offset, m_length;
98     mutable uint m_hash;
99     QChar *prepareAppend(int extraLen);
100     uint updatedHash() const;
101     friend uint qHash(const ProString &str);
102     friend QString operator+(const ProString &one, const ProString &two);
103 };
104 Q_DECLARE_TYPEINFO(ProString, Q_MOVABLE_TYPE);
105
106 uint qHash(const ProString &str);
107 QString operator+(const ProString &one, const ProString &two);
108 inline QString operator+(const ProString &one, const QString &two)
109     { return one + ProString(two, ProStringConstants::NoHash); }
110 inline QString operator+(const QString &one, const ProString &two)
111     { return ProString(one, ProStringConstants::NoHash) + two; }
112
113 class ProStringList : public QVector<ProString> {
114 public:
115     ProStringList() {}
116     ProStringList(const ProString &str) { *this << str; }
117     QString join(const QString &sep) const;
118     void removeDuplicates();
119 };
120
121 // These token definitions affect both ProFileEvaluator and ProWriter
122 enum ProToken {
123     TokTerminator = 0,  // end of stream (possibly not included in length; must be zero)
124     TokLine,            // line marker:
125                         // - line (1)
126     TokAssign,          // variable =
127     TokAppend,          // variable +=
128     TokAppendUnique,    // variable *=
129     TokRemove,          // variable -=
130     TokReplace,         // variable ~=
131                         // previous literal/expansion is a variable manipulation
132                         // - value expression + TokValueTerminator
133     TokValueTerminator, // assignment value terminator
134     TokLiteral,         // literal string (fully dequoted)
135                         // - length (1)
136                         // - string data (length; unterminated)
137     TokHashLiteral,     // literal string with hash (fully dequoted)
138                         // - hash (2)
139                         // - length (1)
140                         // - string data (length; unterminated)
141     TokVariable,        // qmake variable expansion
142                         // - hash (2)
143                         // - name length (1)
144                         // - name (name length; unterminated)
145     TokProperty,        // qmake property expansion
146                         // - name length (1)
147                         // - name (name length; unterminated)
148     TokEnvVar,          // environment variable expansion
149                         // - name length (1)
150                         // - name (name length; unterminated)
151     TokFuncName,        // replace function expansion
152                         // - hash (2)
153                         // - name length (1)
154                         // - name (name length; unterminated)
155                         // - ((nested expansion + TokArgSeparator)* + nested expansion)?
156                         // - TokFuncTerminator
157     TokArgSeparator,    // function argument separator
158     TokFuncTerminator,  // function argument list terminator
159     TokCondition,       // previous literal/expansion is a conditional
160     TokTestCall,        // previous literal/expansion is a test function call
161                         // - ((nested expansion + TokArgSeparator)* + nested expansion)?
162                         // - TokFuncTerminator
163     TokNot,             // '!' operator
164     TokAnd,             // ':' operator
165     TokOr,              // '|' operator
166     TokBranch,          // branch point:
167                         // - then block length (2)
168                         // - then block + TokTerminator (then block length)
169                         // - else block length (2)
170                         // - else block + TokTerminator (else block length)
171     TokForLoop,         // for loop:
172                         // - variable name: hash (2), length (1), chars (length)
173                         // - expression: length (2), bytes + TokValueTerminator (length)
174                         // - body length (2)
175                         // - body + TokTerminator (body length)
176     TokTestDef,         // test function definition:
177     TokReplaceDef,      // replace function definition:
178                         // - function name: hash (2), length (1), chars (length)
179                         // - body length (2)
180                         // - body + TokTerminator (body length)
181     TokMask = 0xff,
182     TokQuoted = 0x100,  // The expression is quoted => join expanded stringlist
183     TokNewStr = 0x200   // Next stringlist element
184 };
185
186 class ProFile
187 {
188 public:
189     explicit ProFile(const QString &fileName);
190     ~ProFile();
191
192     QString fileName() const { return m_fileName; }
193     QString directoryName() const { return m_directoryName; }
194     const QString &items() const { return m_proitems; }
195     QString *itemsRef() { return &m_proitems; }
196     const ushort *tokPtr() const { return (const ushort *)m_proitems.constData(); }
197
198     void ref() { m_refCount.ref(); }
199     void deref() { if (!m_refCount.deref()) delete this; }
200
201     bool isOk() const { return m_ok; }
202     void setOk(bool ok) { m_ok = ok; }
203
204 private:
205     ProItemRefCount m_refCount;
206     QString m_proitems;
207     QString m_fileName;
208     QString m_directoryName;
209     bool m_ok;
210 };
211
212 QT_END_NAMESPACE
213
214 #endif // PROITEMS_H