summaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/qdeclarativescriptparser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative/qml/qdeclarativescriptparser.cpp')
-rw-r--r--src/declarative/qml/qdeclarativescriptparser.cpp99
1 files changed, 37 insertions, 62 deletions
diff --git a/src/declarative/qml/qdeclarativescriptparser.cpp b/src/declarative/qml/qdeclarativescriptparser.cpp
index 219d759..f703cf5 100644
--- a/src/declarative/qml/qdeclarativescriptparser.cpp
+++ b/src/declarative/qml/qdeclarativescriptparser.cpp
@@ -831,7 +831,9 @@ bool QDeclarativeScriptParser::parse(const QByteArray &qmldata, const QUrl &url)
_scriptFile = fileName;
QTextStream stream(qmldata, QIODevice::ReadOnly);
+#ifndef QT_NO_TEXTCODEC
stream.setCodec("UTF-8");
+#endif
const QString code = stream.readAll();
data = new QDeclarativeScriptParserJsASTData(fileName);
@@ -896,6 +898,14 @@ QList<QDeclarativeError> QDeclarativeScriptParser::errors() const
return _errors;
}
+static void replaceWithSpace(QString &str, int idx, int n)
+{
+ QChar *data = str.data() + idx;
+ QChar space(' ');
+ for (int ii = 0; ii < n; ++ii)
+ *data++ = space;
+}
+
/*
Searches for ".pragma <value>" declarations within \a script. Currently supported pragmas
are:
@@ -905,83 +915,48 @@ QDeclarativeParser::Object::ScriptBlock::Pragmas QDeclarativeScriptParser::extra
{
QDeclarativeParser::Object::ScriptBlock::Pragmas rv = QDeclarativeParser::Object::ScriptBlock::None;
- const QChar forwardSlash(QLatin1Char('/'));
- const QChar star(QLatin1Char('*'));
- const QChar newline(QLatin1Char('\n'));
- const QChar dot(QLatin1Char('.'));
- const QChar semicolon(QLatin1Char(';'));
- const QChar space(QLatin1Char(' '));
- const QString pragma(QLatin1String(".pragma "));
-
- const QChar *pragmaData = pragma.constData();
-
- const QChar *data = script.constData();
- const int length = script.count();
- for (int ii = 0; ii < length; ++ii) {
- const QChar &c = data[ii];
-
- if (c.isSpace())
- continue;
-
- if (c == forwardSlash) {
- ++ii;
- if (ii >= length)
- return rv;
-
- const QChar &c = data[ii];
- if (c == forwardSlash) {
- // Find next newline
- while (ii < length && data[++ii] != newline) {};
- } else if (c == star) {
- // Find next star
- while (true) {
- while (ii < length && data[++ii] != star) {};
- if (ii + 1 >= length)
- return rv;
-
- if (data[ii + 1] == forwardSlash) {
- ++ii;
- break;
- }
- }
- } else {
- return rv;
- }
- } else if (c == dot) {
- // Could be a pragma!
- if (ii + pragma.length() >= length ||
- 0 != ::memcmp(data + ii, pragmaData, sizeof(QChar) * pragma.length()))
- return rv;
+ const QString pragma(QLatin1String("pragma"));
+ const QString library(QLatin1String("library"));
- int pragmaStatementIdx = ii;
+ QDeclarativeJS::Lexer l(0);
+ l.setCode(script, 0);
- ii += pragma.length();
+ int token = l.lex();
- while (ii < length && data[ii].isSpace()) { ++ii; }
+ while (true) {
+ if (token != QDeclarativeJSGrammar::T_DOT)
+ return rv;
- int startIdx = ii;
+ int startOffset = l.tokenOffset();
+ int startLine = l.currentLineNo();
- while (ii < length && data[ii].isLetter()) { ++ii; }
+ token = l.lex();
- int endIdx = ii;
+ if (token != QDeclarativeJSGrammar::T_IDENTIFIER ||
+ l.currentLineNo() != startLine ||
+ script.mid(l.tokenOffset(), l.tokenLength()) != pragma)
+ return rv;
- if (ii != length && data[ii] != forwardSlash && !data[ii].isSpace() && data[ii] != semicolon)
- return rv;
+ token = l.lex();
- QString p(data + startIdx, endIdx - startIdx);
+ if (token != QDeclarativeJSGrammar::T_IDENTIFIER ||
+ l.currentLineNo() != startLine)
+ return rv;
- if (p == QLatin1String("library"))
- rv |= QDeclarativeParser::Object::ScriptBlock::Shared;
- else
- return rv;
+ QString pragmaValue = script.mid(l.tokenOffset(), l.tokenLength());
+ int endOffset = l.tokenLength() + l.tokenOffset();
- for (int jj = pragmaStatementIdx; jj < endIdx; ++jj) script[jj] = space;
+ token = l.lex();
+ if (l.currentLineNo() == startLine)
+ return rv;
+ if (pragmaValue == QLatin1String("library")) {
+ rv |= QDeclarativeParser::Object::ScriptBlock::Shared;
+ replaceWithSpace(script, startOffset, endOffset - startOffset);
} else {
return rv;
}
}
-
return rv;
}