OSDN Git Service

It's 2011 now.
[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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef PROITEMS_H
35 #define PROITEMS_H
36
37 #include <QtCore/QString>
38 #include <QtCore/QVector>
39
40 QT_BEGIN_NAMESPACE
41
42 #ifdef PROPARSER_THREAD_SAFE
43 typedef QAtomicInt ProItemRefCount;
44 #else
45 class ProItemRefCount {
46 public:
47     ProItemRefCount(int cnt = 0) : m_cnt(cnt) {}
48     bool ref() { return ++m_cnt != 0; }
49     bool deref() { return --m_cnt != 0; }
50     ProItemRefCount &operator=(int value) { m_cnt = value; return *this; }
51 private:
52     int m_cnt;
53 };
54 #endif
55
56 namespace ProStringConstants {
57 enum OmitPreHashing { NoHash };
58 }
59
60 class ProStringList;
61
62 class ProString {
63 public:
64     ProString();
65     ProString(const ProString &other);
66     ProString(const ProString &other, ProStringConstants::OmitPreHashing);
67     explicit ProString(const QString &str);
68     ProString(const QString &str, ProStringConstants::OmitPreHashing);
69     explicit ProString(const char *str);
70     ProString(const char *str, ProStringConstants::OmitPreHashing);
71     ProString(const QString &str, int offset, int length);
72     ProString(const QString &str, int offset, int length, uint hash);
73     ProString(const QString &str, int offset, int length, ProStringConstants::OmitPreHashing);
74     QString toQString() const;
75     QString &toQString(QString &tmp) const;
76     ProString &operator+=(const ProString &other);
77     ProString &append(const ProString &other, bool *pending = 0);
78     ProString &append(const ProStringList &other, bool *pending = 0, bool skipEmpty1st = false);
79     bool operator==(const ProString &other) const;
80     bool operator==(const QString &other) const;
81     bool operator==(const QLatin1String &other) const;
82     bool operator!=(const ProString &other) const { return !(*this == other); }
83     bool operator!=(const QString &other) const { return !(*this == other); }
84     bool operator!=(const QLatin1String &other) const { return !(*this == other); }
85     bool isEmpty() const { return !m_length; }
86     int size() const { return m_length; }
87     const QChar *constData() const { return m_string.constData() + m_offset; }
88     ProString mid(int off, int len = -1) const;
89     ProString left(int len) const { return mid(0, len); }
90     ProString right(int len) const { return mid(qMax(0, size() - len)); }
91     ProString trimmed() const;
92     void clear() { m_string.clear(); m_length = 0; }
93
94     static uint hash(const QChar *p, int n);
95
96 private:
97     QString m_string;
98     int m_offset, m_length;
99     mutable uint m_hash;
100     QChar *prepareAppend(int extraLen);
101     uint updatedHash() const;
102     friend uint qHash(const ProString &str);
103     friend QString operator+(const ProString &one, const ProString &two);
104 };
105 Q_DECLARE_TYPEINFO(ProString, Q_MOVABLE_TYPE);
106
107 uint qHash(const ProString &str);
108 QString operator+(const ProString &one, const ProString &two);
109 inline QString operator+(const ProString &one, const QString &two)
110     { return one + ProString(two, ProStringConstants::NoHash); }
111 inline QString operator+(const QString &one, const ProString &two)
112     { return ProString(one, ProStringConstants::NoHash) + two; }
113
114 class ProStringList : public QVector<ProString> {
115 public:
116     ProStringList() {}
117     ProStringList(const ProString &str) { *this << str; }
118     QString join(const QString &sep) const;
119     void removeDuplicates();
120 };
121
122 // These token definitions affect both ProFileEvaluator and ProWriter
123 enum ProToken {
124     TokTerminator = 0,  // end of stream (possibly not included in length; must be zero)
125     TokLine,            // line marker:
126                         // - line (1)
127     TokAssign,          // variable =
128     TokAppend,          // variable +=
129     TokAppendUnique,    // variable *=
130     TokRemove,          // variable -=
131     TokReplace,         // variable ~=
132                         // previous literal/expansion is a variable manipulation
133                         // - value expression + TokValueTerminator
134     TokValueTerminator, // assignment value terminator
135     TokLiteral,         // literal string (fully dequoted)
136                         // - length (1)
137                         // - string data (length; unterminated)
138     TokHashLiteral,     // literal string with hash (fully dequoted)
139                         // - hash (2)
140                         // - length (1)
141                         // - string data (length; unterminated)
142     TokVariable,        // qmake variable expansion
143                         // - hash (2)
144                         // - name length (1)
145                         // - name (name length; unterminated)
146     TokProperty,        // qmake property expansion
147                         // - name length (1)
148                         // - name (name length; unterminated)
149     TokEnvVar,          // environment variable expansion
150                         // - name length (1)
151                         // - name (name length; unterminated)
152     TokFuncName,        // replace function expansion
153                         // - hash (2)
154                         // - name length (1)
155                         // - name (name length; unterminated)
156                         // - ((nested expansion + TokArgSeparator)* + nested expansion)?
157                         // - TokFuncTerminator
158     TokArgSeparator,    // function argument separator
159     TokFuncTerminator,  // function argument list terminator
160     TokCondition,       // previous literal/expansion is a conditional
161     TokTestCall,        // previous literal/expansion is a test function call
162                         // - ((nested expansion + TokArgSeparator)* + nested expansion)?
163                         // - TokFuncTerminator
164     TokNot,             // '!' operator
165     TokAnd,             // ':' operator
166     TokOr,              // '|' operator
167     TokBranch,          // branch point:
168                         // - then block length (2)
169                         // - then block + TokTerminator (then block length)
170                         // - else block length (2)
171                         // - else block + TokTerminator (else block length)
172     TokForLoop,         // for loop:
173                         // - variable name: hash (2), length (1), chars (length)
174                         // - expression: length (2), bytes + TokValueTerminator (length)
175                         // - body length (2)
176                         // - body + TokTerminator (body length)
177     TokTestDef,         // test function definition:
178     TokReplaceDef,      // replace function definition:
179                         // - function name: hash (2), length (1), chars (length)
180                         // - body length (2)
181                         // - body + TokTerminator (body length)
182     TokMask = 0xff,
183     TokQuoted = 0x100,  // The expression is quoted => join expanded stringlist
184     TokNewStr = 0x200   // Next stringlist element
185 };
186
187 class ProFile
188 {
189 public:
190     explicit ProFile(const QString &fileName);
191     ~ProFile();
192
193     QString fileName() const { return m_fileName; }
194     QString directoryName() const { return m_directoryName; }
195     const QString &items() const { return m_proitems; }
196     QString *itemsRef() { return &m_proitems; }
197     const ushort *tokPtr() const { return (const ushort *)m_proitems.constData(); }
198
199     void ref() { m_refCount.ref(); }
200     void deref() { if (!m_refCount.deref()) delete this; }
201
202 private:
203     ProItemRefCount m_refCount;
204     QString m_proitems;
205     QString m_fileName;
206     QString m_directoryName;
207 };
208
209 QT_END_NAMESPACE
210
211 #endif // PROITEMS_H