2010-01-18 13:13:34 +01:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
**
|
|
|
|
** 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 "qmljsidcollector.h"
|
|
|
|
#include "qmljsdocument.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-18 16:15:23 +01:00
|
|
|
using namespace QmlJS;
|
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)
|
|
|
|
, _uiProgram(0)
|
|
|
|
, _jsProgram(0)
|
|
|
|
, _fileName(fileName)
|
|
|
|
, _parsedCorrectly(false)
|
|
|
|
{
|
|
|
|
const int slashIdx = fileName.lastIndexOf('/');
|
|
|
|
if (slashIdx != -1)
|
|
|
|
_path = fileName.left(slashIdx);
|
|
|
|
|
|
|
|
if (fileName.toLower().endsWith(".qml"))
|
|
|
|
_componentName = fileName.mid(slashIdx + 1, fileName.size() - (slashIdx + 1) - 4);
|
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
Document::~Document()
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
|
|
|
if (_engine)
|
|
|
|
delete _engine;
|
|
|
|
|
|
|
|
if (_pool)
|
|
|
|
delete _pool;
|
|
|
|
|
|
|
|
qDeleteAll(_symbols);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
return _uiProgram;
|
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
AST::Program *Document::jsProgram() const
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
|
|
|
return _jsProgram;
|
|
|
|
}
|
|
|
|
|
2010-01-19 10:16:57 +01:00
|
|
|
AST::Node *Document::ast() const
|
|
|
|
{
|
|
|
|
Q_ASSERT(!_uiProgram || !_jsProgram);
|
|
|
|
if (_uiProgram)
|
|
|
|
return _uiProgram;
|
|
|
|
return _jsProgram;
|
|
|
|
}
|
|
|
|
|
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-18 16:15:23 +01:00
|
|
|
bool Document::parseQml()
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
|
|
|
Q_ASSERT(! _engine);
|
|
|
|
Q_ASSERT(! _pool);
|
|
|
|
Q_ASSERT(! _uiProgram);
|
|
|
|
Q_ASSERT(! _jsProgram);
|
|
|
|
|
|
|
|
_engine = new Engine();
|
|
|
|
_pool = new NodePool(_fileName, _engine);
|
|
|
|
_ids.clear();
|
|
|
|
|
|
|
|
Lexer lexer(_engine);
|
|
|
|
Parser parser(_engine);
|
|
|
|
|
|
|
|
lexer.setCode(_source, /*line = */ 1);
|
|
|
|
|
|
|
|
_parsedCorrectly = parser.parse();
|
|
|
|
_uiProgram = parser.ast();
|
|
|
|
_diagnosticMessages = parser.diagnosticMessages();
|
|
|
|
|
|
|
|
if (_uiProgram) {
|
|
|
|
for (QmlJS::AST::UiObjectMemberList *iter = _uiProgram->members; iter; iter = iter->next)
|
|
|
|
if (iter->member)
|
2010-01-18 16:15:23 +01:00
|
|
|
_symbols.append(new SymbolFromFile(_fileName, iter->member));
|
2010-01-18 13:13:34 +01:00
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
Internal::IdCollector collect;
|
2010-01-18 13:13:34 +01:00
|
|
|
_ids = collect(*this);
|
|
|
|
if (_diagnosticMessages.isEmpty())
|
|
|
|
_diagnosticMessages = collect.diagnosticMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _parsedCorrectly;
|
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
bool Document::parseJavaScript()
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
|
|
|
Q_ASSERT(! _engine);
|
|
|
|
Q_ASSERT(! _pool);
|
|
|
|
Q_ASSERT(! _uiProgram);
|
|
|
|
Q_ASSERT(! _jsProgram);
|
|
|
|
|
|
|
|
_engine = new Engine();
|
|
|
|
_pool = new NodePool(_fileName, _engine);
|
|
|
|
_ids.clear();
|
|
|
|
|
|
|
|
Lexer lexer(_engine);
|
|
|
|
Parser parser(_engine);
|
|
|
|
|
|
|
|
lexer.setCode(_source, /*line = */ 1);
|
|
|
|
|
|
|
|
_parsedCorrectly = parser.parseProgram();
|
|
|
|
_jsProgram = cast<Program*>(parser.rootNode());
|
|
|
|
_diagnosticMessages = parser.diagnosticMessages();
|
|
|
|
|
|
|
|
return _parsedCorrectly;
|
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
SymbolFromFile *Document::findSymbol(QmlJS::AST::Node *node) const
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
2010-01-18 16:15:23 +01:00
|
|
|
foreach (Symbol *symbol, _symbols)
|
2010-01-18 13:13:34 +01:00
|
|
|
if (symbol->isSymbolFromFile())
|
|
|
|
if (symbol->asSymbolFromFile()->node() == node)
|
|
|
|
return symbol->asSymbolFromFile();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-18 16:15:23 +01:00
|
|
|
QMap<QString, Document::Ptr>::insert(document->fileName(), document);
|
2010-01-18 13:13:34 +01:00
|
|
|
}
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
Document::PtrList Snapshot::importedDocuments(const Document::Ptr &doc, const QString &importPath) const
|
2010-01-18 13:13:34 +01:00
|
|
|
{
|
2010-01-18 16:15:23 +01:00
|
|
|
Document::PtrList 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.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;
|
|
|
|
}
|