1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the Qt Linguist of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
44 #include <translator.h>
46 #include <QtCore/QDebug>
47 #include <QtCore/QFile>
48 #include <QtCore/QString>
50 #include "parser/qdeclarativejsengine_p.h"
51 #include "parser/qdeclarativejsparser_p.h"
52 #include "parser/qdeclarativejslexer_p.h"
53 #include "parser/qdeclarativejsnodepool_p.h"
54 #include "parser/qdeclarativejsastvisitor_p.h"
55 #include "parser/qdeclarativejsast_p.h"
57 #include <QCoreApplication>
61 #include <QStringList>
69 Q_DECLARE_TR_FUNCTIONS(LUpdate)
72 using namespace QDeclarativeJS;
77 Comment() : lastLine(-1) {}
80 TranslatorMessage::ExtraData extra;
85 { return !extracomment.isEmpty() || !msgid.isEmpty() || !sourcetext.isEmpty() || !extra.isEmpty(); }
88 class FindTrCalls: protected AST::Visitor
91 void operator()(Translator *translator, const QString &fileName, AST::Node *node)
93 m_translator = translator;
94 m_fileName = fileName;
95 m_component = QFileInfo(fileName).baseName(); //matches qsTr usage in QScriptEngine
99 QList<Comment> comments;
102 using AST::Visitor::visit;
103 using AST::Visitor::endVisit;
105 void accept(AST::Node *node)
106 { AST::Node::acceptChild(node, this); }
108 virtual void endVisit(AST::CallExpression *node)
111 if (AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression *>(node->base)) {
112 if (idExpr->name->asString() == QLatin1String("qsTr") ||
113 idExpr->name->asString() == QLatin1String("QT_TR_NOOP")) {
114 if (!node->arguments)
116 AST::BinaryExpression *binary = AST::cast<AST::BinaryExpression *>(node->arguments->expression);
118 if (!createString(binary))
121 AST::StringLiteral *literal = AST::cast<AST::StringLiteral *>(node->arguments->expression);
122 if (literal || !m_bSource.isEmpty()) {
123 const QString source = literal ? literal->value->asString() : m_bSource;
127 AST::ArgumentList *commentNode = node->arguments->next;
128 if (commentNode && AST::cast<AST::StringLiteral *>(commentNode->expression)) {
129 literal = AST::cast<AST::StringLiteral *>(commentNode->expression);
130 comment = literal->value->asString();
132 AST::ArgumentList *nNode = commentNode->next;
138 QString extracomment;
139 TranslatorMessage::ExtraData extra;
140 Comment scomment = findComment(node->firstSourceLocation().startLine);
141 if (scomment.isValid()) {
142 extracomment = scomment.extracomment;
143 extra = scomment.extra;
147 TranslatorMessage msg(m_component, source,
148 comment, QString(), m_fileName,
149 node->firstSourceLocation().startLine, QStringList(),
150 TranslatorMessage::Unfinished, plural);
151 msg.setExtraComment(extracomment.simplified());
153 msg.setExtras(extra);
154 m_translator->extend(msg);
156 } else if (idExpr->name->asString() == QLatin1String("qsTranslate") ||
157 idExpr->name->asString() == QLatin1String("QT_TRANSLATE_NOOP")) {
158 if (node->arguments && AST::cast<AST::StringLiteral *>(node->arguments->expression)) {
159 AST::StringLiteral *literal = AST::cast<AST::StringLiteral *>(node->arguments->expression);
160 const QString context = literal->value->asString();
165 AST::ArgumentList *sourceNode = node->arguments->next;
168 literal = AST::cast<AST::StringLiteral *>(sourceNode->expression);
169 AST::BinaryExpression *binary = AST::cast<AST::BinaryExpression *>(sourceNode->expression);
171 if (!createString(binary))
174 if (!literal && m_bSource.isEmpty())
178 QString extracomment;
179 TranslatorMessage::ExtraData extra;
180 Comment scomment = findComment(node->firstSourceLocation().startLine);
181 if (scomment.isValid()) {
182 extracomment = scomment.extracomment;
183 extra = scomment.extra;
187 source = literal ? literal->value->asString() : m_bSource;
188 AST::ArgumentList *commentNode = sourceNode->next;
189 if (commentNode && AST::cast<AST::StringLiteral *>(commentNode->expression)) {
190 literal = AST::cast<AST::StringLiteral *>(commentNode->expression);
191 comment = literal->value->asString();
193 AST::ArgumentList *nNode = commentNode->next;
198 TranslatorMessage msg(context, source,
199 comment, QString(), m_fileName,
200 node->firstSourceLocation().startLine, QStringList(),
201 TranslatorMessage::Unfinished, plural);
202 msg.setExtraComment(extracomment.simplified());
204 msg.setExtras(extra);
205 m_translator->extend(msg);
207 } else if (idExpr->name->asString() == QLatin1String("qsTrId") ||
208 idExpr->name->asString() == QLatin1String("QT_TRID_NOOP")) {
209 if (!node->arguments)
212 AST::StringLiteral *literal = AST::cast<AST::StringLiteral *>(node->arguments->expression);
215 QString extracomment;
217 TranslatorMessage::ExtraData extra;
218 Comment comment = findComment(node->firstSourceLocation().startLine);
219 if (comment.isValid()) {
220 extracomment = comment.extracomment;
221 sourcetext = comment.sourcetext;
222 extra = comment.extra;
225 const QString id = literal->value->asString();
226 bool plural = node->arguments->next;
228 TranslatorMessage msg(QString(), sourcetext,
229 QString(), QString(), m_fileName,
230 node->firstSourceLocation().startLine, QStringList(),
231 TranslatorMessage::Unfinished, plural);
232 msg.setExtraComment(extracomment.simplified());
234 msg.setExtras(extra);
235 m_translator->extend(msg);
242 bool createString(AST::BinaryExpression *b)
244 if (!b || b->op != 0)
246 AST::BinaryExpression *l = AST::cast<AST::BinaryExpression *>(b->left);
247 AST::BinaryExpression *r = AST::cast<AST::BinaryExpression *>(b->right);
248 AST::StringLiteral *ls = AST::cast<AST::StringLiteral *>(b->left);
249 AST::StringLiteral *rs = AST::cast<AST::StringLiteral *>(b->right);
250 if ((!l && !ls) || (!r && !rs))
253 if (!createString(l))
256 m_bSource.prepend(ls->value->asString());
259 if (!createString(r))
262 m_bSource.append(rs->value->asString());
267 Comment findComment(int loc)
269 if (comments.isEmpty())
273 int commentLoc = comments.at(i).lastLine;
274 while (commentLoc <= loc) {
275 if (commentLoc == loc)
276 return comments.at(i);
277 if (i == comments.count()-1)
279 commentLoc = comments.at(++i).lastLine;
284 Translator *m_translator;
290 QString createErrorString(const QString &filename, const QString &code, Parser &parser)
293 QStringList lines = code.split(QLatin1Char('\n'));
294 lines.append(QLatin1String("\n")); // sentinel.
297 foreach (const DiagnosticMessage &m, parser.diagnosticMessages()) {
302 QString error = filename + QLatin1Char(':') + QString::number(m.loc.startLine)
303 + QLatin1Char(':') + QString::number(m.loc.startColumn) + QLatin1String(": error: ")
304 + m.message + QLatin1Char('\n');
307 if (m.loc.startLine > 0)
308 line = m.loc.startLine - 1;
310 const QString textLine = lines.at(line);
312 error += textLine + QLatin1Char('\n');
314 int column = m.loc.startColumn - 1;
318 column = qMin(column, textLine.length());
320 for (int i = 0; i < column; ++i) {
321 const QChar ch = textLine.at(i);
323 error += ch.unicode();
325 error += QLatin1Char(' ');
327 error += QLatin1String("^\n");
328 errorString += error;
333 bool processComment(const QChar *chars, int length, Comment &comment)
335 // Try to match the logic of the QtScript parser.
337 return comment.isValid();
338 if (*chars == QLatin1Char(':') && chars[1].isSpace()) {
339 comment.extracomment += QString(chars+1, length-1);
340 } else if (*chars == QLatin1Char('=') && chars[1].isSpace()) {
341 comment.msgid = QString(chars+2, length-2).simplified();
342 } else if (*chars == QLatin1Char('~') && chars[1].isSpace()) {
343 QString text = QString(chars+2, length-2).trimmed();
344 int k = text.indexOf(QLatin1Char(' '));
346 comment.extra.insert(text.left(k), text.mid(k + 1).trimmed());
347 } else if (*chars == QLatin1Char('%') && chars[1].isSpace()) {
348 comment.sourcetext.reserve(comment.sourcetext.length() + length-2);
349 ushort *ptr = (ushort *)comment.sourcetext.data() + comment.sourcetext.length();
354 c = chars[p++].unicode();
362 c = chars[p++].unicode();
368 c = chars[p++].unicode();
376 comment.sourcetext.resize(ptr - (ushort *)comment.sourcetext.data());
378 return comment.isValid();
381 bool loadQml(Translator &translator, const QString &filename, ConversionData &cd)
383 cd.m_sourceFileName = filename;
384 QFile file(filename);
385 if (!file.open(QIODevice::ReadOnly)) {
386 cd.appendError(LU::tr("Cannot open %1: %2").arg(filename, file.errorString()));
390 const QString code = QTextStream(&file).readAll();
393 Parser parser(&driver);
395 NodePool nodePool(filename, &driver);
396 driver.setNodePool(&nodePool);
398 Lexer lexer(&driver);
399 lexer.setCode(code, /*line = */ 1);
400 driver.setLexer(&lexer);
402 if (parser.parse()) {
405 // build up a list of comments that contain translation information.
406 for (int i = 0; i < driver.comments().size(); ++i) {
407 AST::SourceLocation loc = driver.comments().at(i);
408 QString commentStr = code.mid(loc.offset, loc.length);
410 if (trCalls.comments.isEmpty() || trCalls.comments.last().lastLine != int(loc.startLine)) {
412 comment.lastLine = loc.startLine+1;
413 if (processComment(commentStr.constData(), commentStr.length(), comment))
414 trCalls.comments.append(comment);
416 Comment &lastComment = trCalls.comments.last();
417 lastComment.lastLine += 1;
418 processComment(commentStr.constData(), commentStr.length(), lastComment);
422 //find all tr calls in the code
423 trCalls(&translator, filename, parser.ast());
425 QString error = createErrorString(filename, code, parser);
426 cd.appendError(error);