Add support for minimizing Javascript files containing pragmas.
[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
65 class QmlminLexer: protected Lexer, public Directives
66 {
67     QDeclarativeJS::Engine _engine;
68     QString _fileName;
69     QString _directives;
70
71 public:
72     QmlminLexer(): Lexer(&_engine) {}
73     virtual ~QmlminLexer() {}
74
75     QString fileName() const { return _fileName; }
76
77     bool operator()(const QString &fileName, const QString &code)
78     {
79         int startToken = T_FEED_JS_PROGRAM;
80         const QFileInfo fileInfo(fileName);
81         if (fileInfo.suffix().toLower() == QLatin1String("qml"))
82             startToken = T_FEED_UI_PROGRAM;
83         setCode(code, /*line = */ 1, /*qmlMode = */ startToken == T_FEED_UI_PROGRAM);
84         _fileName = fileName;
85         _directives.clear();
86         return parse(startToken);
87     }
88
89     QString directives()
90     {
91         return _directives;
92     }
93
94     //
95     // Handle the .pragma/.import directives
96     //
97     virtual void pragmaLibrary()
98     {
99         _directives += QLatin1String(".pragma library\n");
100     }
101
102     virtual void importFile(const QString &jsfile, const QString &module)
103     {
104         _directives += QLatin1String(".import");
105         _directives += QLatin1Char('"');
106         _directives += quote(jsfile);
107         _directives += QLatin1Char('"');
108         _directives += QLatin1String("as ");
109         _directives += module;
110         _directives += QLatin1Char('\n');
111     }
112
113     virtual void importModule(const QString &uri, const QString &version, const QString &module)
114     {
115         _directives += QLatin1String(".import ");
116         _directives += uri;
117         _directives += QLatin1Char(' ');
118         _directives += version;
119         _directives += QLatin1String(" as ");
120         _directives += module;
121         _directives += QLatin1Char('\n');
122     }
123
124 protected:
125     bool automatic(int token) const
126     {
127         return token == T_RBRACE || token == 0 || prevTerminator();
128     }
129
130     virtual bool parse(int startToken) = 0;
131
132     static QString quote(const QString &string)
133     {
134         QString quotedString;
135         foreach (const QChar &ch, string) {
136             if (ch == QLatin1Char('"'))
137                 quotedString += QLatin1String("\\\"");
138             else {
139                 if (ch == QLatin1Char('\\')) quotedString += QLatin1String("\\\\");
140                 else if (ch == QLatin1Char('\"')) quotedString += QLatin1String("\\\"");
141                 else if (ch == QLatin1Char('\b')) quotedString += QLatin1String("\\b");
142                 else if (ch == QLatin1Char('\f')) quotedString += QLatin1String("\\f");
143                 else if (ch == QLatin1Char('\n')) quotedString += QLatin1String("\\n");
144                 else if (ch == QLatin1Char('\r')) quotedString += QLatin1String("\\r");
145                 else if (ch == QLatin1Char('\t')) quotedString += QLatin1String("\\t");
146                 else if (ch == QLatin1Char('\v')) quotedString += QLatin1String("\\v");
147                 else quotedString += ch;
148             }
149         }
150         return quotedString;
151     }
152
153     bool isIdentChar(const QChar &ch) const
154     {
155         if (ch.isLetterOrNumber())
156             return true;
157         else if (ch == QLatin1Char('_') || ch == QLatin1Char('$'))
158             return true;
159         return false;
160     }
161
162     bool isRegExpRule(int ruleno) const
163     {
164         return ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ||
165                 ruleno == J_SCRIPT_REGEXPLITERAL_RULE2;
166     }
167
168     bool scanRestOfRegExp(int ruleno, QString *restOfRegExp)
169     {
170         if (! scanRegExp(ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ? Lexer::NoPrefix : Lexer::EqualPrefix))
171             return false;
172
173         *restOfRegExp = regExpPattern();
174         if (ruleno == J_SCRIPT_REGEXPLITERAL_RULE2) {
175             Q_ASSERT(! restOfRegExp->isEmpty());
176             Q_ASSERT(restOfRegExp->at(0) == QLatin1Char('='));
177             *restOfRegExp = restOfRegExp->mid(1); // strip the prefix
178         }
179         *restOfRegExp += QLatin1Char('/');
180         const RegExpFlag flags = (RegExpFlag) regExpFlags();
181         if (flags & Global)
182             *restOfRegExp += QLatin1Char('g');
183         if (flags & IgnoreCase)
184             *restOfRegExp += QLatin1Char('i');
185         if (flags & Multiline)
186             *restOfRegExp += QLatin1Char('m');
187         return true;
188     }
189 };
190
191
192 class Minify: public QmlminLexer
193 {
194     QVector<int> _stateStack;
195     QList<int> _tokens;
196     QList<QString> _tokenStrings;
197     QString _minifiedCode;
198
199 public:
200     Minify();
201
202     QString minifiedCode() const;
203
204 protected:
205     bool parse(int startToken);
206 };
207
208 Minify::Minify()
209     : _stateStack(128)
210 {
211 }
212
213 QString Minify::minifiedCode() const
214 {
215     return _minifiedCode;
216 }
217
218 bool Minify::parse(int startToken)
219 {
220     int yyaction = 0;
221     int yytoken = -1;
222     int yytos = -1;
223     QString yytokentext;
224
225     _minifiedCode.clear();
226     _tokens.append(startToken);
227     _tokenStrings.append(QString());
228
229     if (startToken == T_FEED_JS_PROGRAM) {
230         // parse optional pragma directive
231         if (scanDirectives(this)) {
232             // append the scanned directives to the minifier code.
233             _minifiedCode += directives();
234
235             _tokens.append(tokenKind());
236             _tokenStrings.append(tokenText());
237         } else {
238             std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
239             return false;
240         }
241     }
242
243     do {
244         if (++yytos == _stateStack.size())
245             _stateStack.resize(_stateStack.size() * 2);
246
247         _stateStack[yytos] = yyaction;
248
249     again:
250         if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
251             if (_tokens.isEmpty()) {
252                 _tokens.append(lex());
253                 _tokenStrings.append(tokenText());
254             }
255
256             yytoken = _tokens.takeFirst();
257             yytokentext = _tokenStrings.takeFirst();
258         }
259
260         yyaction = t_action(yyaction, yytoken);
261         if (yyaction > 0) {
262             if (yyaction == ACCEPT_STATE) {
263                 --yytos;
264                 return true;
265             }
266
267             const QChar lastChar = _minifiedCode.isEmpty() ? QChar() : _minifiedCode.at(_minifiedCode.length() - 1);
268
269             if (yytoken == T_SEMICOLON) {
270                 _minifiedCode += QLatin1Char(';');
271
272             } else if (yytoken == T_PLUS || yytoken == T_MINUS || yytoken == T_PLUS_PLUS || yytoken == T_MINUS_MINUS) {
273                 if (lastChar == QLatin1Char(spell[yytoken][0])) {
274                     // don't merge unary signs, additive expressions and postfix/prefix increments.
275                     _minifiedCode += QLatin1Char(' ');
276                 }
277
278                 _minifiedCode += QLatin1String(spell[yytoken]);
279
280             } else if (yytoken == T_NUMERIC_LITERAL) {
281                 if (isIdentChar(lastChar))
282                     _minifiedCode += QLatin1Char(' ');
283
284                 if (yytokentext.startsWith('.'))
285                     _minifiedCode += QLatin1Char('0');
286
287                 _minifiedCode += yytokentext;
288
289                 if (_minifiedCode.endsWith(QLatin1Char('.')))
290                     _minifiedCode += QLatin1Char('0');
291
292             } else if (yytoken == T_IDENTIFIER) {
293                 if (isIdentChar(lastChar))
294                     _minifiedCode += QLatin1Char(' ');
295
296                 foreach (const QChar &ch, yytokentext) {
297                     if (isIdentChar(ch))
298                         _minifiedCode += ch;
299                     else {
300                         _minifiedCode += QLatin1String("\\u");
301                         const QString hx = QString::number(ch.unicode(), 16);
302                         switch (hx.length()) {
303                         case 1: _minifiedCode += QLatin1String("000"); break;
304                         case 2: _minifiedCode += QLatin1String("00"); break;
305                         case 3: _minifiedCode += QLatin1String("0"); break;
306                         case 4: break;
307                         default:
308                             std::cerr << "qmlmin: invalid unicode sequence" << std::endl;
309                             return false;
310                         }
311                         _minifiedCode += hx;
312                     }
313                 }
314
315             } else if (yytoken == T_STRING_LITERAL || yytoken == T_MULTILINE_STRING_LITERAL) {
316                 _minifiedCode += QLatin1Char('"');
317                 _minifiedCode += quote(yytokentext);
318                 _minifiedCode += QLatin1Char('"');
319             } else {
320                 if (isIdentChar(lastChar)) {
321                     if (! yytokentext.isEmpty()) {
322                         const QChar ch = yytokentext.at(0);
323                         if (isIdentChar(ch))
324                             _minifiedCode += QLatin1Char(' ');
325                     }
326                 }
327                 _minifiedCode += yytokentext;
328             }
329             yytoken = -1;
330         } else if (yyaction < 0) {
331             const int ruleno = -yyaction - 1;
332             yytos -= rhs[ruleno];
333
334             if (isRegExpRule(ruleno)) {
335                 QString restOfRegExp;
336
337                 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
338                     break; // break the loop, it wil report a syntax error
339
340                 _minifiedCode += restOfRegExp;
341             }
342             yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
343         }
344     } while (yyaction);
345
346     const int yyerrorstate = _stateStack[yytos];
347
348     // automatic insertion of `;'
349     if (yytoken != -1 && t_action(yyerrorstate, T_AUTOMATIC_SEMICOLON) && automatic(yytoken)) {
350         _tokens.prepend(yytoken);
351         _tokenStrings.prepend(yytokentext);
352         yyaction = yyerrorstate;
353         yytoken = T_SEMICOLON;
354         goto again;
355     }
356
357     std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
358     return false;
359 }
360
361
362 class Tokenize: public QmlminLexer
363 {
364     QVector<int> _stateStack;
365     QList<int> _tokens;
366     QList<QString> _tokenStrings;
367     QStringList _minifiedCode;
368
369 public:
370     Tokenize();
371
372     QStringList tokenStream() const;
373
374 protected:
375     virtual bool parse(int startToken);
376 };
377
378 Tokenize::Tokenize()
379     : _stateStack(128)
380 {
381 }
382
383 QStringList Tokenize::tokenStream() const
384 {
385     return _minifiedCode;
386 }
387
388 bool Tokenize::parse(int startToken)
389 {
390     int yyaction = 0;
391     int yytoken = -1;
392     int yytos = -1;
393     QString yytokentext;
394
395     _minifiedCode.clear();
396     _tokens.append(startToken);
397     _tokenStrings.append(QString());
398
399     if (startToken == T_FEED_JS_PROGRAM) {
400         // parse optional pragma directive
401         if (scanDirectives(this)) {
402             // append the scanned directives as one token to
403             // the token stream.
404             _minifiedCode.append(directives());
405
406             _tokens.append(tokenKind());
407             _tokenStrings.append(tokenText());
408         } else {
409             std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
410             return false;
411         }
412     }
413
414     do {
415         if (++yytos == _stateStack.size())
416             _stateStack.resize(_stateStack.size() * 2);
417
418         _stateStack[yytos] = yyaction;
419
420     again:
421         if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
422             if (_tokens.isEmpty()) {
423                 _tokens.append(lex());
424                 _tokenStrings.append(tokenText());
425             }
426
427             yytoken = _tokens.takeFirst();
428             yytokentext = _tokenStrings.takeFirst();
429         }
430
431         yyaction = t_action(yyaction, yytoken);
432         if (yyaction > 0) {
433             if (yyaction == ACCEPT_STATE) {
434                 --yytos;
435                 return true;
436             }
437
438             if (yytoken == T_SEMICOLON)
439                 _minifiedCode += QLatin1String(";");
440             else
441                 _minifiedCode += yytokentext;
442
443             yytoken = -1;
444         } else if (yyaction < 0) {
445             const int ruleno = -yyaction - 1;
446             yytos -= rhs[ruleno];
447
448             if (isRegExpRule(ruleno)) {
449                 QString restOfRegExp;
450
451                 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
452                     break; // break the loop, it wil report a syntax error
453
454                 _minifiedCode.last().append(restOfRegExp);
455             }
456
457             yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
458         }
459     } while (yyaction);
460
461     const int yyerrorstate = _stateStack[yytos];
462
463     // automatic insertion of `;'
464     if (yytoken != -1 && t_action(yyerrorstate, T_AUTOMATIC_SEMICOLON) && automatic(yytoken)) {
465         _tokens.prepend(yytoken);
466         _tokenStrings.prepend(yytokentext);
467         yyaction = yyerrorstate;
468         yytoken = T_SEMICOLON;
469         goto again;
470     }
471
472     std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
473     return false;
474 }
475
476 } // end of QDeclarativeJS namespace
477
478 static void usage(bool showHelp = false)
479 {
480     std::cerr << "Usage: qmlmin [options] file" << std::endl;
481
482     if (showHelp) {
483         std::cerr << " Removes comments and layout characters" << std::endl
484                   << " The options are:" << std::endl
485                   << "  -o<file>                write output to file rather than stdout" << std::endl
486                   << "  -v --verify-only        just run the verifier, no output" << std::endl
487                   << "  -h                      display this output" << std::endl;
488     }
489 }
490
491 int main(int argc, char *argv[])
492 {
493     QCoreApplication app(argc, argv);
494
495     const QStringList args = app.arguments();
496
497     QString fileName;
498     QString outputFile;
499     bool verifyOnly = false;
500
501     int index = 1;
502     while (index < args.size()) {
503         const QString arg = args.at(index++);
504         const QString next = index < args.size() ? args.at(index) : QString();
505
506         if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
507             usage(/*showHelp*/ true);
508             return 0;
509         } else if (arg == QLatin1String("-v") || arg == QLatin1String("--verify-only")) {
510             verifyOnly = true;
511         } else if (arg == QLatin1String("-o")) {
512             if (next.isEmpty()) {
513                 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
514                 return EXIT_FAILURE;
515             } else {
516                 outputFile = next;
517                 ++index; // consume the next argument
518             }
519         } else if (arg.startsWith(QLatin1String("-o"))) {
520             outputFile = arg.mid(2);
521
522             if (outputFile.isEmpty()) {
523                 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
524                 return EXIT_FAILURE;
525             }
526         } else {
527             const bool isInvalidOpt = arg.startsWith(QLatin1Char('-'));
528             if (! isInvalidOpt && fileName.isEmpty())
529                 fileName = arg;
530             else {
531                 usage(/*show help*/ isInvalidOpt);
532                 if (isInvalidOpt)
533                     std::cerr << "qmlmin: invalid option '" << qPrintable(arg) << "'" << std::endl;
534                 else
535                     std::cerr << "qmlmin: too many input files specified" << std::endl;
536                 return EXIT_FAILURE;
537             }
538         }
539     }
540
541     if (fileName.isEmpty()) {
542         usage();
543         return 0;
544     }
545
546     QFile file(fileName);
547     if (! file.open(QFile::ReadOnly)) {
548         std::cerr << "qmlmin: '" << qPrintable(fileName) << "' no such file or directory" << std::endl;
549         return EXIT_FAILURE;
550     }
551
552     const QString code = QString::fromUtf8(file.readAll()); // QML files are UTF-8 encoded.
553     file.close();
554
555     QDeclarativeJS::Minify minify;
556     if (! minify(fileName, code)) {
557         std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (not a valid QML/JS file)" << std::endl;
558         return EXIT_FAILURE;
559     }
560
561     //
562     // verify the output
563     //
564     QDeclarativeJS::Minify secondMinify;
565     if (! secondMinify(fileName, minify.minifiedCode()) || secondMinify.minifiedCode() != minify.minifiedCode()) {
566         std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
567         return EXIT_FAILURE;
568     }
569
570     QDeclarativeJS::Tokenize originalTokens, minimizedTokens;
571     originalTokens(fileName, code);
572     minimizedTokens(fileName, minify.minifiedCode());
573
574     if (originalTokens.tokenStream().size() != minimizedTokens.tokenStream().size()) {
575         std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
576         return EXIT_FAILURE;
577     }
578
579     if (! verifyOnly) {
580         if (outputFile.isEmpty()) {
581             const QByteArray chars = minify.minifiedCode().toUtf8();
582             std::cout << chars.constData();
583         } else {
584             QFile file(outputFile);
585             if (! file.open(QFile::WriteOnly)) {
586                 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (permission denied)" << std::endl;
587                 return EXIT_FAILURE;
588             }
589
590             file.write(minify.minifiedCode().toUtf8());
591             file.close();
592         }
593     }
594
595     return 0;
596 }