1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
37 #include <QtCore/QString>
38 #include <QtCore/QVector>
42 #ifdef PROPARSER_THREAD_SAFE
43 typedef QAtomicInt ProItemRefCount;
45 class ProItemRefCount {
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; }
56 namespace ProStringConstants {
57 enum OmitPreHashing { NoHash };
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; }
94 static uint hash(const QChar *p, int n);
98 int m_offset, m_length;
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);
105 Q_DECLARE_TYPEINFO(ProString, Q_MOVABLE_TYPE);
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; }
114 class ProStringList : public QVector<ProString> {
117 ProStringList(const ProString &str) { *this << str; }
118 QString join(const QString &sep) const;
119 void removeDuplicates();
122 // These token definitions affect both ProFileEvaluator and ProWriter
124 TokTerminator = 0, // end of stream (possibly not included in length; must be zero)
125 TokLine, // line marker:
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)
137 // - string data (length; unterminated)
138 TokHashLiteral, // literal string with hash (fully dequoted)
141 // - string data (length; unterminated)
142 TokVariable, // qmake variable expansion
145 // - name (name length; unterminated)
146 TokProperty, // qmake property expansion
148 // - name (name length; unterminated)
149 TokEnvVar, // environment variable expansion
151 // - name (name length; unterminated)
152 TokFuncName, // replace function expansion
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)
176 // - body + TokTerminator (body length)
177 TokTestDef, // test function definition:
178 TokReplaceDef, // replace function definition:
179 // - function name: hash (2), length (1), chars (length)
181 // - body + TokTerminator (body length)
183 TokQuoted = 0x100, // The expression is quoted => join expanded stringlist
184 TokNewStr = 0x200 // Next stringlist element
190 explicit ProFile(const QString &fileName);
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(); }
199 void ref() { m_refCount.ref(); }
200 void deref() { if (!m_refCount.deref()) delete this; }
203 ProItemRefCount m_refCount;
206 QString m_directoryName;