2012-11-07 16:38:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2012-11-07 16:38:01 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:58:39 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-11-07 16:38:01 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2012-11-07 16:38:01 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "qmljssimplereader.h"
|
|
|
|
|
|
|
|
|
|
#include "parser/qmljsparser_p.h"
|
|
|
|
|
#include "parser/qmljslexer_p.h"
|
|
|
|
|
#include "parser/qmljsengine_p.h"
|
|
|
|
|
|
|
|
|
|
#include "qmljsutils.h"
|
|
|
|
|
|
|
|
|
|
#include <QFile>
|
2014-08-26 15:53:13 +02:00
|
|
|
#include <QLoggingCategory>
|
2012-11-07 16:38:01 +01:00
|
|
|
|
2018-10-12 09:33:30 +03:00
|
|
|
static Q_LOGGING_CATEGORY(simpleReaderLog, "qtc.qmljs.simpleReader", QtWarningMsg)
|
2014-07-07 13:24:51 +02:00
|
|
|
|
|
|
|
|
namespace QmlJS{
|
2012-11-07 16:38:01 +01:00
|
|
|
|
|
|
|
|
QVariant SimpleReaderNode::property(const QString &name) const
|
|
|
|
|
{
|
|
|
|
|
return m_properties.value(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList SimpleReaderNode::propertyNames() const
|
|
|
|
|
{
|
|
|
|
|
return m_properties.keys();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleReaderNode::PropertyHash SimpleReaderNode::properties() const
|
|
|
|
|
{
|
|
|
|
|
return m_properties;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SimpleReaderNode::isRoot() const
|
|
|
|
|
{
|
|
|
|
|
return m_parentNode.isNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SimpleReaderNode::isValid() const
|
|
|
|
|
{
|
|
|
|
|
return !m_name.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleReaderNode::Ptr SimpleReaderNode::invalidNode()
|
|
|
|
|
{
|
|
|
|
|
return Ptr(new SimpleReaderNode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleReaderNode::WeakPtr SimpleReaderNode::parent() const
|
|
|
|
|
{
|
|
|
|
|
return m_parentNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SimpleReaderNode::name() const
|
|
|
|
|
{
|
|
|
|
|
return m_name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleReaderNode::SimpleReaderNode()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleReaderNode::SimpleReaderNode(const QString &name, WeakPtr parent)
|
|
|
|
|
: m_name(name), m_parentNode(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleReaderNode::Ptr SimpleReaderNode::create(const QString &name, WeakPtr parent)
|
|
|
|
|
{
|
|
|
|
|
Ptr newNode(new SimpleReaderNode(name, parent));
|
|
|
|
|
newNode->m_weakThis = newNode;
|
|
|
|
|
if (parent)
|
|
|
|
|
parent.data()->m_children.append(newNode);
|
|
|
|
|
return newNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SimpleReaderNode::List SimpleReaderNode::children() const
|
|
|
|
|
{
|
|
|
|
|
return m_children;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleReaderNode::setProperty(const QString &name, const QVariant &value)
|
|
|
|
|
{
|
|
|
|
|
m_properties.insert(name, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleAbstractStreamReader::SimpleAbstractStreamReader()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SimpleAbstractStreamReader::readFile(const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
QFile file(fileName);
|
|
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
|
|
|
QByteArray source = file.readAll();
|
|
|
|
|
file.close();
|
2012-11-28 12:34:02 +01:00
|
|
|
return readFromSource(QString::fromLocal8Bit(source));
|
2012-11-07 16:38:01 +01:00
|
|
|
}
|
2013-02-08 17:12:45 +01:00
|
|
|
addError(tr("Cannot find file %1.").arg(fileName));
|
2012-11-07 16:38:01 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SimpleAbstractStreamReader::readFromSource(const QString &source)
|
|
|
|
|
{
|
2012-11-08 13:41:28 +01:00
|
|
|
m_errors.clear();
|
|
|
|
|
m_currentSourceLocation = AST::SourceLocation();
|
|
|
|
|
|
2016-10-04 17:03:46 +02:00
|
|
|
m_source = source;
|
|
|
|
|
|
2012-11-07 16:38:01 +01:00
|
|
|
Engine engine;
|
|
|
|
|
Lexer lexer(&engine);
|
|
|
|
|
Parser parser(&engine);
|
|
|
|
|
|
|
|
|
|
lexer.setCode(source, /*line = */ 1, /*qmlMode = */true);
|
|
|
|
|
|
|
|
|
|
if (!parser.parse()) {
|
2012-11-27 20:20:02 +02:00
|
|
|
QString errorMessage = QString::fromLatin1("%1:%2: %3").arg(
|
2012-11-07 16:38:01 +01:00
|
|
|
QString::number(parser.errorLineNumber()),
|
|
|
|
|
QString::number(parser.errorColumnNumber()),
|
|
|
|
|
parser.errorMessage());
|
|
|
|
|
addError(errorMessage);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return readDocument(parser.ast());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList SimpleAbstractStreamReader::errors() const
|
|
|
|
|
{
|
|
|
|
|
return m_errors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleAbstractStreamReader::addError(const QString &error, const AST::SourceLocation &sourceLocation)
|
|
|
|
|
{
|
2012-11-27 20:20:02 +02:00
|
|
|
m_errors << QString::fromLatin1("%1:%2: %3\n").arg(
|
2012-11-07 16:38:01 +01:00
|
|
|
QString::number(sourceLocation.startLine),
|
|
|
|
|
QString::number(sourceLocation.startColumn),
|
|
|
|
|
error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AST::SourceLocation SimpleAbstractStreamReader::currentSourceLocation() const
|
|
|
|
|
{
|
|
|
|
|
return m_currentSourceLocation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SimpleAbstractStreamReader::readDocument(AST::UiProgram *ast)
|
|
|
|
|
{
|
|
|
|
|
if (!ast) {
|
2013-02-08 17:12:45 +01:00
|
|
|
addError(tr("Could not parse document."));
|
2012-11-14 03:57:55 +01:00
|
|
|
return false;
|
2012-11-07 16:38:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AST::UiObjectDefinition *uiObjectDefinition = AST::cast<AST::UiObjectDefinition *>(ast->members->member);
|
|
|
|
|
if (!uiObjectDefinition) {
|
2013-02-08 17:12:45 +01:00
|
|
|
addError(tr("Expected document to contain a single object definition."));
|
2012-11-14 03:57:55 +01:00
|
|
|
return false;
|
2012-11-07 16:38:01 +01:00
|
|
|
}
|
|
|
|
|
readChild(uiObjectDefinition);
|
|
|
|
|
|
2016-10-04 17:03:46 +02:00
|
|
|
m_source.clear();
|
|
|
|
|
|
2012-11-07 16:38:01 +01:00
|
|
|
return errors().isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleAbstractStreamReader::readChildren(AST::UiObjectDefinition *uiObjectDefinition)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(uiObjectDefinition);
|
|
|
|
|
|
|
|
|
|
for (AST::UiObjectMemberList *it = uiObjectDefinition->initializer->members; it; it = it->next) {
|
|
|
|
|
AST::UiObjectMember *member = it->member;
|
|
|
|
|
AST::UiObjectDefinition *uiObjectDefinition = AST::cast<AST::UiObjectDefinition *>(member);
|
|
|
|
|
if (uiObjectDefinition)
|
|
|
|
|
readChild(uiObjectDefinition);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleAbstractStreamReader::readChild(AST::UiObjectDefinition *uiObjectDefinition)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(uiObjectDefinition);
|
|
|
|
|
|
|
|
|
|
setSourceLocation(uiObjectDefinition->firstSourceLocation());
|
|
|
|
|
|
|
|
|
|
elementStart(toString(uiObjectDefinition->qualifiedTypeNameId));
|
|
|
|
|
|
|
|
|
|
readProperties(uiObjectDefinition);
|
|
|
|
|
readChildren(uiObjectDefinition);
|
|
|
|
|
|
|
|
|
|
elementEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleAbstractStreamReader::readProperties(AST::UiObjectDefinition *uiObjectDefinition)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(uiObjectDefinition);
|
|
|
|
|
|
|
|
|
|
for (AST::UiObjectMemberList *it = uiObjectDefinition->initializer->members; it; it = it->next) {
|
|
|
|
|
AST::UiObjectMember *member = it->member;
|
|
|
|
|
AST::UiScriptBinding *scriptBinding = AST::cast<AST::UiScriptBinding *>(member);
|
|
|
|
|
if (scriptBinding)
|
|
|
|
|
readProperty(scriptBinding);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleAbstractStreamReader::readProperty(AST::UiScriptBinding *uiScriptBinding)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(uiScriptBinding);
|
|
|
|
|
|
|
|
|
|
setSourceLocation(uiScriptBinding->firstSourceLocation());
|
|
|
|
|
|
|
|
|
|
const QString name = toString(uiScriptBinding->qualifiedId);
|
2012-11-09 11:23:32 +01:00
|
|
|
const QVariant value = parsePropertyScriptBinding(uiScriptBinding);
|
2012-11-07 16:38:01 +01:00
|
|
|
|
|
|
|
|
propertyDefinition(name, value);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 11:23:32 +01:00
|
|
|
QVariant SimpleAbstractStreamReader::parsePropertyScriptBinding(AST::UiScriptBinding *uiScriptBinding)
|
2012-11-07 16:38:01 +01:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(uiScriptBinding);
|
|
|
|
|
|
|
|
|
|
AST::ExpressionStatement *expStmt = AST::cast<AST::ExpressionStatement *>(uiScriptBinding->statement);
|
|
|
|
|
if (!expStmt) {
|
2013-02-08 17:12:45 +01:00
|
|
|
addError(tr("Expected expression statement after colon."), uiScriptBinding->statement->firstSourceLocation());
|
2012-11-07 16:38:01 +01:00
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 11:23:32 +01:00
|
|
|
return parsePropertyExpression(expStmt->expression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant SimpleAbstractStreamReader::parsePropertyExpression(AST::ExpressionNode *expressionNode)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(expressionNode);
|
|
|
|
|
|
2018-10-16 15:32:58 +02:00
|
|
|
AST::ArrayPattern *arrayLiteral = AST::cast<AST::ArrayPattern *>(expressionNode);
|
2012-11-09 11:23:32 +01:00
|
|
|
|
|
|
|
|
if (arrayLiteral) {
|
|
|
|
|
QList<QVariant> variantList;
|
2018-10-16 15:32:58 +02:00
|
|
|
for (AST::PatternElementList *it = arrayLiteral->elements; it; it = it->next)
|
|
|
|
|
variantList << parsePropertyExpression(it->element->initializer);
|
2012-11-09 11:23:32 +01:00
|
|
|
return variantList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AST::StringLiteral *stringLiteral = AST::cast<AST::StringLiteral *>(expressionNode);
|
2012-11-07 16:38:01 +01:00
|
|
|
if (stringLiteral)
|
|
|
|
|
return stringLiteral->value.toString();
|
|
|
|
|
|
2012-11-09 11:23:32 +01:00
|
|
|
AST::TrueLiteral *trueLiteral = AST::cast<AST::TrueLiteral *>(expressionNode);
|
2012-11-07 16:38:01 +01:00
|
|
|
if (trueLiteral)
|
|
|
|
|
return true;
|
|
|
|
|
|
2012-11-09 11:23:32 +01:00
|
|
|
AST::FalseLiteral *falseLiteral = AST::cast<AST::FalseLiteral *>(expressionNode);
|
2012-11-07 16:38:01 +01:00
|
|
|
if (falseLiteral)
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-11-09 11:23:32 +01:00
|
|
|
AST::NumericLiteral *numericLiteral = AST::cast<AST::NumericLiteral *>(expressionNode);
|
2012-11-07 16:38:01 +01:00
|
|
|
if (numericLiteral)
|
|
|
|
|
return numericLiteral->value;
|
|
|
|
|
|
2016-10-04 17:03:46 +02:00
|
|
|
return textAt(expressionNode->firstSourceLocation(), expressionNode->lastSourceLocation());
|
2012-11-07 16:38:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleAbstractStreamReader::setSourceLocation(const AST::SourceLocation &sourceLocation)
|
|
|
|
|
{
|
|
|
|
|
m_currentSourceLocation = sourceLocation;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:03:46 +02:00
|
|
|
QString SimpleAbstractStreamReader::textAt(const AST::SourceLocation &from,
|
|
|
|
|
const AST::SourceLocation &to)
|
|
|
|
|
{
|
|
|
|
|
return m_source.mid(from.offset, to.end() - from.begin());
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-07 16:38:01 +01:00
|
|
|
SimpleReader::SimpleReader()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleReaderNode::Ptr SimpleReader::readFile(const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
SimpleAbstractStreamReader::readFile(fileName);
|
|
|
|
|
return m_rootNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleReaderNode::Ptr SimpleReader::readFromSource(const QString &source)
|
|
|
|
|
{
|
|
|
|
|
SimpleAbstractStreamReader::readFromSource(source);
|
|
|
|
|
return m_rootNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleReader::elementStart(const QString &name)
|
|
|
|
|
{
|
2014-05-27 10:39:50 +02:00
|
|
|
qCDebug(simpleReaderLog) << "elementStart()" << name;
|
2012-11-07 16:38:01 +01:00
|
|
|
|
|
|
|
|
SimpleReaderNode::Ptr newNode = SimpleReaderNode::create(name, m_currentNode);
|
|
|
|
|
|
|
|
|
|
if (newNode->isRoot())
|
|
|
|
|
m_rootNode = newNode;
|
|
|
|
|
|
|
|
|
|
Q_ASSERT(newNode->isValid());
|
|
|
|
|
|
|
|
|
|
m_currentNode = newNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleReader::elementEnd()
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(m_currentNode);
|
|
|
|
|
|
2014-05-27 10:39:50 +02:00
|
|
|
qCDebug(simpleReaderLog) << "elementEnd()" << m_currentNode.data()->name();
|
2012-11-07 16:38:01 +01:00
|
|
|
|
|
|
|
|
m_currentNode = m_currentNode.data()->parent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleReader::propertyDefinition(const QString &name, const QVariant &value)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(m_currentNode);
|
|
|
|
|
|
2014-05-27 10:39:50 +02:00
|
|
|
qCDebug(simpleReaderLog) << "propertyDefinition()" << m_currentNode.data()->name() << name << value;
|
2012-11-07 16:38:01 +01:00
|
|
|
|
|
|
|
|
if (m_currentNode.data()->propertyNames().contains(name))
|
2013-02-08 17:12:45 +01:00
|
|
|
addError(tr("Property is defined twice."), currentSourceLocation());
|
2012-11-07 16:38:01 +01:00
|
|
|
|
|
|
|
|
m_currentNode.data()->setProperty(name, value);
|
|
|
|
|
}
|
2014-05-27 10:39:50 +02:00
|
|
|
|
|
|
|
|
} // namespace QmlJS
|