1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qqmldirparser_p.h"
43 #include "qqmlerror.h"
44 #include "qqmlglobal_p.h"
46 #include <QtQml/qqmlfile.h>
47 #include <QtCore/QtDebug>
51 static int parseInt(const QStringRef &str, bool *ok)
55 while (pos < str.length() && str.at(pos).isDigit()) {
58 number += str.at(pos).unicode() - '0';
61 if (pos != str.length())
68 QQmlDirParser::QQmlDirParser()
72 QQmlDirParser::~QQmlDirParser()
76 inline static void scanSpace(const QChar *&ch) {
77 while (ch->isSpace() && !ch->isNull() && *ch != QLatin1Char('\n'))
81 inline static void scanToEnd(const QChar *&ch) {
82 while (*ch != QLatin1Char('\n') && !ch->isNull())
86 inline static void scanWord(const QChar *&ch) {
87 while (!ch->isSpace() && !ch->isNull())
92 \a url is used for generating errors.
94 bool QQmlDirParser::parse(const QString &source)
102 bool firstLine = true;
104 const QChar *ch = source.constData();
105 while (!ch->isNull()) {
108 bool invalidLine = false;
109 const QChar *lineStart = ch;
112 if (*ch == QLatin1Char('\n')) {
120 int sectionCount = 0;
123 if (*ch == QLatin1Char('#')) {
127 const QChar *start = ch;
129 if (sectionCount < 3) {
130 sections[sectionCount++] = source.mid(start-source.constData(), ch-start);
132 reportError(lineNumber, start-lineStart, QLatin1String("unexpected token"));
138 } while (*ch != QLatin1Char('\n') && !ch->isNull());
144 reportError(lineNumber, -1,
145 QString::fromUtf8("invalid qmldir directive contains too many tokens"));
147 } else if (sectionCount == 0) {
148 continue; // no sections, no party.
150 } else if (sections[0] == QLatin1String("module")) {
151 if (sectionCount != 2) {
152 reportError(lineNumber, -1,
153 QString::fromUtf8("module directive requires one argument, but %1 were provided").arg(sectionCount - 1));
156 if (!_typeNamespace.isEmpty()) {
157 reportError(lineNumber, -1,
158 QString::fromUtf8("only one module directive may be defined in a qmldir file"));
162 reportError(lineNumber, -1,
163 QString::fromUtf8("module directive must be the first directive in a qmldir file"));
167 _typeNamespace = sections[1];
169 } else if (sections[0] == QLatin1String("plugin")) {
170 if (sectionCount < 2) {
171 reportError(lineNumber, -1,
172 QString::fromUtf8("plugin directive requires one or two arguments, but %1 were provided").arg(sectionCount - 1));
177 const Plugin entry(sections[1], sections[2]);
179 _plugins.append(entry);
181 } else if (sections[0] == QLatin1String("internal")) {
182 if (sectionCount != 3) {
183 reportError(lineNumber, -1,
184 QString::fromUtf8("internal types require 2 arguments, but %1 were provided").arg(sectionCount - 1));
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, -1,
193 QString::fromUtf8("typeinfo requires 1 argument, but %1 were provided").arg(sectionCount - 1));
197 TypeInfo typeInfo(sections[1]);
198 _typeInfos.append(typeInfo);
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('.'));
209 if (dotIndex == -1) {
210 reportError(lineNumber, -1, QLatin1String("expected '.'"));
211 } else if (version.indexOf(QLatin1Char('.'), dotIndex + 1) != -1) {
212 reportError(lineNumber, -1, QLatin1String("unexpected '.'"));
214 bool validVersionNumber = false;
215 const int majorVersion = parseInt(QStringRef(&version, 0, dotIndex), &validVersionNumber);
217 if (validVersionNumber) {
218 const int minorVersion = parseInt(QStringRef(&version, dotIndex+1, version.length()-dotIndex-1), &validVersionNumber);
220 if (validVersionNumber) {
221 const QString &fileName = sections[2];
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);
228 const Component entry(sections[0], fileName, majorVersion, minorVersion);
229 _components.insertMulti(entry.typeName, entry);
235 reportError(lineNumber, -1,
236 QString::fromUtf8("a component declaration requires two or three arguments, but %1 were provided").arg(sectionCount));
245 void QQmlDirParser::reportError(int line, int column, const QString &description)
249 error.setColumn(column);
250 error.setDescription(description);
251 _errors.append(error);
254 bool QQmlDirParser::hasError() const
256 if (! _errors.isEmpty())
262 void QQmlDirParser::setError(const QQmlError &e)
268 QList<QQmlError> QQmlDirParser::errors(const QString &uri) const
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);
282 QString QQmlDirParser::typeNamespace() const
284 return _typeNamespace;
287 void QQmlDirParser::setTypeNamespace(const QString &s)
292 QList<QQmlDirParser::Plugin> QQmlDirParser::plugins() const
297 QHash<QHashedStringRef,QQmlDirParser::Component> QQmlDirParser::components() const
302 QList<QQmlDirParser::Script> QQmlDirParser::scripts() const
308 QList<QQmlDirParser::TypeInfo> QQmlDirParser::typeInfos() const
314 QDebug &operator<< (QDebug &debug, const QQmlDirParser::Component &component)
316 const QString output = QString::fromLatin1("{%1 %2.%3}").
317 arg(component.typeName).arg(component.majorVersion).arg(component.minorVersion);
318 return debug << qPrintable(output);
321 QDebug &operator<< (QDebug &debug, const QQmlDirParser::Script &script)
323 const QString output = QString::fromLatin1("{%1 %2.%3}").
324 arg(script.nameSpace).arg(script.majorVersion).arg(script.minorVersion);
325 return debug << qPrintable(output);