|
| 1 | +/* |
| 2 | + This file is part of solidity. |
| 3 | +
|
| 4 | + solidity is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + solidity is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with solidity. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | + |
| 18 | +#include <test/libevmasm/PlainAssemblyParser.h> |
| 19 | + |
| 20 | +#include <test/Common.h> |
| 21 | +#include <test/libsolidity/util/SoltestErrors.h> |
| 22 | + |
| 23 | +#include <libevmasm/Instruction.h> |
| 24 | + |
| 25 | +#include <liblangutil/Common.h> |
| 26 | + |
| 27 | +#include <boost/algorithm/string/find.hpp> |
| 28 | + |
| 29 | +#include <fmt/format.h> |
| 30 | + |
| 31 | +#include <sstream> |
| 32 | + |
| 33 | +using namespace std::string_literals; |
| 34 | +using namespace solidity; |
| 35 | +using namespace solidity::test; |
| 36 | +using namespace solidity::evmasm; |
| 37 | +using namespace solidity::evmasm::test; |
| 38 | +using namespace solidity::langutil; |
| 39 | + |
| 40 | +Json PlainAssemblyParser::parse(std::string _sourceName, std::string const& _source) |
| 41 | +{ |
| 42 | + m_sourceName = std::move(_sourceName); |
| 43 | + Json codeJSON = Json::array(); |
| 44 | + std::istringstream sourceStream(_source); |
| 45 | + while (getline(sourceStream, m_line)) |
| 46 | + { |
| 47 | + advanceLine(m_line); |
| 48 | + if (m_lineTokens.empty()) |
| 49 | + continue; |
| 50 | + |
| 51 | + if (c_instructions.contains(currentToken().value)) |
| 52 | + { |
| 53 | + expectNoMoreArguments(); |
| 54 | + codeJSON.push_back({{"name", currentToken().value}}); |
| 55 | + } |
| 56 | + else if (currentToken().value == "PUSH") |
| 57 | + { |
| 58 | + if (hasMoreTokens() && nextToken().value == "[tag]") |
| 59 | + { |
| 60 | + advanceToken(); |
| 61 | + std::string_view tagID = expectArgument(); |
| 62 | + expectNoMoreArguments(); |
| 63 | + codeJSON.push_back({{"name", "PUSH [tag]"}, {"value", tagID}}); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + std::string_view immediateArgument = expectArgument(); |
| 68 | + expectNoMoreArguments(); |
| 69 | + |
| 70 | + if (!immediateArgument.starts_with("0x")) |
| 71 | + BOOST_THROW_EXCEPTION(std::runtime_error(formatError("The immediate argument to PUSH must be a hex number prefixed with '0x'."))); |
| 72 | + |
| 73 | + immediateArgument.remove_prefix("0x"s.size()); |
| 74 | + codeJSON.push_back({{"name", "PUSH"}, {"value", immediateArgument}}); |
| 75 | + } |
| 76 | + } |
| 77 | + else if (currentToken().value == "tag") |
| 78 | + { |
| 79 | + std::string_view tagID = expectArgument(); |
| 80 | + expectNoMoreArguments(); |
| 81 | + |
| 82 | + codeJSON.push_back({{"name", "tag"}, {"value", tagID}}); |
| 83 | + codeJSON.push_back({{"name", "JUMPDEST"}}); |
| 84 | + } |
| 85 | + else |
| 86 | + BOOST_THROW_EXCEPTION(std::runtime_error(formatError("Unknown instruction."))); |
| 87 | + } |
| 88 | + return {{".code", codeJSON}}; |
| 89 | +} |
| 90 | + |
| 91 | +PlainAssemblyParser::Token const& PlainAssemblyParser::currentToken() const |
| 92 | +{ |
| 93 | + soltestAssert(m_tokenIndex < m_lineTokens.size()); |
| 94 | + return m_lineTokens[m_tokenIndex]; |
| 95 | +} |
| 96 | + |
| 97 | +PlainAssemblyParser::Token const& PlainAssemblyParser::nextToken() const |
| 98 | +{ |
| 99 | + soltestAssert(m_tokenIndex + 1 < m_lineTokens.size()); |
| 100 | + return m_lineTokens[m_tokenIndex + 1]; |
| 101 | +} |
| 102 | + |
| 103 | +bool PlainAssemblyParser::advanceToken() |
| 104 | +{ |
| 105 | + if (!hasMoreTokens()) |
| 106 | + return false; |
| 107 | + |
| 108 | + ++m_tokenIndex; |
| 109 | + return true; |
| 110 | +} |
| 111 | + |
| 112 | +std::string_view PlainAssemblyParser::expectArgument() |
| 113 | +{ |
| 114 | + bool hasArgument = advanceToken(); |
| 115 | + if (!hasArgument) |
| 116 | + BOOST_THROW_EXCEPTION(std::runtime_error(formatError("Missing argument(s)."))); |
| 117 | + |
| 118 | + return currentToken().value; |
| 119 | +} |
| 120 | + |
| 121 | +void PlainAssemblyParser::expectNoMoreArguments() |
| 122 | +{ |
| 123 | + bool hasArgument = advanceToken(); |
| 124 | + if (hasArgument) |
| 125 | + BOOST_THROW_EXCEPTION(std::runtime_error(formatError("Too many arguments."))); |
| 126 | +} |
| 127 | + |
| 128 | +void PlainAssemblyParser::advanceLine(std::string_view _line) |
| 129 | +{ |
| 130 | + ++m_lineNumber; |
| 131 | + m_line = _line; |
| 132 | + m_lineTokens = tokenizeLine(m_line); |
| 133 | + m_tokenIndex = 0; |
| 134 | +} |
| 135 | + |
| 136 | +std::vector<PlainAssemblyParser::Token> PlainAssemblyParser::tokenizeLine(std::string_view _line) |
| 137 | +{ |
| 138 | + auto const notWhiteSpace = [](char _c) { return !isWhiteSpace(_c); }; |
| 139 | + |
| 140 | + std::vector<Token> tokens; |
| 141 | + auto tokenLocation = boost::find_token(_line, notWhiteSpace, boost::token_compress_on); |
| 142 | + while (!tokenLocation.empty()) |
| 143 | + { |
| 144 | + std::string_view value{tokenLocation.begin(), tokenLocation.end()}; |
| 145 | + if (value.starts_with("//")) |
| 146 | + break; |
| 147 | + |
| 148 | + tokens.push_back({ |
| 149 | + .value = value, |
| 150 | + .position = static_cast<size_t>(std::distance(_line.begin(), tokenLocation.begin())), |
| 151 | + }); |
| 152 | + soltestAssert(!value.empty()); |
| 153 | + soltestAssert(tokens.back().position < _line.size()); |
| 154 | + soltestAssert(tokens.back().position + value.size() <= _line.size()); |
| 155 | + |
| 156 | + std::string_view tail{tokenLocation.end(), _line.end()}; |
| 157 | + tokenLocation = boost::find_token(tail, notWhiteSpace, boost::token_compress_on); |
| 158 | + } |
| 159 | + |
| 160 | + return tokens; |
| 161 | +} |
| 162 | + |
| 163 | +std::string PlainAssemblyParser::formatError(std::string_view _message) const |
| 164 | +{ |
| 165 | + std::string lineNumberString = std::to_string(m_lineNumber); |
| 166 | + std::string padding(lineNumberString.size(), ' '); |
| 167 | + std::string underline = std::string(currentToken().position, ' ') + std::string(currentToken().value.size(), '^'); |
| 168 | + return fmt::format( |
| 169 | + "Error while parsing plain assembly: {}\n" |
| 170 | + "{}--> {}\n" |
| 171 | + "{} | \n" |
| 172 | + "{} | {}\n" |
| 173 | + "{} | {}\n", |
| 174 | + _message, |
| 175 | + padding, m_sourceName, |
| 176 | + padding, |
| 177 | + m_lineNumber, m_line, |
| 178 | + padding, underline |
| 179 | + ); |
| 180 | +} |
0 commit comments