Use lowercase when reporting instruction error.

This commit is contained in:
chriseth 2017-06-09 14:44:38 +02:00
parent 76667fed4f
commit bf2b5c746a
3 changed files with 33 additions and 14 deletions

View File

@ -206,6 +206,20 @@ std::map<string, dev::solidity::Instruction> const& Parser::instructions()
return s_instructions; return s_instructions;
} }
std::map<dev::solidity::Instruction, string> const& Parser::instructionNames()
{
static map<dev::solidity::Instruction, string> s_instructionNames;
if (s_instructionNames.empty())
{
for (auto const& instr: instructions())
s_instructionNames[instr.second] = instr.first;
// set the ambiguous instructions to a clear default
s_instructionNames[solidity::Instruction::SELFDESTRUCT] = "selfdestruct";
s_instructionNames[solidity::Instruction::KECCAK256] = "keccak256";
}
return s_instructionNames;
}
assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher) assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
{ {
Statement ret; Statement ret;
@ -233,7 +247,7 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
{ {
InstructionInfo info = dev::solidity::instructionInfo(instr); InstructionInfo info = dev::solidity::instructionInfo(instr);
if (info.ret != 1) if (info.ret != 1)
fatalParserError("Instruction " + info.name + " not allowed in this context."); fatalParserError("Instruction \"" + literal + "\" not allowed in this context.");
} }
ret = Instruction{location(), instr}; ret = Instruction{location(), instr};
} }
@ -363,9 +377,9 @@ assembly::Statement Parser::parseCall(assembly::Statement&& _instruction)
/// check for premature closing parentheses /// check for premature closing parentheses
if (currentToken() == Token::RParen) if (currentToken() == Token::RParen)
fatalParserError(string( fatalParserError(string(
"Expected expression (" + "Expected expression (\"" +
instrInfo.name + instructionNames().at(instr) +
" expects " + "\" expects " +
boost::lexical_cast<string>(args) + boost::lexical_cast<string>(args) +
" arguments)" " arguments)"
)); ));
@ -375,9 +389,9 @@ assembly::Statement Parser::parseCall(assembly::Statement&& _instruction)
{ {
if (currentToken() != Token::Comma) if (currentToken() != Token::Comma)
fatalParserError(string( fatalParserError(string(
"Expected comma (" + "Expected comma (\"" +
instrInfo.name + instructionNames().at(instr) +
" expects " + "\" expects " +
boost::lexical_cast<string>(args) + boost::lexical_cast<string>(args) +
" arguments)" " arguments)"
)); ));
@ -387,9 +401,13 @@ assembly::Statement Parser::parseCall(assembly::Statement&& _instruction)
} }
ret.location.end = endPosition(); ret.location.end = endPosition();
if (currentToken() == Token::Comma) if (currentToken() == Token::Comma)
fatalParserError( fatalParserError(string(
string("Expected ')' (" + instrInfo.name + " expects " + boost::lexical_cast<string>(args) + " arguments)") "Expected ')' (\"" +
); instructionNames().at(instr) +
"\" expects " +
boost::lexical_cast<string>(args) +
" arguments)"
));
expectToken(Token::RParen); expectToken(Token::RParen);
return ret; return ret;
} }

View File

@ -65,7 +65,8 @@ protected:
Case parseCase(); Case parseCase();
/// Parses a functional expression that has to push exactly one stack element /// Parses a functional expression that has to push exactly one stack element
Statement parseExpression(); Statement parseExpression();
std::map<std::string, dev::solidity::Instruction> const& instructions(); static std::map<std::string, dev::solidity::Instruction> const& instructions();
static std::map<dev::solidity::Instruction, std::string> const& instructionNames();
Statement parseElementaryOperation(bool _onlySinglePusher = false); Statement parseElementaryOperation(bool _onlySinglePusher = false);
VariableDeclaration parseVariableDeclaration(); VariableDeclaration parseVariableDeclaration();
FunctionDefinition parseFunctionDefinition(); FunctionDefinition parseFunctionDefinition();

View File

@ -330,13 +330,13 @@ BOOST_AUTO_TEST_CASE(invalid_tuple_assignment)
BOOST_AUTO_TEST_CASE(instruction_too_few_arguments) BOOST_AUTO_TEST_CASE(instruction_too_few_arguments)
{ {
CHECK_PARSE_ERROR("{ mul() }", ParserError, "Expected expression (MUL expects 2 arguments)"); CHECK_PARSE_ERROR("{ mul() }", ParserError, "Expected expression (\"mul\" expects 2 arguments)");
CHECK_PARSE_ERROR("{ mul(1) }", ParserError, "Expected comma (MUL expects 2 arguments)"); CHECK_PARSE_ERROR("{ mul(1) }", ParserError, "Expected comma (\"mul\" expects 2 arguments)");
} }
BOOST_AUTO_TEST_CASE(instruction_too_many_arguments) BOOST_AUTO_TEST_CASE(instruction_too_many_arguments)
{ {
CHECK_PARSE_ERROR("{ mul(1, 2, 3) }", ParserError, "Expected ')' (MUL expects 2 arguments)"); CHECK_PARSE_ERROR("{ mul(1, 2, 3) }", ParserError, "Expected ')' (\"mul\" expects 2 arguments)");
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()