ebfc5851dd3e53b16131f282a49f621fe89d669b
[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 <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>
50 #include <iostream>
51 #include <cstdlib>
52
53 QT_BEGIN_NAMESPACE
54
55 //
56 // QML/JS minifier
57 //
58 namespace QDeclarativeJS {
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     QDeclarativeJS::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         return true;
186     }
187 };
188
189
190 class Minify: public QmlminLexer
191 {
192     QVector<int> _stateStack;
193     QList<int> _tokens;
194     QList<QString> _tokenStrings;
195     QString _minifiedCode;
196
197 public:
198     Minify();
199
200     QString minifiedCode() const;
201
202 protected:
203     bool parse(int startToken);
204 };
205
206 Minify::Minify()
207     : _stateStack(128)
208 {
209 }
210
211 QString Minify::minifiedCode() const
212 {
213     return _minifiedCode;
214 }
215
216 bool Minify::parse(int startToken)
217 {
218     int yyaction = 0;
219     int yytoken = -1;
220     int yytos = -1;
221     QString yytokentext;
222
223     _minifiedCode.clear();
224     _tokens.append(startToken);
225     _tokenStrings.append(QString());
226
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();
232
233             _tokens.append(tokenKind());
234             _tokenStrings.append(tokenText());
235         } else {
236             std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
237             return false;
238         }
239     }
240
241     do {
242         if (++yytos == _stateStack.size())
243             _stateStack.resize(_stateStack.size() * 2);
244
245         _stateStack[yytos] = yyaction;
246
247     again:
248         if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
249             if (_tokens.isEmpty()) {
250                 _tokens.append(lex());
251                 _tokenStrings.append(tokenText());
252             }
253
254             yytoken = _tokens.takeFirst();
255             yytokentext = _tokenStrings.takeFirst();
256         }
257
258         yyaction = t_action(yyaction, yytoken);
259         if (yyaction > 0) {
260             if (yyaction == ACCEPT_STATE) {
261                 --yytos;
262                 return true;
263             }
264
265             const QChar lastChar = _minifiedCode.isEmpty() ? QChar() : _minifiedCode.at(_minifiedCode.length() - 1);
266
267             if (yytoken == T_SEMICOLON) {
268                 _minifiedCode += QLatin1Char(';');
269
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(' ');
274                 }
275
276                 _minifiedCode += QLatin1String(spell[yytoken]);
277
278             } else if (yytoken == T_NUMERIC_LITERAL) {
279                 if (isIdentChar(lastChar))
280                     _minifiedCode += QLatin1Char(' ');
281
282                 if (yytokentext.startsWith('.'))
283                     _minifiedCode += QLatin1Char('0');
284
285                 _minifiedCode += yytokentext;
286
287                 if (_minifiedCode.endsWith(QLatin1Char('.')))
288                     _minifiedCode += QLatin1Char('0');
289
290             } else if (yytoken == T_IDENTIFIER) {
291                 if (isIdentChar(lastChar))
292                     _minifiedCode += QLatin1Char(' ');
293
294                 foreach (const QChar &ch, yytokentext) {
295                     if (isIdentChar(ch))
296                         _minifiedCode += ch;
297                     else {
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;
304                         case 4: break;
305                         default:
306                             std::cerr << "qmlmin: invalid unicode sequence" << std::endl;
307                             return false;
308                         }
309                         _minifiedCode += hx;
310                     }
311                 }
312
313             } else if (yytoken == T_STRING_LITERAL || yytoken == T_MULTILINE_STRING_LITERAL) {
314                 _minifiedCode += QLatin1Char('"');
315                 _minifiedCode += quote(yytokentext);
316                 _minifiedCode += QLatin1Char('"');
317             } else {
318                 if (isIdentChar(lastChar)) {
319                     if (! yytokentext.isEmpty()) {
320                         const QChar ch = yytokentext.at(0);
321                         if (isIdentChar(ch))
322                             _minifiedCode += QLatin1Char(' ');
323                     }
324                 }
325                 _minifiedCode += yytokentext;
326             }
327             yytoken = -1;
328         } else if (yyaction < 0) {
329             const int ruleno = -yyaction - 1;
330             yytos -= rhs[ruleno];
331
332             if (isRegExpRule(ruleno)) {
333                 QString restOfRegExp;
334
335                 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
336                     break; // break the loop, it wil report a syntax error
337
338                 _minifiedCode += restOfRegExp;
339             }
340             yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
341         }
342     } while (yyaction);
343
344     const int yyerrorstate = _stateStack[yytos];
345
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;
352         goto again;
353     }
354
355     std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
356     return false;
357 }
358
359
360 class Tokenize: public QmlminLexer
361 {
362     QVector<int> _stateStack;
363     QList<int> _tokens;
364     QList<QString> _tokenStrings;
365     QStringList _minifiedCode;
366
367 public:
368     Tokenize();
369
370     QStringList tokenStream() const;
371
372 protected:
373     virtual bool parse(int startToken);
374 };
375
376 Tokenize::Tokenize()
377     : _stateStack(128)
378 {
379 }
380
381 QStringList Tokenize::tokenStream() const
382 {
383     return _minifiedCode;
384 }
385
386 bool Tokenize::parse(int startToken)
387 {
388     int yyaction = 0;
389     int yytoken = -1;
390     int yytos = -1;
391     QString yytokentext;
392
393     _minifiedCode.clear();
394     _tokens.append(startToken);
395     _tokenStrings.append(QString());
396
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
401             // the token stream.
402             _minifiedCode.append(directives());
403
404             _tokens.append(tokenKind());
405             _tokenStrings.append(tokenText());
406         } else {
407             std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
408             return false;
409         }
410     }
411
412     do {
413         if (++yytos == _stateStack.size())
414             _stateStack.resize(_stateStack.size() * 2);
415
416         _stateStack[yytos] = yyaction;
417
418     again:
419         if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
420             if (_tokens.isEmpty()) {
421                 _tokens.append(lex());
422                 _tokenStrings.append(tokenText());
423             }
424
425             yytoken = _tokens.takeFirst();
426             yytokentext = _tokenStrings.takeFirst();
427         }
428
429         yyaction = t_action(yyaction, yytoken);
430         if (yyaction > 0) {
431             if (yyaction == ACCEPT_STATE) {
432                 --yytos;
433                 return true;
434             }
435
436             if (yytoken == T_SEMICOLON)
437                 _minifiedCode += QLatin1String(";");
438             else
439                 _minifiedCode += yytokentext;
440
441             yytoken = -1;
442         } else if (yyaction < 0) {
443             const int ruleno = -yyaction - 1;
444             yytos -= rhs[ruleno];
445
446             if (isRegExpRule(ruleno)) {
447                 QString restOfRegExp;
448
449                 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
450                     break; // break the loop, it wil report a syntax error
451
452                 _minifiedCode.last().append(restOfRegExp);
453             }
454
455             yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
456         }
457     } while (yyaction);
458
459     const int yyerrorstate = _stateStack[yytos];
460
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;
467         goto again;
468     }
469
470     std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
471     return false;
472 }
473
474 } // end of QDeclarativeJS namespace
475
476 static void usage(bool showHelp = false)
477 {
478     std::cerr << "Usage: qmlmin [options] file" << std::endl;
479
480     if (showHelp) {
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;
486     }
487 }
488
489 int runQmlmin(int argc, char *argv[])
490 {
491     QCoreApplication app(argc, argv);
492
493     const QStringList args = app.arguments();
494
495     QString fileName;
496     QString outputFile;
497     bool verifyOnly = false;
498
499     int index = 1;
500     while (index < args.size()) {
501         const QString arg = args.at(index++);
502         const QString next = index < args.size() ? args.at(index) : QString();
503
504         if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
505             usage(/*showHelp*/ true);
506             return 0;
507         } else if (arg == QLatin1String("-v") || arg == QLatin1String("--verify-only")) {
508             verifyOnly = true;
509         } else if (arg == QLatin1String("-o")) {
510             if (next.isEmpty()) {
511                 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
512                 return EXIT_FAILURE;
513             } else {
514                 outputFile = next;
515                 ++index; // consume the next argument
516             }
517         } else if (arg.startsWith(QLatin1String("-o"))) {
518             outputFile = arg.mid(2);
519
520             if (outputFile.isEmpty()) {
521                 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
522                 return EXIT_FAILURE;
523             }
524         } else {
525             const bool isInvalidOpt = arg.startsWith(QLatin1Char('-'));
526             if (! isInvalidOpt && fileName.isEmpty())
527                 fileName = arg;
528             else {
529                 usage(/*show help*/ isInvalidOpt);
530                 if (isInvalidOpt)
531                     std::cerr << "qmlmin: invalid option '" << qPrintable(arg) << "'" << std::endl;
532                 else
533                     std::cerr << "qmlmin: too many input files specified" << std::endl;
534                 return EXIT_FAILURE;
535             }
536         }
537     }
538
539     if (fileName.isEmpty()) {
540         usage();
541         return 0;
542     }
543
544     QFile file(fileName);
545     if (! file.open(QFile::ReadOnly)) {
546         std::cerr << "qmlmin: '" << qPrintable(fileName) << "' no such file or directory" << std::endl;
547         return EXIT_FAILURE;
548     }
549
550     const QString code = QString::fromUtf8(file.readAll()); // QML files are UTF-8 encoded.
551     file.close();
552
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;
556         return EXIT_FAILURE;
557     }
558
559     //
560     // verify the output
561     //
562     QDeclarativeJS::Minify secondMinify;
563     if (! secondMinify(fileName, minify.minifiedCode()) || secondMinify.minifiedCode() != minify.minifiedCode()) {
564         std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
565         return EXIT_FAILURE;
566     }
567
568     QDeclarativeJS::Tokenize originalTokens, minimizedTokens;
569     originalTokens(fileName, code);
570     minimizedTokens(fileName, minify.minifiedCode());
571
572     if (originalTokens.tokenStream().size() != minimizedTokens.tokenStream().size()) {
573         std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
574         return EXIT_FAILURE;
575     }
576
577     if (! verifyOnly) {
578         if (outputFile.isEmpty()) {
579             const QByteArray chars = minify.minifiedCode().toUtf8();
580             std::cout << chars.constData();
581         } else {
582             QFile file(outputFile);
583             if (! file.open(QFile::WriteOnly)) {
584                 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (permission denied)" << std::endl;
585                 return EXIT_FAILURE;
586             }
587
588             file.write(minify.minifiedCode().toUtf8());
589             file.close();
590         }
591     }
592
593     return 0;
594 }
595
596 QT_END_NAMESPACE
597
598 int main(int argc, char **argv)
599 {
600     return QT_PREPEND_NAMESPACE(runQmlmin(argc, argv));
601 }