Qt 4.8
Classes | Static Public Functions | List of all members
QScriptXmlParser Class Reference

#include <qscriptxmlparser_p.h>

Classes

struct  Result
 

Static Public Functions

static Result parse (const QString &xml)
 

Detailed Description

Definition at line 64 of file qscriptxmlparser_p.h.

Functions

◆ parse()

QScriptXmlParser::Result QScriptXmlParser::parse ( const QString xml)
static

Definition at line 70 of file qscriptxmlparser.cpp.

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 }
QSet< T > toSet() const
Returns a QSet object with the data contained in this QList.
Definition: qset.h:309
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
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
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
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
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
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
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

The documentation for this class was generated from the following files: