Merge pull request #2332 from ethereum/fixLiteralPrint

fix Literalprint
This commit is contained in:
chriseth 2017-06-06 15:09:48 +02:00 committed by GitHub
commit 1fba984966
3 changed files with 19 additions and 3 deletions

View File

@ -704,13 +704,12 @@ bool ASTJsonConverter::visit(ElementaryTypeNameExpression const& _node)
bool ASTJsonConverter::visit(Literal const& _node)
{
char const* tokenString = Token::toString(_node.token());
Json::Value value{_node.value()};
if (!dev::validateUTF8(_node.value()))
value = Json::nullValue;
Token::Value subdenomination = Token::Value(_node.subDenomination());
std::vector<pair<string, Json::Value>> attributes = {
make_pair("token", tokenString ? tokenString : Json::Value()),
make_pair(m_legacy ? "token" : "kind", literalTokenKind(_node.token())),
make_pair("value", value),
make_pair(m_legacy ? "hexvalue" : "hexValue", toHex(_node.value())),
make_pair(
@ -793,6 +792,22 @@ string ASTJsonConverter::functionCallKind(FunctionCallKind _kind)
}
}
string ASTJsonConverter::literalTokenKind(Token::Value _token)
{
switch (_token)
{
case dev::solidity::Token::Number:
return "number";
case dev::solidity::Token::StringLiteral:
return "string";
case dev::solidity::Token::TrueLiteral:
case dev::solidity::Token::FalseLiteral:
return "bool";
default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown kind of literal token."));
}
}
string ASTJsonConverter::type(Expression const& _expression)
{
return _expression.annotation().type ? _expression.annotation().type->toString() : "Unknown";

View File

@ -133,6 +133,7 @@ private:
std::string location(VariableDeclaration::Location _location);
std::string contractKind(ContractDefinition::ContractKind _kind);
std::string functionCallKind(FunctionCallKind _kind);
std::string literalTokenKind(Token::Value _token);
std::string type(Expression const& _expression);
std::string type(VariableDeclaration const& _varDecl);
int nodeId(ASTNode const& _node)

View File

@ -192,7 +192,7 @@ BOOST_AUTO_TEST_CASE(non_utf8)
Json::Value literal = astJson["children"][0]["children"][0]["children"][2]["children"][0]["children"][1];
BOOST_CHECK_EQUAL(literal["name"], "Literal");
BOOST_CHECK_EQUAL(literal["attributes"]["hexvalue"], "ff");
BOOST_CHECK_EQUAL(literal["attributes"]["token"], Json::nullValue);
BOOST_CHECK_EQUAL(literal["attributes"]["token"], "string");
BOOST_CHECK_EQUAL(literal["attributes"]["value"], Json::nullValue);
BOOST_CHECK(literal["attributes"]["type"].asString().find("invalid") != string::npos);
}