Browse Source

Preparing the ground for AST outputing to JSON

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
ddd1438a2c
  1. 490
      libsolidity/ASTJsonConverter.cpp
  2. 127
      libsolidity/ASTJsonConverter.h
  3. 62
      solc/CommandLineInterface.cpp
  4. 1
      solc/CommandLineInterface.h

490
libsolidity/ASTJsonConverter.cpp

@ -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
libsolidity/ASTJsonConverter.h

@ -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;
};
}
}

62
solc/CommandLineInterface.cpp

@ -36,6 +36,7 @@
#include <libsolidity/Scanner.h>
#include <libsolidity/Parser.h>
#include <libsolidity/ASTPrinter.h>
#include <libsolidity/ASTJsonConverter.h>
#include <libsolidity/NameAndTypeResolver.h>
#include <libsolidity/Exceptions.h>
#include <libsolidity/CompilerStack.h>
@ -55,6 +56,7 @@ static string const g_argAbiStr = "json-abi";
static string const g_argSolAbiStr = "sol-abi";
static string const g_argAsmStr = "asm";
static string const g_argAstStr = "ast";
static string const g_argAstJson = "ast-json";
static string const g_argBinaryStr = "binary";
static string const g_argOpcodesStr = "opcodes";
static string const g_argNatspecDevStr = "natspec-dev";
@ -75,9 +77,10 @@ static inline bool argToStdout(po::variables_map const& _args, string const& _na
static bool needStdout(po::variables_map const& _args)
{
return
argToStdout(_args, g_argAbiStr) || argToStdout(_args, g_argSolAbiStr) ||
argToStdout(_args, g_argNatspecUserStr) ||
argToStdout(_args, g_argNatspecUserStr) || argToStdout(_args, g_argAstJson) ||
argToStdout(_args, g_argNatspecDevStr) || argToStdout(_args, g_argAsmStr) ||
argToStdout(_args, g_argOpcodesStr) || argToStdout(_args, g_argBinaryStr);
}
@ -215,6 +218,8 @@ bool CommandLineInterface::parseArguments(int argc, char** argv)
("input-file", po::value<vector<string>>(), "input file")
(g_argAstStr.c_str(), po::value<OutputType>(),
"Request to output the AST of the contract. " OUTPUT_TYPE_STR)
(g_argAstJson.c_str(), po::value<OutputType>(),
"Request to output the AST of the contract in JSON format. " OUTPUT_TYPE_STR)
(g_argAsmStr.c_str(), po::value<OutputType>(),
"Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR)
(g_argOpcodesStr.c_str(), po::value<OutputType>(),
@ -339,20 +344,44 @@ bool CommandLineInterface::processInput()
return true;
}
void CommandLineInterface::actOnInput()
void CommandLineInterface::handleAst(std::string const& _argStr)
{
std::string title;
std::string suffix;
if (_argStr == g_argAstStr)
{
title = "Syntax trees:";
suffix = ".ast";
}
else if (_argStr == g_argAstJson)
{
title = "JSON AST:";
suffix = ".json";
}
else
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Illegal argStr for AST"));
// do we need AST output?
if (m_args.count(g_argAstStr))
if (m_args.count(_argStr))
{
auto choice = m_args[g_argAstStr].as<OutputType>();
auto choice = m_args[_argStr].as<OutputType>();
if (outputToStdout(choice))
{
cout << "Syntax trees:" << endl << endl;
cout << title << endl << endl;
for (auto const& sourceCode: m_sourceCodes)
{
cout << endl << "======= " << sourceCode.first << " =======" << endl;
ASTPrinter printer(m_compiler.getAST(sourceCode.first), sourceCode.second);
printer.print(cout);
if (_argStr == g_argAstStr)
{
ASTPrinter printer(m_compiler.getAST(sourceCode.first), sourceCode.second);
printer.print(cout);
}
else
{
ASTJsonConverter converter(m_compiler.getAST(sourceCode.first));
converter.print(cout);
}
}
}
@ -362,12 +391,27 @@ void CommandLineInterface::actOnInput()
{
boost::filesystem::path p(sourceCode.first);
ofstream outFile(p.stem().string() + ".ast");
ASTPrinter printer(m_compiler.getAST(sourceCode.first), sourceCode.second);
printer.print(outFile);
if (_argStr == g_argAstStr)
{
ASTPrinter printer(m_compiler.getAST(sourceCode.first), sourceCode.second);
printer.print(outFile);
}
else
{
ASTJsonConverter converter(m_compiler.getAST(sourceCode.first));
converter.print(outFile);
}
outFile.close();
}
}
}
}
void CommandLineInterface::actOnInput()
{
// do we need AST output?
handleAst(g_argAstStr);
handleAst(g_argAstJson);
vector<string> contracts = m_compiler.getContractNames();
for (string const& contract: contracts)

1
solc/CommandLineInterface.h

@ -53,6 +53,7 @@ public:
void actOnInput();
private:
void handleAst(std::string const& _argStr);
void handleBinary(std::string const& _contract);
void handleOpcode(std::string const& _contract);
void handleBytecode(std::string const& _contract);

Loading…
Cancel
Save