1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
56 namespace QDeclarativeJS {
65 class QmlminLexer: protected Lexer, public Directives
67 QDeclarativeJS::Engine _engine;
72 QmlminLexer(): Lexer(&_engine) {}
73 virtual ~QmlminLexer() {}
75 QString fileName() const { return _fileName; }
77 bool operator()(const QString &fileName, const QString &code)
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);
86 return parse(startToken);
95 // Handle the .pragma/.import directives
97 virtual void pragmaLibrary()
99 _directives += QLatin1String(".pragma library\n");
102 virtual void importFile(const QString &jsfile, const QString &module)
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');
113 virtual void importModule(const QString &uri, const QString &version, const QString &module)
115 _directives += QLatin1String(".import ");
117 _directives += QLatin1Char(' ');
118 _directives += version;
119 _directives += QLatin1String(" as ");
120 _directives += module;
121 _directives += QLatin1Char('\n');
125 bool automatic(int token) const
127 return token == T_RBRACE || token == 0 || prevTerminator();
130 virtual bool parse(int startToken) = 0;
132 static QString quote(const QString &string)
134 QString quotedString;
135 foreach (const QChar &ch, string) {
136 if (ch == QLatin1Char('"'))
137 quotedString += QLatin1String("\\\"");
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;
153 bool isIdentChar(const QChar &ch) const
155 if (ch.isLetterOrNumber())
157 else if (ch == QLatin1Char('_') || ch == QLatin1Char('$'))
162 bool isRegExpRule(int ruleno) const
164 return ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ||
165 ruleno == J_SCRIPT_REGEXPLITERAL_RULE2;
168 bool scanRestOfRegExp(int ruleno, QString *restOfRegExp)
170 if (! scanRegExp(ruleno == J_SCRIPT_REGEXPLITERAL_RULE1 ? Lexer::NoPrefix : Lexer::EqualPrefix))
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
179 *restOfRegExp += QLatin1Char('/');
180 const RegExpFlag flags = (RegExpFlag) regExpFlags();
182 *restOfRegExp += QLatin1Char('g');
183 if (flags & IgnoreCase)
184 *restOfRegExp += QLatin1Char('i');
185 if (flags & Multiline)
186 *restOfRegExp += QLatin1Char('m');
192 class Minify: public QmlminLexer
194 QVector<int> _stateStack;
196 QList<QString> _tokenStrings;
197 QString _minifiedCode;
202 QString minifiedCode() const;
205 bool parse(int startToken);
213 QString Minify::minifiedCode() const
215 return _minifiedCode;
218 bool Minify::parse(int startToken)
225 _minifiedCode.clear();
226 _tokens.append(startToken);
227 _tokenStrings.append(QString());
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();
235 _tokens.append(tokenKind());
236 _tokenStrings.append(tokenText());
238 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
244 if (++yytos == _stateStack.size())
245 _stateStack.resize(_stateStack.size() * 2);
247 _stateStack[yytos] = yyaction;
250 if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
251 if (_tokens.isEmpty()) {
252 _tokens.append(lex());
253 _tokenStrings.append(tokenText());
256 yytoken = _tokens.takeFirst();
257 yytokentext = _tokenStrings.takeFirst();
260 yyaction = t_action(yyaction, yytoken);
262 if (yyaction == ACCEPT_STATE) {
267 const QChar lastChar = _minifiedCode.isEmpty() ? QChar() : _minifiedCode.at(_minifiedCode.length() - 1);
269 if (yytoken == T_SEMICOLON) {
270 _minifiedCode += QLatin1Char(';');
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(' ');
278 _minifiedCode += QLatin1String(spell[yytoken]);
280 } else if (yytoken == T_NUMERIC_LITERAL) {
281 if (isIdentChar(lastChar))
282 _minifiedCode += QLatin1Char(' ');
284 if (yytokentext.startsWith('.'))
285 _minifiedCode += QLatin1Char('0');
287 _minifiedCode += yytokentext;
289 if (_minifiedCode.endsWith(QLatin1Char('.')))
290 _minifiedCode += QLatin1Char('0');
292 } else if (yytoken == T_IDENTIFIER) {
293 if (isIdentChar(lastChar))
294 _minifiedCode += QLatin1Char(' ');
296 foreach (const QChar &ch, yytokentext) {
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;
308 std::cerr << "qmlmin: invalid unicode sequence" << std::endl;
315 } else if (yytoken == T_STRING_LITERAL || yytoken == T_MULTILINE_STRING_LITERAL) {
316 _minifiedCode += QLatin1Char('"');
317 _minifiedCode += quote(yytokentext);
318 _minifiedCode += QLatin1Char('"');
320 if (isIdentChar(lastChar)) {
321 if (! yytokentext.isEmpty()) {
322 const QChar ch = yytokentext.at(0);
324 _minifiedCode += QLatin1Char(' ');
327 _minifiedCode += yytokentext;
330 } else if (yyaction < 0) {
331 const int ruleno = -yyaction - 1;
332 yytos -= rhs[ruleno];
334 if (isRegExpRule(ruleno)) {
335 QString restOfRegExp;
337 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
338 break; // break the loop, it wil report a syntax error
340 _minifiedCode += restOfRegExp;
342 yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
346 const int yyerrorstate = _stateStack[yytos];
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;
357 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
362 class Tokenize: public QmlminLexer
364 QVector<int> _stateStack;
366 QList<QString> _tokenStrings;
367 QStringList _minifiedCode;
372 QStringList tokenStream() const;
375 virtual bool parse(int startToken);
383 QStringList Tokenize::tokenStream() const
385 return _minifiedCode;
388 bool Tokenize::parse(int startToken)
395 _minifiedCode.clear();
396 _tokens.append(startToken);
397 _tokenStrings.append(QString());
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
404 _minifiedCode.append(directives());
406 _tokens.append(tokenKind());
407 _tokenStrings.append(tokenText());
409 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
415 if (++yytos == _stateStack.size())
416 _stateStack.resize(_stateStack.size() * 2);
418 _stateStack[yytos] = yyaction;
421 if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
422 if (_tokens.isEmpty()) {
423 _tokens.append(lex());
424 _tokenStrings.append(tokenText());
427 yytoken = _tokens.takeFirst();
428 yytokentext = _tokenStrings.takeFirst();
431 yyaction = t_action(yyaction, yytoken);
433 if (yyaction == ACCEPT_STATE) {
438 if (yytoken == T_SEMICOLON)
439 _minifiedCode += QLatin1String(";");
441 _minifiedCode += yytokentext;
444 } else if (yyaction < 0) {
445 const int ruleno = -yyaction - 1;
446 yytos -= rhs[ruleno];
448 if (isRegExpRule(ruleno)) {
449 QString restOfRegExp;
451 if (! scanRestOfRegExp(ruleno, &restOfRegExp))
452 break; // break the loop, it wil report a syntax error
454 _minifiedCode.last().append(restOfRegExp);
457 yyaction = nt_action(_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
461 const int yyerrorstate = _stateStack[yytos];
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;
472 std::cerr << qPrintable(fileName()) << ":" << tokenStartLine() << ":" << tokenStartColumn() << ": syntax error" << std::endl;
476 } // end of QDeclarativeJS namespace
478 static void usage(bool showHelp = false)
480 std::cerr << "Usage: qmlmin [options] file" << std::endl;
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;
491 int main(int argc, char *argv[])
493 QCoreApplication app(argc, argv);
495 const QStringList args = app.arguments();
499 bool verifyOnly = false;
502 while (index < args.size()) {
503 const QString arg = args.at(index++);
504 const QString next = index < args.size() ? args.at(index) : QString();
506 if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
507 usage(/*showHelp*/ true);
509 } else if (arg == QLatin1String("-v") || arg == QLatin1String("--verify-only")) {
511 } else if (arg == QLatin1String("-o")) {
512 if (next.isEmpty()) {
513 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
517 ++index; // consume the next argument
519 } else if (arg.startsWith(QLatin1String("-o"))) {
520 outputFile = arg.mid(2);
522 if (outputFile.isEmpty()) {
523 std::cerr << "qmlmin: argument to '-o' is missing" << std::endl;
527 const bool isInvalidOpt = arg.startsWith(QLatin1Char('-'));
528 if (! isInvalidOpt && fileName.isEmpty())
531 usage(/*show help*/ isInvalidOpt);
533 std::cerr << "qmlmin: invalid option '" << qPrintable(arg) << "'" << std::endl;
535 std::cerr << "qmlmin: too many input files specified" << std::endl;
541 if (fileName.isEmpty()) {
546 QFile file(fileName);
547 if (! file.open(QFile::ReadOnly)) {
548 std::cerr << "qmlmin: '" << qPrintable(fileName) << "' no such file or directory" << std::endl;
552 const QString code = QString::fromUtf8(file.readAll()); // QML files are UTF-8 encoded.
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;
564 QDeclarativeJS::Minify secondMinify;
565 if (! secondMinify(fileName, minify.minifiedCode()) || secondMinify.minifiedCode() != minify.minifiedCode()) {
566 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
570 QDeclarativeJS::Tokenize originalTokens, minimizedTokens;
571 originalTokens(fileName, code);
572 minimizedTokens(fileName, minify.minifiedCode());
574 if (originalTokens.tokenStream().size() != minimizedTokens.tokenStream().size()) {
575 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "'" << std::endl;
580 if (outputFile.isEmpty()) {
581 const QByteArray chars = minify.minifiedCode().toUtf8();
582 std::cout << chars.constData();
584 QFile file(outputFile);
585 if (! file.open(QFile::WriteOnly)) {
586 std::cerr << "qmlmin: cannot minify '" << qPrintable(fileName) << "' (permission denied)" << std::endl;
590 file.write(minify.minifiedCode().toUtf8());