2019-01-24 09:48:01 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
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.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
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
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-01-24 09:48:01 +00:00
|
|
|
|
|
|
|
#include <test/libsolidity/util/TestFileParser.h>
|
2019-07-03 19:37:55 +00:00
|
|
|
|
|
|
|
#include <test/libsolidity/util/BytesUtils.h>
|
2020-05-27 23:17:53 +00:00
|
|
|
#include <test/libsolidity/util/SoltestErrors.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2019-07-03 19:37:55 +00:00
|
|
|
|
2019-02-21 00:04:34 +00:00
|
|
|
#include <liblangutil/Common.h>
|
2019-07-03 19:37:55 +00:00
|
|
|
|
2019-01-24 09:48:01 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/throw_exception.hpp>
|
2019-07-03 19:37:55 +00:00
|
|
|
|
2019-01-24 09:48:01 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
2019-10-28 10:39:30 +00:00
|
|
|
#include <optional>
|
2019-01-24 09:48:01 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace solidity;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::frontend::test;
|
2019-01-24 09:48:01 +00:00
|
|
|
using namespace std;
|
2020-11-03 23:39:29 +00:00
|
|
|
|
|
|
|
using Token = soltest::Token;
|
2019-01-24 09:48:01 +00:00
|
|
|
|
2019-05-15 15:25:35 +00:00
|
|
|
char TestFileParser::Scanner::peek() const noexcept
|
|
|
|
{
|
2021-04-28 21:35:59 +00:00
|
|
|
if (std::distance(m_char, m_source.end()) < 2)
|
2019-05-15 15:25:35 +00:00
|
|
|
return '\0';
|
|
|
|
|
|
|
|
auto next = m_char;
|
|
|
|
std::advance(next, 1);
|
|
|
|
return *next;
|
|
|
|
}
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
vector<solidity::frontend::test::FunctionCall> TestFileParser::parseFunctionCalls(size_t _lineOffset)
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
|
|
|
vector<FunctionCall> calls;
|
2019-02-05 15:52:19 +00:00
|
|
|
if (!accept(Token::EOS))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2021-01-08 03:45:19 +00:00
|
|
|
soltestAssert(m_scanner.currentToken() == Token::Unknown, "");
|
2019-02-02 13:22:46 +00:00
|
|
|
m_scanner.scanNextToken();
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
while (!accept(Token::EOS))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-05 15:52:19 +00:00
|
|
|
if (!accept(Token::Whitespace))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-02 13:22:46 +00:00
|
|
|
/// If this is not the first call in the test,
|
|
|
|
/// the last call to parseParameter could have eaten the
|
|
|
|
/// new line already. This could only be fixed with a one
|
|
|
|
/// token lookahead that checks parseParameter
|
|
|
|
/// if the next token is an identifier.
|
|
|
|
if (calls.empty())
|
2019-02-05 15:52:19 +00:00
|
|
|
expect(Token::Newline);
|
2019-02-02 13:22:46 +00:00
|
|
|
else
|
2019-07-13 10:04:22 +00:00
|
|
|
if (accept(Token::Newline, true))
|
|
|
|
m_lineNumber++;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-03-09 20:26:36 +00:00
|
|
|
if (accept(Token::Gas, true))
|
2020-10-28 18:48:16 +00:00
|
|
|
{
|
2021-03-09 20:26:36 +00:00
|
|
|
if (calls.empty())
|
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Expected function call before gas usage filter."));
|
|
|
|
|
|
|
|
string runType = m_scanner.currentLiteral();
|
|
|
|
if (set<string>{"ir", "irOptimized", "legacy", "legacyOptimized"}.count(runType) > 0)
|
|
|
|
{
|
|
|
|
m_scanner.scanNextToken();
|
|
|
|
expect(Token::Colon);
|
|
|
|
if (calls.back().expectations.gasUsed.count(runType) > 0)
|
|
|
|
throw TestParserError("Gas usage expectation set multiple times.");
|
|
|
|
calls.back().expectations.gasUsed[runType] = u256(parseDecimalNumber());
|
|
|
|
}
|
2020-10-28 18:48:16 +00:00
|
|
|
else
|
2021-03-09 20:26:36 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError(
|
|
|
|
"Expected \"ir\", \"irOptimized\", \"legacy\", or \"legacyOptimized\"."
|
|
|
|
));
|
2020-10-28 18:48:16 +00:00
|
|
|
}
|
2019-11-12 13:13:57 +00:00
|
|
|
else
|
2019-07-13 10:04:22 +00:00
|
|
|
{
|
2021-03-09 20:26:36 +00:00
|
|
|
FunctionCall call;
|
|
|
|
if (accept(Token::Library, true))
|
2019-11-12 13:13:57 +00:00
|
|
|
{
|
2021-03-09 20:26:36 +00:00
|
|
|
expect(Token::Colon);
|
|
|
|
call.signature = m_scanner.currentLiteral();
|
|
|
|
expect(Token::Identifier);
|
|
|
|
call.kind = FunctionCall::Kind::Library;
|
|
|
|
call.expectations.failure = false;
|
2019-11-12 13:13:57 +00:00
|
|
|
}
|
2021-03-09 20:26:36 +00:00
|
|
|
else
|
2019-11-12 13:13:57 +00:00
|
|
|
{
|
2021-03-09 20:26:36 +00:00
|
|
|
bool lowLevelCall = false;
|
|
|
|
tie(call.signature, lowLevelCall) = parseFunctionSignature();
|
|
|
|
if (lowLevelCall)
|
|
|
|
call.kind = FunctionCall::Kind::LowLevel;
|
2021-04-23 14:56:19 +00:00
|
|
|
else if (isBuiltinFunction(call.signature))
|
|
|
|
call.kind = FunctionCall::Kind::Builtin;
|
2021-03-09 20:26:36 +00:00
|
|
|
|
|
|
|
if (accept(Token::Comma, true))
|
|
|
|
call.value = parseFunctionCallValue();
|
|
|
|
|
|
|
|
if (accept(Token::Colon, true))
|
|
|
|
call.arguments = parseFunctionCallArguments();
|
|
|
|
|
2019-11-12 13:13:57 +00:00
|
|
|
if (accept(Token::Newline, true))
|
2021-03-09 20:26:36 +00:00
|
|
|
{
|
|
|
|
call.displayMode = FunctionCall::DisplayMode::MultiLine;
|
2019-11-12 13:13:57 +00:00
|
|
|
m_lineNumber++;
|
2021-03-09 20:26:36 +00:00
|
|
|
}
|
2019-11-12 13:13:57 +00:00
|
|
|
|
2021-03-09 20:26:36 +00:00
|
|
|
call.arguments.comment = parseComment();
|
2019-11-12 13:13:57 +00:00
|
|
|
|
2021-03-09 20:26:36 +00:00
|
|
|
if (accept(Token::Newline, true))
|
|
|
|
{
|
|
|
|
call.displayMode = FunctionCall::DisplayMode::MultiLine;
|
|
|
|
m_lineNumber++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accept(Token::Arrow, true))
|
|
|
|
{
|
|
|
|
call.omitsArrow = false;
|
|
|
|
call.expectations = parseFunctionCallExpectations();
|
|
|
|
if (accept(Token::Newline, true))
|
|
|
|
m_lineNumber++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
call.expectations.failure = false;
|
|
|
|
call.displayMode = FunctionCall::DisplayMode::SingleLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
call.expectations.comment = parseComment();
|
|
|
|
|
|
|
|
if (call.signature == "constructor()")
|
|
|
|
call.kind = FunctionCall::Kind::Constructor;
|
|
|
|
}
|
2019-07-13 10:04:22 +00:00
|
|
|
|
2021-04-28 21:35:59 +00:00
|
|
|
accept(Token::Newline, true);
|
|
|
|
call.expectedSideEffects = parseFunctionCallSideEffects();
|
|
|
|
|
|
|
|
calls.emplace_back(move(call));
|
2021-03-09 20:26:36 +00:00
|
|
|
}
|
2019-07-13 10:04:22 +00:00
|
|
|
}
|
2020-05-27 23:17:53 +00:00
|
|
|
catch (TestParserError const& _e)
|
2019-07-13 10:04:22 +00:00
|
|
|
{
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
TestParserError("Line " + to_string(_lineOffset + m_lineNumber) + ": " + _e.what())
|
|
|
|
);
|
2019-07-13 10:04:22 +00:00
|
|
|
}
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return calls;
|
|
|
|
}
|
|
|
|
|
2021-04-28 21:35:59 +00:00
|
|
|
vector<string> TestFileParser::parseFunctionCallSideEffects()
|
|
|
|
{
|
|
|
|
vector<string> result;
|
|
|
|
while (accept(Token::Tilde, false))
|
|
|
|
{
|
|
|
|
string effect = m_scanner.currentLiteral();
|
|
|
|
result.emplace_back(effect);
|
|
|
|
soltestAssert(m_scanner.currentToken() == Token::Tilde, "");
|
|
|
|
m_scanner.scanNextToken();
|
|
|
|
if (m_scanner.currentToken() == Token::Newline)
|
|
|
|
m_scanner.scanNextToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-03 23:39:29 +00:00
|
|
|
bool TestFileParser::accept(Token _token, bool const _expect)
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-05 15:52:19 +00:00
|
|
|
if (m_scanner.currentToken() != _token)
|
|
|
|
return false;
|
|
|
|
if (_expect)
|
|
|
|
return expect(_token);
|
|
|
|
return true;
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 23:39:29 +00:00
|
|
|
bool TestFileParser::expect(Token _token, bool const _advance)
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-05 15:52:19 +00:00
|
|
|
if (m_scanner.currentToken() != _token || m_scanner.currentToken() == Token::Invalid)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError(
|
2019-02-02 13:22:46 +00:00
|
|
|
"Unexpected " + formatToken(m_scanner.currentToken()) + ": \"" +
|
|
|
|
m_scanner.currentLiteral() + "\". " +
|
|
|
|
"Expected \"" + formatToken(_token) + "\"."
|
2021-02-18 23:49:34 +00:00
|
|
|
)
|
|
|
|
);
|
2019-01-24 09:48:01 +00:00
|
|
|
if (_advance)
|
|
|
|
m_scanner.scanNextToken();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-09 18:09:15 +00:00
|
|
|
pair<string, bool> TestFileParser::parseFunctionSignature()
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-09-09 18:09:15 +00:00
|
|
|
string signature;
|
|
|
|
bool hasName = false;
|
|
|
|
|
|
|
|
if (accept(Token::Identifier, false))
|
|
|
|
{
|
|
|
|
hasName = true;
|
|
|
|
signature = m_scanner.currentLiteral();
|
|
|
|
expect(Token::Identifier);
|
|
|
|
}
|
2019-01-24 09:48:01 +00:00
|
|
|
|
2021-04-28 14:15:58 +00:00
|
|
|
if (isBuiltinFunction(signature) && m_scanner.currentToken() != Token::LParen)
|
2021-01-08 03:45:19 +00:00
|
|
|
return {signature, false};
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
signature += formatToken(Token::LParen);
|
|
|
|
expect(Token::LParen);
|
2019-01-24 09:48:01 +00:00
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
string parameters;
|
|
|
|
if (!accept(Token::RParen, false))
|
|
|
|
parameters = parseIdentifierOrTuple();
|
|
|
|
|
|
|
|
while (accept(Token::Comma))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-05 15:52:19 +00:00
|
|
|
parameters += formatToken(Token::Comma);
|
|
|
|
expect(Token::Comma);
|
|
|
|
parameters += parseIdentifierOrTuple();
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
2019-02-05 15:52:19 +00:00
|
|
|
if (accept(Token::Arrow, true))
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Invalid signature detected: " + signature));
|
2019-02-05 15:52:19 +00:00
|
|
|
|
2019-09-09 18:09:15 +00:00
|
|
|
if (!hasName && !parameters.empty())
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Signatures without a name cannot have parameters: " + signature));
|
2019-09-09 18:09:15 +00:00
|
|
|
else
|
|
|
|
signature += parameters;
|
2019-02-05 15:52:19 +00:00
|
|
|
|
|
|
|
expect(Token::RParen);
|
|
|
|
signature += formatToken(Token::RParen);
|
2019-09-09 18:09:15 +00:00
|
|
|
|
|
|
|
return {signature, !hasName};
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 11:57:48 +00:00
|
|
|
FunctionValue TestFileParser::parseFunctionCallValue()
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-28 13:16:16 +00:00
|
|
|
try
|
|
|
|
{
|
2020-02-18 11:57:48 +00:00
|
|
|
u256 value{ parseDecimalNumber() };
|
|
|
|
Token token = m_scanner.currentToken();
|
|
|
|
if (token != Token::Ether && token != Token::Wei)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Invalid value unit provided. Coins can be wei or ether."));
|
2020-02-18 11:57:48 +00:00
|
|
|
|
|
|
|
m_scanner.scanNextToken();
|
|
|
|
|
|
|
|
FunctionValueUnit unit = token == Token::Wei ? FunctionValueUnit::Wei : FunctionValueUnit::Ether;
|
|
|
|
return { (unit == FunctionValueUnit::Wei ? u256(1) : exp256(u256(10), u256(18))) * value, unit };
|
2019-02-28 13:16:16 +00:00
|
|
|
}
|
|
|
|
catch (std::exception const&)
|
|
|
|
{
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Ether value encoding invalid."));
|
2019-02-28 13:16:16 +00:00
|
|
|
}
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FunctionCallArgs TestFileParser::parseFunctionCallArguments()
|
|
|
|
{
|
|
|
|
FunctionCallArgs arguments;
|
|
|
|
|
2019-02-01 05:29:34 +00:00
|
|
|
auto param = parseParameter();
|
|
|
|
if (param.abiType.type == ABIType::None)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("No argument provided."));
|
2019-02-01 05:29:34 +00:00
|
|
|
arguments.parameters.emplace_back(param);
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
while (accept(Token::Comma, true))
|
2019-02-01 05:29:34 +00:00
|
|
|
arguments.parameters.emplace_back(parseParameter());
|
2019-01-24 09:48:01 +00:00
|
|
|
return arguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionCallExpectations TestFileParser::parseFunctionCallExpectations()
|
|
|
|
{
|
|
|
|
FunctionCallExpectations expectations;
|
|
|
|
|
2019-02-01 05:29:34 +00:00
|
|
|
auto param = parseParameter();
|
|
|
|
if (param.abiType.type == ABIType::None)
|
2019-02-02 13:22:46 +00:00
|
|
|
{
|
|
|
|
expectations.failure = false;
|
2019-02-01 05:29:34 +00:00
|
|
|
return expectations;
|
2019-02-02 13:22:46 +00:00
|
|
|
}
|
|
|
|
expectations.result.emplace_back(param);
|
2019-01-24 09:48:01 +00:00
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
while (accept(Token::Comma, true))
|
2019-02-02 13:22:46 +00:00
|
|
|
expectations.result.emplace_back(parseParameter());
|
2019-02-01 05:29:34 +00:00
|
|
|
|
|
|
|
/// We have always one virtual parameter in the parameter list.
|
|
|
|
/// If its type is FAILURE, the expected result is also a REVERT etc.
|
2019-02-02 13:22:46 +00:00
|
|
|
if (expectations.result.at(0).abiType.type != ABIType::Failure)
|
2019-02-01 05:29:34 +00:00
|
|
|
expectations.failure = false;
|
2019-01-24 09:48:01 +00:00
|
|
|
return expectations;
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:29:34 +00:00
|
|
|
Parameter TestFileParser::parseParameter()
|
|
|
|
{
|
|
|
|
Parameter parameter;
|
2019-02-05 15:52:19 +00:00
|
|
|
if (accept(Token::Newline, true))
|
2019-07-13 10:04:22 +00:00
|
|
|
{
|
2019-02-01 05:29:34 +00:00
|
|
|
parameter.format.newline = true;
|
2019-07-13 10:04:22 +00:00
|
|
|
m_lineNumber++;
|
|
|
|
}
|
2019-07-21 14:04:04 +00:00
|
|
|
parameter.abiType = ABIType{ABIType::None, ABIType::AlignNone, 0};
|
2019-07-03 19:37:55 +00:00
|
|
|
|
2019-02-28 13:16:16 +00:00
|
|
|
bool isSigned = false;
|
|
|
|
if (accept(Token::Left, true))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-07-04 13:23:47 +00:00
|
|
|
parameter.rawString += formatToken(Token::Left);
|
2019-02-28 13:16:16 +00:00
|
|
|
expect(Token::LParen);
|
2019-07-04 13:23:47 +00:00
|
|
|
parameter.rawString += formatToken(Token::LParen);
|
|
|
|
parameter.alignment = Parameter::Alignment::Left;
|
2019-02-28 13:16:16 +00:00
|
|
|
}
|
|
|
|
if (accept(Token::Right, true))
|
|
|
|
{
|
2019-07-04 13:23:47 +00:00
|
|
|
parameter.rawString += formatToken(Token::Right);
|
2019-02-28 13:16:16 +00:00
|
|
|
expect(Token::LParen);
|
2019-07-04 13:23:47 +00:00
|
|
|
parameter.rawString += formatToken(Token::LParen);
|
|
|
|
parameter.alignment = Parameter::Alignment::Right;
|
2019-02-28 13:16:16 +00:00
|
|
|
}
|
2019-02-02 13:22:46 +00:00
|
|
|
|
2019-08-07 10:22:52 +00:00
|
|
|
if (accept(Token::Sub, true))
|
2019-02-28 13:16:16 +00:00
|
|
|
{
|
2019-08-07 10:22:52 +00:00
|
|
|
parameter.rawString += formatToken(Token::Sub);
|
|
|
|
isSigned = true;
|
|
|
|
}
|
|
|
|
if (accept(Token::Boolean))
|
|
|
|
{
|
|
|
|
if (isSigned)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Invalid boolean literal."));
|
2019-08-07 10:22:52 +00:00
|
|
|
|
|
|
|
parameter.abiType = ABIType{ABIType::Boolean, ABIType::AlignRight, 32};
|
|
|
|
string parsed = parseBoolean();
|
|
|
|
parameter.rawString += parsed;
|
|
|
|
parameter.rawBytes = BytesUtils::applyAlign(
|
|
|
|
parameter.alignment,
|
|
|
|
parameter.abiType,
|
|
|
|
BytesUtils::convertBoolean(parsed)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (accept(Token::HexNumber))
|
|
|
|
{
|
|
|
|
if (isSigned)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Invalid hex number literal."));
|
2019-08-07 10:22:52 +00:00
|
|
|
|
|
|
|
parameter.abiType = ABIType{ABIType::Hex, ABIType::AlignRight, 32};
|
|
|
|
string parsed = parseHexNumber();
|
|
|
|
parameter.rawString += parsed;
|
|
|
|
parameter.rawBytes = BytesUtils::applyAlign(
|
|
|
|
parameter.alignment,
|
|
|
|
parameter.abiType,
|
|
|
|
BytesUtils::convertHexNumber(parsed)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (accept(Token::Hex, true))
|
|
|
|
{
|
|
|
|
if (isSigned)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Invalid hex string literal."));
|
2019-07-04 13:23:47 +00:00
|
|
|
if (parameter.alignment != Parameter::Alignment::None)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Hex string literals cannot be aligned or padded."));
|
2019-08-07 10:22:52 +00:00
|
|
|
|
|
|
|
string parsed = parseString();
|
|
|
|
parameter.rawString += "hex\"" + parsed + "\"";
|
|
|
|
parameter.rawBytes = BytesUtils::convertHexNumber(parsed);
|
|
|
|
parameter.abiType = ABIType{
|
|
|
|
ABIType::HexString, ABIType::AlignNone, parameter.rawBytes.size()
|
|
|
|
};
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
2019-08-07 10:22:52 +00:00
|
|
|
else if (accept(Token::String))
|
|
|
|
{
|
|
|
|
if (isSigned)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Invalid string literal."));
|
2019-08-07 10:22:52 +00:00
|
|
|
if (parameter.alignment != Parameter::Alignment::None)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("String literals cannot be aligned or padded."));
|
2019-08-07 10:22:52 +00:00
|
|
|
|
|
|
|
string parsed = parseString();
|
|
|
|
parameter.abiType = ABIType{ABIType::String, ABIType::AlignLeft, parsed.size()};
|
|
|
|
parameter.rawString += "\"" + parsed + "\"";
|
|
|
|
parameter.rawBytes = BytesUtils::applyAlign(
|
|
|
|
Parameter::Alignment::Left,
|
|
|
|
parameter.abiType,
|
|
|
|
BytesUtils::convertString(parsed)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (accept(Token::Number))
|
|
|
|
{
|
|
|
|
auto type = isSigned ? ABIType::SignedDec : ABIType::UnsignedDec;
|
|
|
|
|
|
|
|
parameter.abiType = ABIType{type, ABIType::AlignRight, 32};
|
|
|
|
string parsed = parseDecimalNumber();
|
|
|
|
parameter.rawString += parsed;
|
|
|
|
if (isSigned)
|
|
|
|
parsed = "-" + parsed;
|
|
|
|
|
2021-07-27 14:32:34 +00:00
|
|
|
if (parsed.find('.') == string::npos)
|
|
|
|
parameter.rawBytes = BytesUtils::applyAlign(
|
|
|
|
parameter.alignment,
|
|
|
|
parameter.abiType,
|
|
|
|
BytesUtils::convertNumber(parsed)
|
|
|
|
);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parameter.abiType.type = isSigned ? ABIType::SignedFixedPoint : ABIType::UnsignedFixedPoint;
|
|
|
|
parameter.rawBytes = BytesUtils::convertFixedPoint(parsed, parameter.abiType.fractionalDigits);
|
|
|
|
}
|
2019-08-07 10:22:52 +00:00
|
|
|
}
|
|
|
|
else if (accept(Token::Failure, true))
|
|
|
|
{
|
|
|
|
if (isSigned)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Invalid failure literal."));
|
2019-08-07 10:22:52 +00:00
|
|
|
|
|
|
|
parameter.abiType = ABIType{ABIType::Failure, ABIType::AlignRight, 0};
|
|
|
|
parameter.rawBytes = bytes{};
|
|
|
|
}
|
|
|
|
if (parameter.alignment != Parameter::Alignment::None)
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-08-07 10:22:52 +00:00
|
|
|
expect(Token::RParen);
|
|
|
|
parameter.rawString += formatToken(Token::RParen);
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
2019-07-04 13:23:47 +00:00
|
|
|
|
|
|
|
return parameter;
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
string TestFileParser::parseIdentifierOrTuple()
|
|
|
|
{
|
|
|
|
string identOrTuple;
|
|
|
|
|
2019-03-07 15:58:51 +00:00
|
|
|
auto parseArrayDimensions = [&]()
|
2019-02-05 15:52:19 +00:00
|
|
|
{
|
2019-03-06 15:47:00 +00:00
|
|
|
while (accept(Token::LBrack))
|
|
|
|
{
|
|
|
|
identOrTuple += formatToken(Token::LBrack);
|
|
|
|
expect(Token::LBrack);
|
|
|
|
if (accept(Token::Number))
|
|
|
|
identOrTuple += parseDecimalNumber();
|
|
|
|
identOrTuple += formatToken(Token::RBrack);
|
|
|
|
expect(Token::RBrack);
|
|
|
|
}
|
2019-03-07 15:58:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (accept(Token::Identifier))
|
|
|
|
{
|
|
|
|
identOrTuple = m_scanner.currentLiteral();
|
|
|
|
expect(Token::Identifier);
|
|
|
|
parseArrayDimensions();
|
2019-02-05 15:52:19 +00:00
|
|
|
return identOrTuple;
|
|
|
|
}
|
|
|
|
expect(Token::LParen);
|
|
|
|
identOrTuple += formatToken(Token::LParen);
|
|
|
|
identOrTuple += parseIdentifierOrTuple();
|
|
|
|
|
|
|
|
while (accept(Token::Comma))
|
|
|
|
{
|
|
|
|
identOrTuple += formatToken(Token::Comma);
|
|
|
|
expect(Token::Comma);
|
|
|
|
identOrTuple += parseIdentifierOrTuple();
|
|
|
|
}
|
|
|
|
expect(Token::RParen);
|
|
|
|
identOrTuple += formatToken(Token::RParen);
|
2019-03-07 15:58:51 +00:00
|
|
|
|
|
|
|
parseArrayDimensions();
|
2019-02-05 15:52:19 +00:00
|
|
|
return identOrTuple;
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:25:12 +00:00
|
|
|
string TestFileParser::parseBoolean()
|
|
|
|
{
|
|
|
|
string literal = m_scanner.currentLiteral();
|
|
|
|
expect(Token::Boolean);
|
|
|
|
return literal;
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:29:34 +00:00
|
|
|
string TestFileParser::parseComment()
|
|
|
|
{
|
|
|
|
string comment = m_scanner.currentLiteral();
|
2019-02-05 15:52:19 +00:00
|
|
|
if (accept(Token::Comment, true))
|
2019-02-01 05:29:34 +00:00
|
|
|
return comment;
|
|
|
|
return string{};
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:13:14 +00:00
|
|
|
string TestFileParser::parseDecimalNumber()
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
|
|
|
string literal = m_scanner.currentLiteral();
|
2019-02-05 15:52:19 +00:00
|
|
|
expect(Token::Number);
|
2019-01-24 09:48:01 +00:00
|
|
|
return literal;
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:13:14 +00:00
|
|
|
string TestFileParser::parseHexNumber()
|
|
|
|
{
|
|
|
|
string literal = m_scanner.currentLiteral();
|
|
|
|
expect(Token::HexNumber);
|
|
|
|
return literal;
|
|
|
|
}
|
|
|
|
|
2019-05-06 08:08:10 +00:00
|
|
|
string TestFileParser::parseString()
|
|
|
|
{
|
|
|
|
string literal = m_scanner.currentLiteral();
|
|
|
|
expect(Token::String);
|
|
|
|
return literal;
|
|
|
|
}
|
|
|
|
|
2019-01-24 09:48:01 +00:00
|
|
|
void TestFileParser::Scanner::readStream(istream& _stream)
|
|
|
|
{
|
|
|
|
std::string line;
|
2021-04-28 21:35:59 +00:00
|
|
|
// TODO: std::getline(..) removes newlines '\n', if present. This could be improved.
|
2019-01-24 09:48:01 +00:00
|
|
|
while (std::getline(_stream, line))
|
2021-04-28 21:35:59 +00:00
|
|
|
m_source += line;
|
|
|
|
m_char = m_source.begin();
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileParser::Scanner::scanNextToken()
|
|
|
|
{
|
2019-02-05 15:52:19 +00:00
|
|
|
// Make code coverage happy.
|
2021-01-08 03:45:19 +00:00
|
|
|
soltestAssert(formatToken(Token::NUM_TOKENS).empty(), "");
|
2019-02-05 15:52:19 +00:00
|
|
|
|
2020-11-03 20:19:02 +00:00
|
|
|
auto detectKeyword = [](std::string const& _literal = "") -> std::pair<Token, std::string> {
|
|
|
|
if (_literal == "true") return {Token::Boolean, "true"};
|
|
|
|
if (_literal == "false") return {Token::Boolean, "false"};
|
|
|
|
if (_literal == "ether") return {Token::Ether, ""};
|
|
|
|
if (_literal == "wei") return {Token::Wei, ""};
|
|
|
|
if (_literal == "left") return {Token::Left, ""};
|
|
|
|
if (_literal == "library") return {Token::Library, ""};
|
|
|
|
if (_literal == "right") return {Token::Right, ""};
|
|
|
|
if (_literal == "hex") return {Token::Hex, ""};
|
|
|
|
if (_literal == "FAILURE") return {Token::Failure, ""};
|
2021-03-09 20:26:36 +00:00
|
|
|
if (_literal == "gas") return {Token::Gas, ""};
|
2020-11-03 20:19:02 +00:00
|
|
|
return {Token::Identifier, _literal};
|
2019-01-24 09:48:01 +00:00
|
|
|
};
|
|
|
|
|
2020-11-03 20:19:02 +00:00
|
|
|
auto selectToken = [this](Token _token, std::string const& _literal = "") {
|
2019-01-24 09:48:01 +00:00
|
|
|
advance();
|
2020-11-03 20:19:02 +00:00
|
|
|
m_currentToken = _token;
|
|
|
|
m_currentLiteral = _literal;
|
2019-01-24 09:48:01 +00:00
|
|
|
};
|
|
|
|
|
2020-11-03 20:19:02 +00:00
|
|
|
m_currentToken = Token::Unknown;
|
|
|
|
m_currentLiteral = "";
|
2019-02-01 05:29:34 +00:00
|
|
|
do
|
|
|
|
{
|
2019-01-24 09:48:01 +00:00
|
|
|
switch(current())
|
|
|
|
{
|
|
|
|
case '/':
|
|
|
|
advance();
|
|
|
|
if (current() == '/')
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::Newline);
|
2019-02-05 15:52:19 +00:00
|
|
|
else
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::Invalid);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if (peek() == '>')
|
|
|
|
{
|
|
|
|
advance();
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::Arrow);
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::Sub);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
2021-04-28 21:35:59 +00:00
|
|
|
case '~':
|
|
|
|
advance();
|
|
|
|
selectToken(Token::Tilde, readLine());
|
|
|
|
break;
|
2019-01-24 09:48:01 +00:00
|
|
|
case ':':
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::Colon);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case '#':
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::Comment, scanComment());
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case ',':
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::Comma);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case '(':
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::LParen);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case ')':
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::RParen);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
2019-03-06 15:47:00 +00:00
|
|
|
case '[':
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::LBrack);
|
2019-03-06 15:47:00 +00:00
|
|
|
break;
|
|
|
|
case ']':
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::RBrack);
|
2019-03-06 15:47:00 +00:00
|
|
|
break;
|
2019-02-25 17:10:09 +00:00
|
|
|
case '\"':
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::String, scanString());
|
2019-02-25 17:10:09 +00:00
|
|
|
break;
|
2019-01-24 09:48:01 +00:00
|
|
|
default:
|
2020-11-03 23:39:29 +00:00
|
|
|
if (langutil::isIdentifierStart(current()))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2020-11-03 20:19:02 +00:00
|
|
|
std::tie(m_currentToken, m_currentLiteral) = detectKeyword(scanIdentifierOrKeyword());
|
|
|
|
advance();
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
2020-11-03 23:39:29 +00:00
|
|
|
else if (langutil::isDecimalDigit(current()))
|
2019-02-21 00:13:14 +00:00
|
|
|
{
|
|
|
|
if (current() == '0' && peek() == 'x')
|
|
|
|
{
|
|
|
|
advance();
|
|
|
|
advance();
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::HexNumber, "0x" + scanHexNumber());
|
2019-02-21 00:13:14 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::Number, scanDecimalNumber());
|
2019-02-21 00:13:14 +00:00
|
|
|
}
|
2020-11-03 23:39:29 +00:00
|
|
|
else if (langutil::isWhiteSpace(current()))
|
2020-11-03 20:19:02 +00:00
|
|
|
selectToken(Token::Whitespace);
|
2021-04-28 21:35:59 +00:00
|
|
|
else if (isEndOfFile())
|
2020-11-03 20:19:02 +00:00
|
|
|
{
|
|
|
|
m_currentToken = Token::EOS;
|
|
|
|
m_currentLiteral = "";
|
|
|
|
}
|
2019-04-11 09:48:35 +00:00
|
|
|
else
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Unexpected character: '" + string{current()} + "'"));
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-11-03 20:19:02 +00:00
|
|
|
while (m_currentToken == Token::Whitespace);
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 21:35:59 +00:00
|
|
|
string TestFileParser::Scanner::readLine()
|
|
|
|
{
|
|
|
|
string line;
|
|
|
|
// Right now the scanner discards all (real) new-lines '\n' in TestFileParser::Scanner::readStream(..).
|
|
|
|
// Token::NewLine is defined as `//`, and NOT '\n'. We are just searching here for the next `/`.
|
|
|
|
// Note that `/` anywhere else than at the beginning of a line is currently forbidden (TODO: until we fix newline handling).
|
|
|
|
// Once the end of the file would be reached (or beyond), peek() will return '\0'.
|
|
|
|
while (peek() != '\0' && peek() != '/')
|
|
|
|
{
|
|
|
|
advance();
|
|
|
|
line += current();
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2019-01-24 09:48:01 +00:00
|
|
|
string TestFileParser::Scanner::scanComment()
|
|
|
|
{
|
|
|
|
string comment;
|
|
|
|
advance();
|
|
|
|
|
|
|
|
while (current() != '#')
|
|
|
|
{
|
|
|
|
comment += current();
|
|
|
|
advance();
|
|
|
|
}
|
|
|
|
return comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestFileParser::Scanner::scanIdentifierOrKeyword()
|
|
|
|
{
|
|
|
|
string identifier;
|
|
|
|
identifier += current();
|
2019-02-21 00:04:34 +00:00
|
|
|
while (langutil::isIdentifierPart(peek()))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
|
|
|
advance();
|
|
|
|
identifier += current();
|
|
|
|
}
|
|
|
|
return identifier;
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:13:14 +00:00
|
|
|
string TestFileParser::Scanner::scanDecimalNumber()
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
|
|
|
string number;
|
|
|
|
number += current();
|
2021-07-27 14:32:34 +00:00
|
|
|
while (langutil::isDecimalDigit(peek()) || '.' == peek())
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
|
|
|
advance();
|
|
|
|
number += current();
|
|
|
|
}
|
|
|
|
return number;
|
|
|
|
}
|
2019-02-21 00:13:14 +00:00
|
|
|
|
|
|
|
string TestFileParser::Scanner::scanHexNumber()
|
|
|
|
{
|
|
|
|
string number;
|
|
|
|
number += current();
|
|
|
|
while (langutil::isHexDigit(peek()))
|
|
|
|
{
|
|
|
|
advance();
|
|
|
|
number += current();
|
|
|
|
}
|
|
|
|
return number;
|
|
|
|
}
|
2019-05-06 08:08:10 +00:00
|
|
|
|
|
|
|
string TestFileParser::Scanner::scanString()
|
|
|
|
{
|
|
|
|
string str;
|
|
|
|
advance();
|
|
|
|
|
|
|
|
while (current() != '\"')
|
|
|
|
{
|
2019-07-10 15:11:03 +00:00
|
|
|
if (current() == '\\')
|
|
|
|
{
|
|
|
|
advance();
|
|
|
|
switch (current())
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
str += current();
|
|
|
|
advance();
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
str += '\n';
|
|
|
|
advance();
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
str += '\r';
|
|
|
|
advance();
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
str += '\t';
|
|
|
|
advance();
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
str += '\0';
|
|
|
|
advance();
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
str += scanHexPart();
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Invalid or escape sequence found in string literal."));
|
2019-07-10 15:11:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str += current();
|
|
|
|
advance();
|
|
|
|
}
|
2019-05-06 08:08:10 +00:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
2019-07-10 15:11:03 +00:00
|
|
|
|
2019-12-12 23:39:29 +00:00
|
|
|
// TODO: use fromHex() from CommonData
|
2019-07-10 15:11:03 +00:00
|
|
|
char TestFileParser::Scanner::scanHexPart()
|
|
|
|
{
|
|
|
|
advance(); // skip 'x'
|
|
|
|
|
2019-12-12 23:39:29 +00:00
|
|
|
int value{};
|
2019-07-10 15:11:03 +00:00
|
|
|
if (isdigit(current()))
|
|
|
|
value = current() - '0';
|
|
|
|
else if (tolower(current()) >= 'a' && tolower(current()) <= 'f')
|
|
|
|
value = tolower(current()) - 'a' + 10;
|
|
|
|
else
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("\\x used with no following hex digits."));
|
2019-07-10 15:11:03 +00:00
|
|
|
|
|
|
|
advance();
|
|
|
|
if (current() == '"')
|
2019-12-12 23:39:29 +00:00
|
|
|
return static_cast<char>(value);
|
2019-07-10 15:11:03 +00:00
|
|
|
|
|
|
|
value <<= 4;
|
|
|
|
if (isdigit(current()))
|
|
|
|
value |= current() - '0';
|
|
|
|
else if (tolower(current()) >= 'a' && tolower(current()) <= 'f')
|
|
|
|
value |= tolower(current()) - 'a' + 10;
|
|
|
|
|
|
|
|
advance();
|
|
|
|
|
2019-12-12 23:39:29 +00:00
|
|
|
return static_cast<char>(value);
|
2019-07-10 15:11:03 +00:00
|
|
|
}
|
2021-01-08 03:45:19 +00:00
|
|
|
|
2021-05-19 20:50:00 +00:00
|
|
|
bool TestFileParser::isBuiltinFunction(std::string const& _signature)
|
2021-01-08 03:45:19 +00:00
|
|
|
{
|
2021-05-19 20:50:00 +00:00
|
|
|
return m_builtins.count(_signature) > 0;
|
2021-01-08 03:45:19 +00:00
|
|
|
}
|