src/declarative/qml/qdeclarativescriptparser.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
   894 QList<QDeclarativeError> QDeclarativeScriptParser::errors() const
   894 QList<QDeclarativeError> QDeclarativeScriptParser::errors() const
   895 {
   895 {
   896     return _errors;
   896     return _errors;
   897 }
   897 }
   898 
   898 
       
   899 static void replaceWithSpace(QString &str, int idx, int n) 
       
   900 {
       
   901     QChar *data = str.data() + idx;
       
   902     QChar space(' ');
       
   903     for (int ii = 0; ii < n; ++ii)
       
   904         *data++ = space;
       
   905 }
       
   906 
   899 /*
   907 /*
   900 Searches for ".pragma <value>" declarations within \a script.  Currently supported pragmas
   908 Searches for ".pragma <value>" declarations within \a script.  Currently supported pragmas
   901 are:
   909 are:
   902     library
   910     library
   903 */
   911 */
   904 QDeclarativeParser::Object::ScriptBlock::Pragmas QDeclarativeScriptParser::extractPragmas(QString &script)
   912 QDeclarativeParser::Object::ScriptBlock::Pragmas QDeclarativeScriptParser::extractPragmas(QString &script)
   905 {
   913 {
   906     QDeclarativeParser::Object::ScriptBlock::Pragmas rv = QDeclarativeParser::Object::ScriptBlock::None;
   914     QDeclarativeParser::Object::ScriptBlock::Pragmas rv = QDeclarativeParser::Object::ScriptBlock::None;
   907 
   915 
   908     const QChar forwardSlash(QLatin1Char('/'));
   916     const QString pragma(QLatin1String("pragma"));
   909     const QChar star(QLatin1Char('*'));
   917     const QString library(QLatin1String("library"));
   910     const QChar newline(QLatin1Char('\n'));
   918 
   911     const QChar dot(QLatin1Char('.'));
   919     QDeclarativeJS::Lexer l(0);
   912     const QChar semicolon(QLatin1Char(';'));
   920     l.setCode(script, 0);
   913     const QChar space(QLatin1Char(' '));
   921 
   914     const QString pragma(QLatin1String(".pragma "));
   922     int token = l.lex();
   915 
   923 
   916     const QChar *pragmaData = pragma.constData();
   924     while (true) {
   917 
   925         if (token != QDeclarativeJSGrammar::T_DOT)
   918     const QChar *data = script.constData();
   926             return rv;
   919     const int length = script.count();
   927 
   920     for (int ii = 0; ii < length; ++ii) {
   928         int startOffset = l.tokenOffset();
   921         const QChar &c = data[ii];
   929         int startLine = l.currentLineNo();
   922 
   930 
   923         if (c.isSpace())
   931         token = l.lex();
   924             continue;
   932 
   925 
   933         if (token != QDeclarativeJSGrammar::T_IDENTIFIER ||
   926         if (c == forwardSlash) {
   934             l.currentLineNo() != startLine ||
   927             ++ii;
   935             script.mid(l.tokenOffset(), l.tokenLength()) != pragma)
   928             if (ii >= length)
   936             return rv;
   929                 return rv;
   937 
   930 
   938         token = l.lex();
   931             const QChar &c = data[ii];
   939 
   932             if (c == forwardSlash) {
   940         if (token != QDeclarativeJSGrammar::T_IDENTIFIER ||
   933                 // Find next newline
   941             l.currentLineNo() != startLine)
   934                 while (ii < length && data[++ii] != newline) {};
   942             return rv;
   935             } else if (c == star) {
   943 
   936                 // Find next star
   944         QString pragmaValue = script.mid(l.tokenOffset(), l.tokenLength());
   937                 while (true) {
   945         int endOffset = l.tokenLength() + l.tokenOffset();
   938                     while (ii < length && data[++ii] != star) {};
   946 
   939                     if (ii + 1 >= length)
   947         token = l.lex();
   940                         return rv;
   948         if (l.currentLineNo() == startLine)
   941 
   949             return rv;
   942                     if (data[ii + 1] == forwardSlash) {
   950 
   943                         ++ii;
   951         if (pragmaValue == QLatin1String("library")) {
   944                         break;
   952             rv |= QDeclarativeParser::Object::ScriptBlock::Shared;
   945                     }
   953             replaceWithSpace(script, startOffset, endOffset - startOffset);
   946                 }
       
   947             } else {
       
   948                 return rv;
       
   949             }
       
   950         } else if (c == dot) {
       
   951             // Could be a pragma!
       
   952             if (ii + pragma.length() >= length ||
       
   953                 0 != ::memcmp(data + ii, pragmaData, sizeof(QChar) * pragma.length()))
       
   954                 return rv;
       
   955 
       
   956             int pragmaStatementIdx = ii;
       
   957 
       
   958             ii += pragma.length();
       
   959 
       
   960             while (ii < length && data[ii].isSpace()) { ++ii; }
       
   961 
       
   962             int startIdx = ii;
       
   963 
       
   964             while (ii < length && data[ii].isLetter()) { ++ii; }
       
   965 
       
   966             int endIdx = ii;
       
   967 
       
   968             if (ii != length && data[ii] != forwardSlash && !data[ii].isSpace() && data[ii] != semicolon)
       
   969                 return rv;
       
   970 
       
   971             QString p(data + startIdx, endIdx - startIdx);
       
   972 
       
   973             if (p == QLatin1String("library"))
       
   974                 rv |= QDeclarativeParser::Object::ScriptBlock::Shared;
       
   975             else
       
   976                 return rv;
       
   977 
       
   978             for (int jj = pragmaStatementIdx; jj < endIdx; ++jj) script[jj] = space;
       
   979 
       
   980         } else {
   954         } else {
   981             return rv;
   955             return rv;
   982         }
   956         }
   983     }
   957     }
   984 
       
   985     return rv;
   958     return rv;
   986 }
   959 }
   987 
   960 
   988 void QDeclarativeScriptParser::clear()
   961 void QDeclarativeScriptParser::clear()
   989 {
   962 {