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 QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <private/qdeclarativejsengine_p.h>
43 #include <private/qdeclarativejslexer_p.h>
44 #include <private/qdeclarativejsparser_p.h>
45 #include <QtCore/QCoreApplication>
46 #include <QtCore/QStringList>
47 #include <QtCore/QFile>
48 #include <QtCore/QFileInfo>
49 #include <QtCore/QDir>
58 namespace QDeclarativeJS {
67 class QmlminLexer: protected Lexer, public Directives
69 QDeclarativeJS::Engine _engine;
74 QmlminLexer(): Lexer(&_engine) {}
75 virtual ~QmlminLexer() {}
77 QString fileName() const { return _fileName; }
79 bool operator()(const QString &fileName, const QString &code)
81 int startToken = T_FEED_JS_PROGRAM;
82 const QFileInfo fileInfo(fileName);
83 if (fileInfo.suffix().toLower() == QLatin1String("qml"))
84 startToken = T_FEED_UI_PROGRAM;
85 setCode(code, /*line = */ 1, /*qmlMode = */ startToken == T_FEED_UI_PROGRAM);
88 return parse(startToken);
97 // Handle the .pragma/.import directives
99 virtual void pragmaLibrary()
101 _directives += QLatin1String(".pragma library\n");
104 virtual void importFile(const QString &jsfile, const QString &module)
106 _directives += QLatin1String(".import");
107 _directives += QLatin1Char('"');
108 _directives += quote(jsfile);
109 _directives += QLatin1Char('"');
110 _directives += QLatin1String("as ");
111 _directives += module;
112 _directives += QLatin1Char('\n');
115 virtual void importModule(const QString &uri, const QString &version, const QString &module)
117 _directives += QLatin1String(".import ");
119 _directives += QLatin1Char(' ');
120 _directives += version;
121 _directives += QLatin1String(" as ");
122 _directives += module;
123 _directives += QLatin1Char('\n');
127 virtual bool parse(int startToken) = 0;
129 static QString quote(const QString &string)
131 QString quotedString;
132 foreach (const QChar &ch, string) {
133 if (ch == QLatin1Char('"'))
134 quotedString += QLatin1String("\\\"");
136 if (ch == QLatin1Char('\\')) quotedString += QLatin1String("\\\\");
137 else if (ch == QLatin1Char('\"')) quotedString += QLatin1String("\\\"");
138 else if (ch == QLatin1Char('\b')) quotedString += QLatin1String("\\b");
139 else if (ch == QLatin1Char('\f')) quotedString += QLatin1String("\\f");
140 else if (ch == QLatin1Char('\n')) quotedString += QLatin1String("\\n");
141 else if (ch == QLatin1Char('\r')) quotedString += QLatin1String("\\r");
142 else if (ch == QLatin1Char('\t')) quotedString += QLatin1String("\\t");
143 else if (ch == QLatin1Char('\v')) quotedString += QLatin1String("\\v");
144 else if (ch == QLatin1Char('\0')) quotedString += QLatin1String("\\0");
145 else quotedString += ch;
151 bool isIdentChar(const QChar &ch) const
153 if (ch.isLetterOrNumber())
155 else if (ch == QLatin1Char('_') || ch == QLatin1Char('$'))
160 bool isRegExpRule(int ruleno) const
162 return ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ||
163 ruleno == J_SCRIPT_REGEXPLITERAL_RULE2;
166 bool scanRestOfRegExp(int ruleno, QString *restOfRegExp)
168 if (! scanRegExp(ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ? Lexer::NoPrefix : Lexer::EqualPrefix))
171 *restOfRegExp = regExpPattern();
172 if (ruleno == J_SCRIPT_REGEXPLITERAL_RULE2) {
173 Q_ASSERT(! restOfRegExp->isEmpty());
174 Q_ASSERT(restOfRegExp->at(0) == QLatin1Char('='));
175 *restOfRegExp = restOfRegExp->mid(1); // strip the prefix
177 *restOfRegExp += QLatin1Char('/');
178 const RegExpFlag flags = (RegExpFlag) regExpFlags();
180 *restOfRegExp += QLatin1Char('g');
181 if (flags & IgnoreCase)
182 *restOfRegExp += QLatin1Char('i');
183 if (flags & Multiline)
184 *restOfRegExp += QLatin1Char('m');
190 class Minify: public QmlminLexer
192 QVector<int> _stateStack;
194 QList<QString> _tokenStrings;
195 QString _minifiedCode;
200 QString minifiedCode() const;
203 bool parse(int startToken);
211 QString Minify::minifiedCode() const
213 return _minifiedCode;
216 bool Minify::parse(int startToken)
223 _minifiedCode.clear();
224 _tokens.append(startToken);
225 _tokenStrings.append(QString());
227 if (startToken == T_FEED_JS_PROGRAM) {
228 // parse optional pragma directive
229 if (scanDirectives(this)) {
230 // append the scanned directives to the minifier code.
231 _minifiedCode += directives();
233 _tokens.append(tokenKind());
234 _tokenStrings.append(tokenText());
236 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
242 if (++yytos == _stateStack.size())
243 _stateStack.resize(_stateStack.size() * 2);
245 _stateStack[yytos] = yyaction;
248 if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
249 if (_tokens.isEmpty()) {
250 _tokens.append(lex());
251 _tokenStrings.append(tokenText());
254 yytoken = _tokens.takeFirst();
255 yytokentext = _tokenStrings.takeFirst();
258 yyaction = t_action(yyaction, yytoken);
260 if (yyaction == ACCEPT_STATE) {
265 const QChar lastChar = _minifiedCode.isEmpty() ? QChar() : _minifiedCode.at(_minifiedCode.length() - 1);
267 if (yytoken == T_SEMICOLON) {
268 _minifiedCode += QLatin1Char(';');
270 } else if (yytoken == T_PLUS || yytoken == T_MINUS || yytoken == T_PLUS_PLUS || yytoken == T_MINUS_MINUS) {
271 if (lastChar == QLatin1Char(spell[yytoken][0])) {
272 // don't merge unary signs, additive expressions and postfix/prefix increments.
273 _minifiedCode += QLatin1Char(' ');
276 _minifiedCode += QLatin1String(spell[yytoken]);
278 } else if (yytoken == T_NUMERIC_LITERAL) {
279 if (isIdentChar(lastChar))
280 _minifiedCode += QLatin1Char(' ');
282 if (yytokentext.startsWith('.'))
283 _minifiedCode += QLatin1Char('0');
285 _minifiedCode += yytokentext;
287 if (_minifiedCode.endsWith(QLatin1Char('.')))
288 _minifiedCode += QLatin1Char('0');
290 } else if (yytoken == T_IDENTIFIER) {
291 if (isIdentChar(lastChar))
292 _minifiedCode += QLatin1Char(' ');
294 foreach (const QChar &ch, yytokentext) {
298 _minifiedCode += QLatin1String("\\u");
299 const QString hx = QString::number(ch.unicode(), 16);
300 switch (hx.length()) {
301 case 1: _minifiedCode += QLatin1String("000"); break;
302 case 2: _minifiedCode += QLatin1String("00"); break;
303 case 3: _minifiedCode += QLatin1String("0"); break;
306 std::cerr << "qmlmin: invalid unicode sequence" << std::endl;
313 } else if (yytoken == T_STRING_LITERAL || yytoken == T_MULTILINE_STRING_LITERAL) {
314 _minifiedCode += QLatin1Char('"');
315 _minifiedCode += quote(yytokentext);
316 _minifiedCode += QLatin1Char('"');
318 if (isIdentChar(lastChar)) {
319 if (! yytokentext.isEmpty()) {
320 const QChar ch = yytokentext.at(0);
322 _minifiedCode += QLatin1Char(' ');
325 _minifiedCode += yytokentext;
328 } else if (yyaction < 0) {
329 const int ruleno = -yyaction - 1;
330 yytos -= rhs[ruleno];
332 if (isRegExpRule(ruleno)) {
333 QString restOfRegExp;
335 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
336 break; // break the loop, it wil report a syntax error
338 _minifiedCode += restOfRegExp;
340 yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
344 const int yyerrorstate = _stateStack[yytos];
346 // automatic insertion of `;'
347 if (yytoken != -1 && t_action(yyerrorstate, T_AUTOMATIC_SEMICOLON) && canInsertAutomaticSemicolon(yytoken)) {
348 _tokens.prepend(yytoken);
349 _tokenStrings.prepend(yytokentext);
350 yyaction = yyerrorstate;
351 yytoken = T_SEMICOLON;
355 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
360 class Tokenize: public QmlminLexer
362 QVector<int> _stateStack;
364 QList<QString> _tokenStrings;
365 QStringList _minifiedCode;
370 QStringList tokenStream() const;
373 virtual bool parse(int startToken);
381 QStringList Tokenize::tokenStream() const
383 return _minifiedCode;
386 bool Tokenize::parse(int startToken)
393 _minifiedCode.clear();
394 _tokens.append(startToken);
395 _tokenStrings.append(QString());
397 if (startToken == T_FEED_JS_PROGRAM) {
398 // parse optional pragma directive
399 if (scanDirectives(this)) {
400 // append the scanned directives as one token to
402 _minifiedCode.append(directives());
404 _tokens.append(tokenKind());
405 _tokenStrings.append(tokenText());
407 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
413 if (++yytos == _stateStack.size())
414 _stateStack.resize(_stateStack.size() * 2);
416 _stateStack[yytos] = yyaction;
419 if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
420 if (_tokens.isEmpty()) {
421 _tokens.append(lex());
422 _tokenStrings.append(tokenText());
425 yytoken = _tokens.takeFirst();
426 yytokentext = _tokenStrings.takeFirst();
429 yyaction = t_action(yyaction, yytoken);
431 if (yyaction == ACCEPT_STATE) {
436 if (yytoken == T_SEMICOLON)
437 _minifiedCode += QLatin1String(";");
439 _minifiedCode += yytokentext;
442 } else if (yyaction < 0) {
443 const int ruleno = -yyaction - 1;
444 yytos -= rhs[ruleno];
446 if (isRegExpRule(ruleno)) {
447 QString restOfRegExp;
449 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
450 break; // break the loop, it wil report a syntax error
452 _minifiedCode.last().append(restOfRegExp);
455 yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
459 const int yyerrorstate = _stateStack[yytos];
461 // automatic insertion of `;'
462 if (yytoken != -1 && t_action(yyerrorstate, T_AUTOMATIC_SEMICOLON) && canInsertAutomaticSemicolon(yytoken)) {
463 _tokens.prepend(yytoken);
464 _tokenStrings.prepend(yytokentext);
465 yyaction = yyerrorstate;
466 yytoken = T_SEMICOLON;
470 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
474 } // end of QDeclarativeJS namespace
476 static void usage(bool showHelp = false)
478 std::cerr << "Usage: qmlmin [options] file" << std::endl;
481 std::cerr << " Removes comments and layout characters" << std::endl
482 << " The options are:" << std::endl
483 << " -o<file> write output to file rather than stdout" << std::endl
484 << " -v --verify-only just run the verifier, no output" << std::endl
485 << " -h display this output" << std::endl;
489 int runQmlmin(int argc, char *argv[])
491 QCoreApplication app(argc, argv);
493 const QStringList args = app.arguments();
497 bool verifyOnly = false;
500 while (index < args.size()) {
501 const QString arg = args.at(index++);
502 const QString next = index < args.size() ? args.at(index) : QString();
504 if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
505 usage(/*showHelp*/ true);
507 } else if (arg == QLatin1String("-v") || arg == QLatin1String("--verify-only")) {
509 } else if (arg == QLatin1String("-o")) {
510 if (next.isEmpty()) {
511 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
515 ++index; // consume the next argument
517 } else if (arg.startsWith(QLatin1String("-o"))) {
518 outputFile = arg.mid(2);
520 if (outputFile.isEmpty()) {
521 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
525 const bool isInvalidOpt = arg.startsWith(QLatin1Char('-'));
526 if (! isInvalidOpt && fileName.isEmpty())
529 usage(/*show help*/ isInvalidOpt);
531 std::cerr << "qmlmin: invalid option '" << qPrintable(arg) << "'" << std::endl;
533 std::cerr << "qmlmin: too many input files specified" << std::endl;
539 if (fileName.isEmpty()) {
544 QFile file(fileName);
545 if (! file.open(QFile::ReadOnly)) {
546 std::cerr << "qmlmin: '" << qPrintable(fileName) << "' no such file or directory" << std::endl;
550 const QString code = QString::fromUtf8(file.readAll()); // QML files are UTF-8 encoded.
553 QDeclarativeJS::Minify minify;
554 if (! minify(fileName, code)) {
555 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (not a valid QML/JS file)" << std::endl;
562 QDeclarativeJS::Minify secondMinify;
563 if (! secondMinify(fileName, minify.minifiedCode()) || secondMinify.minifiedCode() != minify.minifiedCode()) {
564 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
568 QDeclarativeJS::Tokenize originalTokens, minimizedTokens;
569 originalTokens(fileName, code);
570 minimizedTokens(fileName, minify.minifiedCode());
572 if (originalTokens.tokenStream().size() != minimizedTokens.tokenStream().size()) {
573 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
578 if (outputFile.isEmpty()) {
579 const QByteArray chars = minify.minifiedCode().toUtf8();
580 std::cout << chars.constData();
582 QFile file(outputFile);
583 if (! file.open(QFile::WriteOnly)) {
584 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (permission denied)" << std::endl;
588 file.write(minify.minifiedCode().toUtf8());
598 int main(int argc, char **argv)
600 return QT_PREPEND_NAMESPACE(runQmlmin(argc, argv));