1/****************************************************************************
2**
3** Copyright (C) 2018 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qscriptxmlparser_p.h"
41
42#include <QtCore/qstringlist.h>
43#include <QtCore/qxmlstream.h>
44#include <QtCore/qdebug.h>
45
46QT_BEGIN_NAMESPACE
47
48static void tokenUntil(QXmlStreamReader &reader, QXmlStreamReader::TokenType target,
49 QList<int> &lineNumbers)
50{
51 int level = 0;
52 while (!reader.atEnd()) {
53 QXmlStreamReader::TokenType t = reader.readNext();
54 if ((t == target) && (level == 0))
55 return;
56 if (t == QXmlStreamReader::StartElement) {
57 ++level;
58 QString line = reader.attributes().value(qualifiedName: QLatin1String("line")).toString();
59 if (!line.isEmpty())
60 lineNumbers.append(t: line.toInt());
61 } else if (t == QXmlStreamReader::EndElement) {
62 --level;
63 }
64 }
65// Q_ASSERT_X(false, "QScriptXmlParser", "premature end of file");
66}
67
68QScriptXmlParser::Result QScriptXmlParser::parse(const QString &xml)
69{
70 QMap<QString, int> functionsInfo;
71 QList<int> lineNumbers;
72 QXmlStreamReader reader(xml);
73 reader.readNext(); // StartDocument
74 reader.readNext(); // <program>
75 reader.readNext(); // <source-elements>
76 while (reader.readNext() == QXmlStreamReader::StartElement) {
77// qDebug() << reader.name().toString();
78 int line = reader.attributes().value(qualifiedName: QLatin1String("line")).toString().toInt();
79 lineNumbers.append(t: line);
80 if (reader.name() == QLatin1String("function-declaration")) {
81 // extract the line number, name and formal parameters
82 reader.readNext(); // <name>
83 reader.readNext(); // Characters
84 QString name = reader.text().toString();
85 reader.readNext(); // </name>
86 reader.readNext(); // <formal-parameter-list>
87 QStringList formalParameters;
88 while (reader.readNext() == QXmlStreamReader::StartElement) {
89 reader.readNext(); // Characters
90 formalParameters.append(t: reader.text().toString());
91 reader.readNext(); // </identifier>
92 }
93 reader.readNext(); // <function-body>
94 tokenUntil(reader, target: QXmlStreamReader::EndElement, lineNumbers);
95
96 QString signature;
97 signature.append(s: name);
98 signature.append(c: QLatin1Char('('));
99 for (int i = 0; i < formalParameters.size(); ++i) {
100 if (i > 0)
101 signature.append(s: QLatin1String(", "));
102 signature.append(s: formalParameters.at(i));
103 }
104 signature.append(c: QLatin1Char(')'));
105 functionsInfo.insert(akey: signature, avalue: line);
106 } else if (reader.name() == QLatin1String("expression-statement")) {
107 reader.readNext();
108 if ((reader.name() == QLatin1String("binary-expression"))
109 && reader.attributes().value(qualifiedName: QLatin1String("op")) == QLatin1String("=")) {
110 // try to match a statement of the form Foo.prototype.bar = function() { ... }
111 // this can be generalized...
112 QString first, second, third;
113 reader.readNext(); // LHS
114 if (reader.name() == QLatin1String("field-member-expression")) {
115 reader.readNext();
116 if (reader.name() == QLatin1String("field-member-expression")) {
117 reader.readNext();
118 if (reader.name() == QLatin1String("identifier")) {
119 reader.readNext();
120 first = reader.text().toString();
121 }
122 tokenUntil(reader, target: QXmlStreamReader::EndElement, lineNumbers);
123 reader.readNext();
124 if (reader.name() == QLatin1String("identifier")) {
125 reader.readNext();
126 second = reader.text().toString();
127 }
128 tokenUntil(reader, target: QXmlStreamReader::EndElement, lineNumbers);
129 } else if (reader.name() == QLatin1String("identifier")) {
130 reader.readNext();
131 first = reader.text().toString();
132 }
133 tokenUntil(reader, target: QXmlStreamReader::EndElement, lineNumbers);
134 reader.readNext();
135 if (reader.name() == QLatin1String("identifier")) {
136 reader.readNext();
137 if (second.isEmpty())
138 second = reader.text().toString();
139 else
140 third = reader.text().toString();
141 }
142 tokenUntil(reader, target: QXmlStreamReader::EndElement, lineNumbers);
143 }
144 tokenUntil(reader, target: QXmlStreamReader::EndElement, lineNumbers);
145 reader.readNext(); // RHS
146 if (reader.name() == QLatin1String("function-expression")) {
147 if (!first.isEmpty()) {
148 QString signature = first;
149 if (!second.isEmpty()) {
150 signature.append(c: QLatin1Char('.'));
151 signature.append(s: second);
152 if (!third.isEmpty()) {
153 signature.append(c: QLatin1Char('.'));
154 signature.append(s: third);
155 }
156 }
157 signature.append(s: QLatin1String("()"));
158 functionsInfo.insert(akey: signature, avalue: line);
159 }
160 }
161 tokenUntil(reader, target: QXmlStreamReader::EndElement, lineNumbers);
162 }
163 tokenUntil(reader, target: QXmlStreamReader::EndElement, lineNumbers);
164 }
165 tokenUntil(reader, target: QXmlStreamReader::EndElement, lineNumbers);
166 }
167 reader.readNext(); // </source-elements>
168 reader.readNext(); // </program>
169 reader.readNext(); // EndDocument
170 Q_ASSERT(reader.atEnd());
171 return Result(functionsInfo, lineNumbers.toSet());
172}
173
174QT_END_NAMESPACE
175

source code of qtscript/src/scripttools/debugging/qscriptxmlparser.cpp