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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/libsolidity/util/TestFileParser.h>
|
2019-07-03 19:37:55 +00:00
|
|
|
|
|
|
|
#include <test/libsolidity/util/BytesUtils.h>
|
2019-01-24 09:48:01 +00:00
|
|
|
#include <test/Options.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/optional.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>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace dev;
|
|
|
|
using namespace langutil;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace dev::solidity::test;
|
|
|
|
using namespace std;
|
2019-02-05 15:52:19 +00:00
|
|
|
using namespace soltest;
|
2019-01-24 09:48:01 +00:00
|
|
|
|
2019-02-28 13:16:16 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
enum class DeclaredAlignment
|
|
|
|
{
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
None,
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bytes alignLeft(bytes _bytes)
|
|
|
|
{
|
|
|
|
return std::move(_bytes) + bytes(32 - _bytes.size(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bytes alignRight(bytes _bytes)
|
|
|
|
{
|
|
|
|
return bytes(32 - _bytes.size(), 0) + std::move(_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bytes applyAlign(DeclaredAlignment _alignment, ABIType& _abiType, bytes _converted)
|
|
|
|
{
|
|
|
|
if (_alignment != DeclaredAlignment::None)
|
|
|
|
_abiType.alignDeclared = true;
|
|
|
|
|
|
|
|
switch (_alignment)
|
|
|
|
{
|
|
|
|
case DeclaredAlignment::Left:
|
|
|
|
_abiType.align = ABIType::AlignLeft;
|
|
|
|
return alignLeft(std::move(_converted));
|
|
|
|
case DeclaredAlignment::Right:
|
|
|
|
_abiType.align = ABIType::AlignRight;
|
|
|
|
return alignRight(std::move(_converted));
|
|
|
|
default:
|
|
|
|
_abiType.align = ABIType::AlignRight;
|
|
|
|
return alignRight(std::move(_converted));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 15:25:35 +00:00
|
|
|
char TestFileParser::Scanner::peek() const noexcept
|
|
|
|
{
|
|
|
|
if (std::distance(m_char, m_line.end()) < 2)
|
|
|
|
return '\0';
|
|
|
|
|
|
|
|
auto next = m_char;
|
|
|
|
std::advance(next, 1);
|
|
|
|
return *next;
|
|
|
|
}
|
|
|
|
|
2019-01-24 09:48:01 +00:00
|
|
|
vector<dev::solidity::test::FunctionCall> TestFileParser::parseFunctionCalls()
|
|
|
|
{
|
|
|
|
vector<FunctionCall> calls;
|
2019-02-05 15:52:19 +00:00
|
|
|
if (!accept(Token::EOS))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-05 15:52:19 +00:00
|
|
|
assert(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
|
|
|
{
|
|
|
|
FunctionCall call;
|
|
|
|
|
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-02-05 15:52:19 +00:00
|
|
|
accept(Token::Newline, true);
|
2019-01-24 09:48:01 +00:00
|
|
|
|
2019-02-02 13:22:46 +00:00
|
|
|
call.signature = parseFunctionSignature();
|
2019-02-05 15:52:19 +00:00
|
|
|
if (accept(Token::Comma, true))
|
2019-01-24 09:48:01 +00:00
|
|
|
call.value = parseFunctionCallValue();
|
2019-02-05 15:52:19 +00:00
|
|
|
if (accept(Token::Colon, true))
|
2019-01-24 09:48:01 +00:00
|
|
|
call.arguments = parseFunctionCallArguments();
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
if (accept(Token::Newline, true))
|
2019-02-02 13:22:46 +00:00
|
|
|
call.displayMode = FunctionCall::DisplayMode::MultiLine;
|
|
|
|
|
2019-02-01 05:29:34 +00:00
|
|
|
call.arguments.comment = parseComment();
|
2019-01-24 09:48:01 +00:00
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
if (accept(Token::Newline, true))
|
2019-02-01 05:29:34 +00:00
|
|
|
call.displayMode = FunctionCall::DisplayMode::MultiLine;
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
expect(Token::Arrow);
|
2019-02-01 05:29:34 +00:00
|
|
|
call.expectations = parseFunctionCallExpectations();
|
2019-02-06 11:10:48 +00:00
|
|
|
|
|
|
|
accept(Token::Newline, true);
|
2019-02-01 05:29:34 +00:00
|
|
|
call.expectations.comment = parseComment();
|
|
|
|
|
2019-05-15 10:01:14 +00:00
|
|
|
if (call.signature == "constructor()")
|
|
|
|
call.isConstructor = true;
|
|
|
|
|
2019-01-24 09:48:01 +00:00
|
|
|
calls.emplace_back(std::move(call));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return calls;
|
|
|
|
}
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
bool TestFileParser::accept(soltest::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
|
|
|
}
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
bool TestFileParser::expect(soltest::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)
|
2019-02-02 13:22:46 +00:00
|
|
|
throw Error(
|
|
|
|
Error::Type::ParserError,
|
|
|
|
"Unexpected " + formatToken(m_scanner.currentToken()) + ": \"" +
|
|
|
|
m_scanner.currentLiteral() + "\". " +
|
|
|
|
"Expected \"" + formatToken(_token) + "\"."
|
|
|
|
);
|
2019-01-24 09:48:01 +00:00
|
|
|
if (_advance)
|
|
|
|
m_scanner.scanNextToken();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestFileParser::parseFunctionSignature()
|
|
|
|
{
|
|
|
|
string signature = m_scanner.currentLiteral();
|
2019-02-05 15:52:19 +00:00
|
|
|
expect(Token::Identifier);
|
2019-01-24 09:48:01 +00:00
|
|
|
|
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))
|
|
|
|
throw Error(Error::Type::ParserError, "Invalid signature detected: " + signature);
|
|
|
|
|
|
|
|
signature += parameters;
|
|
|
|
|
|
|
|
expect(Token::RParen);
|
|
|
|
signature += formatToken(Token::RParen);
|
2019-01-24 09:48:01 +00:00
|
|
|
return signature;
|
|
|
|
}
|
|
|
|
|
|
|
|
u256 TestFileParser::parseFunctionCallValue()
|
|
|
|
{
|
2019-02-28 13:16:16 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
u256 value{parseDecimalNumber()};
|
|
|
|
expect(Token::Ether);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
catch (std::exception const&)
|
|
|
|
{
|
|
|
|
throw Error(Error::Type::ParserError, "Ether value encoding invalid.");
|
|
|
|
}
|
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)
|
|
|
|
throw Error(Error::Type::ParserError, "No argument provided.");
|
|
|
|
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-02-01 05:29:34 +00:00
|
|
|
parameter.format.newline = true;
|
2019-07-03 19:37:55 +00:00
|
|
|
|
2019-02-01 05:29:34 +00:00
|
|
|
auto literal = parseABITypeLiteral();
|
2019-02-06 11:10:48 +00:00
|
|
|
parameter.rawBytes = get<0>(literal);
|
|
|
|
parameter.abiType = get<1>(literal);
|
|
|
|
parameter.rawString = get<2>(literal);
|
2019-07-03 19:37:55 +00:00
|
|
|
|
2019-02-01 05:29:34 +00:00
|
|
|
return parameter;
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:10:48 +00:00
|
|
|
tuple<bytes, ABIType, string> TestFileParser::parseABITypeLiteral()
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-28 13:16:16 +00:00
|
|
|
ABIType abiType{ABIType::None, ABIType::AlignNone, 0};
|
|
|
|
DeclaredAlignment alignment{DeclaredAlignment::None};
|
|
|
|
bytes result{toBigEndian(u256{0})};
|
|
|
|
string rawString;
|
|
|
|
bool isSigned = false;
|
|
|
|
|
|
|
|
if (accept(Token::Left, true))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-28 13:16:16 +00:00
|
|
|
rawString += formatToken(Token::Left);
|
|
|
|
expect(Token::LParen);
|
|
|
|
rawString += formatToken(Token::LParen);
|
|
|
|
alignment = DeclaredAlignment::Left;
|
|
|
|
}
|
|
|
|
if (accept(Token::Right, true))
|
|
|
|
{
|
|
|
|
rawString += formatToken(Token::Right);
|
|
|
|
expect(Token::LParen);
|
|
|
|
rawString += formatToken(Token::LParen);
|
|
|
|
alignment = DeclaredAlignment::Right;
|
|
|
|
}
|
2019-02-02 13:22:46 +00:00
|
|
|
|
2019-02-28 13:16:16 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (accept(Token::Sub, true))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-06 11:10:48 +00:00
|
|
|
rawString += formatToken(Token::Sub);
|
2019-02-28 13:16:16 +00:00
|
|
|
isSigned = true;
|
|
|
|
}
|
|
|
|
if (accept(Token::Boolean))
|
|
|
|
{
|
|
|
|
if (isSigned)
|
|
|
|
throw Error(Error::Type::ParserError, "Invalid boolean literal.");
|
|
|
|
abiType = ABIType{ABIType::Boolean, ABIType::AlignRight, 32};
|
|
|
|
string parsed = parseBoolean();
|
|
|
|
rawString += parsed;
|
2019-07-03 19:37:55 +00:00
|
|
|
result = applyAlign(alignment, abiType, BytesUtils().convertBoolean(parsed));
|
2019-02-28 13:16:16 +00:00
|
|
|
}
|
|
|
|
else if (accept(Token::HexNumber))
|
|
|
|
{
|
|
|
|
if (isSigned)
|
|
|
|
throw Error(Error::Type::ParserError, "Invalid hex number literal.");
|
|
|
|
abiType = ABIType{ABIType::Hex, ABIType::AlignRight, 32};
|
|
|
|
string parsed = parseHexNumber();
|
|
|
|
rawString += parsed;
|
2019-07-03 19:37:55 +00:00
|
|
|
result = applyAlign(alignment, abiType, BytesUtils().convertHexNumber(parsed));
|
2019-02-28 13:16:16 +00:00
|
|
|
}
|
2019-02-25 17:10:09 +00:00
|
|
|
else if (accept(Token::Hex, true))
|
|
|
|
{
|
|
|
|
if (isSigned)
|
|
|
|
throw Error(Error::Type::ParserError, "Invalid hex string literal.");
|
|
|
|
if (alignment != DeclaredAlignment::None)
|
|
|
|
throw Error(Error::Type::ParserError, "Hex string literals cannot be aligned or padded.");
|
2019-05-06 08:08:10 +00:00
|
|
|
string parsed = parseString();
|
2019-03-13 17:52:44 +00:00
|
|
|
rawString += "hex\"" + parsed + "\"";
|
2019-07-03 19:37:55 +00:00
|
|
|
result = BytesUtils().convertHexNumber(parsed);
|
2019-02-25 17:10:09 +00:00
|
|
|
abiType = ABIType{ABIType::HexString, ABIType::AlignNone, result.size()};
|
|
|
|
}
|
2019-05-06 08:08:10 +00:00
|
|
|
else if (accept(Token::String))
|
|
|
|
{
|
|
|
|
if (isSigned)
|
|
|
|
throw Error(Error::Type::ParserError, "Invalid string literal.");
|
|
|
|
if (alignment != DeclaredAlignment::None)
|
|
|
|
throw Error(Error::Type::ParserError, "String literals cannot be aligned or padded.");
|
|
|
|
abiType = ABIType{ABIType::String, ABIType::AlignLeft, 32};
|
|
|
|
string parsed = parseString();
|
|
|
|
rawString += "\"" + parsed + "\"";
|
2019-07-03 19:37:55 +00:00
|
|
|
result = applyAlign(DeclaredAlignment::Left, abiType, BytesUtils().convertString(parsed));
|
2019-05-06 08:08:10 +00:00
|
|
|
}
|
2019-02-28 13:16:16 +00:00
|
|
|
else if (accept(Token::Number))
|
|
|
|
{
|
|
|
|
auto type = isSigned ? ABIType::SignedDec : ABIType::UnsignedDec;
|
|
|
|
abiType = ABIType{type, ABIType::AlignRight, 32};
|
2019-02-21 00:13:14 +00:00
|
|
|
string parsed = parseDecimalNumber();
|
2019-02-06 11:10:48 +00:00
|
|
|
rawString += parsed;
|
2019-02-28 13:16:16 +00:00
|
|
|
if (isSigned)
|
|
|
|
parsed = "-" + parsed;
|
2019-07-03 19:37:55 +00:00
|
|
|
result = applyAlign(alignment, abiType, BytesUtils().convertNumber(parsed));
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
2019-02-28 13:16:16 +00:00
|
|
|
else if (accept(Token::Failure, true))
|
2019-02-01 05:29:34 +00:00
|
|
|
{
|
2019-02-28 13:16:16 +00:00
|
|
|
if (isSigned)
|
|
|
|
throw Error(Error::Type::ParserError, "Invalid failure literal.");
|
|
|
|
abiType = ABIType{ABIType::Failure, ABIType::AlignRight, 0};
|
|
|
|
result = bytes{};
|
2019-02-01 05:29:34 +00:00
|
|
|
}
|
2019-02-28 13:16:16 +00:00
|
|
|
if (alignment != DeclaredAlignment::None)
|
|
|
|
{
|
|
|
|
expect(Token::RParen);
|
|
|
|
rawString += formatToken(Token::RParen);
|
|
|
|
}
|
|
|
|
return make_tuple(result, abiType, rawString);
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
|
|
|
catch (std::exception const&)
|
|
|
|
{
|
2019-02-25 17:10:09 +00:00
|
|
|
throw Error(Error::Type::ParserError, "Literal encoding invalid.");
|
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;
|
|
|
|
while (std::getline(_stream, line))
|
|
|
|
m_line += line;
|
|
|
|
m_char = m_line.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileParser::Scanner::scanNextToken()
|
|
|
|
{
|
2019-02-25 17:10:09 +00:00
|
|
|
using namespace langutil;
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
// Make code coverage happy.
|
|
|
|
assert(formatToken(Token::NUM_TOKENS) == "");
|
|
|
|
|
|
|
|
auto detectKeyword = [](std::string const& _literal = "") -> TokenDesc {
|
2019-02-21 22:25:12 +00:00
|
|
|
if (_literal == "true") return TokenDesc{Token::Boolean, _literal};
|
|
|
|
if (_literal == "false") return TokenDesc{Token::Boolean, _literal};
|
2019-02-05 15:52:19 +00:00
|
|
|
if (_literal == "ether") return TokenDesc{Token::Ether, _literal};
|
2019-02-28 13:16:16 +00:00
|
|
|
if (_literal == "left") return TokenDesc{Token::Left, _literal};
|
|
|
|
if (_literal == "right") return TokenDesc{Token::Right, _literal};
|
2019-02-25 17:10:09 +00:00
|
|
|
if (_literal == "hex") return TokenDesc{Token::Hex, _literal};
|
2019-02-05 15:52:19 +00:00
|
|
|
if (_literal == "FAILURE") return TokenDesc{Token::Failure, _literal};
|
|
|
|
return TokenDesc{Token::Identifier, _literal};
|
2019-01-24 09:48:01 +00:00
|
|
|
};
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
auto selectToken = [this](Token _token, std::string const& _literal = "") -> TokenDesc {
|
2019-01-24 09:48:01 +00:00
|
|
|
advance();
|
|
|
|
return make_pair(_token, !_literal.empty() ? _literal : formatToken(_token));
|
|
|
|
};
|
|
|
|
|
2019-02-05 15:52:19 +00:00
|
|
|
TokenDesc token = make_pair(Token::Unknown, "");
|
2019-02-01 05:29:34 +00:00
|
|
|
do
|
|
|
|
{
|
2019-01-24 09:48:01 +00:00
|
|
|
switch(current())
|
|
|
|
{
|
|
|
|
case '/':
|
|
|
|
advance();
|
|
|
|
if (current() == '/')
|
2019-02-05 15:52:19 +00:00
|
|
|
token = selectToken(Token::Newline);
|
|
|
|
else
|
|
|
|
token = selectToken(Token::Invalid);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if (peek() == '>')
|
|
|
|
{
|
|
|
|
advance();
|
2019-02-05 15:52:19 +00:00
|
|
|
token = selectToken(Token::Arrow);
|
2019-01-24 09:48:01 +00:00
|
|
|
}
|
|
|
|
else
|
2019-02-05 15:52:19 +00:00
|
|
|
token = selectToken(Token::Sub);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case ':':
|
2019-02-05 15:52:19 +00:00
|
|
|
token = selectToken(Token::Colon);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case '#':
|
2019-02-05 15:52:19 +00:00
|
|
|
token = selectToken(Token::Comment, scanComment());
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case ',':
|
2019-02-05 15:52:19 +00:00
|
|
|
token = selectToken(Token::Comma);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case '(':
|
2019-02-05 15:52:19 +00:00
|
|
|
token = selectToken(Token::LParen);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
case ')':
|
2019-02-05 15:52:19 +00:00
|
|
|
token = selectToken(Token::RParen);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
2019-03-06 15:47:00 +00:00
|
|
|
case '[':
|
|
|
|
token = selectToken(Token::LBrack);
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
token = selectToken(Token::RBrack);
|
|
|
|
break;
|
2019-02-25 17:10:09 +00:00
|
|
|
case '\"':
|
2019-05-06 08:08:10 +00:00
|
|
|
token = selectToken(Token::String, scanString());
|
2019-02-25 17:10:09 +00:00
|
|
|
break;
|
2019-01-24 09:48:01 +00:00
|
|
|
default:
|
2019-02-25 17:10:09 +00:00
|
|
|
if (isIdentifierStart(current()))
|
2019-01-24 09:48:01 +00:00
|
|
|
{
|
2019-02-05 15:52:19 +00:00
|
|
|
TokenDesc detectedToken = detectKeyword(scanIdentifierOrKeyword());
|
2019-01-24 09:48:01 +00:00
|
|
|
token = selectToken(detectedToken.first, detectedToken.second);
|
|
|
|
}
|
2019-02-25 17:10:09 +00:00
|
|
|
else if (isDecimalDigit(current()))
|
2019-02-21 00:13:14 +00:00
|
|
|
{
|
|
|
|
if (current() == '0' && peek() == 'x')
|
|
|
|
{
|
|
|
|
advance();
|
|
|
|
advance();
|
|
|
|
token = selectToken(Token::HexNumber, "0x" + scanHexNumber());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
token = selectToken(Token::Number, scanDecimalNumber());
|
|
|
|
}
|
2019-02-25 17:10:09 +00:00
|
|
|
else if (isWhiteSpace(current()))
|
2019-02-05 15:52:19 +00:00
|
|
|
token = selectToken(Token::Whitespace);
|
2019-01-24 09:48:01 +00:00
|
|
|
else if (isEndOfLine())
|
2019-05-15 15:25:35 +00:00
|
|
|
token = make_pair(Token::EOS, "EOS");
|
2019-04-11 09:48:35 +00:00
|
|
|
else
|
|
|
|
throw Error(
|
|
|
|
Error::Type::ParserError,
|
|
|
|
"Unexpected character: '" + string{current()} + "'"
|
|
|
|
);
|
2019-01-24 09:48:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 15:52:19 +00:00
|
|
|
while (token.first == Token::Whitespace);
|
2019-01-24 09:48:01 +00:00
|
|
|
m_currentToken = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2019-02-21 00:04:34 +00:00
|
|
|
while (langutil::isDecimalDigit(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() != '\"')
|
|
|
|
{
|
|
|
|
str += current();
|
|
|
|
advance();
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|