2018-11-04 08:34:21 +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
|
2018-11-04 08:34:21 +00:00
|
|
|
/**
|
|
|
|
* Parser for Yul code and data object container.
|
|
|
|
*/
|
|
|
|
|
2021-04-27 14:53:04 +00:00
|
|
|
#include <libyul/AST.h>
|
2018-11-04 08:34:21 +00:00
|
|
|
#include <libyul/ObjectParser.h>
|
|
|
|
|
|
|
|
#include <libyul/AsmParser.h>
|
|
|
|
#include <libyul/Exceptions.h>
|
|
|
|
|
|
|
|
#include <liblangutil/Token.h>
|
2021-07-12 10:05:26 +00:00
|
|
|
#include <liblangutil/Scanner.h>
|
|
|
|
|
|
|
|
#include <libsolutil/StringUtils.h>
|
|
|
|
|
|
|
|
#include <regex>
|
2018-11-04 08:34:21 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::langutil;
|
2018-11-04 08:34:21 +00:00
|
|
|
|
|
|
|
shared_ptr<Object> ObjectParser::parse(shared_ptr<Scanner> const& _scanner, bool _reuseScanner)
|
|
|
|
{
|
|
|
|
m_recursionDepth = 0;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
shared_ptr<Object> object;
|
|
|
|
m_scanner = _scanner;
|
2021-07-12 10:05:26 +00:00
|
|
|
m_sourceNameMapping = tryParseSourceNameMapping();
|
|
|
|
|
2018-11-04 08:34:21 +00:00
|
|
|
if (currentToken() == Token::LBrace)
|
|
|
|
{
|
|
|
|
// Special case: Code-only form.
|
|
|
|
object = make_shared<Object>();
|
2018-12-10 03:25:51 +00:00
|
|
|
object->name = "object"_yulstring;
|
2018-11-04 08:34:21 +00:00
|
|
|
object->code = parseBlock();
|
|
|
|
if (!object->code)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
object = parseObject();
|
2021-07-13 14:06:07 +00:00
|
|
|
if (!_reuseScanner)
|
2018-11-04 08:34:21 +00:00
|
|
|
expectToken(Token::EOS);
|
2021-07-13 14:06:07 +00:00
|
|
|
object->debugData = make_shared<ObjectDebugData>(ObjectDebugData{m_sourceNameMapping});
|
2018-11-04 08:34:21 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
catch (FatalError const&)
|
|
|
|
{
|
|
|
|
if (m_errorReporter.errors().empty())
|
|
|
|
throw; // Something is weird here, rather throw again.
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Object> ObjectParser::parseObject(Object* _containingObject)
|
|
|
|
{
|
|
|
|
RecursionGuard guard(*this);
|
|
|
|
|
|
|
|
if (currentToken() != Token::Identifier || currentLiteral() != "object")
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(4294_error, "Expected keyword \"object\".");
|
2018-11-04 08:34:21 +00:00
|
|
|
advance();
|
|
|
|
|
|
|
|
shared_ptr<Object> ret = make_shared<Object>();
|
|
|
|
ret->name = parseUniqueName(_containingObject);
|
|
|
|
|
|
|
|
expectToken(Token::LBrace);
|
|
|
|
|
|
|
|
ret->code = parseCode();
|
|
|
|
|
|
|
|
while (currentToken() != Token::RBrace)
|
|
|
|
{
|
|
|
|
if (currentToken() == Token::Identifier && currentLiteral() == "object")
|
|
|
|
parseObject(ret.get());
|
|
|
|
else if (currentToken() == Token::Identifier && currentLiteral() == "data")
|
|
|
|
parseData(*ret);
|
|
|
|
else
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(8143_error, "Expected keyword \"data\" or \"object\" or \"}\".");
|
2018-11-04 08:34:21 +00:00
|
|
|
}
|
|
|
|
if (_containingObject)
|
|
|
|
addNamedSubObject(*_containingObject, ret->name, ret);
|
|
|
|
|
|
|
|
expectToken(Token::RBrace);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Block> ObjectParser::parseCode()
|
|
|
|
{
|
|
|
|
if (currentToken() != Token::Identifier || currentLiteral() != "code")
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(4846_error, "Expected keyword \"code\".");
|
2018-11-04 08:34:21 +00:00
|
|
|
advance();
|
|
|
|
|
|
|
|
return parseBlock();
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
optional<SourceNameMap> ObjectParser::tryParseSourceNameMapping() const
|
2021-07-12 10:05:26 +00:00
|
|
|
{
|
|
|
|
// @use-src 0:"abc.sol", 1:"foo.sol", 2:"bar.sol"
|
|
|
|
//
|
|
|
|
// UseSrcList := UseSrc (',' UseSrc)*
|
|
|
|
// UseSrc := [0-9]+ ':' FileName
|
|
|
|
// FileName := "(([^\"]|\.)*)"
|
|
|
|
|
|
|
|
// Matches some "@use-src TEXT".
|
|
|
|
static std::regex const lineRE = std::regex(
|
|
|
|
"(^|\\s)@use-src\\b",
|
|
|
|
std::regex_constants::ECMAScript | std::regex_constants::optimize
|
|
|
|
);
|
|
|
|
std::smatch sm;
|
|
|
|
if (!std::regex_search(m_scanner->currentCommentLiteral(), sm, lineRE))
|
|
|
|
return nullopt;
|
|
|
|
|
|
|
|
solAssert(sm.size() == 2, "");
|
|
|
|
auto text = m_scanner->currentCommentLiteral().substr(static_cast<size_t>(sm.position() + sm.length()));
|
2021-07-14 10:53:39 +00:00
|
|
|
CharStream charStream(text, "");
|
|
|
|
Scanner scanner(charStream);
|
2021-07-12 10:05:26 +00:00
|
|
|
if (scanner.currentToken() == Token::EOS)
|
|
|
|
return SourceNameMap{};
|
|
|
|
SourceNameMap sourceNames;
|
|
|
|
|
|
|
|
while (scanner.currentToken() != Token::EOS)
|
|
|
|
{
|
|
|
|
if (scanner.currentToken() != Token::Number)
|
|
|
|
break;
|
|
|
|
auto sourceIndex = toUnsignedInt(scanner.currentLiteral());
|
|
|
|
if (!sourceIndex)
|
|
|
|
break;
|
|
|
|
if (scanner.next() != Token::Colon)
|
|
|
|
break;
|
|
|
|
if (scanner.next() != Token::StringLiteral)
|
|
|
|
break;
|
|
|
|
sourceNames[*sourceIndex] = make_shared<string const>(scanner.currentLiteral());
|
|
|
|
|
|
|
|
Token const next = scanner.next();
|
|
|
|
if (next == Token::EOS)
|
|
|
|
return {move(sourceNames)};
|
|
|
|
if (next != Token::Comma)
|
|
|
|
break;
|
|
|
|
scanner.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_errorReporter.syntaxError(
|
|
|
|
9804_error,
|
|
|
|
m_scanner->currentCommentLocation(),
|
|
|
|
"Error parsing arguments to @use-src. Expected: <number> \":\" \"<filename>\", ..."
|
|
|
|
);
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
2018-11-04 08:34:21 +00:00
|
|
|
shared_ptr<Block> ObjectParser::parseBlock()
|
|
|
|
{
|
2021-07-12 10:05:26 +00:00
|
|
|
Parser parser(m_errorReporter, m_dialect, m_sourceNameMapping);
|
2021-08-03 13:36:00 +00:00
|
|
|
shared_ptr<Block> block = parser.parseInline(m_scanner);
|
2018-11-04 08:34:21 +00:00
|
|
|
yulAssert(block || m_errorReporter.hasErrors(), "Invalid block but no error!");
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectParser::parseData(Object& _containingObject)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(
|
2018-11-04 08:34:21 +00:00
|
|
|
currentToken() == Token::Identifier && currentLiteral() == "data",
|
|
|
|
"parseData called on wrong input."
|
|
|
|
);
|
|
|
|
advance();
|
|
|
|
|
|
|
|
YulString name = parseUniqueName(&_containingObject);
|
|
|
|
|
2019-10-05 20:47:23 +00:00
|
|
|
if (currentToken() == Token::HexStringLiteral)
|
|
|
|
expectToken(Token::HexStringLiteral, false);
|
|
|
|
else
|
|
|
|
expectToken(Token::StringLiteral, false);
|
2018-11-04 08:34:21 +00:00
|
|
|
addNamedSubObject(_containingObject, name, make_shared<Data>(name, asBytes(currentLiteral())));
|
|
|
|
advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
YulString ObjectParser::parseUniqueName(Object const* _containingObject)
|
|
|
|
{
|
|
|
|
expectToken(Token::StringLiteral, false);
|
|
|
|
YulString name{currentLiteral()};
|
|
|
|
if (name.empty())
|
2020-05-08 23:28:55 +00:00
|
|
|
parserError(3287_error, "Object name cannot be empty.");
|
2018-11-04 08:34:21 +00:00
|
|
|
else if (_containingObject && _containingObject->name == name)
|
2020-05-08 23:28:55 +00:00
|
|
|
parserError(8311_error, "Object name cannot be the same as the name of the containing object.");
|
2018-11-04 08:34:21 +00:00
|
|
|
else if (_containingObject && _containingObject->subIndexByName.count(name))
|
2020-05-08 23:28:55 +00:00
|
|
|
parserError(8794_error, "Object name \"" + name.str() + "\" already exists inside the containing object.");
|
2018-11-04 08:34:21 +00:00
|
|
|
advance();
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectParser::addNamedSubObject(Object& _container, YulString _name, shared_ptr<ObjectNode> _subObject)
|
|
|
|
{
|
|
|
|
_container.subIndexByName[_name] = _container.subObjects.size();
|
|
|
|
_container.subObjects.emplace_back(std::move(_subObject));
|
|
|
|
}
|