Make the Qml code model read qmldir files for import resolving.

However, it still re-reads for each use of Link. Needs to be optimized.

Reviewed-by: Roberto Raggi
This commit is contained in:
Christian Kamm
2010-03-18 12:06:43 +01:00
parent b3001b95f9
commit 4261aca7b0
8 changed files with 723 additions and 10 deletions

View File

@@ -1,4 +1,3 @@
s/qdeclarativejs/qmljs/g
s/QDECLARATIVEJS/QMLJS/g
s/QDeclarativeJS/QmlJS/g
s/QDeclarativeParser/QmlParser/g
s/qdeclarative/qml/g
s/QDECLARATIVE/QML/g
s/QDeclarative/Qml/g

5
src/libs/qmljs/parser/gen-parser.sh Normal file → Executable file
View File

@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
me=$(dirname $0)
@@ -6,4 +6,7 @@ for i in $QTDIR/src/declarative/qml/parser/*.{h,cpp,pri}; do
sed -f $me/cmd.sed $i > $me/$(echo $(basename $i) | sed s/qdeclarativejs/qmljs/)
done
for i in $QTDIR/src/declarative/qml/qdeclarative{error.{h,cpp},dirparser{_p.h,.cpp}}; do
sed -f $me/cmd.sed $i > $me/$(echo $(basename $i) | sed s/qdeclarative/qml/)
done

View File

@@ -10,7 +10,9 @@ HEADERS += \
$$PWD/qmljsmemorypool_p.h \
$$PWD/qmljsnodepool_p.h \
$$PWD/qmljsparser_p.h \
$$PWD/qmljsglobal_p.h
$$PWD/qmljsglobal_p.h \
$$PWD/qmldirparser_p.h \
$$PWD/qmlerror.h
SOURCES += \
$$PWD/qmljsast.cpp \
@@ -18,4 +20,6 @@ SOURCES += \
$$PWD/qmljsengine_p.cpp \
$$PWD/qmljsgrammar.cpp \
$$PWD/qmljslexer.cpp \
$$PWD/qmljsparser.cpp
$$PWD/qmljsparser.cpp \
$$PWD/qmldirparser.cpp \
$$PWD/qmlerror.cpp

View File

@@ -0,0 +1,220 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qmldirparser_p.h"
#include "qmlerror.h"
#include <QtCore/QTextStream>
#include <QtCore/QtDebug>
QT_BEGIN_NAMESPACE
QmlDirParser::QmlDirParser()
: _isParsed(false)
{
}
QmlDirParser::~QmlDirParser()
{
}
QUrl QmlDirParser::url() const
{
return _url;
}
void QmlDirParser::setUrl(const QUrl &url)
{
_url = url;
}
QString QmlDirParser::source() const
{
return _source;
}
void QmlDirParser::setSource(const QString &source)
{
_isParsed = false;
_source = source;
}
bool QmlDirParser::isParsed() const
{
return _isParsed;
}
bool QmlDirParser::parse()
{
if (_isParsed)
return true;
_isParsed = true;
_errors.clear();
_plugins.clear();
_components.clear();
QTextStream stream(&_source);
int lineNumber = 0;
forever {
++lineNumber;
const QString line = stream.readLine();
if (line.isNull())
break;
QString sections[3];
int sectionCount = 0;
int index = 0;
const int length = line.length();
while (index != length) {
const QChar ch = line.at(index);
if (ch.isSpace()) {
do { ++index; }
while (index != length && line.at(index).isSpace());
} else if (ch == QLatin1Char('#')) {
// recognized a comment
break;
} else {
const int start = index;
do { ++index; }
while (index != length && !line.at(index).isSpace());
const QString lexeme = line.mid(start, index - start);
if (sectionCount >= 3) {
reportError(lineNumber, start, QLatin1String("unexpected token"));
} else {
sections[sectionCount++] = lexeme;
}
}
}
if (sectionCount == 0) {
continue; // no sections, no party.
} else if (sections[0] == QLatin1String("plugin")) {
if (sectionCount < 2) {
reportError(lineNumber, -1,
QString::fromUtf8("plugin directive requires 2 arguments, but %1 were provided").arg(sectionCount + 1));
continue;
}
const Plugin entry(sections[1], sections[2]);
_plugins.append(entry);
} else if (sectionCount == 3) {
const QString &version = sections[1];
const int dotIndex = version.indexOf(QLatin1Char('.'));
if (dotIndex == -1) {
qWarning() << "expected '.'"; // ### use reportError
} else if (version.indexOf(QLatin1Char('.'), dotIndex + 1) != -1) {
qWarning() << "unexpected '.'"; // ### use reportError
} else {
bool validVersionNumber = false;
const int majorVersion = version.left(dotIndex).toInt(&validVersionNumber);
if (validVersionNumber) {
const int minorVersion = version.mid(dotIndex + 1).toInt(&validVersionNumber);
if (validVersionNumber) {
const Component entry(sections[0], sections[2], majorVersion, minorVersion);
_components.append(entry);
}
}
}
} else {
// ### use reportError
qWarning() << "a component declaration requires 3 arguments, but" << (sectionCount + 1) << "were provided";
}
}
return hasError();
}
void QmlDirParser::reportError(int line, int column, const QString &description)
{
QmlError error;
error.setUrl(_url);
error.setLine(line);
error.setColumn(column);
error.setDescription(description);
_errors.append(error);
}
bool QmlDirParser::hasError() const
{
if (! _errors.isEmpty())
return true;
return false;
}
QList<QmlError> QmlDirParser::errors() const
{
return _errors;
}
QList<QmlDirParser::Plugin> QmlDirParser::plugins() const
{
return _plugins;
}
QList<QmlDirParser::Component> QmlDirParser::components() const
{
return _components;
}
QT_END_NAMESPACE

View File

@@ -0,0 +1,125 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QMLDIRPARSER_P_H
#define QMLDIRPARSER_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtCore/QUrl>
#include <QtCore/QHash>
QT_BEGIN_NAMESPACE
class QmlError;
class QmlDirParser
{
Q_DISABLE_COPY(QmlDirParser)
public:
QmlDirParser();
~QmlDirParser();
QUrl url() const;
void setUrl(const QUrl &url);
QString source() const;
void setSource(const QString &source);
bool isParsed() const;
bool parse();
bool hasError() const;
QList<QmlError> errors() const;
struct Plugin
{
Plugin() {}
Plugin(const QString &name, const QString &path)
: name(name), path(path) {}
QString name;
QString path;
};
struct Component
{
Component()
: majorVersion(0), minorVersion(0) {}
Component(const QString &typeName, const QString &fileName, int majorVersion, int minorVersion)
: typeName(typeName), fileName(fileName), majorVersion(majorVersion), minorVersion(minorVersion) {}
QString typeName;
QString fileName;
int majorVersion;
int minorVersion;
};
QList<Component> components() const;
QList<Plugin> plugins() const;
private:
void reportError(int line, int column, const QString &message);
private:
QList<QmlError> _errors;
QUrl _url;
QString _source;
QList<Component> _components;
QList<Plugin> _plugins;
unsigned _isParsed: 1;
};
QT_END_NAMESPACE
#endif // QMLDIRPARSER_P_H

View File

@@ -0,0 +1,258 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qmlerror.h"
#include <QtCore/qdebug.h>
#include <QtCore/qfile.h>
#include <QtCore/qstringlist.h>
QT_BEGIN_NAMESPACE
/*!
\class QmlError
\since 4.7
\brief The QmlError class encapsulates a QML error
*/
class QmlErrorPrivate
{
public:
QmlErrorPrivate();
QUrl url;
QString description;
int line;
int column;
};
QmlErrorPrivate::QmlErrorPrivate()
: line(-1), column(-1)
{
}
/*!
Create an empty error object.
*/
QmlError::QmlError()
: d(0)
{
}
/*!
Create a copy of \a other.
*/
QmlError::QmlError(const QmlError &other)
: d(0)
{
*this = other;
}
/*!
Assign \a other to this error object.
*/
QmlError &QmlError::operator=(const QmlError &other)
{
if (!other.d) {
delete d;
d = 0;
} else {
if (!d) d = new QmlErrorPrivate;
d->url = other.d->url;
d->description = other.d->description;
d->line = other.d->line;
d->column = other.d->column;
}
return *this;
}
/*!
\internal
*/
QmlError::~QmlError()
{
delete d; d = 0;
}
/*!
Return true if this error is valid, otherwise false.
*/
bool QmlError::isValid() const
{
return d != 0;
}
/*!
Return the url for the file that caused this error.
*/
QUrl QmlError::url() const
{
if (d) return d->url;
else return QUrl();
}
/*!
Set the \a url for the file that caused this error.
*/
void QmlError::setUrl(const QUrl &url)
{
if (!d) d = new QmlErrorPrivate;
d->url = url;
}
/*!
Return the error description.
*/
QString QmlError::description() const
{
if (d) return d->description;
else return QString();
}
/*!
Set the error \a description.
*/
void QmlError::setDescription(const QString &description)
{
if (!d) d = new QmlErrorPrivate;
d->description = description;
}
/*!
Return the error line number.
*/
int QmlError::line() const
{
if (d) return d->line;
else return -1;
}
/*!
Set the error \a line number.
*/
void QmlError::setLine(int line)
{
if (!d) d = new QmlErrorPrivate;
d->line = line;
}
/*!
Return the error column number.
*/
int QmlError::column() const
{
if (d) return d->column;
else return -1;
}
/*!
Set the error \a column number.
*/
void QmlError::setColumn(int column)
{
if (!d) d = new QmlErrorPrivate;
d->column = column;
}
/*!
Return the error as a human readable string.
*/
QString QmlError::toString() const
{
QString rv;
rv = url().toString() + QLatin1Char(':') + QString::number(line());
if(column() != -1)
rv += QLatin1Char(':') + QString::number(column());
rv += QLatin1String(": ") + description();
return rv;
}
/*!
\relates QmlError
\fn QDebug operator<<(QDebug debug, const QmlError &error)
Output a human readable version of \a error to \a debug.
*/
QDebug operator<<(QDebug debug, const QmlError &error)
{
debug << qPrintable(error.toString());
QUrl url = error.url();
if (error.line() > 0 && url.scheme() == QLatin1String("file")) {
QString file = url.toLocalFile();
QFile f(file);
if (f.open(QIODevice::ReadOnly)) {
QByteArray data = f.readAll();
QTextStream stream(data, QIODevice::ReadOnly);
stream.setCodec("UTF-8");
const QString code = stream.readAll();
const QStringList lines = code.split(QLatin1Char('\n'));
if (lines.count() >= error.line()) {
const QString &line = lines.at(error.line() - 1);
debug << "\n " << qPrintable(line);
if(error.column() > 0) {
int column = qMax(0, error.column() - 1);
column = qMin(column, line.length());
QByteArray ind;
ind.reserve(column);
for (int i = 0; i < column; ++i) {
const QChar ch = line.at(i);
if (ch.isSpace())
ind.append(ch.unicode());
else
ind.append(' ');
}
ind.append('^');
debug << "\n " << ind.constData();
}
}
}
}
return debug;
}
QT_END_NAMESPACE

View File

@@ -0,0 +1,86 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QMLERROR_H
#define QMLERROR_H
#include <QtCore/qurl.h>
#include <QtCore/qstring.h>
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
QT_MODULE(Declarative)
class QDebug;
class QmlErrorPrivate;
class Q_DECLARATIVE_EXPORT QmlError
{
public:
QmlError();
QmlError(const QmlError &);
QmlError &operator=(const QmlError &);
~QmlError();
bool isValid() const;
QUrl url() const;
void setUrl(const QUrl &);
QString description() const;
void setDescription(const QString &);
int line() const;
void setLine(int);
int column() const;
void setColumn(int);
QString toString() const;
private:
QmlErrorPrivate *d;
};
QDebug Q_DECLARATIVE_EXPORT operator<<(QDebug debug, const QmlError &error);
QT_END_NAMESPACE
QT_END_HEADER
#endif // QMLERROR_H

View File

@@ -1,6 +1,7 @@
#include "qmljslink.h"
#include "parser/qmljsast_p.h"
#include "parser/qmldirparser_p.h"
#include "qmljsdocument.h"
#include "qmljsbind.h"
#include "qmljsscopebuilder.h"
@@ -291,10 +292,27 @@ void Link::importNonFile(Interpreter::ObjectValue *typeEnv, Document::Ptr doc, A
if (!dir.exists("qmldir"))
continue;
QFile qmldirFile(dir.filePath("qmldir"));
qmldirFile.open(QFile::ReadOnly);
QString qmldirData = QString::fromUtf8(qmldirFile.readAll());
QmlDirParser qmldirParser;
qmldirParser.setSource(qmldirData);
qmldirParser.parse();
// ### Should read qmldir file and import accordingly.
foreach (Document::Ptr otherDoc, _documentByPath.values(dir.path())) {
namespaceObject->setProperty(componentName(otherDoc->fileName()), otherDoc->bind()->rootObjectValue());
QSet<QString> importedTypes;
foreach (const QmlDirParser::Component &component, qmldirParser.components()) {
if (importedTypes.contains(component.typeName))
continue;
if (component.majorVersion > majorVersion
|| (component.majorVersion == majorVersion
&& component.minorVersion > minorVersion))
continue;
importedTypes.insert(component.typeName);
if (Document::Ptr importedDoc = _snapshot.document(dir.filePath(component.fileName))) {
namespaceObject->setProperty(component.typeName, importedDoc->bind()->rootObjectValue());
}
}
break;