summaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/qdeclarativescriptparser.cpp
diff options
context:
space:
mode:
authorAaron Kennedy <aaron.kennedy@nokia.com>2010-07-22 06:08:41 (GMT)
committerAaron Kennedy <aaron.kennedy@nokia.com>2010-07-22 06:08:41 (GMT)
commit58b9468d70fcfdd178f17e89d163c6f9879221d8 (patch)
tree8a68ef3e21de397e5e30b7c04cbb1fae8ad40874 /src/declarative/qml/qdeclarativescriptparser.cpp
parent91215b48022b3b3980960f1b5f301ac84f14d49d (diff)
downloadQt-58b9468d70fcfdd178f17e89d163c6f9879221d8.zip
Qt-58b9468d70fcfdd178f17e89d163c6f9879221d8.tar.gz
Qt-58b9468d70fcfdd178f17e89d163c6f9879221d8.tar.bz2
Reuse QML lexer to simplify .pragma script logic
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..0d8bbb5 100644
--- a/src/declarative/qml/qdeclarativescriptparser.cpp
+++ b/src/declarative/qml/qdeclarativescriptparser.cpp
@@ -896,6 +896,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 +913,50 @@ 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 lastLine = -1;
- while (ii < length && data[ii].isSpace()) { ++ii; }
+ int token = l.lex();
- int startIdx = ii;
+ while (true) {
+ if (token != QDeclarativeJSGrammar::T_DOT)
+ return rv;
- while (ii < length && data[ii].isLetter()) { ++ii; }
+ int startOffset = l.tokenOffset();
+ int startLine = l.currentLineNo();
- int endIdx = ii;
+ token = l.lex();
- if (ii != length && data[ii] != forwardSlash && !data[ii].isSpace() && data[ii] != semicolon)
- return rv;
+ if (token != QDeclarativeJSGrammar::T_IDENTIFIER ||
+ l.currentLineNo() != startLine ||
+ script.mid(l.tokenOffset(), l.tokenLength()) != pragma)
+ return rv;
- QString p(data + startIdx, endIdx - startIdx);
+ token = l.lex();
- if (p == QLatin1String("library"))
- rv |= QDeclarativeParser::Object::ScriptBlock::Shared;
- else
- return rv;
+ if (token != QDeclarativeJSGrammar::T_IDENTIFIER ||
+ l.currentLineNo() != startLine)
+ return rv;
- for (int jj = pragmaStatementIdx; jj < endIdx; ++jj) script[jj] = space;
+ QString pragmaValue = script.mid(l.tokenOffset(), l.tokenLength());
+ int endOffset = l.tokenLength() + l.tokenOffset();
+ 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;
}