More work on the AST export. Work in progress

This commit is contained in:
Lefteris Karapetsas 2015-01-06 16:50:04 +01:00
parent d5b1b4d624
commit bcf49095a2
2 changed files with 136 additions and 96 deletions

View File

@ -31,7 +31,8 @@ namespace solidity
{ {
void ASTJsonConverter::addJsonNode(string const& _typeName, void ASTJsonConverter::addJsonNode(string const& _typeName,
initializer_list<pair<string const, string const>> _list) initializer_list<pair<string const, string const>> _list,
bool _hasChildren = false)
{ {
Json::Value node; Json::Value node;
Json::Value attrs; Json::Value attrs;
@ -41,19 +42,29 @@ void ASTJsonConverter::addJsonNode(string const& _typeName,
attrs[e.first] = e.second; attrs[e.first] = e.second;
node["attributes"] = attrs; node["attributes"] = attrs;
m_childrenPtr->append(node); m_jsonNodePtrs.top()->append(node);
if (_hasChildren) {
Json::Value children(Json::arrayValue);
node["children"] = children;
m_jsonNodePtrs.push(&node["children"]);
m_depth ++;
cout << "goDown" << endl;
}
} }
ASTJsonConverter::ASTJsonConverter(ASTNode const& _ast): m_ast(&_ast) ASTJsonConverter::ASTJsonConverter(ASTNode const& _ast): m_ast(&_ast), m_depth(0)
{ {
Json::Value attrs; Json::Value attrs;
Json::Value children; Json::Value children(Json::arrayValue);
m_astJson["type"] = "root"; m_astJson["type"] = "root";
attrs["name"] = "nameoffile"; //TODO
m_astJson["attributes"] = attrs; m_astJson["attributes"] = attrs;
attrs["name"] = "nameoffile"; //TODO
m_astJson["children"] = children; m_astJson["children"] = children;
m_childrenPtr = &m_astJson["children"]; // m_jsonNodePtrs.push(&m_astJson["children"]);
m_jsonNodePtrs.push(&m_astJson["children"]);
// m_jsonNodePtrs.push(&children);
} }
void ASTJsonConverter::print(ostream& _stream) void ASTJsonConverter::print(ostream& _stream)
@ -66,215 +77,225 @@ void ASTJsonConverter::print(ostream& _stream)
bool ASTJsonConverter::visit(ImportDirective const& _node) bool ASTJsonConverter::visit(ImportDirective const& _node)
{ {
addJsonNode("import", { make_pair("file", _node.getIdentifier())}); addJsonNode("import", { make_pair("file", _node.getIdentifier())});
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(ContractDefinition const& _node) bool ASTJsonConverter::visit(ContractDefinition const& _node)
{ {
// writeLine("ContractDefinition \"" + _node.getName() + "\""); addJsonNode("contract", { make_pair("name", _node.getName())}, true);
// printSourcePart(_node); return true;
addJsonNode("contract", { make_pair("name", _node.getName())});
return goDeeper();
} }
bool ASTJsonConverter::visit(StructDefinition const& _node) bool ASTJsonConverter::visit(StructDefinition const& _node)
{ {
// writeLine("StructDefinition \"" + _node.getName() + "\""); addJsonNode("struct", { make_pair("name", _node.getName())}, true);
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(ParameterList const& _node) bool ASTJsonConverter::visit(ParameterList const& _node)
{ {
// writeLine("ParameterList"); addJsonNode("parameter_list", {}, true);
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(FunctionDefinition const& _node) bool ASTJsonConverter::visit(FunctionDefinition const& _node)
{ {
// writeLine("FunctionDefinition \"" + _node.getName() + "\"" + addJsonNode("function",
// (_node.isPublic() ? " - public" : "") + { make_pair("name", _node.getName()),
// (_node.isDeclaredConst() ? " - const" : "")); make_pair("public", boost::lexical_cast<std::string>(_node.isPublic())),
// printSourcePart(_node); make_pair("const", boost::lexical_cast<std::string>(_node.isDeclaredConst()))},
return goDeeper(); true);
return true;
} }
bool ASTJsonConverter::visit(VariableDeclaration const& _node) bool ASTJsonConverter::visit(VariableDeclaration const& _node)
{ {
// writeLine("VariableDeclaration \"" + _node.getName() + "\""); addJsonNode("variable_declaration",
// printSourcePart(_node); { //make_pair("type", _node.getTypeName()->getName()),
return goDeeper(); make_pair("name", _node.getName()),
make_pair("local", boost::lexical_cast<std::string>(_node.isLocalVariable()))});
return true;
} }
bool ASTJsonConverter::visit(TypeName const& _node) bool ASTJsonConverter::visit(TypeName const& _node)
{ {
// writeLine("TypeName"); // writeLine("TypeName");
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(ElementaryTypeName const& _node) bool ASTJsonConverter::visit(ElementaryTypeName const& _node)
{ {
// writeLine(string("ElementaryTypeName ") + Token::toString(_node.getTypeName())); // writeLine(string("ElementaryTypeName ") + Token::toString(_node.getTypeName()));
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(UserDefinedTypeName const& _node) bool ASTJsonConverter::visit(UserDefinedTypeName const& _node)
{ {
// writeLine("UserDefinedTypeName \"" + _node.getName() + "\""); // writeLine("UserDefinedTypeName \"" + _node.getName() + "\"");
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(Mapping const& _node) bool ASTJsonConverter::visit(Mapping const& _node)
{ {
// writeLine("Mapping"); // writeLine("Mapping");
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(Statement const& _node) bool ASTJsonConverter::visit(Statement const& _node)
{ {
// writeLine("Statement"); addJsonNode("statement", {}, true);
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(Block const& _node) bool ASTJsonConverter::visit(Block const& _node)
{ {
// writeLine("Block"); addJsonNode("block", {}, true);
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(IfStatement const& _node) bool ASTJsonConverter::visit(IfStatement const& _node)
{ {
// writeLine("IfStatement"); addJsonNode("if_statement", {}, true);
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(BreakableStatement const& _node) bool ASTJsonConverter::visit(BreakableStatement const& _node)
{ {
// writeLine("BreakableStatement"); // writeLine("BreakableStatement");
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(WhileStatement const& _node) bool ASTJsonConverter::visit(WhileStatement const& _node)
{ {
// writeLine("WhileStatement"); addJsonNode("while_statement", {}, true);
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(ForStatement const& _node) bool ASTJsonConverter::visit(ForStatement const& _node)
{ {
// writeLine("ForStatement"); addJsonNode("for_statement", {}, true);
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(Continue const& _node) bool ASTJsonConverter::visit(Continue const& _node)
{ {
// writeLine("Continue"); addJsonNode("continue", {});
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(Break const& _node) bool ASTJsonConverter::visit(Break const& _node)
{ {
// writeLine("Break"); addJsonNode("break", {});
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(Return const& _node) bool ASTJsonConverter::visit(Return const& _node)
{ {
// writeLine("Return"); addJsonNode("return", {});;
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(VariableDefinition const& _node) bool ASTJsonConverter::visit(VariableDefinition const& _node)
{ {
// writeLine("VariableDefinition"); addJsonNode("variable_definition", {}, true);
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(ExpressionStatement const& _node) bool ASTJsonConverter::visit(ExpressionStatement const& _node)
{ {
// writeLine("ExpressionStatement"); addJsonNode("expression_statement", {}, true);
// printSourcePart(_node); return true;
return goDeeper();
} }
bool ASTJsonConverter::visit(Expression const& _node) bool ASTJsonConverter::visit(Expression const& _node)
{ {
addJsonNode("expression",
{
make_pair("type", _node.getType()->toString()),
make_pair("lvalue", boost::lexical_cast<std::string>(_node.isLValue())),
make_pair("local_lvalue", boost::lexical_cast<std::string>(_node.isLocalLValue())),
},
true);
// writeLine("Expression"); // writeLine("Expression");
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(Assignment const& _node) bool ASTJsonConverter::visit(Assignment const& _node)
{ {
addJsonNode("assignment", {make_pair("operator", Token::toString(_node.getAssignmentOperator()))}, true);
// writeLine(string("Assignment using operator ") + Token::toString(_node.getAssignmentOperator())); // writeLine(string("Assignment using operator ") + Token::toString(_node.getAssignmentOperator()));
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(UnaryOperation const& _node) bool ASTJsonConverter::visit(UnaryOperation const& _node)
{ {
addJsonNode("unary_op",
{make_pair("prefix", boost::lexical_cast<std::string>(_node.isPrefixOperation())),
make_pair("operator", Token::toString(_node.getOperator()))},
true);
// writeLine(string("UnaryOperation (") + (_node.isPrefixOperation() ? "prefix" : "postfix") + // writeLine(string("UnaryOperation (") + (_node.isPrefixOperation() ? "prefix" : "postfix") +
// ") " + Token::toString(_node.getOperator())); // ") " + Token::toString(_node.getOperator()));
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(BinaryOperation const& _node) bool ASTJsonConverter::visit(BinaryOperation const& _node)
{ {
addJsonNode("binary_op",
{make_pair("operator", Token::toString(_node.getOperator()))},
true);
// writeLine(string("BinaryOperation using operator ") + Token::toString(_node.getOperator())); // writeLine(string("BinaryOperation using operator ") + Token::toString(_node.getOperator()));
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(FunctionCall const& _node) bool ASTJsonConverter::visit(FunctionCall const& _node)
{ {
addJsonNode("function_call",
{make_pair("type_conversion", boost::lexical_cast<std::string>(_node.isTypeConversion()))},
true);
// writeLine("FunctionCall"); // writeLine("FunctionCall");
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(NewExpression const& _node) bool ASTJsonConverter::visit(NewExpression const& _node)
{ {
addJsonNode("new_expression", {}, true);
// writeLine("NewExpression"); // writeLine("NewExpression");
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(MemberAccess const& _node) bool ASTJsonConverter::visit(MemberAccess const& _node)
{ {
addJsonNode("member_access", {make_pair("member_name", _node.getMemberName())}, true);
// writeLine("MemberAccess to member " + _node.getMemberName()); // writeLine("MemberAccess to member " + _node.getMemberName());
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(IndexAccess const& _node) bool ASTJsonConverter::visit(IndexAccess const& _node)
{ {
// writeLine("IndexAccess"); addJsonNode("index_access", {}, true);
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(PrimaryExpression const& _node) bool ASTJsonConverter::visit(PrimaryExpression const& _node)
@ -282,34 +303,43 @@ bool ASTJsonConverter::visit(PrimaryExpression const& _node)
// writeLine("PrimaryExpression"); // writeLine("PrimaryExpression");
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(Identifier const& _node) bool ASTJsonConverter::visit(Identifier const& _node)
{ {
addJsonNode("identifier", {make_pair("value", _node.getName())});
// writeLine(string("Identifier ") + _node.getName()); // writeLine(string("Identifier ") + _node.getName());
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(ElementaryTypeNameExpression const& _node) bool ASTJsonConverter::visit(ElementaryTypeNameExpression const& _node)
{ {
addJsonNode("elementary_typename_expression",
{make_pair("value", Token::toString(_node.getTypeToken()))});
// writeLine(string("ElementaryTypeNameExpression ") + Token::toString(_node.getTypeToken())); // writeLine(string("ElementaryTypeNameExpression ") + Token::toString(_node.getTypeToken()));
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
bool ASTJsonConverter::visit(Literal const& _node) bool ASTJsonConverter::visit(Literal const& _node)
{ {
char const* tokenString = Token::toString(_node.getToken());
addJsonNode("literal",
{
make_pair("string", (tokenString) ? tokenString : "null"),
make_pair("value", _node.getValue())});
// char const* tokenString = Token::toString(_node.getToken()); // char const* tokenString = Token::toString(_node.getToken());
// if (!tokenString) // if (!tokenString)
// tokenString = "[no token]"; // tokenString = "[no token]";
// writeLine(string("Literal, token: ") + tokenString + " value: " + _node.getValue()); // writeLine(string("Literal, token: ") + tokenString + " value: " + _node.getValue());
// printType(_node); // printType(_node);
// printSourcePart(_node); // printSourcePart(_node);
return goDeeper(); return true;
} }
void ASTJsonConverter::endVisit(ImportDirective const&) void ASTJsonConverter::endVisit(ImportDirective const&)
@ -319,27 +349,26 @@ void ASTJsonConverter::endVisit(ImportDirective const&)
void ASTJsonConverter::endVisit(ContractDefinition const&) void ASTJsonConverter::endVisit(ContractDefinition const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(StructDefinition const&) void ASTJsonConverter::endVisit(StructDefinition const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(ParameterList const&) void ASTJsonConverter::endVisit(ParameterList const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(FunctionDefinition const&) void ASTJsonConverter::endVisit(FunctionDefinition const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(VariableDeclaration const&) void ASTJsonConverter::endVisit(VariableDeclaration const&)
{ {
} }
void ASTJsonConverter::endVisit(TypeName const&) void ASTJsonConverter::endVisit(TypeName const&)
@ -364,17 +393,17 @@ void ASTJsonConverter::endVisit(Mapping const&)
void ASTJsonConverter::endVisit(Statement const&) void ASTJsonConverter::endVisit(Statement const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(Block const&) void ASTJsonConverter::endVisit(Block const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(IfStatement const&) void ASTJsonConverter::endVisit(IfStatement const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(BreakableStatement const&) void ASTJsonConverter::endVisit(BreakableStatement const&)
@ -384,12 +413,12 @@ void ASTJsonConverter::endVisit(BreakableStatement const&)
void ASTJsonConverter::endVisit(WhileStatement const&) void ASTJsonConverter::endVisit(WhileStatement const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(ForStatement const&) void ASTJsonConverter::endVisit(ForStatement const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(Continue const&) void ASTJsonConverter::endVisit(Continue const&)
@ -409,52 +438,52 @@ void ASTJsonConverter::endVisit(Return const&)
void ASTJsonConverter::endVisit(VariableDefinition const&) void ASTJsonConverter::endVisit(VariableDefinition const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(ExpressionStatement const&) void ASTJsonConverter::endVisit(ExpressionStatement const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(Expression const&) void ASTJsonConverter::endVisit(Expression const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(Assignment const&) void ASTJsonConverter::endVisit(Assignment const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(UnaryOperation const&) void ASTJsonConverter::endVisit(UnaryOperation const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(BinaryOperation const&) void ASTJsonConverter::endVisit(BinaryOperation const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(FunctionCall const&) void ASTJsonConverter::endVisit(FunctionCall const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(NewExpression const&) void ASTJsonConverter::endVisit(NewExpression const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(MemberAccess const&) void ASTJsonConverter::endVisit(MemberAccess const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(IndexAccess const&) void ASTJsonConverter::endVisit(IndexAccess const&)
{ {
goUp();
} }
void ASTJsonConverter::endVisit(PrimaryExpression const&) void ASTJsonConverter::endVisit(PrimaryExpression const&)

View File

@ -23,7 +23,9 @@
#pragma once #pragma once
#include <ostream> #include <ostream>
#include <stack>
#include <libsolidity/ASTVisitor.h> #include <libsolidity/ASTVisitor.h>
#include <libsolidity/Exceptions.h>
#include <jsoncpp/json/json.h> #include <jsoncpp/json/json.h>
namespace dev namespace dev
@ -113,14 +115,23 @@ public:
private: private:
void addJsonNode(std::string const& _typeName, void addJsonNode(std::string const& _typeName,
std::initializer_list<std::pair<std::string const, std::string const>> _list); std::initializer_list<std::pair<std::string const, std::string const>> _list,
bool _hasChildren);
void printType(Expression const& _expression); void printType(Expression const& _expression);
bool goDeeper() { return true; } inline void goUp()
{
std::cout << "goUp" << std::endl;
m_jsonNodePtrs.pop();
m_depth--;
if (m_depth < 0)
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Internal error"));
};
Json::Value m_astJson; Json::Value m_astJson;
Json::Value *m_childrenPtr; std::stack<Json::Value *> m_jsonNodePtrs;
std::string m_source; std::string m_source;
ASTNode const* m_ast; ASTNode const* m_ast;
int m_depth;
}; };
} }