From d080c1c2ded0d59974f86f9f3dac91b099bda0a9 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Thu, 16 Jul 2009 15:49:44 +1000 Subject: Move the QML rewriting stuff into a central location --- src/declarative/qml/qml.pri | 2 + src/declarative/qml/qmlexpression.cpp | 98 +------------------- src/declarative/qml/qmlrewrite.cpp | 157 ++++++++++++++++++++++++++++++++ src/declarative/qml/qmlrewrite_p.h | 103 +++++++++++++++++++++ src/declarative/qml/qmlscriptparser.cpp | 46 +--------- 5 files changed, 266 insertions(+), 140 deletions(-) create mode 100644 src/declarative/qml/qmlrewrite.cpp create mode 100644 src/declarative/qml/qmlrewrite_p.h diff --git a/src/declarative/qml/qml.pri b/src/declarative/qml/qml.pri index 7f1c3f7..0b91ed3 100644 --- a/src/declarative/qml/qml.pri +++ b/src/declarative/qml/qml.pri @@ -26,6 +26,7 @@ SOURCES += qml/qmlparser.cpp \ qml/qmlerror.cpp \ qml/qmlscriptparser.cpp \ qml/qmlenginedebug.cpp \ + qml/qmlrewrite.cpp \ qml/qmlbasicscript.cpp HEADERS += qml/qmlparser_p.h \ @@ -67,6 +68,7 @@ HEADERS += qml/qmlparser_p.h \ qml/qmlscriptparser_p.h \ qml/qmlbasicscript_p.h \ qml/qmlenginedebug_p.h \ + qml/qmlrewrite_p.h \ qml/qpodvector_p.h # for qtscript debugger diff --git a/src/declarative/qml/qmlexpression.cpp b/src/declarative/qml/qmlexpression.cpp index 4fe7d0c..2aa1a8a 100644 --- a/src/declarative/qml/qmlexpression.cpp +++ b/src/declarative/qml/qmlexpression.cpp @@ -43,10 +43,7 @@ #include "qmlexpression_p.h" #include "qmlengine_p.h" #include "qmlcontext_p.h" -#include "rewriter/textwriter_p.h" -#include "parser/qmljslexer_p.h" -#include "parser/qmljsparser_p.h" -#include "parser/qmljsnodepool_p.h" +#include "qmlrewrite_p.h" #include "QtCore/qdebug.h" Q_DECLARE_METATYPE(QList); @@ -55,97 +52,6 @@ QT_BEGIN_NAMESPACE DEFINE_BOOL_CONFIG_OPTION(qmlDebugger, QML_DEBUGGER) -namespace { - -using namespace QmlJS; - -class RewriteBinding: protected AST::Visitor -{ - unsigned _position; - TextWriter *_writer; - -public: - QString operator()(const QString &code) - { - Engine engine; - NodePool pool(QString(), &engine); - Lexer lexer(&engine); - Parser parser(&engine); - lexer.setCode(code, 0); - parser.parseStatement(); - return rewrite(code, 0, parser.statement()); - } - -protected: - using AST::Visitor::visit; - - void accept(AST::Node *node) - { - AST::Node::acceptChild(node, this); - } - - QString rewrite(QString code, unsigned position, AST::Statement *node) - { - TextWriter w; - _writer = &w; - _position = position; - - accept(node); - - unsigned startOfStatement = node->firstSourceLocation().begin() - _position; - unsigned endOfStatement = node->lastSourceLocation().end() - _position; - - _writer->replace(startOfStatement, 0, QLatin1String("function() {\n")); - _writer->replace(endOfStatement, 0, QLatin1String("\n}")); - - w.write(&code); - - return code; - } - - virtual bool visit(AST::Block *ast) - { - for (AST::StatementList *it = ast->statements; it; it = it->next) { - if (! it->next) { - // we need to rewrite only the last statement of a block. - accept(it->statement); - } - } - - return false; - } - - virtual bool visit(AST::ExpressionStatement *ast) - { - unsigned startOfExpressionStatement = ast->firstSourceLocation().begin() - _position; - _writer->replace(startOfExpressionStatement, 0, QLatin1String("return ")); - - return false; - } - - virtual bool visit(AST::NumericLiteral *node) - { - if (node->suffix != AST::NumericLiteral::noSuffix) { - const int suffixLength = AST::NumericLiteral::suffixLength[node->suffix]; - const char *suffixSpell = AST::NumericLiteral::suffixSpell[node->suffix]; - QString pre; - pre += QLatin1String("qmlNumberFrom"); - pre += QChar(QLatin1Char(suffixSpell[0])).toUpper(); - pre += QLatin1String(&suffixSpell[1]); - pre += QLatin1Char('('); - _writer->replace(node->literalToken.begin() - _position, 0, pre); - _writer->replace(node->literalToken.end() - _position - suffixLength, - suffixLength, - QLatin1String(")")); - } - - return false; - } -}; - -} // end of anonymous namespace - - QmlExpressionPrivate::QmlExpressionPrivate(QmlExpression *b) : q(b), ctxt(0), expressionFunctionValid(false), sseData(0), proxy(0), me(0), trackChange(false), line(-1), id(0), log(0) { @@ -332,7 +238,7 @@ QVariant QmlExpressionPrivate::evalQtScript() scriptEngine->currentContext()->pushScope(ctxtPriv->scopeChain.at(i)); if (!expressionFunctionValid) { - RewriteBinding rewriteBinding; + QmlRewrite::RewriteBinding rewriteBinding; const QString code = rewriteBinding(expression); expressionFunction = scriptEngine->evaluate(code, fileName.toString(), line); diff --git a/src/declarative/qml/qmlrewrite.cpp b/src/declarative/qml/qmlrewrite.cpp new file mode 100644 index 0000000..02bf8fa --- /dev/null +++ b/src/declarative/qml/qmlrewrite.cpp @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** 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.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmlrewrite_p.h" + +QT_BEGIN_NAMESPACE + +namespace QmlRewrite { + +QString RewriteBinding::operator()(const QString &code) +{ + Engine engine; + NodePool pool(QString(), &engine); + Lexer lexer(&engine); + Parser parser(&engine); + lexer.setCode(code, 0); + parser.parseStatement(); + return rewrite(code, 0, parser.statement()); +} + +void RewriteBinding::accept(AST::Node *node) +{ + AST::Node::acceptChild(node, this); +} + +QString RewriteBinding::rewrite(QString code, unsigned position, + AST::Statement *node) +{ + TextWriter w; + _writer = &w; + _position = position; + + accept(node); + + unsigned startOfStatement = node->firstSourceLocation().begin() - _position; + unsigned endOfStatement = node->lastSourceLocation().end() - _position; + + _writer->replace(startOfStatement, 0, QLatin1String("function() {\n")); + _writer->replace(endOfStatement, 0, QLatin1String("\n}")); + + w.write(&code); + + return code; +} + +bool RewriteBinding::visit(AST::Block *ast) +{ + for (AST::StatementList *it = ast->statements; it; it = it->next) { + if (! it->next) { + // we need to rewrite only the last statement of a block. + accept(it->statement); + } + } + + return false; +} + +bool RewriteBinding::visit(AST::ExpressionStatement *ast) +{ + unsigned startOfExpressionStatement = ast->firstSourceLocation().begin() - _position; + _writer->replace(startOfExpressionStatement, 0, QLatin1String("return ")); + + return false; +} + +bool RewriteBinding::visit(AST::NumericLiteral *node) +{ + if (node->suffix != AST::NumericLiteral::noSuffix) { + const int suffixLength = AST::NumericLiteral::suffixLength[node->suffix]; + const char *suffixSpell = AST::NumericLiteral::suffixSpell[node->suffix]; + QString pre; + pre += QLatin1String("qmlNumberFrom"); + pre += QChar(QLatin1Char(suffixSpell[0])).toUpper(); + pre += QLatin1String(&suffixSpell[1]); + pre += QLatin1Char('('); + _writer->replace(node->literalToken.begin() - _position, 0, pre); + _writer->replace(node->literalToken.end() - _position - suffixLength, + suffixLength, + QLatin1String(")")); + } + + return false; +} + +QString RewriteNumericLiterals::operator()(QString code, unsigned position, AST::Node *node) +{ + TextWriter w; + _writer = &w; + _position = position; + + AST::Node::acceptChild(node, this); + + w.write(&code); + + return code; +} + +bool RewriteNumericLiterals::visit(AST::NumericLiteral *node) +{ + if (node->suffix != AST::NumericLiteral::noSuffix) { + const int suffixLength = AST::NumericLiteral::suffixLength[node->suffix]; + const char *suffixSpell = AST::NumericLiteral::suffixSpell[node->suffix]; + QString pre; + pre += QLatin1String("qmlNumberFrom"); + pre += QChar(QLatin1Char(suffixSpell[0])).toUpper(); + pre += QLatin1String(&suffixSpell[1]); + pre += QLatin1Char('('); + _writer->replace(node->literalToken.begin() - _position, 0, pre); + _writer->replace(node->literalToken.end() - _position - suffixLength, + suffixLength, + QLatin1String(")")); + } + + return false; +} + +} // namespace QmlRewrite + +QT_END_NAMESPACE diff --git a/src/declarative/qml/qmlrewrite_p.h b/src/declarative/qml/qmlrewrite_p.h new file mode 100644 index 0000000..51a8015 --- /dev/null +++ b/src/declarative/qml/qmlrewrite_p.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** 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.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLREWRITE_P_H +#define QMLREWRITE_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 "rewriter/textwriter_p.h" +#include "parser/qmljslexer_p.h" +#include "parser/qmljsparser_p.h" +#include "parser/qmljsnodepool_p.h" + +QT_BEGIN_NAMESPACE + +namespace QmlRewrite { +using namespace QmlJS; + +class RewriteBinding: protected AST::Visitor +{ + unsigned _position; + TextWriter *_writer; + +public: + QString operator()(const QString &code); + +protected: + using AST::Visitor::visit; + + void accept(AST::Node *node); + QString rewrite(QString code, unsigned position, AST::Statement *node); + virtual bool visit(AST::Block *ast); + virtual bool visit(AST::ExpressionStatement *ast); + virtual bool visit(AST::NumericLiteral *node); +}; + +class RewriteNumericLiterals: protected AST::Visitor +{ + unsigned _position; + TextWriter *_writer; + +public: + QString operator()(QString code, unsigned position, AST::Node *node); + +protected: + using AST::Visitor::visit; + + virtual bool visit(AST::NumericLiteral *node); +}; + +} // namespace QmlRewrite + +QT_END_NAMESPACE + +#endif // QMLREWRITE_P_H + diff --git a/src/declarative/qml/qmlscriptparser.cpp b/src/declarative/qml/qmlscriptparser.cpp index f26266b..5f97c71 100644 --- a/src/declarative/qml/qmlscriptparser.cpp +++ b/src/declarative/qml/qmlscriptparser.cpp @@ -49,7 +49,7 @@ #include "parser/qmljsastvisitor_p.h" #include "parser/qmljsast_p.h" -#include "rewriter/textwriter_p.h" +#include "qmlrewrite_p.h" #include #include @@ -64,48 +64,6 @@ using namespace QmlParser; namespace { -class RewriteNumericLiterals: protected AST::Visitor -{ - unsigned _position; - TextWriter *_writer; - -public: - QString operator()(QString code, unsigned position, AST::Node *node) - { - TextWriter w; - _writer = &w; - _position = position; - - AST::Node::acceptChild(node, this); - - w.write(&code); - - return code; - } - -protected: - using AST::Visitor::visit; - - virtual bool visit(AST::NumericLiteral *node) - { - if (node->suffix != AST::NumericLiteral::noSuffix) { - const int suffixLength = AST::NumericLiteral::suffixLength[node->suffix]; - const char *suffixSpell = AST::NumericLiteral::suffixSpell[node->suffix]; - QString pre; - pre += QLatin1String("qmlNumberFrom"); - pre += QChar(QLatin1Char(suffixSpell[0])).toUpper(); - pre += QLatin1String(&suffixSpell[1]); - pre += QLatin1Char('('); - _writer->replace(node->literalToken.begin() - _position, 0, pre); - _writer->replace(node->literalToken.end() - _position - suffixLength, - suffixLength, - QLatin1String(")")); - } - - return false; - } -}; - class ProcessAST: protected AST::Visitor { struct State { @@ -196,7 +154,7 @@ protected: const AST::SourceLocation &last) const { return _contents.mid(first.offset, last.offset + last.length - first.offset); } - RewriteNumericLiterals rewriteNumericLiterals; + QmlRewrite::RewriteNumericLiterals rewriteNumericLiterals; QString asString(AST::ExpressionNode *expr) { -- cgit v0.12