Add missing QT_{BEGIN,END}_NAMESPACE
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmldirparser.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qqmldirparser_p.h"
43 #include "qqmlerror.h"
44 #include "qqmlglobal_p.h"
45
46 #include <QtQml/qqmlfile.h>
47 #include <QtCore/QtDebug>
48
49 QT_BEGIN_NAMESPACE
50
51 static int parseInt(const QStringRef &str, bool *ok)
52 {
53     int pos = 0;
54     int number = 0;
55     while (pos < str.length() && str.at(pos).isDigit()) {
56         if (pos != 0)
57             number *= 10;
58         number += str.at(pos).unicode() - '0';
59         ++pos;
60     }
61     if (pos != str.length())
62         *ok = false;
63     else
64         *ok = true;
65     return number;
66 }
67
68 QQmlDirParser::QQmlDirParser()
69 {
70 }
71
72 QQmlDirParser::~QQmlDirParser()
73 {
74 }
75
76 inline static void scanSpace(const QChar *&ch) {
77     while (ch->isSpace() && !ch->isNull() && *ch != QLatin1Char('\n'))
78         ++ch;
79 }
80
81 inline static void scanToEnd(const QChar *&ch) {
82     while (*ch != QLatin1Char('\n') && !ch->isNull())
83         ++ch;
84 }
85
86 inline static void scanWord(const QChar *&ch) {
87     while (!ch->isSpace() && !ch->isNull())
88         ++ch;
89 }
90
91 /*!
92 \a url is used for generating errors.
93 */
94 bool QQmlDirParser::parse(const QString &source)
95 {
96     _errors.clear();
97     _plugins.clear();
98     _components.clear();
99     _scripts.clear();
100
101     quint16 lineNumber = 0;
102     bool firstLine = true;
103
104     const QChar *ch = source.constData();
105     while (!ch->isNull()) {
106         ++lineNumber;
107
108         bool invalidLine = false;
109         const QChar *lineStart = ch;
110
111         scanSpace(ch);
112         if (*ch == QLatin1Char('\n')) {
113             ++ch;
114             continue;
115         }
116         if (ch->isNull())
117             break;
118
119         QString sections[3];
120         int sectionCount = 0;
121
122         do {
123             if (*ch == QLatin1Char('#')) {
124                 scanToEnd(ch);
125                 break;
126             }
127             const QChar *start = ch;
128             scanWord(ch);
129             if (sectionCount < 3) {
130                 sections[sectionCount++] = source.mid(start-source.constData(), ch-start);
131             } else {
132                 reportError(lineNumber, start-lineStart, QLatin1String("unexpected token"));
133                 scanToEnd(ch);
134                 invalidLine = true;
135                 break;
136             }
137             scanSpace(ch);
138         } while (*ch != QLatin1Char('\n') && !ch->isNull());
139
140         if (!ch->isNull())
141             ++ch;
142
143         if (invalidLine) {
144             reportError(lineNumber, 0,
145                         QString::fromUtf8("invalid qmldir directive contains too many tokens"));
146             continue;
147         } else if (sectionCount == 0) {
148             continue; // no sections, no party.
149
150         } else if (sections[0] == QLatin1String("module")) {
151             if (sectionCount != 2) {
152                 reportError(lineNumber, 0,
153                             QString::fromUtf8("module identifier directive requires one argument, but %1 were provided").arg(sectionCount - 1));
154                 continue;
155             }
156             if (!_typeNamespace.isEmpty()) {
157                 reportError(lineNumber, 0,
158                             QString::fromUtf8("only one module identifier directive may be defined in a qmldir file"));
159                 continue;
160             }
161             if (!firstLine) {
162                 reportError(lineNumber, 0,
163                             QString::fromUtf8("module identifier directive must be the first command in a qmldir file"));
164                 continue;
165             }
166
167             _typeNamespace = sections[1];
168
169         } else if (sections[0] == QLatin1String("plugin")) {
170             if (sectionCount < 2) {
171                 reportError(lineNumber, 0,
172                             QString::fromUtf8("plugin directive requires one or two arguments, but %1 were provided").arg(sectionCount - 1));
173
174                 continue;
175             }
176
177             const Plugin entry(sections[1], sections[2]);
178
179             _plugins.append(entry);
180
181         } else if (sections[0] == QLatin1String("internal")) {
182             if (sectionCount != 3) {
183                 reportError(lineNumber, 0,
184                             QString::fromUtf8("internal types require 2 arguments, but %1 were provided").arg(sectionCount - 1));
185                 continue;
186             }
187             Component entry(sections[1], sections[2], -1, -1);
188             entry.internal = true;
189             _components.insertMulti(entry.typeName, entry);
190         } else if (sections[0] == QLatin1String("typeinfo")) {
191             if (sectionCount != 2) {
192                 reportError(lineNumber, 0,
193                             QString::fromUtf8("typeinfo requires 1 argument, but %1 were provided").arg(sectionCount - 1));
194                 continue;
195             }
196 #ifdef QT_CREATOR
197             TypeInfo typeInfo(sections[1]);
198             _typeInfos.append(typeInfo);
199 #endif
200
201         } else if (sectionCount == 2) {
202             // No version specified (should only be used for relative qmldir files)
203             const Component entry(sections[0], sections[1], -1, -1);
204             _components.insertMulti(entry.typeName, entry);
205         } else if (sectionCount == 3) {
206             const QString &version = sections[1];
207             const int dotIndex = version.indexOf(QLatin1Char('.'));
208
209             if (dotIndex == -1) {
210                 reportError(lineNumber, 0, QLatin1String("expected '.'"));
211             } else if (version.indexOf(QLatin1Char('.'), dotIndex + 1) != -1) {
212                 reportError(lineNumber, 0, QLatin1String("unexpected '.'"));
213             } else {
214                 bool validVersionNumber = false;
215                 const int majorVersion = parseInt(QStringRef(&version, 0, dotIndex), &validVersionNumber);
216
217                 if (validVersionNumber) {
218                     const int minorVersion = parseInt(QStringRef(&version, dotIndex+1, version.length()-dotIndex-1), &validVersionNumber);
219
220                     if (validVersionNumber) {
221                         const QString &fileName = sections[2];
222
223                         if (fileName.endsWith(QLatin1String(".js"))) {
224                             // A 'js' extension indicates a namespaced script import
225                             const Script entry(sections[0], fileName, majorVersion, minorVersion);
226                             _scripts.append(entry);
227                         } else {
228                             const Component entry(sections[0], fileName, majorVersion, minorVersion);
229                             _components.insertMulti(entry.typeName, entry);
230                         }
231                     }
232                 }
233             }
234         } else {
235             reportError(lineNumber, 0,
236                         QString::fromUtf8("a component declaration requires two or three arguments, but %1 were provided").arg(sectionCount));
237         }
238
239         firstLine = false;
240     }
241
242     return hasError();
243 }
244
245 void QQmlDirParser::reportError(quint16 line, quint16 column, const QString &description)
246 {
247     QQmlError error;
248     error.setLine(line);
249     error.setColumn(column);
250     error.setDescription(description);
251     _errors.append(error);
252 }
253
254 bool QQmlDirParser::hasError() const
255 {
256     if (! _errors.isEmpty())
257         return true;
258
259     return false;
260 }
261
262 void QQmlDirParser::setError(const QQmlError &e)
263 {
264     _errors.clear();
265     _errors.append(e);
266 }
267
268 QList<QQmlError> QQmlDirParser::errors(const QString &uri) const
269 {
270     QUrl url(uri);
271     QList<QQmlError> errors = _errors;
272     for (int i = 0; i < errors.size(); ++i) {
273         QQmlError &e = errors[i];
274         QString description = e.description();
275         description.replace(QLatin1String("$$URI$$"), uri);
276         e.setDescription(description);
277         e.setUrl(url);
278     }
279     return errors;
280 }
281
282 QString QQmlDirParser::typeNamespace() const
283 {
284     return _typeNamespace;
285 }
286
287 void QQmlDirParser::setTypeNamespace(const QString &s)
288 {
289     _typeNamespace = s;
290 }
291
292 QList<QQmlDirParser::Plugin> QQmlDirParser::plugins() const
293 {
294     return _plugins;
295 }
296
297 QHash<QHashedStringRef,QQmlDirParser::Component> QQmlDirParser::components() const
298 {
299     return _components;
300 }
301
302 QList<QQmlDirParser::Script> QQmlDirParser::scripts() const
303 {
304     return _scripts;
305 }
306
307 #ifdef QT_CREATOR
308 QList<QQmlDirParser::TypeInfo> QQmlDirParser::typeInfos() const
309 {
310     return _typeInfos;
311 }
312 #endif
313
314 QDebug &operator<< (QDebug &debug, const QQmlDirParser::Component &component)
315 {
316     const QString output = QString::fromLatin1("{%1 %2.%3}").
317         arg(component.typeName).arg(component.majorVersion).arg(component.minorVersion);
318     return debug << qPrintable(output);
319 }
320
321 QDebug &operator<< (QDebug &debug, const QQmlDirParser::Script &script)
322 {
323     const QString output = QString::fromLatin1("{%1 %2.%3}").
324         arg(script.nameSpace).arg(script.majorVersion).arg(script.minorVersion);
325     return debug << qPrintable(output);
326 }
327
328 QT_END_NAMESPACE