Merge pull request #10212 from ethereum/testfileparser

Refactor and simplify TestFileParser
This commit is contained in:
chriseth 2020-11-10 13:15:35 +01:00 committed by GitHub
commit 3db94fbc48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 61 deletions

View File

@ -36,11 +36,9 @@
using namespace solidity;
using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace std;
using namespace soltest;
bytes BytesUtils::alignLeft(bytes _bytes)
{

View File

@ -41,7 +41,6 @@ using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::frontend::test;
using namespace std;
using namespace soltest;
namespace
{

View File

@ -16,7 +16,6 @@
#include <libsolutil/AnsiColorized.h>
#include <libsolutil/CommonData.h>
#include <libsolidity/ast/Types.h>
namespace solidity::frontend::test
{

View File

@ -34,11 +34,11 @@
#include <stdexcept>
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace std;
using namespace soltest;
using Token = soltest::Token;
char TestFileParser::Scanner::peek() const noexcept
{
@ -158,7 +158,7 @@ vector<solidity::frontend::test::FunctionCall> TestFileParser::parseFunctionCall
return calls;
}
bool TestFileParser::accept(soltest::Token _token, bool const _expect)
bool TestFileParser::accept(Token _token, bool const _expect)
{
if (m_scanner.currentToken() != _token)
return false;
@ -167,7 +167,7 @@ bool TestFileParser::accept(soltest::Token _token, bool const _expect)
return true;
}
bool TestFileParser::expect(soltest::Token _token, bool const _advance)
bool TestFileParser::expect(Token _token, bool const _advance)
{
if (m_scanner.currentToken() != _token || m_scanner.currentToken() == Token::Invalid)
throw TestParserError(
@ -484,31 +484,31 @@ void TestFileParser::Scanner::readStream(istream& _stream)
void TestFileParser::Scanner::scanNextToken()
{
using namespace langutil;
// Make code coverage happy.
assert(formatToken(Token::NUM_TOKENS) == "");
auto detectKeyword = [](std::string const& _literal = "") -> TokenDesc {
if (_literal == "true") return TokenDesc{Token::Boolean, _literal};
if (_literal == "false") return TokenDesc{Token::Boolean, _literal};
if (_literal == "ether") return TokenDesc{Token::Ether, _literal};
if (_literal == "wei") return TokenDesc{Token::Wei, _literal};
if (_literal == "left") return TokenDesc{Token::Left, _literal};
if (_literal == "library") return TokenDesc{Token::Library, _literal};
if (_literal == "right") return TokenDesc{Token::Right, _literal};
if (_literal == "hex") return TokenDesc{Token::Hex, _literal};
if (_literal == "FAILURE") return TokenDesc{Token::Failure, _literal};
if (_literal == "storage") return TokenDesc{Token::Storage, _literal};
return TokenDesc{Token::Identifier, _literal};
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, ""};
if (_literal == "storage") return {Token::Storage, ""};
return {Token::Identifier, _literal};
};
auto selectToken = [this](Token _token, std::optional<std::string> _literal = std::nullopt) -> TokenDesc {
auto selectToken = [this](Token _token, std::string const& _literal = "") {
advance();
return make_pair(_token, _literal.has_value() ? *_literal : formatToken(_token));
m_currentToken = _token;
m_currentLiteral = _literal;
};
TokenDesc token = make_pair(Token::Unknown, "");
m_currentToken = Token::Unknown;
m_currentLiteral = "";
do
{
switch(current())
@ -516,71 +516,73 @@ void TestFileParser::Scanner::scanNextToken()
case '/':
advance();
if (current() == '/')
token = selectToken(Token::Newline);
selectToken(Token::Newline);
else
token = selectToken(Token::Invalid);
selectToken(Token::Invalid);
break;
case '-':
if (peek() == '>')
{
advance();
token = selectToken(Token::Arrow);
selectToken(Token::Arrow);
}
else
token = selectToken(Token::Sub);
selectToken(Token::Sub);
break;
case ':':
token = selectToken(Token::Colon);
selectToken(Token::Colon);
break;
case '#':
token = selectToken(Token::Comment, scanComment());
selectToken(Token::Comment, scanComment());
break;
case ',':
token = selectToken(Token::Comma);
selectToken(Token::Comma);
break;
case '(':
token = selectToken(Token::LParen);
selectToken(Token::LParen);
break;
case ')':
token = selectToken(Token::RParen);
selectToken(Token::RParen);
break;
case '[':
token = selectToken(Token::LBrack);
selectToken(Token::LBrack);
break;
case ']':
token = selectToken(Token::RBrack);
selectToken(Token::RBrack);
break;
case '\"':
token = selectToken(Token::String, scanString());
selectToken(Token::String, scanString());
break;
default:
if (isIdentifierStart(current()))
if (langutil::isIdentifierStart(current()))
{
TokenDesc detectedToken = detectKeyword(scanIdentifierOrKeyword());
token = selectToken(detectedToken.first, detectedToken.second);
std::tie(m_currentToken, m_currentLiteral) = detectKeyword(scanIdentifierOrKeyword());
advance();
}
else if (isDecimalDigit(current()))
else if (langutil::isDecimalDigit(current()))
{
if (current() == '0' && peek() == 'x')
{
advance();
advance();
token = selectToken(Token::HexNumber, "0x" + scanHexNumber());
selectToken(Token::HexNumber, "0x" + scanHexNumber());
}
else
token = selectToken(Token::Number, scanDecimalNumber());
selectToken(Token::Number, scanDecimalNumber());
}
else if (isWhiteSpace(current()))
token = selectToken(Token::Whitespace);
else if (langutil::isWhiteSpace(current()))
selectToken(Token::Whitespace);
else if (isEndOfLine())
token = make_pair(Token::EOS, "EOS");
{
m_currentToken = Token::EOS;
m_currentLiteral = "";
}
else
throw TestParserError("Unexpected character: '" + string{current()} + "'");
break;
}
}
while (token.first == Token::Whitespace);
m_currentToken = token;
while (m_currentToken == Token::Whitespace);
}
string TestFileParser::Scanner::scanComment()

View File

@ -15,7 +15,6 @@
#pragma once
#include <libsolutil/CommonData.h>
#include <libsolidity/ast/Types.h>
#include <liblangutil/Exceptions.h>
#include <test/libsolidity/util/SoltestTypes.h>
@ -64,7 +63,6 @@ public:
std::vector<FunctionCall> parseFunctionCalls(std::size_t _lineOffset);
private:
using Token = soltest::Token;
/**
* Token scanner that is used internally to abstract away character traversal.
*/
@ -81,8 +79,8 @@ private:
/// Reads character stream and creates token.
void scanNextToken();
soltest::Token currentToken() { return m_currentToken.first; }
std::string currentLiteral() { return m_currentToken.second; }
soltest::Token currentToken() { return m_currentToken; }
std::string currentLiteral() { return m_currentLiteral; }
std::string scanComment();
std::string scanIdentifierOrKeyword();
@ -92,8 +90,6 @@ private:
char scanHexPart();
private:
using TokenDesc = std::pair<Token, std::string>;
/// Advances current position in the input stream.
void advance(unsigned n = 1)
{
@ -120,7 +116,8 @@ private:
std::string m_line;
std::string::const_iterator m_char;
TokenDesc m_currentToken;
std::string m_currentLiteral;
soltest::Token m_currentToken = soltest::Token::Unknown;
};
bool accept(soltest::Token _token, bool const _expect = false);

View File

@ -30,6 +30,8 @@ using namespace solidity::util;
using namespace solidity::frontend::test;
using namespace std;
using Token = soltest::Token;
string TestFunctionCall::format(
ErrorReporter& _errorReporter,
string const& _linePrefix,
@ -37,9 +39,6 @@ string TestFunctionCall::format(
bool const _highlight
) const
{
using namespace soltest;
using Token = soltest::Token;
stringstream stream;
bool highlight = !matchesExpectation() && _highlight;
@ -279,8 +278,6 @@ string TestFunctionCall::formatFailure(
bool _highlight
) const
{
using Token = soltest::Token;
stringstream os;
os << formatToken(Token::Failure);

View File

@ -17,7 +17,6 @@
#include <test/libsolidity/util/TestFileParser.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <libsolidity/ast/Types.h>
#include <liblangutil/Exceptions.h>
#include <libsolutil/AnsiColorized.h>
#include <libsolutil/CommonData.h>