Qt 4.8
qscriptxmlparser.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qscriptxmlparser_p.h"
43 
44 #include <QtCore/qstringlist.h>
45 #include <QtCore/qxmlstream.h>
46 #include <QtCore/qdebug.h>
47 
49 
51  QList<int> &lineNumbers)
52 {
53  int level = 0;
54  while (!reader.atEnd()) {
56  if ((t == target) && (level == 0))
57  return;
59  ++level;
60  QString line = reader.attributes().value(QLatin1String("line")).toString();
61  if (!line.isEmpty())
62  lineNumbers.append(line.toInt());
63  } else if (t == QXmlStreamReader::EndElement) {
64  --level;
65  }
66  }
67 // Q_ASSERT_X(false, "QScriptXmlParser", "premature end of file");
68 }
69 
71 {
72  QMap<QString, int> functionsInfo;
73  QList<int> lineNumbers;
74  QXmlStreamReader reader(xml);
75  reader.readNext(); // StartDocument
76  reader.readNext(); // <program>
77  reader.readNext(); // <source-elements>
78  while (reader.readNext() == QXmlStreamReader::StartElement) {
79 // qDebug() << reader.name().toString();
80  int line = reader.attributes().value(QLatin1String("line")).toString().toInt();
81  lineNumbers.append(line);
82  if (reader.name() == QLatin1String("function-declaration")) {
83  // extract the line number, name and formal parameters
84  reader.readNext(); // <name>
85  reader.readNext(); // Characters
86  QString name = reader.text().toString();
87  reader.readNext(); // </name>
88  reader.readNext(); // <formal-parameter-list>
89  QStringList formalParameters;
90  while (reader.readNext() == QXmlStreamReader::StartElement) {
91  reader.readNext(); // Characters
92  formalParameters.append(reader.text().toString());
93  reader.readNext(); // </identifier>
94  }
95  reader.readNext(); // <function-body>
96  tokenUntil(reader, QXmlStreamReader::EndElement, lineNumbers);
97 
98  QString signature;
99  signature.append(name);
100  signature.append(QLatin1Char('('));
101  for (int i = 0; i < formalParameters.size(); ++i) {
102  if (i > 0)
103  signature.append(QLatin1String(", "));
104  signature.append(formalParameters.at(i));
105  }
106  signature.append(QLatin1Char(')'));
107  functionsInfo.insert(signature, line);
108  } else if (reader.name() == QLatin1String("expression-statement")) {
109  reader.readNext();
110  if ((reader.name() == QLatin1String("binary-expression"))
111  && reader.attributes().value(QLatin1String("op")) == QLatin1String("=")) {
112  // try to match a statement of the form Foo.prototype.bar = function() { ... }
113  // this can be generalized...
114  QString first, second, third;
115  reader.readNext(); // LHS
116  if (reader.name() == QLatin1String("field-member-expression")) {
117  reader.readNext();
118  if (reader.name() == QLatin1String("field-member-expression")) {
119  reader.readNext();
120  if (reader.name() == QLatin1String("identifier")) {
121  reader.readNext();
122  first = reader.text().toString();
123  }
124  tokenUntil(reader, QXmlStreamReader::EndElement, lineNumbers);
125  reader.readNext();
126  if (reader.name() == QLatin1String("identifier")) {
127  reader.readNext();
128  second = reader.text().toString();
129  }
130  tokenUntil(reader, QXmlStreamReader::EndElement, lineNumbers);
131  } else if (reader.name() == QLatin1String("identifier")) {
132  reader.readNext();
133  first = reader.text().toString();
134  }
135  tokenUntil(reader, QXmlStreamReader::EndElement, lineNumbers);
136  reader.readNext();
137  if (reader.name() == QLatin1String("identifier")) {
138  reader.readNext();
139  if (second.isEmpty())
140  second = reader.text().toString();
141  else
142  third = reader.text().toString();
143  }
144  tokenUntil(reader, QXmlStreamReader::EndElement, lineNumbers);
145  }
146  tokenUntil(reader, QXmlStreamReader::EndElement, lineNumbers);
147  reader.readNext(); // RHS
148  if (reader.name() == QLatin1String("function-expression")) {
149  if (!first.isEmpty()) {
150  QString signature = first;
151  if (!second.isEmpty()) {
152  signature.append(QLatin1Char('.'));
153  signature.append(second);
154  if (!third.isEmpty()) {
155  signature.append(QLatin1Char('.'));
156  signature.append(third);
157  }
158  }
159  signature.append(QLatin1String("()"));
160  functionsInfo.insert(signature, line);
161  }
162  }
163  tokenUntil(reader, QXmlStreamReader::EndElement, lineNumbers);
164  }
165  tokenUntil(reader, QXmlStreamReader::EndElement, lineNumbers);
166  }
167  tokenUntil(reader, QXmlStreamReader::EndElement, lineNumbers);
168  }
169  reader.readNext(); // </source-elements>
170  reader.readNext(); // </program>
171  reader.readNext(); // EndDocument
172  Q_ASSERT(reader.atEnd());
173  return Result(functionsInfo, lineNumbers.toSet());
174 }
175 
QString toString() const
Returns a copy of the string reference as a QString object.
Definition: qstring.cpp:8653
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QSet< T > toSet() const
Returns a QSet object with the data contained in this QList.
Definition: qset.h:309
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QStringRef name() const
Returns the local name of a StartElement, EndElement, or an EntityReference.
bool atEnd() const
Returns true if the reader has read until the end of the XML document, or if an error() has occurred ...
Definition: qxmlstream.cpp:590
The QString class provides a Unicode character string.
Definition: qstring.h:83
static void tokenUntil(QXmlStreamReader &reader, QXmlStreamReader::TokenType target, QList< int > &lineNumbers)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
TokenType
This enum specifies the type of token the reader just read.
Definition: qxmlstream.h:293
QStringRef value(const QString &namespaceUri, const QString &name) const
Returns the value of the attribute name in the namespace described with namespaceUri, or an empty string reference if the attribute is not defined.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
TokenType readNext()
Reads the next token and returns its type.
Definition: qxmlstream.cpp:623
QString & append(QChar c)
Definition: qstring.cpp:1777
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
static Result parse(const QString &xml)
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
The QXmlStreamReader class provides a fast parser for reading well-formed XML via a simple streaming ...
Definition: qxmlstream.h:290
QStringRef text() const
Returns the text of Characters , Comment , DTD , or EntityReference.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QXmlStreamAttributes attributes() const
Returns the attributes of a StartElement.