mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #2205 from ethereum/julia-strict-parser
Stricter parser for Julia
This commit is contained in:
commit
b0f2a5c162
@ -84,6 +84,7 @@ assembly::Statement Parser::parseStatement()
|
||||
}
|
||||
case Token::Return: // opcode
|
||||
case Token::Byte: // opcode
|
||||
case Token::Address: // opcode
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -91,7 +92,7 @@ assembly::Statement Parser::parseStatement()
|
||||
// Simple instruction (might turn into functional),
|
||||
// literal,
|
||||
// identifier (might turn into label or functional assignment)
|
||||
Statement statement(parseElementaryOperation());
|
||||
Statement statement(parseElementaryOperation(false));
|
||||
switch (m_scanner->currentToken())
|
||||
{
|
||||
case Token::LParen:
|
||||
@ -119,12 +120,16 @@ assembly::Statement Parser::parseStatement()
|
||||
else
|
||||
{
|
||||
// label
|
||||
if (m_julia)
|
||||
fatalParserError("Labels are not supported.");
|
||||
Label label = createWithLocation<Label>(identifier.location);
|
||||
label.name = identifier.name;
|
||||
return label;
|
||||
}
|
||||
}
|
||||
default:
|
||||
if (m_julia)
|
||||
fatalParserError("Call or assignment expected.");
|
||||
break;
|
||||
}
|
||||
return statement;
|
||||
@ -209,7 +214,11 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
|
||||
break;
|
||||
}
|
||||
default:
|
||||
fatalParserError("Expected elementary inline assembly operation.");
|
||||
fatalParserError(
|
||||
m_julia ?
|
||||
"Literal or identifier expected." :
|
||||
"Expected elementary inline assembly operation."
|
||||
);
|
||||
}
|
||||
m_scanner->next();
|
||||
return ret;
|
||||
@ -317,7 +326,11 @@ assembly::Statement Parser::parseFunctionalInstruction(assembly::Statement&& _in
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
fatalParserError("Assembly instruction or function name required in front of \"(\")");
|
||||
fatalParserError(
|
||||
m_julia ?
|
||||
"Function name expected." :
|
||||
"Assembly instruction or function name required in front of \"(\")"
|
||||
);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
@ -21,8 +21,8 @@
|
||||
*/
|
||||
|
||||
#include <libsolidity/inlineasm/AsmPrinter.h>
|
||||
|
||||
#include <libsolidity/inlineasm/AsmData.h>
|
||||
#include <libsolidity/interface/Utils.h>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
@ -40,6 +40,7 @@ using namespace dev::solidity::assembly;
|
||||
|
||||
string AsmPrinter::operator()(assembly::Instruction const& _instruction)
|
||||
{
|
||||
solAssert(!m_julia, "");
|
||||
return boost::to_lower_copy(instructionInfo(_instruction.instruction).name);
|
||||
}
|
||||
|
||||
@ -83,6 +84,7 @@ string AsmPrinter::operator()(assembly::Identifier const& _identifier)
|
||||
|
||||
string AsmPrinter::operator()(assembly::FunctionalInstruction const& _functionalInstruction)
|
||||
{
|
||||
solAssert(!m_julia, "");
|
||||
return
|
||||
(*this)(_functionalInstruction.instruction) +
|
||||
"(" +
|
||||
@ -94,11 +96,13 @@ string AsmPrinter::operator()(assembly::FunctionalInstruction const& _functional
|
||||
|
||||
string AsmPrinter::operator()(assembly::Label const& _label)
|
||||
{
|
||||
solAssert(!m_julia, "");
|
||||
return _label.name + ":";
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(assembly::Assignment const& _assignment)
|
||||
{
|
||||
solAssert(!m_julia, "");
|
||||
return "=: " + (*this)(_assignment.variableName);
|
||||
}
|
||||
|
||||
|
@ -45,6 +45,8 @@ struct Block;
|
||||
class AsmPrinter: public boost::static_visitor<std::string>
|
||||
{
|
||||
public:
|
||||
explicit AsmPrinter(bool _julia = false): m_julia(_julia) {}
|
||||
|
||||
std::string operator()(assembly::Instruction const& _instruction);
|
||||
std::string operator()(assembly::Literal const& _literal);
|
||||
std::string operator()(assembly::Identifier const& _identifier);
|
||||
@ -56,6 +58,9 @@ public:
|
||||
std::string operator()(assembly::FunctionDefinition const& _functionDefinition);
|
||||
std::string operator()(assembly::FunctionCall const& _functionCall);
|
||||
std::string operator()(assembly::Block const& _block);
|
||||
|
||||
private:
|
||||
bool m_julia = false;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ aux_source_directory(libevmasm SRC_LIST)
|
||||
aux_source_directory(libsolidity SRC_LIST)
|
||||
aux_source_directory(contracts SRC_LIST)
|
||||
aux_source_directory(liblll SRC_LIST)
|
||||
aux_source_directory(libjulia SRC_LIST)
|
||||
|
||||
list(REMOVE_ITEM SRC_LIST "./fuzzer.cpp")
|
||||
|
||||
|
192
test/libjulia/Parser.cpp
Normal file
192
test/libjulia/Parser.cpp
Normal file
@ -0,0 +1,192 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* @date 2017
|
||||
* Unit tests for parsing Julia.
|
||||
*/
|
||||
|
||||
#include "../TestHelper.h"
|
||||
|
||||
#include <libsolidity/inlineasm/AsmParser.h>
|
||||
#include <libsolidity/inlineasm/AsmAnalysis.h>
|
||||
#include <libsolidity/inlineasm/AsmAnalysisInfo.h>
|
||||
#include <libsolidity/parsing/Scanner.h>
|
||||
#include <test/libsolidity/ErrorCheck.h>
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace dev
|
||||
{
|
||||
namespace solidity
|
||||
{
|
||||
namespace test
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
bool parse(string const& _source, ErrorList& errors)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto scanner = make_shared<Scanner>(CharStream(_source));
|
||||
auto parserResult = assembly::Parser(errors, true).parse(scanner);
|
||||
if (parserResult)
|
||||
return true;
|
||||
}
|
||||
catch (FatalError const&)
|
||||
{
|
||||
BOOST_FAIL("Fatal error leaked.");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boost::optional<Error> parseAndReturnFirstError(string const& _source, bool _allowWarnings = true)
|
||||
{
|
||||
ErrorList errors;
|
||||
if (!parse(_source, errors))
|
||||
{
|
||||
BOOST_REQUIRE_EQUAL(errors.size(), 1);
|
||||
return *errors.front();
|
||||
}
|
||||
else
|
||||
{
|
||||
// If success is true, there might still be an error in the assembly stage.
|
||||
if (_allowWarnings && Error::containsOnlyWarnings(errors))
|
||||
return {};
|
||||
else if (!errors.empty())
|
||||
{
|
||||
if (!_allowWarnings)
|
||||
BOOST_CHECK_EQUAL(errors.size(), 1);
|
||||
return *errors.front();
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
bool successParse(std::string const& _source, bool _allowWarnings = true)
|
||||
{
|
||||
return !parseAndReturnFirstError(_source, _allowWarnings);
|
||||
}
|
||||
|
||||
bool successAssemble(string const& _source, bool _allowWarnings = true)
|
||||
{
|
||||
return successParse(_source, _allowWarnings);
|
||||
}
|
||||
|
||||
Error expectError(std::string const& _source, bool _allowWarnings = false)
|
||||
{
|
||||
|
||||
auto error = parseAndReturnFirstError(_source, _allowWarnings);
|
||||
BOOST_REQUIRE(error);
|
||||
return *error;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#define CHECK_ERROR(text, typ, substring) \
|
||||
do \
|
||||
{ \
|
||||
Error err = expectError((text), false); \
|
||||
BOOST_CHECK(err.type() == (Error::Type::typ)); \
|
||||
BOOST_CHECK(searchErrorMessage(err, (substring))); \
|
||||
} while(0)
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(JuliaParser)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(smoke_test)
|
||||
{
|
||||
BOOST_CHECK(successParse("{ }"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(vardecl)
|
||||
{
|
||||
BOOST_CHECK(successParse("{ let x := 7 }"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(assignment)
|
||||
{
|
||||
BOOST_CHECK(successParse("{ let x := 2 let y := x }"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_call)
|
||||
{
|
||||
BOOST_CHECK(successParse("{ fun() fun(fun()) }"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(vardecl_complex)
|
||||
{
|
||||
BOOST_CHECK(successParse("{ let y := 2 let x := add(7, mul(6, y)) }"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(blocks)
|
||||
{
|
||||
BOOST_CHECK(successParse("{ let x := 7 { let y := 3 } { let z := 2 } }"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_definitions)
|
||||
{
|
||||
BOOST_CHECK(successParse("{ function f() { } function g(a) -> x { } }"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_definitions_multiple_args)
|
||||
{
|
||||
BOOST_CHECK(successParse("{ function f(a, d) { } function g(a, d) -> x, y { } }"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_calls)
|
||||
{
|
||||
BOOST_CHECK(successParse("{ function f(a) -> b {} function g(a, b, c) {} function x() { g(1, 2, f(mul(2, 3))) x() } }"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(label)
|
||||
{
|
||||
CHECK_ERROR("{ label: }", ParserError, "Labels are not supported.");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(instructions)
|
||||
{
|
||||
CHECK_ERROR("{ pop }", ParserError, "Call or assignment expected.");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(push)
|
||||
{
|
||||
CHECK_ERROR("{ 0x42 }", ParserError, "Call or assignment expected.");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(assign_from_stack)
|
||||
{
|
||||
CHECK_ERROR("{ =: x }", ParserError, "Literal or identifier expected.");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(empty_call)
|
||||
{
|
||||
CHECK_ERROR("{ () }", ParserError, "Literal or identifier expected.");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
}
|
||||
} // end namespaces
|
Loading…
Reference in New Issue
Block a user