Preparing the ground for AST outputing to JSON

This commit is contained in:
Lefteris Karapetsas 2015-01-05 15:46:40 +01:00
parent 245fba5055
commit d0f02c28c6
2 changed files with 55 additions and 10 deletions

View File

@ -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)
{
// do we need AST output?
if (m_args.count(g_argAstStr))
std::string title;
std::string suffix;
if (_argStr == g_argAstStr)
{
auto choice = m_args[g_argAstStr].as<OutputType>();
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(_argStr))
{
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)

View File

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