1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
67 class QmlminLexer: protected Lexer, public Directives
69 QQmlJS::Engine _engine;
74 QmlminLexer(): Lexer(&_engine) {}
75 virtual ~QmlminLexer() {}
77 QString fileName() const { return _fileName; }
79 bool operator()(const QString &fileName, const QString &code)
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);
88 return parse(startToken);
97 // Handle the .pragma/.import directives
99 virtual void pragmaLibrary()
101 _directives += QLatin1String(".pragma library\n");
104 virtual void importFile(const QString &jsfile, const QString &module)
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');
115 virtual void importModule(const QString &uri, const QString &version, const QString &module)
117 _directives += QLatin1String(".import ");
119 _directives += QLatin1Char(' ');
120 _directives += version;
121 _directives += QLatin1String(" as ");
122 _directives += module;
123 _directives += QLatin1Char('\n');
127 virtual bool parse(int startToken) = 0;
129 static QString quote(const QString &string)
131 QString quotedString;
132 foreach (const QChar &ch, string) {
133 if (ch == QLatin1Char('"'))
134 quotedString += QLatin1String("\\\"");
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;
151 bool isIdentChar(const QChar &ch) const
153 if (ch.isLetterOrNumber())
155 else if (ch == QLatin1Char('_') || ch == QLatin1Char('$'))
160 bool isRegExpRule(int ruleno) const
162 return ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ||
163 ruleno == J_SCRIPT_REGEXPLITERAL_RULE2;
166 bool scanRestOfRegExp(int ruleno, QString *restOfRegExp)
168 if (! scanRegExp(ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ? Lexer::NoPrefix : Lexer::EqualPrefix))
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
177 *restOfRegExp += QLatin1Char('/');
178 const RegExpFlag flags = (RegExpFlag) regExpFlags();
180 *restOfRegExp += QLatin1Char('g');
181 if (flags & IgnoreCase)
182 *restOfRegExp += QLatin1Char('i');
183 if (flags & Multiline)
184 *restOfRegExp += QLatin1Char('m');
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(' ');
197 class Minify: public QmlminLexer
199 QVector<int> _stateStack;
201 QList<QString> _tokenStrings;
202 QString _minifiedCode;
207 Minify(int maxWidth);
209 QString minifiedCode() const;
212 void append(const QString &s);
213 bool parse(int startToken);
214 void escape(const QChar &ch, QString *out);
217 Minify::Minify(int maxWidth)
218 : _stateStack(128), _maxWidth(maxWidth), _width(0)
222 QString Minify::minifiedCode() const
224 return _minifiedCode;
227 void Minify::append(const QString &s)
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'));
238 _width += segmentLength;
241 _minifiedCode.append(s);
245 void Minify::escape(const QChar &ch, QString *out)
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;
254 default: Q_ASSERT(!"unreachable");
259 bool Minify::parse(int startToken)
267 _minifiedCode.clear();
268 _tokens.append(startToken);
269 _tokenStrings.append(QString());
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());
277 _tokens.append(tokenKind());
278 _tokenStrings.append(tokenText());
280 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
286 if (++yytos == _stateStack.size())
287 _stateStack.resize(_stateStack.size() * 2);
289 _stateStack[yytos] = yyaction;
292 if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
293 if (_tokens.isEmpty()) {
294 _tokens.append(lex());
295 _tokenStrings.append(tokenText());
298 yytoken = _tokens.takeFirst();
299 yytokentext = _tokenStrings.takeFirst();
302 yyaction = t_action(yyaction, yytoken);
304 if (yyaction == ACCEPT_STATE) {
306 if (!assembled.isEmpty())
311 const QChar lastChar = assembled.isEmpty() ? (_minifiedCode.isEmpty() ? QChar()
312 : _minifiedCode.at(_minifiedCode.length() - 1))
313 : assembled.at(assembled.length() - 1);
315 if (yytoken == T_SEMICOLON) {
316 assembled += QLatin1Char(';');
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(' ');
327 assembled += QLatin1String(spell[yytoken]);
329 } else if (yytoken == T_NUMERIC_LITERAL) {
330 if (isIdentChar(lastChar))
331 assembled += QLatin1Char(' ');
333 if (yytokentext.startsWith('.'))
334 assembled += QLatin1Char('0');
336 assembled += yytokentext;
338 if (assembled.endsWith(QLatin1Char('.')))
339 assembled += QLatin1Char('0');
341 } else if (yytoken == T_IDENTIFIER) {
342 QString identifier = yytokentext;
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);
349 escape(ch, &identifier);
352 if (isIdentChar(lastChar))
353 assembled += QLatin1Char(' ');
355 foreach (const QChar &ch, identifier) {
359 escape(ch, &assembled);
363 } else if (yytoken == T_STRING_LITERAL || yytoken == T_MULTILINE_STRING_LITERAL) {
364 assembled += QLatin1Char('"');
365 assembled += quote(yytokentext);
366 assembled += QLatin1Char('"');
368 if (isIdentChar(lastChar)) {
369 if (! yytokentext.isEmpty()) {
370 const QChar ch = yytokentext.at(0);
372 assembled += QLatin1Char(' ');
375 assembled += yytokentext;
378 } else if (yyaction < 0) {
379 const int ruleno = -yyaction - 1;
380 yytos -= rhs[ruleno];
382 if (isRegExpRule(ruleno)) {
383 QString restOfRegExp;
385 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
386 break; // break the loop, it wil report a syntax error
388 assembled += restOfRegExp;
390 yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
394 const int yyerrorstate = _stateStack[yytos];
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;
406 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
411 class Tokenize: public QmlminLexer
413 QVector<int> _stateStack;
415 QList<QString> _tokenStrings;
416 QStringList _minifiedCode;
421 QStringList tokenStream() const;
424 virtual bool parse(int startToken);
432 QStringList Tokenize::tokenStream() const
434 return _minifiedCode;
437 bool Tokenize::parse(int startToken)
444 _minifiedCode.clear();
445 _tokens.append(startToken);
446 _tokenStrings.append(QString());
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
453 _minifiedCode.append(directives());
455 _tokens.append(tokenKind());
456 _tokenStrings.append(tokenText());
458 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
464 if (++yytos == _stateStack.size())
465 _stateStack.resize(_stateStack.size() * 2);
467 _stateStack[yytos] = yyaction;
470 if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
471 if (_tokens.isEmpty()) {
472 _tokens.append(lex());
473 _tokenStrings.append(tokenText());
476 yytoken = _tokens.takeFirst();
477 yytokentext = _tokenStrings.takeFirst();
480 yyaction = t_action(yyaction, yytoken);
482 if (yyaction == ACCEPT_STATE) {
487 if (yytoken == T_SEMICOLON)
488 _minifiedCode += QLatin1String(";");
490 _minifiedCode += yytokentext;
493 } else if (yyaction < 0) {
494 const int ruleno = -yyaction - 1;
495 yytos -= rhs[ruleno];
497 if (isRegExpRule(ruleno)) {
498 QString restOfRegExp;
500 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
501 break; // break the loop, it wil report a syntax error
503 _minifiedCode.last().append(restOfRegExp);
506 yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
510 const int yyerrorstate = _stateStack[yytos];
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;
522 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
526 } // end of QQmlJS namespace
528 static void usage(bool showHelp = false)
530 std::cerr << "Usage: qmlmin [options] file" << std::endl;
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;
542 int runQmlmin(int argc, char *argv[])
544 QCoreApplication app(argc, argv);
546 const QStringList args = app.arguments();
550 bool verifyOnly = false;
552 // By default ensure the output character width is less than 16-bits (pass 0 to disable)
553 int width = USHRT_MAX;
556 while (index < args.size()) {
557 const QString arg = args.at(index++);
558 const QString next = index < args.size() ? args.at(index) : QString();
560 if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
561 usage(/*showHelp*/ true);
563 } else if (arg == QLatin1String("-v") || arg == QLatin1String("--verify-only")) {
565 } else if (arg == QLatin1String("-o")) {
566 if (next.isEmpty()) {
567 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
571 ++index; // consume the next argument
573 } else if (arg.startsWith(QLatin1String("-o"))) {
574 outputFile = arg.mid(2);
576 if (outputFile.isEmpty()) {
577 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
580 } else if (arg == QLatin1String("-w")) {
581 if (next.isEmpty()) {
582 std::cerr << "qmlmin: argument to '-w' is missing" << std::endl;
586 width = next.toInt(&ok);
589 std::cerr << "qmlmin: argument to '-w' is invalid" << std::endl;
593 ++index; // consume the next argument
595 } else if (arg.startsWith(QLatin1String("-w"))) {
597 width = arg.mid(2).toInt(&ok);
600 std::cerr << "qmlmin: argument to '-w' is invalid" << std::endl;
604 const bool isInvalidOpt = arg.startsWith(QLatin1Char('-'));
605 if (! isInvalidOpt && fileName.isEmpty())
608 usage(/*show help*/ isInvalidOpt);
610 std::cerr << "qmlmin: invalid option '" << qPrintable(arg) << "'" << std::endl;
612 std::cerr << "qmlmin: too many input files specified" << std::endl;
618 if (fileName.isEmpty()) {
623 QFile file(fileName);
624 if (! file.open(QFile::ReadOnly)) {
625 std::cerr << "qmlmin: '" << qPrintable(fileName) << "' no such file or directory" << std::endl;
629 const QString code = QString::fromUtf8(file.readAll()); // QML files are UTF-8 encoded.
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;
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;
647 QQmlJS::Tokenize originalTokens, minimizedTokens;
648 originalTokens(fileName, code);
649 minimizedTokens(fileName, minify.minifiedCode());
651 if (originalTokens.tokenStream().size() != minimizedTokens.tokenStream().size()) {
652 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
657 if (outputFile.isEmpty()) {
658 const QByteArray chars = minify.minifiedCode().toUtf8();
659 std::cout << chars.constData();
661 QFile file(outputFile);
662 if (! file.open(QFile::WriteOnly)) {
663 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (permission denied)" << std::endl;
667 file.write(minify.minifiedCode().toUtf8());
677 int main(int argc, char **argv)
679 return QT_PREPEND_NAMESPACE(runQmlmin(argc, argv));