From dad5d1cc82a57a4f657aa3f37ad2f55b69d5b015 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 9 Feb 2018 13:33:35 +0100 Subject: Add support for ES6 template strings This requires a bit of bookeeping in the lexer, as we can have arbitrary expressions inside the ${...}. To make this work, keep a stack of template states, in which we count the unclosed braces to match up with the correct closing brace. Implements support for `...`. Expressions of the type Foo`...` and Foo()`...` will come in follow-up commits. Change-Id: Ia332796cfb77895583d0093732e6f56c8b0662c9 Reviewed-by: Simon Hausmann --- tests/auto/qml/qqmlparser/tst_qqmlparser.cpp | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'tests/auto/qml/qqmlparser') diff --git a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp index ba2b836a6d..2722a536b6 100644 --- a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp +++ b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp @@ -50,6 +50,9 @@ private slots: void qmlParser(); #endif void invalidEscapeSequence(); + void stringLiteral(); + void noSubstitutionTemplateLiteral(); + void templateLiteral(); private: QStringList excludedDirs; @@ -204,6 +207,67 @@ void tst_qqmlparser::invalidEscapeSequence() parser.parse(); } +void tst_qqmlparser::stringLiteral() +{ + using namespace QQmlJS; + + Engine engine; + Lexer lexer(&engine); + QLatin1String code("'hello string'"); + lexer.setCode(code , 1); + Parser parser(&engine); + QVERIFY(parser.parseExpression()); + AST::ExpressionNode *expression = parser.expression(); + QVERIFY(expression); + auto *literal = QQmlJS::AST::cast(expression); + QVERIFY(literal); + QCOMPARE(literal->value, "hello string"); + QCOMPARE(literal->firstSourceLocation().begin(), 0); + QCOMPARE(literal->lastSourceLocation().end(), code.size()); +} + +void tst_qqmlparser::noSubstitutionTemplateLiteral() +{ + using namespace QQmlJS; + + Engine engine; + Lexer lexer(&engine); + QLatin1String code("`hello template`"); + lexer.setCode(code, 1); + Parser parser(&engine); + QVERIFY(parser.parseExpression()); + AST::ExpressionNode *expression = parser.expression(); + QVERIFY(expression); + + auto *literal = QQmlJS::AST::cast(expression); + QVERIFY(literal); + + QCOMPARE(literal->value, "hello template"); + QCOMPARE(literal->firstSourceLocation().begin(), 0); + QCOMPARE(literal->lastSourceLocation().end(), code.size()); +} + +void tst_qqmlparser::templateLiteral() +{ + using namespace QQmlJS; + + Engine engine; + Lexer lexer(&engine); + QLatin1String code("`one plus one equals ${1+1}!`"); + lexer.setCode(code, 1); + Parser parser(&engine); + QVERIFY(parser.parseExpression()); + AST::ExpressionNode *expression = parser.expression(); + QVERIFY(expression); + + auto *templateLiteral = QQmlJS::AST::cast(expression); + QVERIFY(templateLiteral); + + QCOMPARE(templateLiteral->firstSourceLocation().begin(), 0); + auto *e = templateLiteral->expression; + QVERIFY(e); +} + QTEST_MAIN(tst_qqmlparser) #include "tst_qqmlparser.moc" -- cgit v1.2.3