Preparing the ground for AST outputing to JSON

This commit is contained in:
Lefteris Karapetsas 2015-01-05 15:46:40 +01:00
parent 1515f140b3
commit d5b1b4d624
2 changed files with 617 additions and 0 deletions

490
ASTJsonConverter.cpp Normal file
View File

@ -0,0 +1,490 @@
/*
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 Lefteris <lefteris@ethdev.com>
* @date 2015
* Converts the AST into json format
*/
#include <libsolidity/ASTJsonConverter.h>
#include <libsolidity/AST.h>
using namespace std;
namespace dev
{
namespace solidity
{
void ASTJsonConverter::addJsonNode(string const& _typeName,
initializer_list<pair<string const, string const>> _list)
{
Json::Value node;
Json::Value attrs;
node["type"] = _typeName;
for (auto &e: _list)
attrs[e.first] = e.second;
node["attributes"] = attrs;
m_childrenPtr->append(node);
}
ASTJsonConverter::ASTJsonConverter(ASTNode const& _ast): m_ast(&_ast)
{
Json::Value attrs;
Json::Value children;
m_astJson["type"] = "root";
attrs["name"] = "nameoffile"; //TODO
m_astJson["attributes"] = attrs;
m_astJson["children"] = children;
m_childrenPtr = &m_astJson["children"];
}
void ASTJsonConverter::print(ostream& _stream)
{
m_ast->accept(*this);
_stream << m_astJson;
}
bool ASTJsonConverter::visit(ImportDirective const& _node)
{
addJsonNode("import", { make_pair("file", _node.getIdentifier())});
return goDeeper();
}
bool ASTJsonConverter::visit(ContractDefinition const& _node)
{
// writeLine("ContractDefinition \"" + _node.getName() + "\"");
// printSourcePart(_node);
addJsonNode("contract", { make_pair("name", _node.getName())});
return goDeeper();
}
bool ASTJsonConverter::visit(StructDefinition const& _node)
{
// writeLine("StructDefinition \"" + _node.getName() + "\"");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(ParameterList const& _node)
{
// writeLine("ParameterList");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(FunctionDefinition const& _node)
{
// writeLine("FunctionDefinition \"" + _node.getName() + "\"" +
// (_node.isPublic() ? " - public" : "") +
// (_node.isDeclaredConst() ? " - const" : ""));
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(VariableDeclaration const& _node)
{
// writeLine("VariableDeclaration \"" + _node.getName() + "\"");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(TypeName const& _node)
{
// writeLine("TypeName");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(ElementaryTypeName const& _node)
{
// writeLine(string("ElementaryTypeName ") + Token::toString(_node.getTypeName()));
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(UserDefinedTypeName const& _node)
{
// writeLine("UserDefinedTypeName \"" + _node.getName() + "\"");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(Mapping const& _node)
{
// writeLine("Mapping");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(Statement const& _node)
{
// writeLine("Statement");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(Block const& _node)
{
// writeLine("Block");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(IfStatement const& _node)
{
// writeLine("IfStatement");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(BreakableStatement const& _node)
{
// writeLine("BreakableStatement");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(WhileStatement const& _node)
{
// writeLine("WhileStatement");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(ForStatement const& _node)
{
// writeLine("ForStatement");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(Continue const& _node)
{
// writeLine("Continue");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(Break const& _node)
{
// writeLine("Break");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(Return const& _node)
{
// writeLine("Return");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(VariableDefinition const& _node)
{
// writeLine("VariableDefinition");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(ExpressionStatement const& _node)
{
// writeLine("ExpressionStatement");
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(Expression const& _node)
{
// writeLine("Expression");
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(Assignment const& _node)
{
// writeLine(string("Assignment using operator ") + Token::toString(_node.getAssignmentOperator()));
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(UnaryOperation const& _node)
{
// writeLine(string("UnaryOperation (") + (_node.isPrefixOperation() ? "prefix" : "postfix") +
// ") " + Token::toString(_node.getOperator()));
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(BinaryOperation const& _node)
{
// writeLine(string("BinaryOperation using operator ") + Token::toString(_node.getOperator()));
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(FunctionCall const& _node)
{
// writeLine("FunctionCall");
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(NewExpression const& _node)
{
// writeLine("NewExpression");
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(MemberAccess const& _node)
{
// writeLine("MemberAccess to member " + _node.getMemberName());
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(IndexAccess const& _node)
{
// writeLine("IndexAccess");
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(PrimaryExpression const& _node)
{
// writeLine("PrimaryExpression");
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(Identifier const& _node)
{
// writeLine(string("Identifier ") + _node.getName());
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(ElementaryTypeNameExpression const& _node)
{
// writeLine(string("ElementaryTypeNameExpression ") + Token::toString(_node.getTypeToken()));
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
bool ASTJsonConverter::visit(Literal const& _node)
{
// char const* tokenString = Token::toString(_node.getToken());
// if (!tokenString)
// tokenString = "[no token]";
// writeLine(string("Literal, token: ") + tokenString + " value: " + _node.getValue());
// printType(_node);
// printSourcePart(_node);
return goDeeper();
}
void ASTJsonConverter::endVisit(ImportDirective const&)
{
}
void ASTJsonConverter::endVisit(ContractDefinition const&)
{
}
void ASTJsonConverter::endVisit(StructDefinition const&)
{
}
void ASTJsonConverter::endVisit(ParameterList const&)
{
}
void ASTJsonConverter::endVisit(FunctionDefinition const&)
{
}
void ASTJsonConverter::endVisit(VariableDeclaration const&)
{
}
void ASTJsonConverter::endVisit(TypeName const&)
{
}
void ASTJsonConverter::endVisit(ElementaryTypeName const&)
{
}
void ASTJsonConverter::endVisit(UserDefinedTypeName const&)
{
}
void ASTJsonConverter::endVisit(Mapping const&)
{
}
void ASTJsonConverter::endVisit(Statement const&)
{
}
void ASTJsonConverter::endVisit(Block const&)
{
}
void ASTJsonConverter::endVisit(IfStatement const&)
{
}
void ASTJsonConverter::endVisit(BreakableStatement const&)
{
}
void ASTJsonConverter::endVisit(WhileStatement const&)
{
}
void ASTJsonConverter::endVisit(ForStatement const&)
{
}
void ASTJsonConverter::endVisit(Continue const&)
{
}
void ASTJsonConverter::endVisit(Break const&)
{
}
void ASTJsonConverter::endVisit(Return const&)
{
}
void ASTJsonConverter::endVisit(VariableDefinition const&)
{
}
void ASTJsonConverter::endVisit(ExpressionStatement const&)
{
}
void ASTJsonConverter::endVisit(Expression const&)
{
}
void ASTJsonConverter::endVisit(Assignment const&)
{
}
void ASTJsonConverter::endVisit(UnaryOperation const&)
{
}
void ASTJsonConverter::endVisit(BinaryOperation const&)
{
}
void ASTJsonConverter::endVisit(FunctionCall const&)
{
}
void ASTJsonConverter::endVisit(NewExpression const&)
{
}
void ASTJsonConverter::endVisit(MemberAccess const&)
{
}
void ASTJsonConverter::endVisit(IndexAccess const&)
{
}
void ASTJsonConverter::endVisit(PrimaryExpression const&)
{
}
void ASTJsonConverter::endVisit(Identifier const&)
{
}
void ASTJsonConverter::endVisit(ElementaryTypeNameExpression const&)
{
}
void ASTJsonConverter::endVisit(Literal const&)
{
}
void ASTJsonConverter::printType(Expression const& _expression)
{
// if (_expression.getType())
// *m_ostream << getIndentation() << " Type: " << _expression.getType()->toString() << "\n";
// else
// *m_ostream << getIndentation() << " Type unknown.\n";
}
}
}

127
ASTJsonConverter.h Normal file
View File

@ -0,0 +1,127 @@
/*
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 Lefteris <lefteris@ethdev.com>
* @date 2015
* Converts the AST into json format
*/
#pragma once
#include <ostream>
#include <libsolidity/ASTVisitor.h>
#include <jsoncpp/json/json.h>
namespace dev
{
namespace solidity
{
/**
* Converter of the AST into JSON format
*/
class ASTJsonConverter: public ASTConstVisitor
{
public:
/// Create a converter for the given abstract syntax tree. If the source is specified,
/// the corresponding parts of the source are printed with each node.
ASTJsonConverter(ASTNode const& _ast);
/// Output the json 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(ForStatement 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(ForStatement 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 addJsonNode(std::string const& _typeName,
std::initializer_list<std::pair<std::string const, std::string const>> _list);
void printType(Expression const& _expression);
bool goDeeper() { return true; }
Json::Value m_astJson;
Json::Value *m_childrenPtr;
std::string m_source;
ASTNode const* m_ast;
};
}
}