aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmlformat/dumpastvisitor.h
blob: e56e55615a62ca2601dd92a09a10ab2239732a28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef DUMPAST_H
#define DUMPAST_H

#include <QtQml/private/qqmljsastvisitor_p.h>
#include <QtQml/private/qqmljsast_p.h>

#include <QHash>
#include <QStack>

#include "commentastvisitor.h"

using namespace QQmlJS::AST;
using namespace QQmlJS;

class DumpAstVisitor : protected Visitor
{
public:
    DumpAstVisitor(QQmlJS::Engine *engine, Node *rootNode, CommentAstVisitor *comment);

    QString toString() const { return m_result; }

    bool preVisit(Node *) override;

    bool visit(UiScriptBinding *node) override;

    bool visit(UiArrayBinding *node) override;
    void endVisit(UiArrayBinding *node) override;

    bool visit(UiObjectBinding *node) override;
    void endVisit(UiObjectBinding *node) override;

    bool visit(FunctionDeclaration *node) override;
    void endVisit(FunctionDeclaration *node) override;

    bool visit(UiInlineComponent *node) override;

    bool visit(UiObjectDefinition *node) override;
    void endVisit(UiObjectDefinition *node) override;

    bool visit(UiEnumDeclaration *node) override;
    void endVisit(UiEnumDeclaration *node) override;

    bool visit(UiEnumMemberList *node) override;
    bool visit(UiPublicMember *node) override;
    bool visit(UiImport *node) override;
    bool visit(UiPragma *node) override;

    bool visit(UiAnnotation *node) override;
    void endVisit(UiAnnotation *node) override;

    void throwRecursionDepthError() override {}

    bool error() const { return m_error; }
private:
    struct ScopeProperties {
        bool m_firstOfAll = true;
        bool m_firstSignal = true;
        bool m_firstProperty = true;
        bool m_firstBinding = true;
        bool m_firstObject = true;
        bool m_firstFunction = true;
        bool m_inArrayBinding = false;
        bool m_pendingBinding = false;

        UiObjectMember* m_lastInArrayBinding = nullptr;
        QHash<QString, UiObjectMember*> m_bindings;
    };

    QString generateIndent() const;
    QString formatLine(QString line, bool newline = true) const;

    QString formatComment(const Comment &comment) const;

    QString getComment(Node *node, Comment::Location location) const;
    QString getListItemComment(SourceLocation srcLocation, Comment::Location location) const;

    void addNewLine(bool always = false);
    void addLine(QString line);

    QString getOrphanedComments(Node *node) const;

    QString parseStatement(Statement *statement, bool blockHasNext = false,
                           bool blockAllowBraceless = false);
    QString parseStatementList(StatementList *list);

    QString parseExpression(ExpressionNode *expression);

    QString parsePatternElement(PatternElement *element, bool scope = true);
    QString parsePatternElementList(PatternElementList *element);

    QString parsePatternProperty(PatternProperty *property);
    QString parsePatternPropertyList(PatternPropertyList *list);

    QString parseArgumentList(ArgumentList *list);

    QString parseUiParameterList(UiParameterList *list);

    QString parseVariableDeclarationList(VariableDeclarationList *list);

    QString parseCaseBlock(CaseBlock *block);
    QString parseBlock(Block *block, bool hasNext, bool allowBraceless);

    QString parseExportsList(ExportsList *list);
    QString parseExportSpecifier(ExportSpecifier *specifier);

    QString parseFormalParameterList(FormalParameterList *list);

    QString parseType(Type *type);

    QString parseFunctionExpression(FunctionExpression *expression, bool omitFunction = false);

    ScopeProperties& scope() { return m_scope_properties.top(); }

    int m_indentLevel = 0;

    bool m_error = false;
    bool m_blockNeededBraces = false;

    QStack<ScopeProperties> m_scope_properties;

    QString m_result = "";
    QString m_component_name = "";
    QQmlJS::Engine *m_engine;
    CommentAstVisitor *m_comment;
};

#endif // DUMPAST_H