Doc: Enabling Qt QML linking to Qt Quick.
[profile/ivi/qtdeclarative.git] / tools / qmlmin / main.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <private/qqmljsengine_p.h>
43 #include <private/qqmljslexer_p.h>
44 #include <private/qqmljsparser_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 QT_BEGIN_NAMESPACE
54
55 //
56 // QML/JS minifier
57 //
58 namespace QQmlJS {
59
60 enum RegExpFlag {
61     Global     = 0x01,
62     IgnoreCase = 0x02,
63     Multiline  = 0x04
64 };
65
66
67 class QmlminLexer: protected Lexer, public Directives
68 {
69     QQmlJS::Engine _engine;
70     QString _fileName;
71     QString _directives;
72
73 public:
74     QmlminLexer(): Lexer(&_engine) {}
75     virtual ~QmlminLexer() {}
76
77     QString fileName() const { return _fileName; }
78
79     bool operator()(const QString &fileName, const QString &code)
80     {
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);
86         _fileName = fileName;
87         _directives.clear();
88         return parse(startToken);
89     }
90
91     QString directives()
92     {
93         return _directives;
94     }
95
96     //
97     // Handle the .pragma/.import directives
98     //
99     virtual void pragmaLibrary()
100     {
101         _directives += QLatin1String(".pragma library\n");
102     }
103
104     virtual void importFile(const QString &jsfile, const QString &module)
105     {
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');
113     }
114
115     virtual void importModule(const QString &uri, const QString &version, const QString &module)
116     {
117         _directives += QLatin1String(".import ");
118         _directives += uri;
119         _directives += QLatin1Char(' ');
120         _directives += version;
121         _directives += QLatin1String(" as ");
122         _directives += module;
123         _directives += QLatin1Char('\n');
124     }
125
126 protected:
127     virtual bool parse(int startToken) = 0;
128
129     static QString quote(const QString &string)
130     {
131         QString quotedString;
132         foreach (const QChar &ch, string) {
133             if (ch == QLatin1Char('"'))
134                 quotedString += QLatin1String("\\\"");
135             else {
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;
146             }
147         }
148         return quotedString;
149     }
150
151     bool isIdentChar(const QChar &ch) const
152     {
153         if (ch.isLetterOrNumber())
154             return true;
155         else if (ch == QLatin1Char('_') || ch == QLatin1Char('$'))
156             return true;
157         return false;
158     }
159
160     bool isRegExpRule(int ruleno) const
161     {
162         return ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ||
163                 ruleno == J_SCRIPT_REGEXPLITERAL_RULE2;
164     }
165
166     bool scanRestOfRegExp(int ruleno, QString *restOfRegExp)
167     {
168         if (! scanRegExp(ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ? Lexer::NoPrefix : Lexer::EqualPrefix))
169             return false;
170
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
176         }
177         *restOfRegExp += QLatin1Char('/');
178         const RegExpFlag flags = (RegExpFlag) regExpFlags();
179         if (flags & Global)
180             *restOfRegExp += QLatin1Char('g');
181         if (flags & IgnoreCase)
182             *restOfRegExp += QLatin1Char('i');
183         if (flags & Multiline)
184             *restOfRegExp += QLatin1Char('m');
185
186         if (regExpFlags() == 0) {
187             // Add an extra space after the regexp literal delimiter (aka '/').
188             // This will avoid possible problems when pasting tokens like `instanceof'
189             // after the regexp literal.
190             *restOfRegExp += QLatin1Char(' ');
191         }
192         return true;
193     }
194 };
195
196
197 class Minify: public QmlminLexer
198 {
199     QVector<int> _stateStack;
200     QList<int> _tokens;
201     QList<QString> _tokenStrings;
202     QString _minifiedCode;
203     int _maxWidth;
204     int _width;
205
206 public:
207     Minify(int maxWidth);
208
209     QString minifiedCode() const;
210
211 protected:
212     void append(const QString &s);
213     bool parse(int startToken);
214     void escape(const QChar &ch, QString *out);
215 };
216
217 Minify::Minify(int maxWidth)
218     : _stateStack(128), _maxWidth(maxWidth), _width(0)
219 {
220 }
221
222 QString Minify::minifiedCode() const
223 {
224     return _minifiedCode;
225 }
226
227 void Minify::append(const QString &s)
228 {
229     if (!s.isEmpty()) {
230         if (_maxWidth) {
231             // Prefer not to exceed the maximum chars per line (but don't break up segments)
232             int segmentLength = s.count();
233             if (_width && ((_width + segmentLength) > _maxWidth)) {
234                 _minifiedCode.append(QLatin1Char('\n'));
235                 _width = 0;
236             }
237
238             _width += segmentLength;
239         }
240
241         _minifiedCode.append(s);
242     }
243 }
244
245 void Minify::escape(const QChar &ch, QString *out)
246 {
247     out->append(QLatin1String("\\u"));
248     const QString hx = QString::number(ch.unicode(), 16);
249     switch (hx.length()) {
250     case 1: out->append(QLatin1String("000")); break;
251     case 2: out->append(QLatin1String("00")); break;
252     case 3: out->append(QLatin1String("0")); break;
253     case 4: break;
254     default: Q_ASSERT(!"unreachable");
255     }
256     out->append(hx);
257 }
258
259 bool Minify::parse(int startToken)
260 {
261     int yyaction = 0;
262     int yytoken = -1;
263     int yytos = -1;
264     QString yytokentext;
265     QString assembled;
266
267     _minifiedCode.clear();
268     _tokens.append(startToken);
269     _tokenStrings.append(QString());
270
271     if (startToken == T_FEED_JS_PROGRAM) {
272         // parse optional pragma directive
273         if (scanDirectives(this)) {
274             // append the scanned directives to the minifier code.
275             append(directives());
276
277             _tokens.append(tokenKind());
278             _tokenStrings.append(tokenText());
279         } else {
280             std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
281             return false;
282         }
283     }
284
285     do {
286         if (++yytos == _stateStack.size())
287             _stateStack.resize(_stateStack.size() * 2);
288
289         _stateStack[yytos] = yyaction;
290
291     again:
292         if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
293             if (_tokens.isEmpty()) {
294                 _tokens.append(lex());
295                 _tokenStrings.append(tokenText());
296             }
297
298             yytoken = _tokens.takeFirst();
299             yytokentext = _tokenStrings.takeFirst();
300         }
301
302         yyaction = t_action(yyaction, yytoken);
303         if (yyaction > 0) {
304             if (yyaction == ACCEPT_STATE) {
305                 --yytos;
306                 if (!assembled.isEmpty())
307                     append(assembled);
308                 return true;
309             }
310
311             const QChar lastChar = assembled.isEmpty() ? (_minifiedCode.isEmpty() ? QChar()
312                                                                                   : _minifiedCode.at(_minifiedCode.length() - 1))
313                                                        : assembled.at(assembled.length() - 1);
314
315             if (yytoken == T_SEMICOLON) {
316                 assembled += QLatin1Char(';');
317
318                 append(assembled);
319                 assembled.clear();
320
321             } else if (yytoken == T_PLUS || yytoken == T_MINUS || yytoken == T_PLUS_PLUS || yytoken == T_MINUS_MINUS) {
322                 if (lastChar == QLatin1Char(spell[yytoken][0])) {
323                     // don't merge unary signs, additive expressions and postfix/prefix increments.
324                     assembled += QLatin1Char(' ');
325                 }
326
327                 assembled += QLatin1String(spell[yytoken]);
328
329             } else if (yytoken == T_NUMERIC_LITERAL) {
330                 if (isIdentChar(lastChar))
331                     assembled += QLatin1Char(' ');
332
333                 if (yytokentext.startsWith('.'))
334                     assembled += QLatin1Char('0');
335
336                 assembled += yytokentext;
337
338                 if (assembled.endsWith(QLatin1Char('.')))
339                     assembled += QLatin1Char('0');
340
341             } else if (yytoken == T_IDENTIFIER) {
342                 QString identifier = yytokentext;
343
344                 if (classify(identifier.constData(), identifier.size(), qmlMode()) != T_IDENTIFIER) {
345                     // the unescaped identifier is a keyword. In this case just replace
346                     // the last character of the identifier with it escape sequence.
347                     const QChar ch = identifier.at(identifier.length() - 1);
348                     identifier.chop(1);
349                     escape(ch, &identifier);
350                 }
351
352                 if (isIdentChar(lastChar))
353                     assembled += QLatin1Char(' ');
354
355                 foreach (const QChar &ch, identifier) {
356                     if (isIdentChar(ch))
357                         assembled += ch;
358                     else {
359                         escape(ch, &assembled);
360                     }
361                 }
362
363             } else if (yytoken == T_STRING_LITERAL || yytoken == T_MULTILINE_STRING_LITERAL) {
364                 assembled += QLatin1Char('"');
365                 assembled += quote(yytokentext);
366                 assembled += QLatin1Char('"');
367             } else {
368                 if (isIdentChar(lastChar)) {
369                     if (! yytokentext.isEmpty()) {
370                         const QChar ch = yytokentext.at(0);
371                         if (isIdentChar(ch))
372                             assembled += QLatin1Char(' ');
373                     }
374                 }
375                 assembled += yytokentext;
376             }
377             yytoken = -1;
378         } else if (yyaction < 0) {
379             const int ruleno = -yyaction - 1;
380             yytos -= rhs[ruleno];
381
382             if (isRegExpRule(ruleno)) {
383                 QString restOfRegExp;
384
385                 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
386                     break; // break the loop, it wil report a syntax error
387
388                 assembled += restOfRegExp;
389             }
390             yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
391         }
392     } while (yyaction);
393
394     const int yyerrorstate = _stateStack[yytos];
395
396     // automatic insertion of `;'
397     if (yytoken != -1 && ((t_action(yyerrorstate, T_AUTOMATIC_SEMICOLON) && canInsertAutomaticSemicolon(yytoken))
398                           || t_action(yyerrorstate, T_COMPATIBILITY_SEMICOLON))) {
399         _tokens.prepend(yytoken);
400         _tokenStrings.prepend(yytokentext);
401         yyaction = yyerrorstate;
402         yytoken = T_SEMICOLON;
403         goto again;
404     }
405
406     std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
407     return false;
408 }
409
410
411 class Tokenize: public QmlminLexer
412 {
413     QVector<int> _stateStack;
414     QList<int> _tokens;
415     QList<QString> _tokenStrings;
416     QStringList _minifiedCode;
417
418 public:
419     Tokenize();
420
421     QStringList tokenStream() const;
422
423 protected:
424     virtual bool parse(int startToken);
425 };
426
427 Tokenize::Tokenize()
428     : _stateStack(128)
429 {
430 }
431
432 QStringList Tokenize::tokenStream() const
433 {
434     return _minifiedCode;
435 }
436
437 bool Tokenize::parse(int startToken)
438 {
439     int yyaction = 0;
440     int yytoken = -1;
441     int yytos = -1;
442     QString yytokentext;
443
444     _minifiedCode.clear();
445     _tokens.append(startToken);
446     _tokenStrings.append(QString());
447
448     if (startToken == T_FEED_JS_PROGRAM) {
449         // parse optional pragma directive
450         if (scanDirectives(this)) {
451             // append the scanned directives as one token to
452             // the token stream.
453             _minifiedCode.append(directives());
454
455             _tokens.append(tokenKind());
456             _tokenStrings.append(tokenText());
457         } else {
458             std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
459             return false;
460         }
461     }
462
463     do {
464         if (++yytos == _stateStack.size())
465             _stateStack.resize(_stateStack.size() * 2);
466
467         _stateStack[yytos] = yyaction;
468
469     again:
470         if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
471             if (_tokens.isEmpty()) {
472                 _tokens.append(lex());
473                 _tokenStrings.append(tokenText());
474             }
475
476             yytoken = _tokens.takeFirst();
477             yytokentext = _tokenStrings.takeFirst();
478         }
479
480         yyaction = t_action(yyaction, yytoken);
481         if (yyaction > 0) {
482             if (yyaction == ACCEPT_STATE) {
483                 --yytos;
484                 return true;
485             }
486
487             if (yytoken == T_SEMICOLON)
488                 _minifiedCode += QLatin1String(";");
489             else
490                 _minifiedCode += yytokentext;
491
492             yytoken = -1;
493         } else if (yyaction < 0) {
494             const int ruleno = -yyaction - 1;
495             yytos -= rhs[ruleno];
496
497             if (isRegExpRule(ruleno)) {
498                 QString restOfRegExp;
499
500                 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
501                     break; // break the loop, it wil report a syntax error
502
503                 _minifiedCode.last().append(restOfRegExp);
504             }
505
506             yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
507         }
508     } while (yyaction);
509
510     const int yyerrorstate = _stateStack[yytos];
511
512     // automatic insertion of `;'
513     if (yytoken != -1 && ((t_action(yyerrorstate, T_AUTOMATIC_SEMICOLON) && canInsertAutomaticSemicolon(yytoken))
514                           || t_action(yyerrorstate, T_COMPATIBILITY_SEMICOLON))) {
515         _tokens.prepend(yytoken);
516         _tokenStrings.prepend(yytokentext);
517         yyaction = yyerrorstate;
518         yytoken = T_SEMICOLON;
519         goto again;
520     }
521
522     std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
523     return false;
524 }
525
526 } // end of QQmlJS namespace
527
528 static void usage(bool showHelp = false)
529 {
530     std::cerr << "Usage: qmlmin [options] file" << std::endl;
531
532     if (showHelp) {
533         std::cerr << " Removes comments and layout characters" << std::endl
534                   << " The options are:" << std::endl
535                   << "  -o<file>                write output to file rather than stdout" << std::endl
536                   << "  -v --verify-only        just run the verifier, no output" << std::endl
537                   << "  -w<width>               restrict line characters to width" << std::endl
538                   << "  -h                      display this output" << std::endl;
539     }
540 }
541
542 int runQmlmin(int argc, char *argv[])
543 {
544     QCoreApplication app(argc, argv);
545
546     const QStringList args = app.arguments();
547
548     QString fileName;
549     QString outputFile;
550     bool verifyOnly = false;
551
552     // By default ensure the output character width is less than 16-bits (pass 0 to disable)
553     int width = USHRT_MAX;
554
555     int index = 1;
556     while (index < args.size()) {
557         const QString arg = args.at(index++);
558         const QString next = index < args.size() ? args.at(index) : QString();
559
560         if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
561             usage(/*showHelp*/ true);
562             return 0;
563         } else if (arg == QLatin1String("-v") || arg == QLatin1String("--verify-only")) {
564             verifyOnly = true;
565         } else if (arg == QLatin1String("-o")) {
566             if (next.isEmpty()) {
567                 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
568                 return EXIT_FAILURE;
569             } else {
570                 outputFile = next;
571                 ++index; // consume the next argument
572             }
573         } else if (arg.startsWith(QLatin1String("-o"))) {
574             outputFile = arg.mid(2);
575
576             if (outputFile.isEmpty()) {
577                 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
578                 return EXIT_FAILURE;
579             }
580         } else if (arg == QLatin1String("-w")) {
581             if (next.isEmpty()) {
582                 std::cerr << "qmlmin: argument to '-w' is missing" << std::endl;
583                 return EXIT_FAILURE;
584             } else {
585                 bool ok;
586                 width = next.toInt(&ok);
587
588                 if (!ok) {
589                     std::cerr << "qmlmin: argument to '-w' is invalid" << std::endl;
590                     return EXIT_FAILURE;
591                 }
592
593                 ++index; // consume the next argument
594             }
595         } else if (arg.startsWith(QLatin1String("-w"))) {
596             bool ok;
597             width = arg.mid(2).toInt(&ok);
598
599             if (!ok) {
600                 std::cerr << "qmlmin: argument to '-w' is invalid" << std::endl;
601                 return EXIT_FAILURE;
602             }
603         } else {
604             const bool isInvalidOpt = arg.startsWith(QLatin1Char('-'));
605             if (! isInvalidOpt && fileName.isEmpty())
606                 fileName = arg;
607             else {
608                 usage(/*show help*/ isInvalidOpt);
609                 if (isInvalidOpt)
610                     std::cerr << "qmlmin: invalid option '" << qPrintable(arg) << "'" << std::endl;
611                 else
612                     std::cerr << "qmlmin: too many input files specified" << std::endl;
613                 return EXIT_FAILURE;
614             }
615         }
616     }
617
618     if (fileName.isEmpty()) {
619         usage();
620         return 0;
621     }
622
623     QFile file(fileName);
624     if (! file.open(QFile::ReadOnly)) {
625         std::cerr << "qmlmin: '" << qPrintable(fileName) << "' no such file or directory" << std::endl;
626         return EXIT_FAILURE;
627     }
628
629     const QString code = QString::fromUtf8(file.readAll()); // QML files are UTF-8 encoded.
630     file.close();
631
632     QQmlJS::Minify minify(width);
633     if (! minify(fileName, code)) {
634         std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (not a valid QML/JS file)" << std::endl;
635         return EXIT_FAILURE;
636     }
637
638     //
639     // verify the output
640     //
641     QQmlJS::Minify secondMinify(width);
642     if (! secondMinify(fileName, minify.minifiedCode()) || secondMinify.minifiedCode() != minify.minifiedCode()) {
643         std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
644         return EXIT_FAILURE;
645     }
646
647     QQmlJS::Tokenize originalTokens, minimizedTokens;
648     originalTokens(fileName, code);
649     minimizedTokens(fileName, minify.minifiedCode());
650
651     if (originalTokens.tokenStream().size() != minimizedTokens.tokenStream().size()) {
652         std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
653         return EXIT_FAILURE;
654     }
655
656     if (! verifyOnly) {
657         if (outputFile.isEmpty()) {
658             const QByteArray chars = minify.minifiedCode().toUtf8();
659             std::cout << chars.constData();
660         } else {
661             QFile file(outputFile);
662             if (! file.open(QFile::WriteOnly)) {
663                 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (permission denied)" << std::endl;
664                 return EXIT_FAILURE;
665             }
666
667             file.write(minify.minifiedCode().toUtf8());
668             file.close();
669         }
670     }
671
672     return 0;
673 }
674
675 QT_END_NAMESPACE
676
677 int main(int argc, char **argv)
678 {
679     return QT_PREPEND_NAMESPACE(runQmlmin(argc, argv));
680 }