1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml 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 | #ifndef ASTDUMPER_H |
39 | #define ASTDUMPER_H |
40 | |
41 | |
42 | // |
43 | // W A R N I N G |
44 | // ------------- |
45 | // |
46 | // This file is not part of the Qt API. It exists purely as an |
47 | // implementation detail. This header file may change from version to |
48 | // version without notice, or even be removed. |
49 | // |
50 | // We mean it. |
51 | // |
52 | |
53 | #include <private/qqmljsglobal_p.h> |
54 | #include <private/qqmljsastvisitor_p.h> |
55 | #include <QtCore/QString> |
56 | #include <functional> |
57 | #include <ostream> |
58 | |
59 | QT_BEGIN_NAMESPACE |
60 | class QDebug; |
61 | |
62 | namespace QQmlJS { |
63 | |
64 | enum class DumperOptions { |
65 | None=0, |
66 | NoLocations=0x1, |
67 | NoAnnotations=0x2, |
68 | DumpNode=0x4 |
69 | }; |
70 | bool operator & (DumperOptions lhs, DumperOptions rhs); |
71 | DumperOptions operator | (DumperOptions lhs, DumperOptions rhs); |
72 | |
73 | // no export, currently just a supporting file... |
74 | class AstDumper: public AST::BaseVisitor |
75 | { |
76 | public: |
77 | static QString printNode2(AST::Node *); |
78 | |
79 | static QString diff(AST::Node *n1, AST::Node *n2, int nContext=3, DumperOptions opt=DumperOptions::None, int indent=0); |
80 | static QString printNode(AST::Node *n, DumperOptions opt=DumperOptions::None, int indent=1, int baseIndent=0); |
81 | |
82 | AstDumper(const std::function <void (const QString &)> &dumper, DumperOptions options=DumperOptions::None, |
83 | int indent=1, int baseIndent=0); |
84 | |
85 | void start(const QString &str); |
86 | void start(const char *str); |
87 | void stop(const QString &str); |
88 | void stop(const char *str); |
89 | |
90 | QString qs(const QString &s); |
91 | QString qs(const char *s); |
92 | QString qs(const QStringRef &s); |
93 | |
94 | QString loc(const SourceLocation &s); |
95 | |
96 | QString boolStr(bool v); |
97 | |
98 | bool preVisit(AST::Node *el) override; |
99 | void postVisit(AST::Node *el) override; |
100 | |
101 | // Ui |
102 | bool visit(AST::UiProgram *el) override; |
103 | bool (AST::UiHeaderItemList *) override; |
104 | bool visit(AST::UiPragma *el) override; |
105 | bool visit(AST::UiImport *el) override; |
106 | bool visit(AST::UiPublicMember *el) override; |
107 | bool visit(AST::UiSourceElement *) override; |
108 | bool visit(AST::UiObjectDefinition *) override; |
109 | bool visit(AST::UiObjectInitializer *) override; |
110 | bool visit(AST::UiObjectBinding *) override; |
111 | bool visit(AST::UiScriptBinding *) override; |
112 | bool visit(AST::UiArrayBinding *) override; |
113 | bool visit(AST::UiParameterList *) override; |
114 | bool visit(AST::UiObjectMemberList *) override; |
115 | bool visit(AST::UiArrayMemberList *) override; |
116 | bool visit(AST::UiQualifiedId *) override; |
117 | bool visit(AST::UiEnumDeclaration *) override; |
118 | bool visit(AST::UiEnumMemberList *) override; |
119 | bool visit(AST::UiVersionSpecifier *) override; |
120 | bool visit(AST::UiInlineComponent *) override; |
121 | bool visit(AST::UiRequired *) override; |
122 | bool visit(AST::UiAnnotation *) override; |
123 | bool visit(AST::UiAnnotationList *) override; |
124 | |
125 | void endVisit(AST::UiProgram *) override; |
126 | void endVisit(AST::UiImport *) override; |
127 | void (AST::UiHeaderItemList *) override; |
128 | void endVisit(AST::UiPragma *) override; |
129 | void endVisit(AST::UiPublicMember *) override; |
130 | void endVisit(AST::UiSourceElement *) override; |
131 | void endVisit(AST::UiObjectDefinition *) override; |
132 | void endVisit(AST::UiObjectInitializer *) override; |
133 | void endVisit(AST::UiObjectBinding *) override; |
134 | void endVisit(AST::UiScriptBinding *) override; |
135 | void endVisit(AST::UiArrayBinding *) override; |
136 | void endVisit(AST::UiParameterList *) override; |
137 | void endVisit(AST::UiObjectMemberList *) override; |
138 | void endVisit(AST::UiArrayMemberList *) override; |
139 | void endVisit(AST::UiQualifiedId *) override; |
140 | void endVisit(AST::UiEnumDeclaration *) override; |
141 | void endVisit(AST::UiEnumMemberList *) override; |
142 | void endVisit(AST::UiVersionSpecifier *) override; |
143 | void endVisit(AST::UiInlineComponent *) override; |
144 | void endVisit(AST::UiRequired *) override; |
145 | void endVisit(AST::UiAnnotation *) override; |
146 | void endVisit(AST::UiAnnotationList *) override; |
147 | |
148 | // QQmlJS |
149 | bool visit(AST::ThisExpression *) override; |
150 | void endVisit(AST::ThisExpression *) override; |
151 | |
152 | bool visit(AST::IdentifierExpression *) override; |
153 | void endVisit(AST::IdentifierExpression *) override; |
154 | |
155 | bool visit(AST::NullExpression *) override; |
156 | void endVisit(AST::NullExpression *) override; |
157 | |
158 | bool visit(AST::TrueLiteral *) override; |
159 | void endVisit(AST::TrueLiteral *) override; |
160 | |
161 | bool visit(AST::FalseLiteral *) override; |
162 | void endVisit(AST::FalseLiteral *) override; |
163 | |
164 | bool visit(AST::SuperLiteral *) override; |
165 | void endVisit(AST::SuperLiteral *) override; |
166 | |
167 | bool visit(AST::StringLiteral *) override; |
168 | void endVisit(AST::StringLiteral *) override; |
169 | |
170 | bool visit(AST::TemplateLiteral *) override; |
171 | void endVisit(AST::TemplateLiteral *) override; |
172 | |
173 | bool visit(AST::NumericLiteral *) override; |
174 | void endVisit(AST::NumericLiteral *) override; |
175 | |
176 | bool visit(AST::RegExpLiteral *) override; |
177 | void endVisit(AST::RegExpLiteral *) override; |
178 | |
179 | bool visit(AST::ArrayPattern *) override; |
180 | void endVisit(AST::ArrayPattern *) override; |
181 | |
182 | bool visit(AST::ObjectPattern *) override; |
183 | void endVisit(AST::ObjectPattern *) override; |
184 | |
185 | bool visit(AST::PatternElementList *) override; |
186 | void endVisit(AST::PatternElementList *) override; |
187 | |
188 | bool visit(AST::PatternPropertyList *) override; |
189 | void endVisit(AST::PatternPropertyList *) override; |
190 | |
191 | bool visit(AST::PatternElement *) override; |
192 | void endVisit(AST::PatternElement *) override; |
193 | |
194 | bool visit(AST::PatternProperty *) override; |
195 | void endVisit(AST::PatternProperty *) override; |
196 | |
197 | bool visit(AST::Elision *) override; |
198 | void endVisit(AST::Elision *) override; |
199 | |
200 | bool visit(AST::NestedExpression *) override; |
201 | void endVisit(AST::NestedExpression *) override; |
202 | |
203 | bool visit(AST::IdentifierPropertyName *) override; |
204 | void endVisit(AST::IdentifierPropertyName *) override; |
205 | |
206 | bool visit(AST::StringLiteralPropertyName *) override; |
207 | void endVisit(AST::StringLiteralPropertyName *) override; |
208 | |
209 | bool visit(AST::NumericLiteralPropertyName *) override; |
210 | void endVisit(AST::NumericLiteralPropertyName *) override; |
211 | |
212 | bool visit(AST::ComputedPropertyName *) override; |
213 | void endVisit(AST::ComputedPropertyName *) override; |
214 | |
215 | bool visit(AST::ArrayMemberExpression *) override; |
216 | void endVisit(AST::ArrayMemberExpression *) override; |
217 | |
218 | bool visit(AST::FieldMemberExpression *) override; |
219 | void endVisit(AST::FieldMemberExpression *) override; |
220 | |
221 | bool visit(AST::TaggedTemplate *) override; |
222 | void endVisit(AST::TaggedTemplate *) override; |
223 | |
224 | bool visit(AST::NewMemberExpression *) override; |
225 | void endVisit(AST::NewMemberExpression *) override; |
226 | |
227 | bool visit(AST::NewExpression *) override; |
228 | void endVisit(AST::NewExpression *) override; |
229 | |
230 | bool visit(AST::CallExpression *) override; |
231 | void endVisit(AST::CallExpression *) override; |
232 | |
233 | bool visit(AST::ArgumentList *) override; |
234 | void endVisit(AST::ArgumentList *) override; |
235 | |
236 | bool visit(AST::PostIncrementExpression *) override; |
237 | void endVisit(AST::PostIncrementExpression *) override; |
238 | |
239 | bool visit(AST::PostDecrementExpression *) override; |
240 | void endVisit(AST::PostDecrementExpression *) override; |
241 | |
242 | bool visit(AST::DeleteExpression *) override; |
243 | void endVisit(AST::DeleteExpression *) override; |
244 | |
245 | bool visit(AST::VoidExpression *) override; |
246 | void endVisit(AST::VoidExpression *) override; |
247 | |
248 | bool visit(AST::TypeOfExpression *) override; |
249 | void endVisit(AST::TypeOfExpression *) override; |
250 | |
251 | bool visit(AST::PreIncrementExpression *) override; |
252 | void endVisit(AST::PreIncrementExpression *) override; |
253 | |
254 | bool visit(AST::PreDecrementExpression *) override; |
255 | void endVisit(AST::PreDecrementExpression *) override; |
256 | |
257 | bool visit(AST::UnaryPlusExpression *) override; |
258 | void endVisit(AST::UnaryPlusExpression *) override; |
259 | |
260 | bool visit(AST::UnaryMinusExpression *) override; |
261 | void endVisit(AST::UnaryMinusExpression *) override; |
262 | |
263 | bool visit(AST::TildeExpression *) override; |
264 | void endVisit(AST::TildeExpression *) override; |
265 | |
266 | bool visit(AST::NotExpression *) override; |
267 | void endVisit(AST::NotExpression *) override; |
268 | |
269 | bool visit(AST::BinaryExpression *) override; |
270 | void endVisit(AST::BinaryExpression *) override; |
271 | |
272 | bool visit(AST::ConditionalExpression *) override; |
273 | void endVisit(AST::ConditionalExpression *) override; |
274 | |
275 | bool visit(AST::Expression *) override; |
276 | void endVisit(AST::Expression *) override; |
277 | |
278 | bool visit(AST::Block *) override; |
279 | void endVisit(AST::Block *) override; |
280 | |
281 | bool visit(AST::StatementList *) override; |
282 | void endVisit(AST::StatementList *) override; |
283 | |
284 | bool visit(AST::VariableStatement *) override; |
285 | void endVisit(AST::VariableStatement *) override; |
286 | |
287 | bool visit(AST::VariableDeclarationList *) override; |
288 | void endVisit(AST::VariableDeclarationList *) override; |
289 | |
290 | bool visit(AST::EmptyStatement *) override; |
291 | void endVisit(AST::EmptyStatement *) override; |
292 | |
293 | bool visit(AST::ExpressionStatement *) override; |
294 | void endVisit(AST::ExpressionStatement *) override; |
295 | |
296 | bool visit(AST::IfStatement *) override; |
297 | void endVisit(AST::IfStatement *) override; |
298 | |
299 | bool visit(AST::DoWhileStatement *) override; |
300 | void endVisit(AST::DoWhileStatement *) override; |
301 | |
302 | bool visit(AST::WhileStatement *) override; |
303 | void endVisit(AST::WhileStatement *) override; |
304 | |
305 | bool visit(AST::ForStatement *) override; |
306 | void endVisit(AST::ForStatement *) override; |
307 | |
308 | bool visit(AST::ForEachStatement *) override; |
309 | void endVisit(AST::ForEachStatement *) override; |
310 | |
311 | bool visit(AST::ContinueStatement *) override; |
312 | void endVisit(AST::ContinueStatement *) override; |
313 | |
314 | bool visit(AST::BreakStatement *) override; |
315 | void endVisit(AST::BreakStatement *) override; |
316 | |
317 | bool visit(AST::ReturnStatement *) override; |
318 | void endVisit(AST::ReturnStatement *) override; |
319 | |
320 | bool visit(AST::YieldExpression *) override; |
321 | void endVisit(AST::YieldExpression *) override; |
322 | |
323 | bool visit(AST::WithStatement *) override; |
324 | void endVisit(AST::WithStatement *) override; |
325 | |
326 | bool visit(AST::SwitchStatement *) override; |
327 | void endVisit(AST::SwitchStatement *) override; |
328 | |
329 | bool visit(AST::CaseBlock *) override; |
330 | void endVisit(AST::CaseBlock *) override; |
331 | |
332 | bool visit(AST::CaseClauses *) override; |
333 | void endVisit(AST::CaseClauses *) override; |
334 | |
335 | bool visit(AST::CaseClause *) override; |
336 | void endVisit(AST::CaseClause *) override; |
337 | |
338 | bool visit(AST::DefaultClause *) override; |
339 | void endVisit(AST::DefaultClause *) override; |
340 | |
341 | bool visit(AST::LabelledStatement *) override; |
342 | void endVisit(AST::LabelledStatement *) override; |
343 | |
344 | bool visit(AST::ThrowStatement *) override; |
345 | void endVisit(AST::ThrowStatement *) override; |
346 | |
347 | bool visit(AST::TryStatement *) override; |
348 | void endVisit(AST::TryStatement *) override; |
349 | |
350 | bool visit(AST::Catch *) override; |
351 | void endVisit(AST::Catch *) override; |
352 | |
353 | bool visit(AST::Finally *) override; |
354 | void endVisit(AST::Finally *) override; |
355 | |
356 | bool visit(AST::FunctionDeclaration *) override; |
357 | void endVisit(AST::FunctionDeclaration *) override; |
358 | |
359 | bool visit(AST::FunctionExpression *) override; |
360 | void endVisit(AST::FunctionExpression *) override; |
361 | |
362 | bool visit(AST::FormalParameterList *) override; |
363 | void endVisit(AST::FormalParameterList *) override; |
364 | |
365 | bool visit(AST::ClassExpression *) override; |
366 | void endVisit(AST::ClassExpression *) override; |
367 | |
368 | bool visit(AST::ClassDeclaration *) override; |
369 | void endVisit(AST::ClassDeclaration *) override; |
370 | |
371 | bool visit(AST::ClassElementList *) override; |
372 | void endVisit(AST::ClassElementList *) override; |
373 | |
374 | bool visit(AST::Program *) override; |
375 | void endVisit(AST::Program *) override; |
376 | |
377 | bool visit(AST::NameSpaceImport *) override; |
378 | void endVisit(AST::NameSpaceImport *) override; |
379 | |
380 | bool visit(AST::ImportSpecifier *) override; |
381 | void endVisit(AST::ImportSpecifier *) override; |
382 | |
383 | bool visit(AST::ImportsList *) override; |
384 | void endVisit(AST::ImportsList *) override; |
385 | |
386 | bool visit(AST::NamedImports *) override; |
387 | void endVisit(AST::NamedImports *) override; |
388 | |
389 | bool visit(AST::FromClause *) override; |
390 | void endVisit(AST::FromClause *) override; |
391 | |
392 | bool visit(AST::ImportClause *) override; |
393 | void endVisit(AST::ImportClause *) override; |
394 | |
395 | bool visit(AST::ImportDeclaration *) override; |
396 | void endVisit(AST::ImportDeclaration *) override; |
397 | |
398 | bool visit(AST::ExportSpecifier *) override; |
399 | void endVisit(AST::ExportSpecifier *) override; |
400 | |
401 | bool visit(AST::ExportsList *) override; |
402 | void endVisit(AST::ExportsList *) override; |
403 | |
404 | bool visit(AST::ExportClause *) override; |
405 | void endVisit(AST::ExportClause *) override; |
406 | |
407 | bool visit(AST::ExportDeclaration *) override; |
408 | void endVisit(AST::ExportDeclaration *) override; |
409 | |
410 | bool visit(AST::ESModule *) override; |
411 | void endVisit(AST::ESModule *) override; |
412 | |
413 | bool visit(AST::DebuggerStatement *) override; |
414 | void endVisit(AST::DebuggerStatement *) override; |
415 | |
416 | bool visit(AST::Type *) override; |
417 | void endVisit(AST::Type *) override; |
418 | |
419 | bool visit(AST::TypeArgumentList *) override; |
420 | void endVisit(AST::TypeArgumentList *) override; |
421 | |
422 | bool visit(AST::TypeAnnotation *) override; |
423 | void endVisit(AST::TypeAnnotation *) override; |
424 | |
425 | void throwRecursionDepthError() override; |
426 | |
427 | private: |
428 | // attributes |
429 | std::function <void (const QString &)> dumper; |
430 | DumperOptions options = DumperOptions::None; |
431 | int indent = 0; |
432 | int baseIndent = 0; |
433 | bool dumpNode(); |
434 | bool noLocations(); |
435 | bool noAnnotations(); |
436 | }; |
437 | |
438 | QDebug operator<<(QDebug d, AST::Node *n); |
439 | |
440 | std::ostream &operator<<(std::ostream &stream, AST::Node *n); |
441 | |
442 | } // namespace AST |
443 | |
444 | QT_END_NAMESPACE |
445 | |
446 | #endif // ASTDUMPER_H |
447 | |