aboutsummaryrefslogtreecommitdiffstats
path: root/ASTPrinter.h
blob: 15b65e3f0e87d839f66145ed8fbea6a573405f08 (plain) (blame)
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
/*
    This file is part of cpp-ethereum.

    cpp-ethereum is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    cpp-ethereum is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2014
 * Pretty-printer for the abstract syntax tree (the "pretty" is arguable), used for debugging.
 */

#pragma once

#include <ostream>
#include <libsolidity/ASTVisitor.h>

namespace dev
{
namespace solidity
{

/**
 * Pretty-printer for the abstract syntax tree (the "pretty" is arguable) for debugging purposes.
 */
class ASTPrinter: public ASTConstVisitor
{
public:
    /// Create a printer for the given abstract syntax tree. If the source is specified,
    /// the corresponding parts of the source are printed with each node.
    ASTPrinter(ASTNode const& _ast, std::string const& _source = std::string());
    /// Output the string representation of the AST to _stream.
    void print(std::ostream& _stream);

    bool visit(ImportDirective const& _node) override;
    bool visit(ContractDefinition const& _node) override;
    bool visit(StructDefinition const& _node) override;
    bool visit(ParameterList const& _node) override;
    bool visit(FunctionDefinition const& _node) override;
    bool visit(VariableDeclaration const& _node) override;
    bool visit(TypeName const& _node) override;
    bool visit(ElementaryTypeName const& _node) override;
    bool visit(UserDefinedTypeName const& _node) override;
    bool visit(Mapping const& _node) override;
    bool visit(Statement const& _node) override;
    bool visit(Block const& _node) override;
    bool visit(IfStatement const& _node) override;
    bool visit(BreakableStatement const& _node) override;
    bool visit(WhileStatement const& _node) override;
    bool visit(Continue const& _node) override;
    bool visit(Break const& _node) override;
    bool visit(Return const& _node) override;
    bool visit(VariableDefinition const& _node) override;
    bool visit(ExpressionStatement const& _node) override;
    bool visit(Expression const& _node) override;
    bool visit(Assignment const& _node) override;
    bool visit(UnaryOperation const& _node) override;
    bool visit(BinaryOperation const& _node) override;
    bool visit(FunctionCall const& _node) override;
    bool visit(NewExpression const& _node) override;
    bool visit(MemberAccess const& _node) override;
    bool visit(IndexAccess const& _node) override;
    bool visit(PrimaryExpression const& _node) override;
    bool visit(Identifier const& _node) override;
    bool visit(ElementaryTypeNameExpression const& _node) override;
    bool visit(Literal const& _node) override;

    void endVisit(ImportDirective const&) override;
    void endVisit(ContractDefinition const&) override;
    void endVisit(StructDefinition const&) override;
    void endVisit(ParameterList const&) override;
    void endVisit(FunctionDefinition const&) override;
    void endVisit(VariableDeclaration const&) override;
    void endVisit(TypeName const&) override;
    void endVisit(ElementaryTypeName const&) override;
    void endVisit(UserDefinedTypeName const&) override;
    void endVisit(Mapping const&) override;
    void endVisit(Statement const&) override;
    void endVisit(Block const&) override;
    void endVisit(IfStatement const&) override;
    void endVisit(BreakableStatement const&) override;
    void endVisit(WhileStatement const&) override;
    void endVisit(Continue const&) override;
    void endVisit(Break const&) override;
    void endVisit(Return const&) override;
    void endVisit(VariableDefinition const&) override;
    void endVisit(ExpressionStatement const&) override;
    void endVisit(Expression const&) override;
    void endVisit(Assignment const&) override;
    void endVisit(UnaryOperation const&) override;
    void endVisit(BinaryOperation const&) override;
    void endVisit(FunctionCall const&) override;
    void endVisit(NewExpression const&) override;
    void endVisit(MemberAccess const&) override;
    void endVisit(IndexAccess const&) override;
    void endVisit(PrimaryExpression const&) override;
    void endVisit(Identifier const&) override;
    void endVisit(ElementaryTypeNameExpression const&) override;
    void endVisit(Literal const&) override;

private:
    void printSourcePart(ASTNode const& _node);
    void printType(Expression const& _expression);
    std::string getIndentation() const;
    void writeLine(std::string const& _line);
    bool goDeeper() { m_indentation++; return true; }

    int m_indentation;
    std::string m_source;
    ASTNode const* m_ast;
    std::ostream* m_ostream;
};

}
}