1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#ifndef DUMPAST_H
30#define DUMPAST_H
31
32#include <QtQml/private/qqmljsastvisitor_p.h>
33#include <QtQml/private/qqmljsast_p.h>
34
35#include <QHash>
36#include <QStack>
37
38#include "commentastvisitor.h"
39
40using namespace QQmlJS::AST;
41using namespace QQmlJS;
42
43class DumpAstVisitor : protected Visitor
44{
45public:
46 DumpAstVisitor(QQmlJS::Engine *engine, Node *rootNode, CommentAstVisitor *comment);
47
48 QString toString() const { return m_result; }
49
50 bool preVisit(Node *) override;
51
52 bool visit(UiScriptBinding *node) override;
53
54 bool visit(UiArrayBinding *node) override;
55 void endVisit(UiArrayBinding *node) override;
56
57 bool visit(UiObjectBinding *node) override;
58 void endVisit(UiObjectBinding *node) override;
59
60 bool visit(FunctionDeclaration *node) override;
61 void endVisit(FunctionDeclaration *node) override;
62
63 bool visit(UiInlineComponent *node) override;
64
65 bool visit(UiObjectDefinition *node) override;
66 void endVisit(UiObjectDefinition *node) override;
67
68 bool visit(UiEnumDeclaration *node) override;
69 void endVisit(UiEnumDeclaration *node) override;
70
71 bool visit(UiEnumMemberList *node) override;
72 bool visit(UiPublicMember *node) override;
73 bool visit(UiImport *node) override;
74 bool visit(UiPragma *node) override;
75
76 bool visit(UiAnnotation *node) override;
77 void endVisit(UiAnnotation *node) override;
78
79 void throwRecursionDepthError() override {}
80
81 bool error() const { return m_error; }
82private:
83 struct ScopeProperties {
84 bool m_firstOfAll = true;
85 bool m_firstSignal = true;
86 bool m_firstProperty = true;
87 bool m_firstBinding = true;
88 bool m_firstObject = true;
89 bool m_firstFunction = true;
90 bool m_inArrayBinding = false;
91 bool m_pendingBinding = false;
92
93 UiObjectMember* m_lastInArrayBinding = nullptr;
94 QHash<QString, UiObjectMember*> m_bindings;
95 };
96
97 QString generateIndent() const;
98 QString formatLine(QString line, bool newline = true) const;
99
100 QString formatComment(const Comment &comment) const;
101
102 QString getComment(Node *node, Comment::Location location) const;
103 QString getListItemComment(SourceLocation srcLocation, Comment::Location location) const;
104
105 void addNewLine(bool always = false);
106 void addLine(QString line);
107
108 QString getOrphanedComments(Node *node) const;
109
110 QString parseStatement(Statement *statement, bool blockHasNext = false,
111 bool blockAllowBraceless = false);
112 QString parseStatementList(StatementList *list);
113
114 QString parseExpression(ExpressionNode *expression);
115
116 QString parsePatternElement(PatternElement *element, bool scope = true);
117 QString parsePatternElementList(PatternElementList *element);
118
119 QString parsePatternProperty(PatternProperty *property);
120 QString parsePatternPropertyList(PatternPropertyList *list);
121
122 QString parseArgumentList(ArgumentList *list);
123
124 QString parseUiParameterList(UiParameterList *list);
125
126 QString parseVariableDeclarationList(VariableDeclarationList *list);
127
128 QString parseCaseBlock(CaseBlock *block);
129 QString parseBlock(Block *block, bool hasNext, bool allowBraceless);
130
131 QString parseExportsList(ExportsList *list);
132 QString parseExportSpecifier(ExportSpecifier *specifier);
133
134 QString parseFormalParameterList(FormalParameterList *list);
135
136 QString parseType(Type *type);
137
138 QString parseFunctionExpression(FunctionExpression *expression, bool omitFunction = false);
139
140 ScopeProperties& scope() { return m_scope_properties.top(); }
141
142 int m_indentLevel = 0;
143
144 bool m_error = false;
145 bool m_blockNeededBraces = false;
146
147 QStack<ScopeProperties> m_scope_properties;
148
149 QString m_result = "";
150 QString m_component_name = "";
151 QQmlJS::Engine *m_engine;
152 CommentAstVisitor *m_comment;
153};
154
155#endif // DUMPAST_H
156

source code of qtdeclarative/tools/qmlformat/dumpastvisitor.h