2010-01-18 13:13:34 +01:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
2010-01-18 13:13:34 +01:00
|
|
|
**
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
**
|
|
|
|
** Commercial Usage
|
|
|
|
**
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
**
|
|
|
|
**************************************************************************/
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
#include "qmljsdocument.h"
|
2010-02-02 15:55:17 +01:00
|
|
|
#include "qmljsbind.h"
|
2010-01-18 13:13:34 +01:00
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
|
|
|
#include <qmljs/parser/qmljslexer_p.h>
|
|
|
|
#include <qmljs/parser/qmljsparser_p.h>
|
|
|
|
#include <qmljs/parser/qmljsnodepool_p.h>
|
|
|
|
#include <qmljs/parser/qmljsastfwd_p.h>
|
2010-01-26 17:23:18 +01:00
|
|
|
#include <QtCore/QDir>
|
2010-01-18 13:13:34 +01:00
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
Document::Document(const QString &fileName)
|
2010-01-18 13:13:34 +01:00
|
|
|
: _engine(0)
|
|
|
|
, _pool(0)
|
2010-01-22 10:26:25 +01:00
|
|
|
, _ast(0)
|
2010-02-03 15:59:15 +01:00
|
|
|
, _bind(0)
|
2010-01-25 14:18:53 +01:00
|
|
|
, _documentRevision(0)
|
2010-01-18 13:13:34 +01:00
|
|
|
, _parsedCorrectly(false)
|
2010-01-25 14:18:53 +01:00
|
|
|
, _fileName(fileName)
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
2010-01-26 17:23:18 +01:00
|
|
|
QFileInfo fileInfo(fileName);
|
|
|
|
_path = fileInfo.absolutePath();
|
2010-01-27 09:24:49 +01:00
|
|
|
|
|
|
|
if (fileInfo.suffix() == QLatin1String("qml")) {
|
|
|
|
_componentName = fileInfo.baseName();
|
|
|
|
|
|
|
|
if (! _componentName.isEmpty()) {
|
|
|
|
// ### TODO: check the component name.
|
|
|
|
|
|
|
|
if (! _componentName.at(0).isUpper())
|
|
|
|
_componentName.clear();
|
|
|
|
}
|
2010-01-26 17:23:18 +01:00
|
|
|
}
|
2010-01-18 13:13:34 +01:00
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
Document::~Document()
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
2010-02-03 15:59:15 +01:00
|
|
|
if (_bind)
|
|
|
|
delete _bind;
|
|
|
|
|
2010-01-18 13:13:34 +01:00
|
|
|
if (_engine)
|
|
|
|
delete _engine;
|
|
|
|
|
|
|
|
if (_pool)
|
|
|
|
delete _pool;
|
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
Document::Ptr Document::create(const QString &fileName)
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
2010-01-18 16:15:23 +01:00
|
|
|
Document::Ptr doc(new Document(fileName));
|
2010-01-18 13:13:34 +01:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
AST::UiProgram *Document::qmlProgram() const
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
2010-01-22 10:26:25 +01:00
|
|
|
return cast<UiProgram *>(_ast);
|
2010-01-18 13:13:34 +01:00
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
AST::Program *Document::jsProgram() const
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
2010-01-22 10:26:25 +01:00
|
|
|
return cast<Program *>(_ast);
|
2010-01-18 13:13:34 +01:00
|
|
|
}
|
|
|
|
|
2010-01-26 14:53:11 +01:00
|
|
|
AST::ExpressionNode *Document::expression() const
|
|
|
|
{
|
|
|
|
if (_ast)
|
|
|
|
return _ast->expressionCast();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-19 10:16:57 +01:00
|
|
|
AST::Node *Document::ast() const
|
|
|
|
{
|
2010-01-22 10:26:25 +01:00
|
|
|
return _ast;
|
2010-01-19 10:16:57 +01:00
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
QList<DiagnosticMessage> Document::diagnosticMessages() const
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
|
|
|
return _diagnosticMessages;
|
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
QString Document::source() const
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
|
|
|
return _source;
|
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
void Document::setSource(const QString &source)
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
|
|
|
_source = source;
|
|
|
|
}
|
|
|
|
|
2010-01-25 14:18:53 +01:00
|
|
|
int Document::documentRevision() const
|
|
|
|
{
|
|
|
|
return _documentRevision;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::setDocumentRevision(int revision)
|
|
|
|
{
|
|
|
|
_documentRevision = revision;
|
|
|
|
}
|
|
|
|
|
2010-02-11 10:19:41 +01:00
|
|
|
QString Document::fileName() const
|
|
|
|
{
|
|
|
|
return _fileName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Document::path() const
|
|
|
|
{
|
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Document::componentName() const
|
|
|
|
{
|
|
|
|
return _componentName;
|
|
|
|
}
|
|
|
|
|
2010-02-08 21:37:59 +01:00
|
|
|
bool Document::parse_helper(int startToken)
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
|
|
|
Q_ASSERT(! _engine);
|
|
|
|
Q_ASSERT(! _pool);
|
2010-01-22 10:26:25 +01:00
|
|
|
Q_ASSERT(! _ast);
|
2010-02-03 15:59:15 +01:00
|
|
|
Q_ASSERT(! _bind);
|
2010-01-18 13:13:34 +01:00
|
|
|
|
|
|
|
_engine = new Engine();
|
|
|
|
_pool = new NodePool(_fileName, _engine);
|
|
|
|
|
|
|
|
Lexer lexer(_engine);
|
|
|
|
Parser parser(_engine);
|
|
|
|
|
|
|
|
lexer.setCode(_source, /*line = */ 1);
|
|
|
|
|
2010-02-08 21:37:59 +01:00
|
|
|
switch (startToken) {
|
|
|
|
case QmlJSGrammar::T_FEED_UI_PROGRAM:
|
|
|
|
_parsedCorrectly = parser.parse();
|
|
|
|
break;
|
|
|
|
case QmlJSGrammar::T_FEED_JS_PROGRAM:
|
|
|
|
_parsedCorrectly = parser.parseProgram();
|
|
|
|
break;
|
|
|
|
case QmlJSGrammar::T_FEED_JS_EXPRESSION:
|
|
|
|
_parsedCorrectly = parser.parseExpression();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Q_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
_ast = parser.rootNode();
|
2010-01-18 13:13:34 +01:00
|
|
|
_diagnosticMessages = parser.diagnosticMessages();
|
|
|
|
|
2010-02-03 15:59:15 +01:00
|
|
|
_bind = new Bind(this);
|
2010-02-02 15:55:17 +01:00
|
|
|
|
2010-01-18 13:13:34 +01:00
|
|
|
return _parsedCorrectly;
|
|
|
|
}
|
|
|
|
|
2010-02-08 21:37:59 +01:00
|
|
|
bool Document::parseQml()
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
2010-02-08 21:37:59 +01:00
|
|
|
return parse_helper(QmlJSGrammar::T_FEED_UI_PROGRAM);
|
|
|
|
}
|
2010-02-02 15:55:17 +01:00
|
|
|
|
2010-02-08 21:37:59 +01:00
|
|
|
bool Document::parseJavaScript()
|
|
|
|
{
|
|
|
|
return parse_helper(QmlJSGrammar::T_FEED_JS_PROGRAM);
|
2010-01-22 10:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Document::parseExpression()
|
|
|
|
{
|
2010-02-08 21:37:59 +01:00
|
|
|
return parse_helper(QmlJSGrammar::T_FEED_JS_EXPRESSION);
|
2010-01-18 13:13:34 +01:00
|
|
|
}
|
|
|
|
|
2010-02-03 15:59:15 +01:00
|
|
|
Bind *Document::bind() const
|
2010-02-02 15:55:17 +01:00
|
|
|
{
|
|
|
|
return _bind;
|
|
|
|
}
|
|
|
|
|
2010-03-18 15:43:33 +01:00
|
|
|
LibraryInfo::LibraryInfo()
|
|
|
|
: _valid(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LibraryInfo::LibraryInfo(const QmlDirParser &parser)
|
|
|
|
: _valid(true)
|
|
|
|
, _components(parser.components())
|
|
|
|
, _plugins(parser.plugins())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LibraryInfo::~LibraryInfo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-01-18 13:13:34 +01:00
|
|
|
Snapshot::Snapshot()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Snapshot::~Snapshot()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
void Snapshot::insert(const Document::Ptr &document)
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
|
|
|
if (document && (document->qmlProgram() || document->jsProgram()))
|
2010-01-26 17:23:18 +01:00
|
|
|
_documents.insert(document->fileName(), document);
|
2010-01-18 13:13:34 +01:00
|
|
|
}
|
|
|
|
|
2010-03-18 15:43:33 +01:00
|
|
|
void Snapshot::insertLibraryInfo(const QString &path, const LibraryInfo &info)
|
|
|
|
{
|
|
|
|
_libraries.insert(path, info);
|
|
|
|
}
|
|
|
|
|
2010-02-16 10:36:09 +01:00
|
|
|
Document::Ptr Snapshot::documentFromSource(const QString &code,
|
|
|
|
const QString &fileName) const
|
|
|
|
{
|
|
|
|
Document::Ptr newDoc = Document::create(fileName);
|
|
|
|
|
|
|
|
if (Document::Ptr thisDocument = document(fileName)) {
|
|
|
|
newDoc->_documentRevision = thisDocument->_documentRevision;
|
|
|
|
}
|
|
|
|
|
|
|
|
newDoc->setSource(code);
|
|
|
|
return newDoc;
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:59:15 +01:00
|
|
|
QList<Document::Ptr> Snapshot::importedDocuments(const Document::Ptr &doc, const QString &importPath) const
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
2010-01-26 14:50:52 +01:00
|
|
|
// ### TODO: maybe we should add all imported documents in the parse Document::parse() method, regardless of whether they're in the path or not.
|
|
|
|
|
2010-02-03 15:59:15 +01:00
|
|
|
QList<Document::Ptr> result;
|
2010-01-18 13:13:34 +01:00
|
|
|
|
2010-01-26 17:23:18 +01:00
|
|
|
QString docPath = doc->path();
|
|
|
|
docPath += QLatin1Char('/');
|
|
|
|
docPath += importPath;
|
|
|
|
docPath = QDir::cleanPath(docPath);
|
2010-01-18 13:13:34 +01:00
|
|
|
|
2010-01-26 17:23:18 +01:00
|
|
|
foreach (Document::Ptr candidate, _documents) {
|
2010-01-18 13:13:34 +01:00
|
|
|
if (candidate == doc)
|
2010-01-26 17:23:18 +01:00
|
|
|
continue; // ignore this document
|
|
|
|
else if (! candidate->qmlProgram())
|
|
|
|
continue; // skip JS documents
|
2010-01-18 13:13:34 +01:00
|
|
|
|
|
|
|
if (candidate->path() == doc->path() || candidate->path() == docPath)
|
|
|
|
result.append(candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
QMap<QString, Document::Ptr> Snapshot::componentsDefinedByImportedDocuments(const Document::Ptr &doc, const QString &importPath) const
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
2010-01-18 16:15:23 +01:00
|
|
|
QMap<QString, Document::Ptr> result;
|
2010-01-18 13:13:34 +01:00
|
|
|
|
|
|
|
const QString docPath = doc->path() + '/' + importPath;
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
foreach (Document::Ptr candidate, *this) {
|
2010-01-18 13:13:34 +01:00
|
|
|
if (candidate == doc)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (candidate->path() == doc->path() || candidate->path() == docPath)
|
|
|
|
result.insert(candidate->componentName(), candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|