2014-05-26 09:22:19 +00:00
|
|
|
/*
|
2017-02-02 10:06:28 +00:00
|
|
|
This file is part of solidity.
|
2014-05-26 09:22:19 +00:00
|
|
|
|
2017-02-02 10:06:28 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2014-05-26 09:22:19 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2017-02-02 10:06:28 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2014-05-26 09:22:19 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2017-02-02 10:06:28 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-05-26 09:22:19 +00:00
|
|
|
*/
|
|
|
|
/** @file Parser.cpp
|
|
|
|
* @author Gav Wood <i@gavwood.com>
|
|
|
|
* @date 2014
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Parser.h"
|
|
|
|
|
2016-05-13 13:38:23 +00:00
|
|
|
#if _MSC_VER
|
|
|
|
#pragma warning(disable:4348)
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 13:00:22 +00:00
|
|
|
#define BOOST_RESULT_OF_USE_DECLTYPE
|
|
|
|
#define BOOST_SPIRIT_USE_PHOENIX_V3
|
2014-05-26 09:22:19 +00:00
|
|
|
#include <boost/spirit/include/qi.hpp>
|
|
|
|
#include <boost/spirit/include/phoenix.hpp>
|
|
|
|
#include <boost/spirit/include/support_utree.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
2014-09-05 15:09:58 +00:00
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::eth;
|
2014-05-26 09:22:19 +00:00
|
|
|
namespace qi = boost::spirit::qi;
|
|
|
|
namespace px = boost::phoenix;
|
|
|
|
namespace sp = boost::spirit;
|
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
void dev::eth::killBigints(sp::utree const& _this)
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
|
|
|
switch (_this.which())
|
|
|
|
{
|
|
|
|
case sp::utree_type::list_type: for (auto const& i: _this) killBigints(i); break;
|
|
|
|
case sp::utree_type::any_type: delete _this.get<bigint*>(); break;
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
void dev::eth::debugOutAST(ostream& _out, sp::utree const& _this)
|
2014-05-26 10:12:22 +00:00
|
|
|
{
|
|
|
|
switch (_this.which())
|
|
|
|
{
|
|
|
|
case sp::utree_type::list_type:
|
|
|
|
switch (_this.tag())
|
|
|
|
{
|
|
|
|
case 0: _out << "( "; for (auto const& i: _this) { debugOutAST(_out, i); _out << " "; } _out << ")"; break;
|
|
|
|
case 1: _out << "@ "; debugOutAST(_out, _this.front()); break;
|
|
|
|
case 2: _out << "@@ "; debugOutAST(_out, _this.front()); break;
|
|
|
|
case 3: _out << "[ "; debugOutAST(_out, _this.front()); _out << " ] "; debugOutAST(_out, _this.back()); break;
|
|
|
|
case 4: _out << "[[ "; debugOutAST(_out, _this.front()); _out << " ]] "; debugOutAST(_out, _this.back()); break;
|
|
|
|
case 5: _out << "{ "; for (auto const& i: _this) { debugOutAST(_out, i); _out << " "; } _out << "}"; break;
|
2014-07-06 02:23:13 +00:00
|
|
|
case 6: _out << "$ "; debugOutAST(_out, _this.front()); break;
|
2014-05-26 10:12:22 +00:00
|
|
|
default:;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case sp::utree_type::int_type: _out << _this.get<int>(); break;
|
|
|
|
case sp::utree_type::string_type: _out << "\"" << _this.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::string_type>>() << "\""; break;
|
|
|
|
case sp::utree_type::symbol_type: _out << _this.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>(); break;
|
|
|
|
case sp::utree_type::any_type: _out << *_this.get<bigint*>(); break;
|
|
|
|
default: _out << "nil";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
namespace dev { namespace eth {
|
2014-07-09 18:43:39 +00:00
|
|
|
namespace parseTreeLLL_ {
|
|
|
|
|
|
|
|
template<unsigned N>
|
|
|
|
struct tagNode
|
|
|
|
{
|
|
|
|
void operator()(sp::utree& n, qi::rule<string::const_iterator, qi::ascii::space_type, sp::utree()>::context_type& c) const
|
|
|
|
{
|
|
|
|
(boost::fusion::at_c<0>(c.attributes) = n).tag(N);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
}}}
|
2014-07-09 18:43:39 +00:00
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
void dev::eth::parseTreeLLL(string const& _s, sp::utree& o_out)
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
2014-08-09 22:42:34 +00:00
|
|
|
using qi::standard::space;
|
|
|
|
using qi::standard::space_type;
|
2014-09-05 15:09:58 +00:00
|
|
|
using dev::eth::parseTreeLLL_::tagNode;
|
2015-02-06 07:19:22 +00:00
|
|
|
using symbol_type = sp::basic_string<std::string, sp::utree_type::symbol_type>;
|
|
|
|
using it = string::const_iterator;
|
2014-05-26 09:22:19 +00:00
|
|
|
|
2014-08-09 22:42:34 +00:00
|
|
|
qi::rule<it, space_type, sp::utree()> element;
|
2014-05-26 09:22:19 +00:00
|
|
|
qi::rule<it, string()> str = '"' > qi::lexeme[+(~qi::char_(std::string("\"") + '\0'))] > '"';
|
2014-07-06 02:23:13 +00:00
|
|
|
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'))];
|
2016-11-01 00:17:53 +00:00
|
|
|
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]];
|
2016-11-01 01:01:35 +00:00
|
|
|
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 = qi::_1] | (str | strsh)[qi::_val = qi::_1] | symbol[qi::_val = qi::_1];
|
2014-08-09 22:42:34 +00:00
|
|
|
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()> sload = qi::lit("@@") > element;
|
|
|
|
qi::rule<it, space_type, sp::utree::list_type()> mstore = '[' > element > ']' > -qi::lit(":") > element;
|
|
|
|
qi::rule<it, space_type, sp::utree::list_type()> sstore = qi::lit("[[") > element > qi::lit("]]") > -qi::lit(":") > element;
|
|
|
|
qi::rule<it, space_type, sp::utree::list_type()> calldataload = qi::lit("$") > element;
|
2017-06-12 12:52:04 +00:00
|
|
|
qi::rule<it, space_type, sp::utree::list_type()> list = '(' > *element > ')';
|
2014-08-09 22:42:34 +00:00
|
|
|
|
|
|
|
qi::rule<it, space_type, sp::utree()> extra = sload[tagNode<2>()] | mload[tagNode<1>()] | sstore[tagNode<4>()] | mstore[tagNode<3>()] | seq[tagNode<5>()] | calldataload[tagNode<6>()];
|
2014-05-26 09:22:19 +00:00
|
|
|
element = atom | list | extra;
|
|
|
|
|
|
|
|
string s;
|
|
|
|
s.reserve(_s.size());
|
|
|
|
bool incomment = false;
|
|
|
|
bool instring = false;
|
|
|
|
bool insstring = false;
|
|
|
|
for (auto i: _s)
|
|
|
|
{
|
|
|
|
if (i == ';' && !instring && !insstring)
|
|
|
|
incomment = true;
|
|
|
|
else if (i == '\n')
|
|
|
|
incomment = instring = insstring = false;
|
|
|
|
else if (i == '"' && !insstring)
|
|
|
|
instring = !instring;
|
|
|
|
else if (i == '\'')
|
|
|
|
insstring = true;
|
|
|
|
else if (i == ' ')
|
|
|
|
insstring = false;
|
|
|
|
if (!incomment)
|
|
|
|
s.push_back(i);
|
|
|
|
}
|
2014-06-22 19:36:16 +00:00
|
|
|
auto ret = s.cbegin();
|
2016-10-26 13:29:08 +00:00
|
|
|
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));
|
|
|
|
}
|
2014-07-06 02:23:13 +00:00
|
|
|
for (auto i = ret; i != s.cend(); ++i)
|
2016-10-15 12:26:16 +00:00
|
|
|
if (!isspace(*i)) {
|
|
|
|
BOOST_THROW_EXCEPTION(ParserException() << errinfo_comment("Non-whitespace left in parser"));
|
|
|
|
}
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|