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 "qdeclarativejsengine_p.h"
43 #include "qdeclarativejslexer_p.h"
44 #include "qdeclarativejsparser_p.h"
45 #include <QtCore/QCoreApplication>
46 #include <QtCore/QStringList>
47 #include <QtCore/QFile>
48 #include <QtCore/QFileInfo>
49 #include <QtCore/QDir>
56 namespace QDeclarativeJS {
64 class QmlminLexer: protected Lexer
66 QDeclarativeJS::Engine _engine;
70 QmlminLexer(): Lexer(&_engine) {}
71 virtual ~QmlminLexer() {}
73 QString fileName() const { return _fileName; }
75 bool operator()(const QString &fileName, const QString &code)
77 int startToken = T_FEED_JS_PROGRAM;
78 const QFileInfo fileInfo(fileName);
79 if (fileInfo.suffix().toLower() == QLatin1String("qml"))
80 startToken = T_FEED_UI_PROGRAM;
81 setCode(code, /*line = */ 1, /*qmlMode = */ startToken == T_FEED_UI_PROGRAM);
83 return parse(startToken);
87 virtual bool parse(int startToken) = 0;
89 bool automatic(int token) const
91 return token == T_RBRACE || token == 0 || prevTerminator();
94 bool isIdentChar(const QChar &ch) const
96 if (ch.isLetterOrNumber())
98 else if (ch == QLatin1Char('_') || ch == QLatin1Char('$'))
103 bool isRegExpRule(int ruleno) const
105 return ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ||
106 ruleno == J_SCRIPT_REGEXPLITERAL_RULE2;
109 bool scanRestOfRegExp(int ruleno, QString *restOfRegExp)
111 if (! scanRegExp(ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ? Lexer::NoPrefix : Lexer::EqualPrefix))
114 *restOfRegExp = regExpPattern();
115 if (ruleno == J_SCRIPT_REGEXPLITERAL_RULE2) {
116 Q_ASSERT(! restOfRegExp->isEmpty());
117 Q_ASSERT(restOfRegExp->at(0) == QLatin1Char('='));
118 *restOfRegExp = restOfRegExp->mid(1); // strip the prefix
120 *restOfRegExp += QLatin1Char('/');
121 const RegExpFlag flags = (RegExpFlag) regExpFlags();
123 *restOfRegExp += QLatin1Char('g');
124 if (flags & IgnoreCase)
125 *restOfRegExp += QLatin1Char('i');
126 if (flags & Multiline)
127 *restOfRegExp += QLatin1Char('m');
128 qDebug() << *restOfRegExp;
134 class Minify: public QmlminLexer
136 QVector<int> _stateStack;
138 QList<QString> _tokenStrings;
139 QString _minifiedCode;
144 QString minifiedCode() const;
147 bool parse(int startToken);
155 QString Minify::minifiedCode() const
157 return _minifiedCode;
160 bool Minify::parse(int startToken)
167 _minifiedCode.clear();
168 _tokens.append(startToken);
169 _tokenStrings.append(QString());
172 if (++yytos == _stateStack.size())
173 _stateStack.resize(_stateStack.size() * 2);
175 _stateStack[yytos] = yyaction;
178 if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
179 if (_tokens.isEmpty()) {
180 _tokens.append(lex());
181 _tokenStrings.append(tokenText());
184 yytoken = _tokens.takeFirst();
185 yytokentext = _tokenStrings.takeFirst();
188 yyaction = t_action(yyaction, yytoken);
190 if (yyaction == ACCEPT_STATE) {
195 const QChar lastChar = _minifiedCode.isEmpty() ? QChar() : _minifiedCode.at(_minifiedCode.length() - 1);
197 if (yytoken == T_SEMICOLON) {
198 _minifiedCode += QLatin1Char(';');
200 } else if (yytoken == T_PLUS || yytoken == T_MINUS || yytoken == T_PLUS_PLUS || yytoken == T_MINUS_MINUS) {
201 if (lastChar == QLatin1Char(spell[yytoken][0])) {
202 // don't merge unary signs, additive expressions and postfix/prefix increments.
203 _minifiedCode += QLatin1Char(' ');
206 _minifiedCode += QLatin1String(spell[yytoken]);
208 } else if (yytoken == T_NUMERIC_LITERAL) {
209 if (isIdentChar(lastChar))
210 _minifiedCode += QLatin1Char(' ');
212 if (yytokentext.startsWith('.'))
213 _minifiedCode += QLatin1Char('0');
215 _minifiedCode += yytokentext;
217 if (_minifiedCode.endsWith(QLatin1Char('.')))
218 _minifiedCode += QLatin1Char('0');
220 } else if (yytoken == T_IDENTIFIER) {
221 if (isIdentChar(lastChar))
222 _minifiedCode += QLatin1Char(' ');
224 foreach (const QChar &ch, yytokentext) {
228 _minifiedCode += QLatin1String("\\u");
229 const QString hx = QString::number(ch.unicode(), 16);
230 switch (hx.length()) {
231 case 1: _minifiedCode += QLatin1String("000"); break;
232 case 2: _minifiedCode += QLatin1String("00"); break;
233 case 3: _minifiedCode += QLatin1String("0"); break;
236 std::cerr << "qmlmin: invalid unicode sequence" << std::endl;
243 } else if (yytoken == T_STRING_LITERAL || yytoken == T_MULTILINE_STRING_LITERAL) {
244 _minifiedCode += QLatin1Char('"');
245 foreach (const QChar &ch, yytokentext) {
246 if (ch == QLatin1Char('"'))
247 _minifiedCode += QLatin1String("\\\"");
249 if (ch == QLatin1Char('\\')) _minifiedCode += QLatin1String("\\\\");
250 else if (ch == QLatin1Char('\"')) _minifiedCode += QLatin1String("\\\"");
251 else if (ch == QLatin1Char('\b')) _minifiedCode += QLatin1String("\\b");
252 else if (ch == QLatin1Char('\f')) _minifiedCode += QLatin1String("\\f");
253 else if (ch == QLatin1Char('\n')) _minifiedCode += QLatin1String("\\n");
254 else if (ch == QLatin1Char('\r')) _minifiedCode += QLatin1String("\\r");
255 else if (ch == QLatin1Char('\t')) _minifiedCode += QLatin1String("\\t");
256 else if (ch == QLatin1Char('\v')) _minifiedCode += QLatin1String("\\v");
257 else _minifiedCode += ch;
261 _minifiedCode += QLatin1Char('"');
263 if (isIdentChar(lastChar)) {
264 if (! yytokentext.isEmpty()) {
265 const QChar ch = yytokentext.at(0);
267 _minifiedCode += QLatin1Char(' ');
270 _minifiedCode += yytokentext;
273 } else if (yyaction < 0) {
274 const int ruleno = -yyaction - 1;
275 yytos -= rhs[ruleno];
277 if (isRegExpRule(ruleno)) {
278 QString restOfRegExp;
280 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
281 break; // break the loop, it wil report a syntax error
283 _minifiedCode += restOfRegExp;
285 yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
289 const int yyerrorstate = _stateStack[yytos];
291 // automatic insertion of `;'
292 if (yytoken != -1 && t_action(yyerrorstate, T_AUTOMATIC_SEMICOLON) && automatic(yytoken)) {
293 _tokens.prepend(yytoken);
294 _tokenStrings.prepend(yytokentext);
295 yyaction = yyerrorstate;
296 yytoken = T_SEMICOLON;
300 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
305 class Tokenize: public QmlminLexer
307 QVector<int> _stateStack;
309 QList<QString> _tokenStrings;
310 QStringList _minifiedCode;
315 QStringList tokenStream() const;
318 virtual bool parse(int startToken);
326 QStringList Tokenize::tokenStream() const
328 return _minifiedCode;
331 bool Tokenize::parse(int startToken)
338 _minifiedCode.clear();
339 _tokens.append(startToken);
340 _tokenStrings.append(QString());
343 if (++yytos == _stateStack.size())
344 _stateStack.resize(_stateStack.size() * 2);
346 _stateStack[yytos] = yyaction;
349 if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
350 if (_tokens.isEmpty()) {
351 _tokens.append(lex());
352 _tokenStrings.append(tokenText());
355 yytoken = _tokens.takeFirst();
356 yytokentext = _tokenStrings.takeFirst();
359 yyaction = t_action(yyaction, yytoken);
361 if (yyaction == ACCEPT_STATE) {
366 if (yytoken == T_SEMICOLON)
367 _minifiedCode += QLatin1String(";");
369 _minifiedCode += yytokentext;
372 } else if (yyaction < 0) {
373 const int ruleno = -yyaction - 1;
374 yytos -= rhs[ruleno];
376 if (isRegExpRule(ruleno)) {
377 QString restOfRegExp;
379 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
380 break; // break the loop, it wil report a syntax error
382 _minifiedCode.last().append(restOfRegExp);
385 yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
389 const int yyerrorstate = _stateStack[yytos];
391 // automatic insertion of `;'
392 if (yytoken != -1 && t_action(yyerrorstate, T_AUTOMATIC_SEMICOLON) && automatic(yytoken)) {
393 _tokens.prepend(yytoken);
394 _tokenStrings.prepend(yytokentext);
395 yyaction = yyerrorstate;
396 yytoken = T_SEMICOLON;
400 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
404 } // end of QDeclarativeJS namespace
406 static void usage(bool showHelp = false)
408 std::cerr << "Usage: qmlmin [options] file" << std::endl;
411 std::cerr << " Removes comments and layout characters" << std::endl
412 << " The options are:" << std::endl
413 << " -o<file> write output to file rather than stdout" << std::endl
414 << " -v --verify-only just run the verifier, no output" << std::endl
415 << " -h display this output" << std::endl;
419 int main(int argc, char *argv[])
421 QCoreApplication app(argc, argv);
423 const QStringList args = app.arguments();
427 bool verifyOnly = false;
430 while (index < args.size()) {
431 const QString arg = args.at(index++);
432 const QString next = index < args.size() ? args.at(index) : QString();
434 if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
435 usage(/*showHelp*/ true);
437 } else if (arg == QLatin1String("-v") || arg == QLatin1String("--verify-only")) {
439 } else if (arg == QLatin1String("-o")) {
440 if (next.isEmpty()) {
441 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
445 ++index; // consume the next argument
447 } else if (arg.startsWith(QLatin1String("-o"))) {
448 outputFile = arg.mid(2);
450 if (outputFile.isEmpty()) {
451 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
455 const bool isInvalidOpt = arg.startsWith(QLatin1Char('-'));
456 if (! isInvalidOpt && fileName.isEmpty())
459 usage(/*show help*/ isInvalidOpt);
461 std::cerr << "qmlmin: invalid option '" << qPrintable(arg) << "'" << std::endl;
463 std::cerr << "qmlmin: too many input files specified" << std::endl;
469 if (fileName.isEmpty()) {
474 QFile file(fileName);
475 if (! file.open(QFile::ReadOnly)) {
476 std::cerr << "qmlmin: '" << qPrintable(fileName) << "' no such file or directory" << std::endl;
480 const QString code = QString::fromUtf8(file.readAll()); // QML files are UTF-8 encoded.
483 QDeclarativeJS::Minify minify;
484 if (! minify(fileName, code)) {
485 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (not a valid QML/JS file)" << std::endl;
492 QDeclarativeJS::Minify secondMinify;
493 if (! secondMinify(fileName, minify.minifiedCode()) || secondMinify.minifiedCode() != minify.minifiedCode()) {
494 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
498 QDeclarativeJS::Tokenize originalTokens, minimizedTokens;
499 originalTokens(fileName, code);
500 minimizedTokens(fileName, minify.minifiedCode());
502 if (originalTokens.tokenStream().size() != minimizedTokens.tokenStream().size()) {
503 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
508 if (outputFile.isEmpty()) {
509 const QByteArray chars = minify.minifiedCode().toUtf8();
510 std::cout << chars.constData();
512 QFile file(outputFile);
513 if (! file.open(QFile::WriteOnly)) {
514 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (permission denied)" << std::endl;
518 file.write(minify.minifiedCode().toUtf8());