Merge pull request #1298 from ethereum/lll-error-reporting

LLL: further improve error reporting
This commit is contained in:
chriseth 2016-11-01 11:59:05 +01:00 committed by GitHub
commit 1a2c150e3b
4 changed files with 24 additions and 14 deletions

View File

@ -35,9 +35,6 @@
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;
namespace sp = boost::spirit;
void CodeFragment::finalise(CompilerState const& _cs) void CodeFragment::finalise(CompilerState const& _cs)
{ {
@ -90,7 +87,7 @@ CodeFragment::CodeFragment(sp::utree const& _t, CompilerState& _s, bool _allowAS
m_asm.append(_s.args.at(s).m_asm); m_asm.append(_s.args.at(s).m_asm);
else if (_s.outers.count(s)) else if (_s.outers.count(s))
m_asm.append(_s.outers.at(s).m_asm); m_asm.append(_s.outers.at(s).m_asm);
else if (us.find_first_of("1234567890") != 0 && us.find_first_not_of("QWERTYUIOPASDFGHJKLZXCVBNM1234567890_") == string::npos) else if (us.find_first_of("1234567890") != 0 && us.find_first_not_of("QWERTYUIOPASDFGHJKLZXCVBNM1234567890_-") == string::npos)
{ {
auto it = _s.vars.find(s); auto it = _s.vars.find(s);
if (it == _s.vars.end()) if (it == _s.vars.end())
@ -584,10 +581,10 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
{ {
m_asm.appendJump(m_asm.errorTag()); m_asm.appendJump(m_asm.errorTag());
} }
else if (us.find_first_of("1234567890") != 0 && us.find_first_not_of("QWERTYUIOPASDFGHJKLZXCVBNM1234567890_") == string::npos) else if (us.find_first_of("1234567890") != 0 && us.find_first_not_of("QWERTYUIOPASDFGHJKLZXCVBNM1234567890_-") == string::npos)
m_asm.append((u256)varAddress(s)); m_asm.append((u256)varAddress(s));
else else
error<InvalidOperation>(); error<InvalidOperation>("Unsupported keyword: '" + us + "'");
} }
} }

View File

@ -51,6 +51,11 @@ private:
void finalise(CompilerState const& _cs); void finalise(CompilerState const& _cs);
template <class T> void error() const { BOOST_THROW_EXCEPTION(T() ); } template <class T> void error() const { BOOST_THROW_EXCEPTION(T() ); }
template <class T> void error(std::string const& reason) const {
auto err = T();
err << errinfo_comment(reason);
BOOST_THROW_EXCEPTION(err);
}
void constructOperation(sp::utree const& _t, CompilerState& _s); void constructOperation(sp::utree const& _t, CompilerState& _s);
bool m_finalised = false; bool m_finalised = false;

View File

@ -34,8 +34,7 @@ bytes dev::eth::compileLLL(string const& _src, bool _opt, vector<string>* _error
{ {
CompilerState cs; CompilerState cs;
cs.populateStandard(); cs.populateStandard();
auto f = CodeFragment::compile(_src, cs); bytes ret = CodeFragment::compile(_src, cs).assembly(cs).optimise(_opt).assemble().bytecode;
bytes ret = f.assembly(cs).optimise(_opt).assemble().bytecode;
for (auto i: cs.treesToKill) for (auto i: cs.treesToKill)
killBigints(i); killBigints(i);
return ret; return ret;
@ -59,7 +58,7 @@ bytes dev::eth::compileLLL(string const& _src, bool _opt, vector<string>* _error
catch (...) catch (...)
{ {
if (_errors) if (_errors)
_errors->push_back("Internal parse exception."); _errors->push_back("Internal compiler exception.");
} }
return bytes(); return bytes();
} }
@ -93,7 +92,7 @@ std::string dev::eth::compileLLLToAsm(std::string const& _src, bool _opt, std::v
catch (...) catch (...)
{ {
if (_errors) if (_errors)
_errors->push_back("Internal parse exception."); _errors->push_back("Internal compiler exception.");
} }
return string(); return string();
} }

View File

@ -101,8 +101,8 @@ void dev::eth::parseTreeLLL(string const& _s, sp::utree& o_out)
qi::rule<it, string()> strsh = '\'' > qi::lexeme[+(~qi::char_(std::string(" ;$@()[]{}:\n\t") + '\0'))]; qi::rule<it, string()> strsh = '\'' > qi::lexeme[+(~qi::char_(std::string(" ;$@()[]{}:\n\t") + '\0'))];
qi::rule<it, symbol_type()> symbol = qi::lexeme[+(~qi::char_(std::string(" $@[]{}:();\"\x01-\x1f\x7f") + '\0'))]; qi::rule<it, symbol_type()> symbol = qi::lexeme[+(~qi::char_(std::string(" $@[]{}:();\"\x01-\x1f\x7f") + '\0'))];
qi::rule<it, string()> intstr = qi::lexeme[ qi::no_case["0x"][qi::_val = "0x"] >> *qi::char_("0-9a-fA-F")[qi::_val += qi::_1]] | qi::lexeme[+qi::char_("0-9")[qi::_val += qi::_1]]; qi::rule<it, string()> intstr = qi::lexeme[ qi::no_case["0x"][qi::_val = "0x"] >> *qi::char_("0-9a-fA-F")[qi::_val += qi::_1]] | qi::lexeme[+qi::char_("0-9")[qi::_val += qi::_1]];
qi::rule<it, bigint()> integer = intstr; qi::rule<it, sp::utree()> integer = intstr[qi::_val = px::construct<sp::any_ptr>(px::new_<bigint>(qi::_1))];
qi::rule<it, space_type, sp::utree()> atom = integer[qi::_val = px::construct<sp::any_ptr>(px::new_<bigint>(qi::_1))] | (str | strsh)[qi::_val = qi::_1] | symbol[qi::_val = qi::_1]; qi::rule<it, space_type, sp::utree()> atom = integer[qi::_val = qi::_1] | (str | strsh)[qi::_val = qi::_1] | symbol[qi::_val = qi::_1];
qi::rule<it, space_type, sp::utree::list_type()> seq = '{' > *element > '}'; qi::rule<it, space_type, sp::utree::list_type()> seq = '{' > *element > '}';
qi::rule<it, space_type, sp::utree::list_type()> mload = '@' > element; qi::rule<it, space_type, sp::utree::list_type()> mload = '@' > element;
qi::rule<it, space_type, sp::utree::list_type()> sload = qi::lit("@@") > element; qi::rule<it, space_type, sp::utree::list_type()> sload = qi::lit("@@") > element;
@ -135,10 +135,19 @@ void dev::eth::parseTreeLLL(string const& _s, sp::utree& o_out)
s.push_back(i); s.push_back(i);
} }
auto ret = s.cbegin(); auto ret = s.cbegin();
qi::phrase_parse(ret, s.cend(), element, space, qi::skip_flag::dont_postskip, o_out); try
{
qi::phrase_parse(ret, s.cend(), element, space, qi::skip_flag::dont_postskip, o_out);
}
catch (qi::expectation_failure<it> const& e)
{
std::string fragment(e.first, e.last);
std::string loc = std::to_string(std::distance(s.cbegin(), e.first) - 1);
std::string reason("Lexer failure at " + loc + ": '" + fragment + "'");
BOOST_THROW_EXCEPTION(ParserException() << errinfo_comment(reason));
}
for (auto i = ret; i != s.cend(); ++i) for (auto i = ret; i != s.cend(); ++i)
if (!isspace(*i)) { if (!isspace(*i)) {
BOOST_THROW_EXCEPTION(ParserException() << errinfo_comment("Non-whitespace left in parser")); BOOST_THROW_EXCEPTION(ParserException() << errinfo_comment("Non-whitespace left in parser"));
} }
} }