Added --verify-only option to qmlmin.
[profile/ivi/qtdeclarative.git] / tools / qmlmin / main.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
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>
50 #include <iostream>
51 #include <cstdlib>
52
53 //
54 // QML/JS minifier
55 //
56 namespace QDeclarativeJS {
57
58 enum RegExpFlag {
59     Global     = 0x01,
60     IgnoreCase = 0x02,
61     Multiline  = 0x04
62 };
63
64 class QmlminLexer: protected Lexer
65 {
66     QDeclarativeJS::Engine _engine;
67     QString _fileName;
68
69 public:
70     QmlminLexer(): Lexer(&_engine) {}
71     virtual ~QmlminLexer() {}
72
73     QString fileName() const { return _fileName; }
74
75     bool operator()(const QString &fileName, const QString &code)
76     {
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);
82         _fileName = fileName;
83         return parse(startToken);
84     }
85
86 protected:
87     virtual bool parse(int startToken) = 0;
88
89     bool automatic(int token) const
90     {
91         return token == T_RBRACE || token == 0 || prevTerminator();
92     }
93
94     bool isIdentChar(const QChar &ch) const
95     {
96         if (ch.isLetterOrNumber())
97             return true;
98         else if (ch == QLatin1Char('_') || ch == QLatin1Char('$'))
99             return true;
100         return false;
101     }
102
103     bool isRegExpRule(int ruleno) const
104     {
105         return ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ||
106                 ruleno == J_SCRIPT_REGEXPLITERAL_RULE2;
107     }
108
109     bool scanRestOfRegExp(int ruleno, QString *restOfRegExp)
110     {
111         if (! scanRegExp(ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ? Lexer::NoPrefix : Lexer::EqualPrefix))
112             return false;
113
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
119         }
120         *restOfRegExp += QLatin1Char('/');
121         const RegExpFlag flags = (RegExpFlag) regExpFlags();
122         if (flags & Global)
123             *restOfRegExp += QLatin1Char('g');
124         if (flags & IgnoreCase)
125             *restOfRegExp += QLatin1Char('i');
126         if (flags & Multiline)
127             *restOfRegExp += QLatin1Char('m');
128         qDebug() << *restOfRegExp;
129         return true;
130     }
131 };
132
133
134 class Minify: public QmlminLexer
135 {
136     QVector<int> _stateStack;
137     QList<int> _tokens;
138     QList<QString> _tokenStrings;
139     QString _minifiedCode;
140
141 public:
142     Minify();
143
144     QString minifiedCode() const;
145
146 protected:
147     bool parse(int startToken);
148 };
149
150 Minify::Minify()
151     : _stateStack(128)
152 {
153 }
154
155 QString Minify::minifiedCode() const
156 {
157     return _minifiedCode;
158 }
159
160 bool Minify::parse(int startToken)
161 {
162     int yyaction = 0;
163     int yytoken = -1;
164     int yytos = -1;
165     QString yytokentext;
166
167     _minifiedCode.clear();
168     _tokens.append(startToken);
169     _tokenStrings.append(QString());
170
171     do {
172         if (++yytos == _stateStack.size())
173             _stateStack.resize(_stateStack.size() * 2);
174
175         _stateStack[yytos] = yyaction;
176
177     again:
178         if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
179             if (_tokens.isEmpty()) {
180                 _tokens.append(lex());
181                 _tokenStrings.append(tokenText());
182             }
183
184             yytoken = _tokens.takeFirst();
185             yytokentext = _tokenStrings.takeFirst();
186         }
187
188         yyaction = t_action(yyaction, yytoken);
189         if (yyaction > 0) {
190             if (yyaction == ACCEPT_STATE) {
191                 --yytos;
192                 return true;
193             }
194
195             const QChar lastChar = _minifiedCode.isEmpty() ? QChar() : _minifiedCode.at(_minifiedCode.length() - 1);
196
197             if (yytoken == T_SEMICOLON) {
198                 _minifiedCode += QLatin1Char(';');
199
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(' ');
204                 }
205
206                 _minifiedCode += QLatin1String(spell[yytoken]);
207
208             } else if (yytoken == T_NUMERIC_LITERAL) {
209                 if (isIdentChar(lastChar))
210                     _minifiedCode += QLatin1Char(' ');
211
212                 if (yytokentext.startsWith('.'))
213                     _minifiedCode += QLatin1Char('0');
214
215                 _minifiedCode += yytokentext;
216
217                 if (_minifiedCode.endsWith(QLatin1Char('.')))
218                     _minifiedCode += QLatin1Char('0');
219
220             } else if (yytoken == T_IDENTIFIER) {
221                 if (isIdentChar(lastChar))
222                     _minifiedCode += QLatin1Char(' ');
223
224                 foreach (const QChar &ch, yytokentext) {
225                     if (isIdentChar(ch))
226                         _minifiedCode += ch;
227                     else {
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;
234                         case 4: break;
235                         default:
236                             std::cerr << "qmlmin: invalid unicode sequence" << std::endl;
237                             return false;
238                         }
239                         _minifiedCode += hx;
240                     }
241                 }
242
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("\\\"");
248                     else {
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;
258                     }
259                 }
260
261                 _minifiedCode += QLatin1Char('"');
262             } else {
263                 if (isIdentChar(lastChar)) {
264                     if (! yytokentext.isEmpty()) {
265                         const QChar ch = yytokentext.at(0);
266                         if (isIdentChar(ch))
267                             _minifiedCode += QLatin1Char(' ');
268                     }
269                 }
270                 _minifiedCode += yytokentext;
271             }
272             yytoken = -1;
273         } else if (yyaction < 0) {
274             const int ruleno = -yyaction - 1;
275             yytos -= rhs[ruleno];
276
277             if (isRegExpRule(ruleno)) {
278                 QString restOfRegExp;
279
280                 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
281                     break; // break the loop, it wil report a syntax error
282
283                 _minifiedCode += restOfRegExp;
284             }
285             yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
286         }
287     } while (yyaction);
288
289     const int yyerrorstate = _stateStack[yytos];
290
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;
297         goto again;
298     }
299
300     std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
301     return false;
302 }
303
304
305 class Tokenize: public QmlminLexer
306 {
307     QVector<int> _stateStack;
308     QList<int> _tokens;
309     QList<QString> _tokenStrings;
310     QStringList _minifiedCode;
311
312 public:
313     Tokenize();
314
315     QStringList tokenStream() const;
316
317 protected:
318     virtual bool parse(int startToken);
319 };
320
321 Tokenize::Tokenize()
322     : _stateStack(128)
323 {
324 }
325
326 QStringList Tokenize::tokenStream() const
327 {
328     return _minifiedCode;
329 }
330
331 bool Tokenize::parse(int startToken)
332 {
333     int yyaction = 0;
334     int yytoken = -1;
335     int yytos = -1;
336     QString yytokentext;
337
338     _minifiedCode.clear();
339     _tokens.append(startToken);
340     _tokenStrings.append(QString());
341
342     do {
343         if (++yytos == _stateStack.size())
344             _stateStack.resize(_stateStack.size() * 2);
345
346         _stateStack[yytos] = yyaction;
347
348     again:
349         if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
350             if (_tokens.isEmpty()) {
351                 _tokens.append(lex());
352                 _tokenStrings.append(tokenText());
353             }
354
355             yytoken = _tokens.takeFirst();
356             yytokentext = _tokenStrings.takeFirst();
357         }
358
359         yyaction = t_action(yyaction, yytoken);
360         if (yyaction > 0) {
361             if (yyaction == ACCEPT_STATE) {
362                 --yytos;
363                 return true;
364             }
365
366             if (yytoken == T_SEMICOLON)
367                 _minifiedCode += QLatin1String(";");
368             else
369                 _minifiedCode += yytokentext;
370
371             yytoken = -1;
372         } else if (yyaction < 0) {
373             const int ruleno = -yyaction - 1;
374             yytos -= rhs[ruleno];
375
376             if (isRegExpRule(ruleno)) {
377                 QString restOfRegExp;
378
379                 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
380                     break; // break the loop, it wil report a syntax error
381
382                 _minifiedCode.last().append(restOfRegExp);
383             }
384
385             yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
386         }
387     } while (yyaction);
388
389     const int yyerrorstate = _stateStack[yytos];
390
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;
397         goto again;
398     }
399
400     std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
401     return false;
402 }
403
404 } // end of QDeclarativeJS namespace
405
406 static void usage(bool showHelp = false)
407 {
408     std::cerr << "Usage: qmlmin [options] file" << std::endl;
409
410     if (showHelp) {
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;
416     }
417 }
418
419 int main(int argc, char *argv[])
420 {
421     QCoreApplication app(argc, argv);
422
423     const QStringList args = app.arguments();
424
425     QString fileName;
426     QString outputFile;
427     bool verifyOnly = false;
428
429     int index = 1;
430     while (index < args.size()) {
431         const QString arg = args.at(index++);
432         const QString next = index < args.size() ? args.at(index) : QString();
433
434         if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
435             usage(/*showHelp*/ true);
436             return 0;
437         } else if (arg == QLatin1String("-v") || arg == QLatin1String("--verify-only")) {
438             verifyOnly = true;
439         } else if (arg == QLatin1String("-o")) {
440             if (next.isEmpty()) {
441                 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
442                 return EXIT_FAILURE;
443             } else {
444                 outputFile = next;
445                 ++index; // consume the next argument
446             }
447         } else if (arg.startsWith(QLatin1String("-o"))) {
448             outputFile = arg.mid(2);
449
450             if (outputFile.isEmpty()) {
451                 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
452                 return EXIT_FAILURE;
453             }
454         } else {
455             const bool isInvalidOpt = arg.startsWith(QLatin1Char('-'));
456             if (! isInvalidOpt && fileName.isEmpty())
457                 fileName = arg;
458             else {
459                 usage(/*show help*/ isInvalidOpt);
460                 if (isInvalidOpt)
461                     std::cerr << "qmlmin: invalid option '" << qPrintable(arg) << "'" << std::endl;
462                 else
463                     std::cerr << "qmlmin: too many input files specified" << std::endl;
464                 return EXIT_FAILURE;
465             }
466         }
467     }
468
469     if (fileName.isEmpty()) {
470         usage();
471         return 0;
472     }
473
474     QFile file(fileName);
475     if (! file.open(QFile::ReadOnly)) {
476         std::cerr << "qmlmin: '" << qPrintable(fileName) << "' no such file or directory" << std::endl;
477         return EXIT_FAILURE;
478     }
479
480     const QString code = QString::fromUtf8(file.readAll()); // QML files are UTF-8 encoded.
481     file.close();
482
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;
486         return EXIT_FAILURE;
487     }
488
489     //
490     // verify the output
491     //
492     QDeclarativeJS::Minify secondMinify;
493     if (! secondMinify(fileName, minify.minifiedCode()) || secondMinify.minifiedCode() != minify.minifiedCode()) {
494         std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
495         return EXIT_FAILURE;
496     }
497
498     QDeclarativeJS::Tokenize originalTokens, minimizedTokens;
499     originalTokens(fileName, code);
500     minimizedTokens(fileName, minify.minifiedCode());
501
502     if (originalTokens.tokenStream().size() != minimizedTokens.tokenStream().size()) {
503         std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
504         return EXIT_FAILURE;
505     }
506
507     if (! verifyOnly) {
508         if (outputFile.isEmpty()) {
509             const QByteArray chars = minify.minifiedCode().toUtf8();
510             std::cout << chars.constData();
511         } else {
512             QFile file(outputFile);
513             if (! file.open(QFile::WriteOnly)) {
514                 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (permission denied)" << std::endl;
515                 return EXIT_FAILURE;
516             }
517
518             file.write(minify.minifiedCode().toUtf8());
519             file.close();
520         }
521     }
522
523     return 0;
524 }