diff --git a/src/qml/parser/qqmljslexer.cpp b/src/qml/parser/qqmljslexer.cpp index 4f694447bb..73e6893786 100644 --- a/src/qml/parser/qqmljslexer.cpp +++ b/src/qml/parser/qqmljslexer.cpp @@ -925,6 +925,7 @@ int Lexer::scanString(ScanStringMode mode) // rewind by one char, so things gets scanned correctly --_codePtr; + --_currentColumnNumber; _validTokenText = true; _tokenText = QString(startCode, _codePtr - startCode); diff --git a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp index e4bcfa7796..10e9e49aa5 100644 --- a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp +++ b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp @@ -288,7 +288,7 @@ void tst_qqmlparser::stringLiteral() Engine engine; Lexer lexer(&engine); - QLatin1String code("'hello string'"); + QString code("'hello string'"); lexer.setCode(code , 1); Parser parser(&engine); QVERIFY(parser.parseExpression()); @@ -299,6 +299,35 @@ void tst_qqmlparser::stringLiteral() QCOMPARE(literal->value, "hello string"); QCOMPARE(literal->firstSourceLocation().begin(), 0u); QCOMPARE(literal->lastSourceLocation().end(), quint32(code.size())); + + // test for correct handling escape sequences inside strings + QLatin1String leftCode("'hello\\n\\tstring'"); + QLatin1String plusCode(" + "); + QLatin1String rightCode("'\\nbye'"); + code = leftCode + plusCode + rightCode; + lexer.setCode(code , 1); + QVERIFY(parser.parseExpression()); + + expression = parser.expression(); + QVERIFY(expression); + auto *binaryExpression = QQmlJS::AST::cast(expression); + QVERIFY(binaryExpression); + + literal = QQmlJS::AST::cast(binaryExpression->left); + QVERIFY(literal); + QCOMPARE(literal->value, "hello\n\tstring"); + QCOMPARE(literal->firstSourceLocation().begin(), 0u); + QCOMPARE(literal->firstSourceLocation().startLine, 1u); + QCOMPARE(literal->lastSourceLocation().end(), quint32(leftCode.size())); + + QVERIFY(binaryExpression->right); + literal = QQmlJS::AST::cast(binaryExpression->right); + QVERIFY(literal); + QCOMPARE(literal->value, "\nbye"); + quint32 offset = quint32(leftCode.size() + plusCode.size()); + QCOMPARE(literal->firstSourceLocation().begin(), offset); + QCOMPARE(literal->firstSourceLocation().startLine, 1u); + QCOMPARE(literal->lastSourceLocation().end(), quint32(code.size())); } void tst_qqmlparser::noSubstitutionTemplateLiteral()