diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-05-26 01:17:20 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-05-26 01:17:20 (GMT) |
commit | 4058b501914216bf28ab62c02b78abcbf7f5a3c9 (patch) | |
tree | 5dc502f69aa8f2151c9adfee3580f1d1aa0833aa /src/declarative/qml/qmlscriptparser.cpp | |
parent | 334e406ff6441d3980741683714087c59c7dd123 (diff) | |
download | Qt-4058b501914216bf28ab62c02b78abcbf7f5a3c9.zip Qt-4058b501914216bf28ab62c02b78abcbf7f5a3c9.tar.gz Qt-4058b501914216bf28ab62c02b78abcbf7f5a3c9.tar.bz2 |
roberto: Added support for CSS like numeric literals e.g. 10p
Diffstat (limited to 'src/declarative/qml/qmlscriptparser.cpp')
-rw-r--r-- | src/declarative/qml/qmlscriptparser.cpp | 74 |
1 files changed, 61 insertions, 13 deletions
diff --git a/src/declarative/qml/qmlscriptparser.cpp b/src/declarative/qml/qmlscriptparser.cpp index 5b3564f..07f6b17 100644 --- a/src/declarative/qml/qmlscriptparser.cpp +++ b/src/declarative/qml/qmlscriptparser.cpp @@ -9,6 +9,8 @@ #include "parser/javascriptastvisitor_p.h" #include "parser/javascriptast_p.h" +#include "rewriter/textwriter_p.h" + #include <QStack> #include <QCoreApplication> #include <QtDebug> @@ -22,6 +24,46 @@ using namespace QmlParser; namespace { +class RewriteNumericLiterals: protected AST::Visitor +{ + unsigned _position; + TextWriter _writer; + +public: + QString operator()(QString code, unsigned position, AST::Node *node) + { + _position = position; + + AST::Node::acceptChild(node, this); + + _writer.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 { @@ -107,24 +149,30 @@ protected: QString textAt(const AST::SourceLocation &loc) const { return _contents.mid(loc.offset, loc.length); } + QString textAt(const AST::SourceLocation &first, const AST::SourceLocation &last) const { return _contents.mid(first.offset, last.offset + last.length - first.offset); } - QString asString(AST::ExpressionNode *expr) const + RewriteNumericLiterals rewriteNumericLiterals; + + QString asString(AST::ExpressionNode *expr) { if (! expr) return QString(); - return textAt(expr->firstSourceLocation(), expr->lastSourceLocation()); + return rewriteNumericLiterals(textAt(expr->firstSourceLocation(), expr->lastSourceLocation()), + expr->firstSourceLocation().offset, expr); } - QString asString(AST::Statement *stmt) const + QString asString(AST::Statement *stmt) { if (! stmt) return QString(); - QString s = textAt(stmt->firstSourceLocation(), stmt->lastSourceLocation()); + QString s = rewriteNumericLiterals(textAt(stmt->firstSourceLocation(), stmt->lastSourceLocation()), + stmt->firstSourceLocation().offset, stmt); + s += QLatin1Char('\n'); return s; } @@ -214,7 +262,7 @@ ProcessAST::defineObjectBinding_helper(AST::UiQualifiedId *propertyName, int propertyCount = 0; for (; propertyName; propertyName = propertyName->next){ ++propertyCount; - _stateStack.pushProperty(propertyName->name->asString(), + _stateStack.pushProperty(propertyName->name->asString(), this->location(propertyName)); } @@ -322,7 +370,7 @@ Object *ProcessAST::defineObjectBinding(AST::UiQualifiedId *qualifiedId, script = asString(scriptBinding->statement); } - LocationSpan l = this->location(scriptBinding->statement->firstSourceLocation(), + LocationSpan l = this->location(scriptBinding->statement->firstSourceLocation(), scriptBinding->statement->lastSourceLocation()); _stateStack.pushProperty(QLatin1String("script"), l); @@ -414,7 +462,7 @@ bool ProcessAST::visit(AST::UiPublicMember *node) { "var", Object::DynamicProperty::Variant }, { "variant", Object::DynamicProperty::Variant } }; - const int propTypeNameToTypesCount = sizeof(propTypeNameToTypes) / + const int propTypeNameToTypesCount = sizeof(propTypeNameToTypes) / sizeof(propTypeNameToTypes[0]); bool typeFound = false; @@ -425,7 +473,7 @@ bool ProcessAST::visit(AST::UiPublicMember *node) typeFound = true; } } - + if(!typeFound) { QmlError error; error.setDescription(QCoreApplication::translate("QmlParser","Expected property type")); @@ -515,7 +563,7 @@ bool ProcessAST::visit(AST::UiScriptBinding *node) AST::UiQualifiedId *propertyName = node->qualifiedId; for (; propertyName; propertyName = propertyName->next){ ++propertyCount; - _stateStack.pushProperty(propertyName->name->asString(), + _stateStack.pushProperty(propertyName->name->asString(), location(propertyName)); } @@ -526,7 +574,7 @@ bool ProcessAST::visit(AST::UiScriptBinding *node) if (AST::ExpressionStatement *stmt = AST::cast<AST::ExpressionStatement *>(node->statement)) { primitive = getVariant(stmt->expression); } else { // do binding - primitive = QmlParser::Variant(asString(node->statement), + primitive = QmlParser::Variant(asString(node->statement), QmlParser::Variant::Script); } @@ -550,7 +598,7 @@ bool ProcessAST::visit(AST::UiArrayBinding *node) AST::UiQualifiedId *propertyName = node->qualifiedId; for (; propertyName; propertyName = propertyName->next){ ++propertyCount; - _stateStack.pushProperty(propertyName->name->asString(), + _stateStack.pushProperty(propertyName->name->asString(), location(propertyName)); } @@ -608,7 +656,7 @@ bool ProcessAST::visit(AST::UiSourceElement *node) } Value *value = new Value; - value->location = location(node->firstSourceLocation(), + value->location = location(node->firstSourceLocation(), node->lastSourceLocation()); value->value = QmlParser::Variant(source); @@ -673,7 +721,7 @@ bool QmlScriptParser::parse(const QByteArray &data, const QUrl &url) process(code, parser.ast()); // Set the url for process errors - for(int ii = 0; ii < _errors.count(); ++ii) + for(int ii = 0; ii < _errors.count(); ++ii) _errors[ii].setUrl(url); } |