Merge pull request #919 from asinyagin/develop

Add the missing AST-JSON converters
This commit is contained in:
chriseth 2016-08-19 12:55:24 +02:00 committed by GitHub
commit 64cec9793a
3 changed files with 275 additions and 14 deletions

View File

@ -89,11 +89,6 @@ ASTJsonConverter::ASTJsonConverter(
map<string, unsigned> _sourceIndices
): m_ast(&_ast), m_sourceIndices(_sourceIndices)
{
Json::Value children(Json::arrayValue);
m_astJson["name"] = "root";
m_astJson["children"] = children;
m_jsonNodePtrs.push(&m_astJson["children"]);
}
void ASTJsonConverter::print(ostream& _stream)
@ -108,21 +103,56 @@ Json::Value const& ASTJsonConverter::json()
return m_astJson;
}
bool ASTJsonConverter::visit(SourceUnit const&)
{
Json::Value children(Json::arrayValue);
m_astJson["name"] = "SourceUnit";
m_astJson["children"] = children;
m_jsonNodePtrs.push(&m_astJson["children"]);
return true;
}
bool ASTJsonConverter::visit(ImportDirective const& _node)
{
addJsonNode(_node, "Import", { make_pair("file", _node.path())});
addJsonNode(_node, "ImportDirective", { make_pair("file", _node.path())});
return true;
}
bool ASTJsonConverter::visit(ContractDefinition const& _node)
{
addJsonNode(_node, "Contract", { make_pair("name", _node.name()) }, true);
addJsonNode(_node, "ContractDefinition", { make_pair("name", _node.name()) }, true);
return true;
}
bool ASTJsonConverter::visit(InheritanceSpecifier const& _node)
{
addJsonNode(_node, "InheritanceSpecifier", {}, true);
return true;
}
bool ASTJsonConverter::visit(UsingForDirective const& _node)
{
addJsonNode(_node, "UsingForDirective", {}, true);
return true;
}
bool ASTJsonConverter::visit(StructDefinition const& _node)
{
addJsonNode(_node, "Struct", { make_pair("name", _node.name()) }, true);
addJsonNode(_node, "StructDefinition", { make_pair("name", _node.name()) }, true);
return true;
}
bool ASTJsonConverter::visit(EnumDefinition const& _node)
{
addJsonNode(_node, "EnumDefinition", { make_pair("name", _node.name()) }, true);
return true;
}
bool ASTJsonConverter::visit(EnumValue const& _node)
{
addJsonNode(_node, "EnumValue", { make_pair("name", _node.name()) });
return true;
}
@ -134,7 +164,7 @@ bool ASTJsonConverter::visit(ParameterList const& _node)
bool ASTJsonConverter::visit(FunctionDefinition const& _node)
{
addJsonNode(_node, "Function",
addJsonNode(_node, "FunctionDefinition",
{ make_pair("name", _node.name()),
make_pair("public", boost::lexical_cast<std::string>(_node.isPublic())),
make_pair("const", boost::lexical_cast<std::string>(_node.isDeclaredConst())) },
@ -151,11 +181,29 @@ bool ASTJsonConverter::visit(VariableDeclaration const& _node)
return true;
}
bool ASTJsonConverter::visit(ModifierDefinition const& _node)
{
addJsonNode(_node, "ModifierDefinition", { make_pair("name", _node.name()) }, true);
return true;
}
bool ASTJsonConverter::visit(ModifierInvocation const& _node)
{
addJsonNode(_node, "ModifierInvocation", {}, true);
return true;
}
bool ASTJsonConverter::visit(TypeName const&)
{
return true;
}
bool ASTJsonConverter::visit(EventDefinition const& _node)
{
addJsonNode(_node, "EventDefinition", { make_pair("name", _node.name()) }, true);
return true;
}
bool ASTJsonConverter::visit(ElementaryTypeName const& _node)
{
addJsonNode(_node, "ElementaryTypeName", { make_pair("name", _node.typeName().toString()) });
@ -176,6 +224,12 @@ bool ASTJsonConverter::visit(Mapping const& _node)
return true;
}
bool ASTJsonConverter::visit(ArrayTypeName const& _node)
{
addJsonNode(_node, "ArrayTypeName", {}, true);
return true;
}
bool ASTJsonConverter::visit(InlineAssembly const& _node)
{
addJsonNode(_node, "InlineAssembly", {}, true);
@ -188,6 +242,12 @@ bool ASTJsonConverter::visit(Block const& _node)
return true;
}
bool ASTJsonConverter::visit(PlaceholderStatement const& _node)
{
addJsonNode(_node, "PlaceholderStatement", {});
return true;
}
bool ASTJsonConverter::visit(IfStatement const& _node)
{
addJsonNode(_node, "IfStatement", {}, true);
@ -232,7 +292,7 @@ bool ASTJsonConverter::visit(Throw const& _node)
bool ASTJsonConverter::visit(VariableDeclarationStatement const& _node)
{
addJsonNode(_node, "VariableDefinition", {}, true);
addJsonNode(_node, "VariableDefinitionStatement", {}, true);
return true;
}
@ -336,6 +396,11 @@ bool ASTJsonConverter::visit(Literal const& _node)
return true;
}
void ASTJsonConverter::endVisit(SourceUnit const&)
{
goUp();
}
void ASTJsonConverter::endVisit(ImportDirective const&)
{
}
@ -345,11 +410,30 @@ void ASTJsonConverter::endVisit(ContractDefinition const&)
goUp();
}
void ASTJsonConverter::endVisit(InheritanceSpecifier const&)
{
goUp();
}
void ASTJsonConverter::endVisit(UsingForDirective const&)
{
goUp();
}
void ASTJsonConverter::endVisit(StructDefinition const&)
{
goUp();
}
void ASTJsonConverter::endVisit(EnumDefinition const&)
{
goUp();
}
void ASTJsonConverter::endVisit(EnumValue const&)
{
}
void ASTJsonConverter::endVisit(ParameterList const&)
{
goUp();
@ -365,6 +449,21 @@ void ASTJsonConverter::endVisit(VariableDeclaration const&)
goUp();
}
void ASTJsonConverter::endVisit(ModifierDefinition const&)
{
goUp();
}
void ASTJsonConverter::endVisit(ModifierInvocation const&)
{
goUp();
}
void ASTJsonConverter::endVisit(EventDefinition const&)
{
goUp();
}
void ASTJsonConverter::endVisit(TypeName const&)
{
}
@ -382,6 +481,11 @@ void ASTJsonConverter::endVisit(Mapping const&)
goUp();
}
void ASTJsonConverter::endVisit(ArrayTypeName const&)
{
goUp();
}
void ASTJsonConverter::endVisit(InlineAssembly const&)
{
goUp();
@ -392,6 +496,10 @@ void ASTJsonConverter::endVisit(Block const&)
goUp();
}
void ASTJsonConverter::endVisit(PlaceholderStatement const&)
{
}
void ASTJsonConverter::endVisit(IfStatement const&)
{
goUp();

View File

@ -51,18 +51,28 @@ public:
void print(std::ostream& _stream);
Json::Value const& json();
bool visit(SourceUnit const& _node) override;
bool visit(ImportDirective const& _node) override;
bool visit(ContractDefinition const& _node) override;
bool visit(InheritanceSpecifier const& _node) override;
bool visit(UsingForDirective const& _node) override;
bool visit(StructDefinition const& _node) override;
bool visit(EnumDefinition const& _node) override;
bool visit(EnumValue const& _node) override;
bool visit(ParameterList const& _node) override;
bool visit(FunctionDefinition const& _node) override;
bool visit(VariableDeclaration const& _node) override;
bool visit(ModifierDefinition const& _node) override;
bool visit(ModifierInvocation const& _node) override;
bool visit(EventDefinition 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(ArrayTypeName const& _node) override;
bool visit(InlineAssembly const& _node) override;
bool visit(Block const& _node) override;
bool visit(PlaceholderStatement const& _node) override;
bool visit(IfStatement const& _node) override;
bool visit(WhileStatement const& _node) override;
bool visit(ForStatement const& _node) override;
@ -85,18 +95,28 @@ public:
bool visit(ElementaryTypeNameExpression const& _node) override;
bool visit(Literal const& _node) override;
void endVisit(SourceUnit const&) override;
void endVisit(ImportDirective const&) override;
void endVisit(ContractDefinition const&) override;
void endVisit(InheritanceSpecifier const&) override;
void endVisit(UsingForDirective const&) override;
void endVisit(StructDefinition const&) override;
void endVisit(EnumDefinition const&) override;
void endVisit(EnumValue const&) override;
void endVisit(ParameterList const&) override;
void endVisit(FunctionDefinition const&) override;
void endVisit(VariableDeclaration const&) override;
void endVisit(ModifierDefinition const&) override;
void endVisit(ModifierInvocation const&) override;
void endVisit(EventDefinition const&) override;
void endVisit(TypeName const&) override;
void endVisit(ElementaryTypeName const&) override;
void endVisit(UserDefinedTypeName const&) override;
void endVisit(Mapping const&) override;
void endVisit(ArrayTypeName const&) override;
void endVisit(InlineAssembly const&) override;
void endVisit(Block const&) override;
void endVisit(PlaceholderStatement const&) override;
void endVisit(IfStatement const&) override;
void endVisit(WhileStatement const&) override;
void endVisit(ForStatement const&) override;

View File

@ -45,7 +45,7 @@ BOOST_AUTO_TEST_CASE(smoke_test)
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
BOOST_CHECK_EQUAL(astJson["name"], "root");
BOOST_CHECK_EQUAL(astJson["name"], "SourceUnit");
}
BOOST_AUTO_TEST_CASE(source_location)
@ -56,12 +56,145 @@ BOOST_AUTO_TEST_CASE(source_location)
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
BOOST_CHECK_EQUAL(astJson["name"], "root");
BOOST_CHECK_EQUAL(astJson["children"][0]["name"], "Contract");
BOOST_CHECK_EQUAL(astJson["children"][0]["children"][0]["name"], "Function");
BOOST_CHECK_EQUAL(astJson["name"], "SourceUnit");
BOOST_CHECK_EQUAL(astJson["children"][0]["name"], "ContractDefinition");
BOOST_CHECK_EQUAL(astJson["children"][0]["children"][0]["name"], "FunctionDefinition");
BOOST_CHECK_EQUAL(astJson["children"][0]["children"][0]["src"], "13:32:1");
}
BOOST_AUTO_TEST_CASE(inheritance_specifier)
{
CompilerStack c;
c.addSource("a", "contract C1 {} contract C2 is C1 {}");
c.parse();
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
BOOST_CHECK_EQUAL(astJson["children"][1]["attributes"]["name"], "C2");
BOOST_CHECK_EQUAL(astJson["children"][1]["children"][0]["name"], "InheritanceSpecifier");
BOOST_CHECK_EQUAL(astJson["children"][1]["children"][0]["src"], "30:2:1");
BOOST_CHECK_EQUAL(astJson["children"][1]["children"][0]["children"][0]["name"], "UserDefinedTypeName");
BOOST_CHECK_EQUAL(astJson["children"][1]["children"][0]["children"][0]["attributes"]["name"], "C1");
}
BOOST_AUTO_TEST_CASE(using_for_directive)
{
CompilerStack c;
c.addSource("a", "library L {} contract C { using L for uint; }");
c.parse();
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
Json::Value usingFor = astJson["children"][1]["children"][0];
BOOST_CHECK_EQUAL(usingFor["name"], "UsingForDirective");
BOOST_CHECK_EQUAL(usingFor["src"], "26:17:1");
BOOST_CHECK_EQUAL(usingFor["children"][0]["name"], "UserDefinedTypeName");
BOOST_CHECK_EQUAL(usingFor["children"][0]["attributes"]["name"], "L");
BOOST_CHECK_EQUAL(usingFor["children"][1]["name"], "ElementaryTypeName");
BOOST_CHECK_EQUAL(usingFor["children"][1]["attributes"]["name"], "uint");
}
BOOST_AUTO_TEST_CASE(enum_definition)
{
CompilerStack c;
c.addSource("a", "contract C { enum E {} }");
c.parse();
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
Json::Value enumDefinition = astJson["children"][0]["children"][0];
BOOST_CHECK_EQUAL(enumDefinition["name"], "EnumDefinition");
BOOST_CHECK_EQUAL(enumDefinition["attributes"]["name"], "E");
BOOST_CHECK_EQUAL(enumDefinition["src"], "13:9:1");
}
BOOST_AUTO_TEST_CASE(enum_value)
{
CompilerStack c;
c.addSource("a", "contract C { enum E { A, B } }");
c.parse();
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
Json::Value enumDefinition = astJson["children"][0]["children"][0];
BOOST_CHECK_EQUAL(enumDefinition["children"][0]["name"], "EnumValue");
BOOST_CHECK_EQUAL(enumDefinition["children"][0]["attributes"]["name"], "A");
BOOST_CHECK_EQUAL(enumDefinition["children"][0]["src"], "22:1:1");
BOOST_CHECK_EQUAL(enumDefinition["children"][1]["name"], "EnumValue");
BOOST_CHECK_EQUAL(enumDefinition["children"][1]["attributes"]["name"], "B");
BOOST_CHECK_EQUAL(enumDefinition["children"][1]["src"], "25:1:1");
}
BOOST_AUTO_TEST_CASE(modifier_definition)
{
CompilerStack c;
c.addSource("a", "contract C { modifier M(uint i) { _ } function F() M(1) {} }");
c.parse();
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
Json::Value modifier = astJson["children"][0]["children"][0];
BOOST_CHECK_EQUAL(modifier["name"], "ModifierDefinition");
BOOST_CHECK_EQUAL(modifier["attributes"]["name"], "M");
BOOST_CHECK_EQUAL(modifier["src"], "13:24:1");
}
BOOST_AUTO_TEST_CASE(modifier_invocation)
{
CompilerStack c;
c.addSource("a", "contract C { modifier M(uint i) { _ } function F() M(1) {} }");
c.parse();
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
Json::Value modifier = astJson["children"][0]["children"][1]["children"][2];
BOOST_CHECK_EQUAL(modifier["name"], "ModifierInvocation");
BOOST_CHECK_EQUAL(modifier["src"], "51:4:1");
BOOST_CHECK_EQUAL(modifier["children"][0]["attributes"]["type"], "modifier (uint256)");
BOOST_CHECK_EQUAL(modifier["children"][0]["attributes"]["value"], "M");
BOOST_CHECK_EQUAL(modifier["children"][1]["attributes"]["value"], "1");
}
BOOST_AUTO_TEST_CASE(event_definition)
{
CompilerStack c;
c.addSource("a", "contract C { event E(); }");
c.parse();
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
Json::Value event = astJson["children"][0]["children"][0];
BOOST_CHECK_EQUAL(event["name"], "EventDefinition");
BOOST_CHECK_EQUAL(event["attributes"]["name"], "E");
BOOST_CHECK_EQUAL(event["src"], "13:10:1");
}
BOOST_AUTO_TEST_CASE(array_type_name)
{
CompilerStack c;
c.addSource("a", "contract C { uint[] i; }");
c.parse();
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
Json::Value array = astJson["children"][0]["children"][0]["children"][0];
BOOST_CHECK_EQUAL(array["name"], "ArrayTypeName");
BOOST_CHECK_EQUAL(array["src"], "13:6:1");
}
BOOST_AUTO_TEST_CASE(placeholder_statement)
{
CompilerStack c;
c.addSource("a", "contract C { modifier M { _ } }");
c.parse();
map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
Json::Value placeholder = astJson["children"][0]["children"][0]["children"][1]["children"][0];
BOOST_CHECK_EQUAL(placeholder["name"], "PlaceholderStatement");
BOOST_CHECK_EQUAL(placeholder["src"], "26:1:1");
}
BOOST_AUTO_TEST_SUITE_END()
}