QmlJS:SimpleReader: adding support for arrays

Also adding test.

Change-Id: Idaf0aeacbb6f78e5c8404db740c2a8b7b297dad2
Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com>
This commit is contained in:
Thomas Hartmann
2012-11-09 11:23:32 +01:00
parent cb02dd812a
commit 6cf321a316
3 changed files with 64 additions and 8 deletions

View File

@@ -232,12 +232,12 @@ void SimpleAbstractStreamReader::readProperty(AST::UiScriptBinding *uiScriptBind
setSourceLocation(uiScriptBinding->firstSourceLocation());
const QString name = toString(uiScriptBinding->qualifiedId);
const QVariant value = parseProperty(uiScriptBinding);
const QVariant value = parsePropertyScriptBinding(uiScriptBinding);
propertyDefinition(name, value);
}
QVariant SimpleAbstractStreamReader::parseProperty(AST::UiScriptBinding *uiScriptBinding)
QVariant SimpleAbstractStreamReader::parsePropertyScriptBinding(AST::UiScriptBinding *uiScriptBinding)
{
Q_ASSERT(uiScriptBinding);
@@ -247,23 +247,39 @@ QVariant SimpleAbstractStreamReader::parseProperty(AST::UiScriptBinding *uiScrip
return QVariant();
}
AST::StringLiteral *stringLiteral = AST::cast<AST::StringLiteral *>(expStmt->expression);
return parsePropertyExpression(expStmt->expression);
}
QVariant SimpleAbstractStreamReader::parsePropertyExpression(AST::ExpressionNode *expressionNode)
{
Q_ASSERT(expressionNode);
AST::ArrayLiteral *arrayLiteral = AST::cast<AST::ArrayLiteral *>(expressionNode);
if (arrayLiteral) {
QList<QVariant> variantList;
for (AST::ElementList *it = arrayLiteral->elements; it; it = it->next)
variantList << parsePropertyExpression(it->expression);
return variantList;
}
AST::StringLiteral *stringLiteral = AST::cast<AST::StringLiteral *>(expressionNode);
if (stringLiteral)
return stringLiteral->value.toString();
AST::TrueLiteral *trueLiteral = AST::cast<AST::TrueLiteral *>(expStmt->expression);
AST::TrueLiteral *trueLiteral = AST::cast<AST::TrueLiteral *>(expressionNode);
if (trueLiteral)
return true;
AST::FalseLiteral *falseLiteral = AST::cast<AST::FalseLiteral *>(expStmt->expression);
AST::FalseLiteral *falseLiteral = AST::cast<AST::FalseLiteral *>(expressionNode);
if (falseLiteral)
return false;
AST::NumericLiteral *numericLiteral = AST::cast<AST::NumericLiteral *>(expStmt->expression);
AST::NumericLiteral *numericLiteral = AST::cast<AST::NumericLiteral *>(expressionNode);
if (numericLiteral)
return numericLiteral->value;
addError(tr("Expected expression statement to be a literal"), uiScriptBinding->statement->firstSourceLocation());
addError(tr("Expected expression statement to be a literal"), expressionNode->firstSourceLocation());
return QVariant();
}

View File

@@ -102,7 +102,8 @@ private:
void readChild(AST::UiObjectDefinition *uiObjectDefinition);
void readProperties(AST::UiObjectDefinition *ast);
void readProperty(AST::UiScriptBinding *uiScriptBinding);
QVariant parseProperty(AST::UiScriptBinding *ast);
QVariant parsePropertyScriptBinding(AST::UiScriptBinding *ExpressionNode);
QVariant parsePropertyExpression(AST::ExpressionNode *expressionNode);
void setSourceLocation(const AST::SourceLocation &sourceLocation);
QStringList m_errors;

View File

@@ -44,6 +44,7 @@ private slots:
void testWellFormed();
void testIllFormed01();
void testIllFormed02();
void testArrays();
};
tst_SimpleReader::tst_SimpleReader()
@@ -175,6 +176,44 @@ void tst_SimpleReader::testIllFormed02()
QCOMPARE(firstChild->property("property01").toString(), QLatin1String("20"));
}
void tst_SimpleReader::testArrays()
{
char source[] = "RootNode {\n"
" propertyArray: [\"string01\", \"string02\" ]\n"
" ChildNode {\n"
" propertyArray: [\"string01\", \"string02\" ]\n"
" propertyArrayMixed: [\"string03\", [\"string01\", \"string02\"] ]\n"
" }\n"
"}\n";
QList<QVariant> variantList;
variantList << QVariant(QLatin1String("string01")) << QVariant(QLatin1String("string02"));
const QVariant variant = variantList;
QmlJS::SimpleReader reader;
QmlJS::SimpleReaderNode::Ptr rootNode = reader.readFromSource(source);
QVERIFY(rootNode);
QVERIFY(rootNode->isValid());
QVERIFY(rootNode->isRoot());
QCOMPARE(rootNode->property("propertyArray"), variant);
QmlJS::SimpleReaderNode::Ptr firstChild = rootNode->children().at(0);
QVERIFY(firstChild);
QVERIFY(firstChild->isValid());
QVERIFY(!firstChild->isRoot());
QCOMPARE(firstChild->property("propertyArray"), variant);
QList<QVariant> variantList2;
variantList2 << QVariant(QLatin1String("string03")) << variant;
const QVariant variant2 = variantList2;
QCOMPARE(firstChild->property("propertyArrayMixed"), variant2);
}
QTEST_MAIN(tst_SimpleReader);
#include "tst_qmljssimplereader.moc"