Warning: That file was not part of the compilation database. It may have many parsing errors.
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 tools applications 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 | /* |
43 | qmlcodeparser.cpp |
44 | */ |
45 | |
46 | #include "declarativeparser/qdeclarativejsast_p.h" |
47 | #include "declarativeparser/qdeclarativejsastvisitor_p.h" |
48 | #include "declarativeparser/qdeclarativejsnodepool_p.h" |
49 | |
50 | #include "qmlcodeparser.h" |
51 | #include "node.h" |
52 | #include "tree.h" |
53 | #include "config.h" |
54 | #include "qmlvisitor.h" |
55 | |
56 | QT_BEGIN_NAMESPACE |
57 | |
58 | #define COMMAND_STARTPAGE Doc::alias("startpage") |
59 | #define COMMAND_VARIABLE Doc::alias("variable") |
60 | |
61 | #define COMMAND_QMLCLASS Doc::alias("qmlclass") |
62 | #define COMMAND_QMLPROPERTY Doc::alias("qmlproperty") |
63 | #define COMMAND_QMLATTACHEDPROPERTY Doc::alias("qmlattachedproperty") |
64 | #define COMMAND_QMLINHERITS Doc::alias("inherits") |
65 | #define COMMAND_QMLSIGNAL Doc::alias("qmlsignal") |
66 | #define COMMAND_QMLATTACHEDSIGNAL Doc::alias("qmlattachedsignal") |
67 | #define COMMAND_QMLMETHOD Doc::alias("qmlmethod") |
68 | #define COMMAND_QMLATTACHEDMETHOD Doc::alias("qmlattachedmethod") |
69 | #define COMMAND_QMLDEFAULT Doc::alias("default") |
70 | #define COMMAND_QMLBASICTYPE Doc::alias("qmlbasictype") |
71 | |
72 | QmlCodeParser::QmlCodeParser() |
73 | { |
74 | } |
75 | |
76 | QmlCodeParser::~QmlCodeParser() |
77 | { |
78 | } |
79 | |
80 | /*! |
81 | Initialize the code parser base class. |
82 | */ |
83 | void QmlCodeParser::initializeParser(const Config &config) |
84 | { |
85 | CodeParser::initializeParser(config); |
86 | |
87 | lexer = new QDeclarativeJS::Lexer(&engine); |
88 | parser = new QDeclarativeJS::Parser(&engine); |
89 | } |
90 | |
91 | void QmlCodeParser::terminateParser() |
92 | { |
93 | delete lexer; |
94 | delete parser; |
95 | } |
96 | |
97 | QString QmlCodeParser::language() |
98 | { |
99 | return "QML"; |
100 | } |
101 | |
102 | QStringList QmlCodeParser::sourceFileNameFilter() |
103 | { |
104 | return QStringList("*.qml"); |
105 | } |
106 | |
107 | void QmlCodeParser::parseSourceFile(const Location& location, |
108 | const QString& filePath, |
109 | Tree *tree) |
110 | { |
111 | QFile in(filePath); |
112 | if (!in.open(QIODevice::ReadOnly)) { |
113 | location.error(tr("Cannot open QML file '%1'").arg(filePath)); |
114 | return; |
115 | } |
116 | |
117 | QString document = in.readAll(); |
118 | in.close(); |
119 | |
120 | Location fileLocation(filePath); |
121 | |
122 | QString newCode = document; |
123 | extractPragmas(newCode); |
124 | lexer->setCode(newCode, 1); |
125 | |
126 | QSet<QString> topicCommandsAllowed = topicCommands(); |
127 | QSet<QString> otherMetacommandsAllowed = otherMetaCommands(); |
128 | QSet<QString> metacommandsAllowed = topicCommandsAllowed + |
129 | otherMetacommandsAllowed; |
130 | |
131 | QDeclarativeJS::NodePool m_nodePool(filePath, &engine); |
132 | |
133 | if (parser->parse()) { |
134 | QDeclarativeJS::AST::UiProgram *ast = parser->ast(); |
135 | QmlDocVisitor visitor(filePath, newCode, &engine, tree, metacommandsAllowed); |
136 | QDeclarativeJS::AST::Node::accept(ast, &visitor); |
137 | } |
138 | } |
139 | |
140 | void QmlCodeParser::doneParsingSourceFiles(Tree *) |
141 | { |
142 | } |
143 | |
144 | /*! |
145 | Returns the set of strings representing the topic commands. |
146 | */ |
147 | QSet<QString> QmlCodeParser::topicCommands() |
148 | { |
149 | return QSet<QString>() << COMMAND_VARIABLE |
150 | << COMMAND_QMLCLASS |
151 | << COMMAND_QMLPROPERTY |
152 | << COMMAND_QMLATTACHEDPROPERTY |
153 | << COMMAND_QMLSIGNAL |
154 | << COMMAND_QMLATTACHEDSIGNAL |
155 | << COMMAND_QMLMETHOD |
156 | << COMMAND_QMLATTACHEDMETHOD |
157 | << COMMAND_QMLBASICTYPE; |
158 | } |
159 | |
160 | /*! |
161 | Returns the set of strings representing the common metacommands |
162 | plus some other metacommands. |
163 | */ |
164 | QSet<QString> QmlCodeParser::otherMetaCommands() |
165 | { |
166 | return commonMetaCommands() << COMMAND_STARTPAGE |
167 | << COMMAND_QMLINHERITS |
168 | << COMMAND_QMLDEFAULT; |
169 | } |
170 | |
171 | /* |
172 | Copied and pasted from src/declarative/qml/qdeclarativescriptparser.cpp. |
173 | */ |
174 | static void replaceWithSpace(QString &str, int idx, int n) |
175 | { |
176 | QChar *data = str.data() + idx; |
177 | const QChar space(QLatin1Char( |
178 | for (int ii = 0; ii < n; ++ii) |
179 | *data++ = space; |
180 | } |
181 | |
182 | /* |
183 | Copied and pasted from src/declarative/qml/qdeclarativescriptparser.cpp then |
184 | modified to return no values. |
185 | |
186 | Searches for ".pragma <value>" declarations within \a script. Currently supported pragmas |
187 | are: |
188 | library |
189 | */ |
190 | void QmlCodeParser::extractPragmas(QString &script) |
191 | { |
192 | const QString pragma(QLatin1String("pragma")); |
193 | const QString library(QLatin1String("library")); |
194 | |
195 | QDeclarativeJS::Lexer l(0); |
196 | l.setCode(script, 0); |
197 | |
198 | int token = l.lex(); |
199 | |
200 | while (true) { |
201 | if (token != QDeclarativeJSGrammar::T_DOT) |
202 | return; |
203 | |
204 | int startOffset = l.tokenOffset(); |
205 | int startLine = l.currentLineNo(); |
206 | |
207 | token = l.lex(); |
208 | |
209 | if (token != QDeclarativeJSGrammar::T_IDENTIFIER || |
210 | l.currentLineNo() != startLine || |
211 | script.mid(l.tokenOffset(), l.tokenLength()) != pragma) |
212 | return; |
213 | |
214 | token = l.lex(); |
215 | |
216 | if (token != QDeclarativeJSGrammar::T_IDENTIFIER || |
217 | l.currentLineNo() != startLine) |
218 | return; |
219 | |
220 | QString pragmaValue = script.mid(l.tokenOffset(), l.tokenLength()); |
221 | int endOffset = l.tokenLength() + l.tokenOffset(); |
222 | |
223 | token = l.lex(); |
224 | if (l.currentLineNo() == startLine) |
225 | return; |
226 | |
227 | if (pragmaValue == QLatin1String("library")) |
228 | replaceWithSpace(script, startOffset, endOffset - startOffset); |
229 | else |
230 | return; |
231 | } |
232 | return; |
233 | } |
234 | |
235 | QT_END_NAMESPACE |
236 |
Warning: That file was not part of the compilation database. It may have many parsing errors.