1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
36 #include <QtCore/QString>
37 #include <QtCore/QVector>
41 #ifdef PROPARSER_THREAD_SAFE
42 typedef QAtomicInt ProItemRefCount;
44 class ProItemRefCount {
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; }
55 namespace ProStringConstants {
56 enum OmitPreHashing { NoHash };
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; }
93 static uint hash(const QChar *p, int n);
97 int m_offset, m_length;
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);
104 Q_DECLARE_TYPEINFO(ProString, Q_MOVABLE_TYPE);
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; }
113 class ProStringList : public QVector<ProString> {
116 ProStringList(const ProString &str) { *this << str; }
117 QString join(const QString &sep) const;
118 void removeDuplicates();
121 // These token definitions affect both ProFileEvaluator and ProWriter
123 TokTerminator = 0, // end of stream (possibly not included in length; must be zero)
124 TokLine, // line marker:
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)
136 // - string data (length; unterminated)
137 TokHashLiteral, // literal string with hash (fully dequoted)
140 // - string data (length; unterminated)
141 TokVariable, // qmake variable expansion
144 // - name (name length; unterminated)
145 TokProperty, // qmake property expansion
147 // - name (name length; unterminated)
148 TokEnvVar, // environment variable expansion
150 // - name (name length; unterminated)
151 TokFuncName, // replace function expansion
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)
175 // - body + TokTerminator (body length)
176 TokTestDef, // test function definition:
177 TokReplaceDef, // replace function definition:
178 // - function name: hash (2), length (1), chars (length)
180 // - body + TokTerminator (body length)
182 TokQuoted = 0x100, // The expression is quoted => join expanded stringlist
183 TokNewStr = 0x200 // Next stringlist element
189 explicit ProFile(const QString &fileName);
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(); }
198 void ref() { m_refCount.ref(); }
199 void deref() { if (!m_refCount.deref()) delete this; }
201 bool isOk() const { return m_ok; }
202 void setOk(bool ok) { m_ok = ok; }
205 ProItemRefCount m_refCount;
208 QString m_directoryName;