2016-02-22 01:13:41 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2016-02-22 01:13:41 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2016-02-22 01:13:41 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-22 01:13:41 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2016-02-22 01:13:41 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2016
|
|
|
|
* Solidity inline assembly parser.
|
|
|
|
*/
|
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2021-04-27 14:53:04 +00:00
|
|
|
#include <libyul/AsmParser.h>
|
2019-11-26 21:52:09 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2021-08-03 12:41:10 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
#include <liblangutil/Scanner.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Common.h>
|
2020-12-03 16:44:11 +00:00
|
|
|
#include <libsolutil/Visitor.h>
|
2017-08-21 10:08:29 +00:00
|
|
|
|
2021-06-16 10:38:34 +00:00
|
|
|
#include <range/v3/view/subrange.hpp>
|
|
|
|
|
2017-08-21 10:08:29 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
#include <algorithm>
|
2021-06-16 10:38:34 +00:00
|
|
|
#include <regex>
|
2016-02-22 01:13:41 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::yul;
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2021-05-27 15:41:04 +00:00
|
|
|
namespace
|
|
|
|
{
|
2021-04-27 14:53:04 +00:00
|
|
|
|
2021-06-16 10:38:34 +00:00
|
|
|
optional<int> toInt(string const& _value)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return stoi(_value);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 14:53:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 12:41:10 +00:00
|
|
|
std::shared_ptr<DebugData const> Parser::createDebugData() const
|
|
|
|
{
|
|
|
|
switch (m_useSourceLocationFrom)
|
|
|
|
{
|
|
|
|
case UseSourceLocationFrom::Scanner:
|
2021-09-20 15:51:01 +00:00
|
|
|
return DebugData::create(ParserBase::currentLocation(), ParserBase::currentLocation());
|
2021-08-03 12:41:10 +00:00
|
|
|
case UseSourceLocationFrom::LocationOverride:
|
2021-09-20 15:51:01 +00:00
|
|
|
return DebugData::create(m_locationOverride, m_locationOverride);
|
2021-08-03 12:41:10 +00:00
|
|
|
case UseSourceLocationFrom::Comments:
|
2021-09-20 15:51:01 +00:00
|
|
|
return DebugData::create(ParserBase::currentLocation(), m_locationFromComment, m_astIDFromComment);
|
2021-08-03 12:41:10 +00:00
|
|
|
}
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:51:01 +00:00
|
|
|
void Parser::updateLocationEndFrom(
|
|
|
|
shared_ptr<DebugData const>& _debugData,
|
|
|
|
SourceLocation const& _location
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
solAssert(_debugData, "");
|
|
|
|
|
|
|
|
switch (m_useSourceLocationFrom)
|
|
|
|
{
|
|
|
|
case UseSourceLocationFrom::Scanner:
|
|
|
|
{
|
|
|
|
DebugData updatedDebugData = *_debugData;
|
|
|
|
updatedDebugData.nativeLocation.end = _location.end;
|
|
|
|
updatedDebugData.originLocation.end = _location.end;
|
|
|
|
_debugData = make_shared<DebugData const>(move(updatedDebugData));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UseSourceLocationFrom::LocationOverride:
|
|
|
|
// Ignore the update. The location we're overriding with is not supposed to change
|
|
|
|
break;
|
|
|
|
case UseSourceLocationFrom::Comments:
|
|
|
|
{
|
|
|
|
DebugData updatedDebugData = *_debugData;
|
|
|
|
updatedDebugData.nativeLocation.end = _location.end;
|
|
|
|
_debugData = make_shared<DebugData const>(move(updatedDebugData));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 13:36:00 +00:00
|
|
|
unique_ptr<Block> Parser::parse(CharStream& _charStream)
|
|
|
|
{
|
|
|
|
m_scanner = make_shared<Scanner>(_charStream);
|
|
|
|
unique_ptr<Block> block = parseInline(m_scanner);
|
|
|
|
expectToken(Token::EOS);
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<Block> Parser::parseInline(std::shared_ptr<Scanner> const& _scanner)
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
m_recursionDepth = 0;
|
2019-04-24 11:16:43 +00:00
|
|
|
|
2020-07-10 15:05:52 +00:00
|
|
|
_scanner->setScannerMode(ScannerKind::Yul);
|
|
|
|
ScopeGuard resetScanner([&]{ _scanner->setScannerMode(ScannerKind::Solidity); });
|
2019-04-24 11:16:43 +00:00
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_scanner = _scanner;
|
2021-09-15 13:55:03 +00:00
|
|
|
if (m_useSourceLocationFrom == UseSourceLocationFrom::Comments)
|
2021-09-15 13:23:22 +00:00
|
|
|
fetchDebugDataFromComment();
|
2021-08-03 13:36:00 +00:00
|
|
|
return make_unique<Block>(parseBlock());
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
catch (FatalError const&)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!m_errorReporter.errors().empty(), "Fatal error detected, but no error is reported.");
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2019-04-24 11:16:43 +00:00
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-06-16 10:38:34 +00:00
|
|
|
langutil::Token Parser::advance()
|
|
|
|
{
|
|
|
|
auto const token = ParserBase::advance();
|
|
|
|
if (m_useSourceLocationFrom == UseSourceLocationFrom::Comments)
|
2021-09-15 13:23:22 +00:00
|
|
|
fetchDebugDataFromComment();
|
2021-06-16 10:38:34 +00:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2021-09-15 13:23:22 +00:00
|
|
|
void Parser::fetchDebugDataFromComment()
|
2021-06-16 10:38:34 +00:00
|
|
|
{
|
2021-06-29 12:38:59 +00:00
|
|
|
solAssert(m_sourceNames.has_value(), "");
|
2021-06-16 10:38:34 +00:00
|
|
|
|
2021-09-08 14:04:45 +00:00
|
|
|
static regex const tagRegex = regex(
|
2021-09-09 16:27:29 +00:00
|
|
|
R"~~((?:^|\s+)(@[a-zA-Z0-9\-_]+)(?:\s+|$))~~", // tag, e.g: @src
|
2021-09-08 14:08:05 +00:00
|
|
|
regex_constants::ECMAScript | regex_constants::optimize
|
|
|
|
);
|
2021-06-16 10:38:34 +00:00
|
|
|
|
2021-09-15 13:23:22 +00:00
|
|
|
string_view commentLiteral = m_scanner->currentCommentLiteral();
|
|
|
|
match_results<string_view::const_iterator> match;
|
|
|
|
|
2021-09-20 15:51:01 +00:00
|
|
|
langutil::SourceLocation originLocation = m_locationFromComment;
|
2021-09-15 13:55:03 +00:00
|
|
|
// Empty for each new node.
|
|
|
|
optional<int> astID;
|
2021-06-16 10:38:34 +00:00
|
|
|
|
2021-09-15 13:23:22 +00:00
|
|
|
while (regex_search(commentLiteral.cbegin(), commentLiteral.cend(), match, tagRegex))
|
2021-06-16 10:38:34 +00:00
|
|
|
{
|
2021-09-15 13:23:22 +00:00
|
|
|
solAssert(match.size() == 2, "");
|
|
|
|
commentLiteral = commentLiteral.substr(static_cast<size_t>(match.position() + match.length()));
|
2021-09-08 14:08:05 +00:00
|
|
|
|
2021-09-15 13:23:22 +00:00
|
|
|
if (match[1] == "@src")
|
2021-06-16 10:38:34 +00:00
|
|
|
{
|
2021-09-15 13:23:22 +00:00
|
|
|
if (auto parseResult = parseSrcComment(commentLiteral, m_scanner->currentCommentLocation()))
|
2021-09-20 15:51:01 +00:00
|
|
|
tie(commentLiteral, originLocation) = *parseResult;
|
2021-09-08 14:08:05 +00:00
|
|
|
else
|
2021-09-15 13:23:22 +00:00
|
|
|
break;
|
2021-06-16 10:38:34 +00:00
|
|
|
}
|
2021-09-15 13:55:03 +00:00
|
|
|
else if (match[1] == "@ast-id")
|
|
|
|
{
|
|
|
|
if (auto parseResult = parseASTIDComment(commentLiteral, m_scanner->currentCommentLocation()))
|
|
|
|
tie(commentLiteral, astID) = *parseResult;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2021-09-08 14:08:05 +00:00
|
|
|
else
|
|
|
|
// Ignore unrecognized tags.
|
|
|
|
continue;
|
2021-06-16 10:38:34 +00:00
|
|
|
}
|
2021-09-15 13:23:22 +00:00
|
|
|
|
2021-09-20 15:51:01 +00:00
|
|
|
m_locationFromComment = originLocation;
|
2021-09-21 12:52:51 +00:00
|
|
|
m_astIDFromComment = astID;
|
2021-09-15 13:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
optional<pair<string_view, SourceLocation>> Parser::parseSrcComment(
|
|
|
|
string_view const _arguments,
|
|
|
|
langutil::SourceLocation const& _commentLocation
|
|
|
|
)
|
|
|
|
{
|
|
|
|
static regex const argsRegex = regex(
|
|
|
|
R"~~(^(-1|\d+):(-1|\d+):(-1|\d+)(?:\s+|$))~~" // index and location, e.g.: 1:234:-1
|
|
|
|
R"~~(("(?:[^"\\]|\\.)*"?)?)~~", // optional code snippet, e.g.: "string memory s = \"abc\";..."
|
|
|
|
regex_constants::ECMAScript | regex_constants::optimize
|
|
|
|
);
|
|
|
|
match_results<string_view::const_iterator> match;
|
|
|
|
if (!regex_search(_arguments.cbegin(), _arguments.cend(), match, argsRegex))
|
|
|
|
{
|
|
|
|
m_errorReporter.syntaxError(
|
|
|
|
8387_error,
|
|
|
|
_commentLocation,
|
|
|
|
"Invalid values in source location mapping. Could not parse location specification."
|
|
|
|
);
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
solAssert(match.size() == 5, "");
|
|
|
|
string_view tail = _arguments.substr(static_cast<size_t>(match.position() + match.length()));
|
|
|
|
|
|
|
|
if (match[4].matched && (
|
|
|
|
!boost::algorithm::ends_with(match[4].str(), "\"") ||
|
|
|
|
boost::algorithm::ends_with(match[4].str(), "\\\"")
|
|
|
|
))
|
|
|
|
{
|
|
|
|
m_errorReporter.syntaxError(
|
|
|
|
1544_error,
|
|
|
|
_commentLocation,
|
|
|
|
"Invalid code snippet in source location mapping. Quote is not terminated."
|
|
|
|
);
|
|
|
|
return {{tail, SourceLocation{}}};
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<int> const sourceIndex = toInt(match[1].str());
|
|
|
|
optional<int> const start = toInt(match[2].str());
|
|
|
|
optional<int> const end = toInt(match[3].str());
|
|
|
|
|
|
|
|
if (!sourceIndex.has_value() || !start.has_value() || !end.has_value())
|
|
|
|
m_errorReporter.syntaxError(
|
|
|
|
6367_error,
|
|
|
|
_commentLocation,
|
|
|
|
"Invalid value in source location mapping. "
|
|
|
|
"Expected non-negative integer values or -1 for source index and location."
|
|
|
|
);
|
|
|
|
else if (sourceIndex == -1)
|
|
|
|
return {{tail, SourceLocation{start.value(), end.value(), nullptr}}};
|
|
|
|
else if (!(sourceIndex >= 0 && m_sourceNames->count(static_cast<unsigned>(sourceIndex.value()))))
|
|
|
|
m_errorReporter.syntaxError(
|
|
|
|
2674_error,
|
|
|
|
_commentLocation,
|
|
|
|
"Invalid source mapping. Source index not defined via @use-src."
|
|
|
|
);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shared_ptr<string const> sourceName = m_sourceNames->at(static_cast<unsigned>(sourceIndex.value()));
|
|
|
|
solAssert(sourceName, "");
|
|
|
|
return {{tail, SourceLocation{start.value(), end.value(), move(sourceName)}}};
|
|
|
|
}
|
|
|
|
return {{tail, SourceLocation{}}};
|
2021-06-16 10:38:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 13:55:03 +00:00
|
|
|
optional<pair<string_view, optional<int>>> Parser::parseASTIDComment(
|
|
|
|
string_view _arguments,
|
|
|
|
langutil::SourceLocation const& _commentLocation
|
|
|
|
)
|
|
|
|
{
|
|
|
|
static regex const argRegex = regex(
|
|
|
|
R"~~(^(\d+)(?:\s|$))~~",
|
|
|
|
regex_constants::ECMAScript | regex_constants::optimize
|
|
|
|
);
|
|
|
|
match_results<string_view::const_iterator> match;
|
|
|
|
optional<int> astID;
|
|
|
|
bool matched = regex_search(_arguments.cbegin(), _arguments.cend(), match, argRegex);
|
|
|
|
string_view tail = _arguments;
|
|
|
|
if (matched)
|
|
|
|
{
|
|
|
|
solAssert(match.size() == 2, "");
|
|
|
|
tail = _arguments.substr(static_cast<size_t>(match.position() + match.length()));
|
|
|
|
|
|
|
|
astID = toInt(match[1].str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!matched || !astID || *astID < 0 || static_cast<int64_t>(*astID) != *astID)
|
|
|
|
{
|
|
|
|
m_errorReporter.syntaxError(1749_error, _commentLocation, "Invalid argument for @ast-id.");
|
|
|
|
astID = nullopt;
|
|
|
|
}
|
|
|
|
if (matched)
|
|
|
|
return {{_arguments, astID}};
|
|
|
|
else
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
Block Parser::parseBlock()
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
RecursionGuard recursionGuard(*this);
|
2018-11-21 11:42:34 +00:00
|
|
|
Block block = createWithLocation<Block>();
|
2016-02-22 01:13:41 +00:00
|
|
|
expectToken(Token::LBrace);
|
2017-05-24 23:13:51 +00:00
|
|
|
while (currentToken() != Token::RBrace)
|
2016-02-22 01:13:41 +00:00
|
|
|
block.statements.emplace_back(parseStatement());
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(block.debugData, currentLocation());
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2016-02-22 01:13:41 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
Statement Parser::parseStatement()
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
RecursionGuard recursionGuard(*this);
|
2017-05-24 23:13:51 +00:00
|
|
|
switch (currentToken())
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
case Token::Let:
|
|
|
|
return parseVariableDeclaration();
|
2017-01-31 22:59:41 +00:00
|
|
|
case Token::Function:
|
|
|
|
return parseFunctionDefinition();
|
2016-02-22 01:13:41 +00:00
|
|
|
case Token::LBrace:
|
|
|
|
return parseBlock();
|
2017-11-21 12:36:41 +00:00
|
|
|
case Token::If:
|
|
|
|
{
|
2018-11-21 11:42:34 +00:00
|
|
|
If _if = createWithLocation<If>();
|
2019-02-21 19:19:39 +00:00
|
|
|
advance();
|
2019-01-09 13:05:03 +00:00
|
|
|
_if.condition = make_unique<Expression>(parseExpression());
|
2017-11-21 12:36:41 +00:00
|
|
|
_if.body = parseBlock();
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(_if.debugData, nativeLocationOf(_if.body));
|
2019-01-09 13:05:03 +00:00
|
|
|
return Statement{move(_if)};
|
2017-11-21 12:36:41 +00:00
|
|
|
}
|
2017-04-28 13:27:56 +00:00
|
|
|
case Token::Switch:
|
|
|
|
{
|
2018-11-21 11:42:34 +00:00
|
|
|
Switch _switch = createWithLocation<Switch>();
|
2019-02-21 19:19:39 +00:00
|
|
|
advance();
|
2019-01-09 13:05:03 +00:00
|
|
|
_switch.expression = make_unique<Expression>(parseExpression());
|
2019-02-21 19:19:39 +00:00
|
|
|
while (currentToken() == Token::Case)
|
2017-04-28 13:27:56 +00:00
|
|
|
_switch.cases.emplace_back(parseCase());
|
2019-02-21 19:19:39 +00:00
|
|
|
if (currentToken() == Token::Default)
|
2017-05-19 17:04:40 +00:00
|
|
|
_switch.cases.emplace_back(parseCase());
|
2019-02-21 19:19:39 +00:00
|
|
|
if (currentToken() == Token::Default)
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(6931_error, "Only one default case allowed.");
|
2019-02-21 19:19:39 +00:00
|
|
|
else if (currentToken() == Token::Case)
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(4904_error, "Case not allowed after default case.");
|
2018-10-09 03:29:37 +00:00
|
|
|
if (_switch.cases.empty())
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(2418_error, "Switch statement without any cases.");
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(_switch.debugData, nativeLocationOf(_switch.cases.back().body));
|
2019-01-09 13:05:03 +00:00
|
|
|
return Statement{move(_switch)};
|
2017-04-28 13:27:56 +00:00
|
|
|
}
|
2017-04-28 13:33:52 +00:00
|
|
|
case Token::For:
|
|
|
|
return parseForLoop();
|
2019-03-04 14:38:05 +00:00
|
|
|
case Token::Break:
|
2019-04-18 10:51:28 +00:00
|
|
|
{
|
|
|
|
Statement stmt{createWithLocation<Break>()};
|
|
|
|
checkBreakContinuePosition("break");
|
2020-07-29 13:20:59 +00:00
|
|
|
advance();
|
2019-04-18 10:51:28 +00:00
|
|
|
return stmt;
|
|
|
|
}
|
2019-03-04 14:38:05 +00:00
|
|
|
case Token::Continue:
|
2019-04-18 10:51:28 +00:00
|
|
|
{
|
|
|
|
Statement stmt{createWithLocation<Continue>()};
|
|
|
|
checkBreakContinuePosition("continue");
|
2020-07-29 13:20:59 +00:00
|
|
|
advance();
|
2019-04-18 10:51:28 +00:00
|
|
|
return stmt;
|
|
|
|
}
|
2020-07-27 18:11:38 +00:00
|
|
|
case Token::Leave:
|
|
|
|
{
|
|
|
|
Statement stmt{createWithLocation<Leave>()};
|
|
|
|
if (!m_insideFunction)
|
|
|
|
m_errorReporter.syntaxError(8149_error, currentLocation(), "Keyword \"leave\" can only be used inside a function.");
|
|
|
|
advance();
|
|
|
|
return stmt;
|
|
|
|
}
|
2016-02-22 01:13:41 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-07-30 11:20:15 +00:00
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
// Options left:
|
2020-07-30 11:20:15 +00:00
|
|
|
// Expression/FunctionCall
|
|
|
|
// Assignment
|
2020-12-03 16:44:11 +00:00
|
|
|
variant<Literal, Identifier> elementary(parseLiteralOrIdentifier());
|
2019-05-16 19:19:50 +00:00
|
|
|
|
2017-05-24 23:13:51 +00:00
|
|
|
switch (currentToken())
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
case Token::LParen:
|
2017-12-08 13:01:22 +00:00
|
|
|
{
|
|
|
|
Expression expr = parseCall(std::move(elementary));
|
2021-04-27 14:53:04 +00:00
|
|
|
return ExpressionStatement{debugDataOf(expr), move(expr)};
|
2017-12-08 13:01:22 +00:00
|
|
|
}
|
2017-05-19 16:06:26 +00:00
|
|
|
case Token::Comma:
|
2019-02-18 14:07:15 +00:00
|
|
|
case Token::AssemblyAssign:
|
2017-05-19 16:06:26 +00:00
|
|
|
{
|
2020-06-17 15:56:51 +00:00
|
|
|
Assignment assignment;
|
2021-04-27 14:53:04 +00:00
|
|
|
assignment.debugData = debugDataOf(elementary);
|
2017-05-19 16:06:26 +00:00
|
|
|
|
2019-02-18 14:07:15 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
if (!holds_alternative<Identifier>(elementary))
|
2019-02-18 14:07:15 +00:00
|
|
|
{
|
|
|
|
auto const token = currentToken() == Token::Comma ? "," : ":=";
|
2017-05-19 16:06:26 +00:00
|
|
|
|
2019-02-18 14:07:15 +00:00
|
|
|
fatalParserError(
|
2020-05-08 23:28:55 +00:00
|
|
|
2856_error,
|
2019-02-18 14:07:15 +00:00
|
|
|
std::string("Variable name must precede \"") +
|
|
|
|
token +
|
|
|
|
"\"" +
|
|
|
|
(currentToken() == Token::Comma ? " in multiple assignment." : " in assignment.")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
auto const& identifier = std::get<Identifier>(elementary);
|
2019-02-18 14:07:15 +00:00
|
|
|
|
2019-05-16 08:56:56 +00:00
|
|
|
if (m_dialect.builtin(identifier.name))
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(6272_error, "Cannot assign to builtin function \"" + identifier.name.str() + "\".");
|
2019-02-18 14:07:15 +00:00
|
|
|
|
2020-06-17 15:56:51 +00:00
|
|
|
assignment.variableNames.emplace_back(identifier);
|
2019-02-18 14:07:15 +00:00
|
|
|
|
|
|
|
if (currentToken() != Token::Comma)
|
|
|
|
break;
|
2017-05-19 16:06:26 +00:00
|
|
|
|
|
|
|
expectToken(Token::Comma);
|
2019-02-18 14:07:15 +00:00
|
|
|
|
2020-07-30 11:20:15 +00:00
|
|
|
elementary = parseLiteralOrIdentifier();
|
2017-05-19 16:06:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 14:07:15 +00:00
|
|
|
expectToken(Token::AssemblyAssign);
|
2017-05-19 16:06:26 +00:00
|
|
|
|
2019-11-27 16:24:21 +00:00
|
|
|
assignment.value = make_unique<Expression>(parseExpression());
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(assignment.debugData, nativeLocationOf(*assignment.value));
|
2019-02-18 14:07:15 +00:00
|
|
|
|
2020-08-31 14:29:12 +00:00
|
|
|
return Statement{move(assignment)};
|
2017-05-19 16:06:26 +00:00
|
|
|
}
|
2016-02-22 01:13:41 +00:00
|
|
|
default:
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(6913_error, "Call or assignment expected.");
|
2016-02-22 01:13:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-05-16 19:19:50 +00:00
|
|
|
|
2020-11-05 01:25:53 +00:00
|
|
|
yulAssert(false, "");
|
|
|
|
return {};
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
Case Parser::parseCase()
|
2017-04-28 13:27:56 +00:00
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
RecursionGuard recursionGuard(*this);
|
2018-11-21 11:42:34 +00:00
|
|
|
Case _case = createWithLocation<Case>();
|
2019-02-21 19:19:39 +00:00
|
|
|
if (currentToken() == Token::Default)
|
|
|
|
advance();
|
|
|
|
else if (currentToken() == Token::Case)
|
2017-04-28 13:27:56 +00:00
|
|
|
{
|
2019-02-21 19:19:39 +00:00
|
|
|
advance();
|
2020-12-03 16:44:11 +00:00
|
|
|
variant<Literal, Identifier> literal = parseLiteralOrIdentifier();
|
2019-11-19 15:42:49 +00:00
|
|
|
if (!holds_alternative<Literal>(literal))
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(4805_error, "Literal expected.");
|
2019-11-19 15:42:49 +00:00
|
|
|
_case.value = make_unique<Literal>(std::get<Literal>(std::move(literal)));
|
2017-04-28 13:27:56 +00:00
|
|
|
}
|
2017-05-19 17:04:40 +00:00
|
|
|
else
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(false, "Case or default case expected.");
|
2017-04-28 13:27:56 +00:00
|
|
|
_case.body = parseBlock();
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(_case.debugData, nativeLocationOf(_case.body));
|
2017-04-28 13:27:56 +00:00
|
|
|
return _case;
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
ForLoop Parser::parseForLoop()
|
2017-04-28 13:33:52 +00:00
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
RecursionGuard recursionGuard(*this);
|
2019-04-18 10:51:28 +00:00
|
|
|
|
|
|
|
ForLoopComponent outerForLoopComponent = m_currentForLoopComponent;
|
|
|
|
|
2017-04-28 13:33:52 +00:00
|
|
|
ForLoop forLoop = createWithLocation<ForLoop>();
|
|
|
|
expectToken(Token::For);
|
2019-04-18 10:51:28 +00:00
|
|
|
m_currentForLoopComponent = ForLoopComponent::ForLoopPre;
|
2017-04-28 13:33:52 +00:00
|
|
|
forLoop.pre = parseBlock();
|
2019-04-18 10:51:28 +00:00
|
|
|
m_currentForLoopComponent = ForLoopComponent::None;
|
2019-01-09 13:05:03 +00:00
|
|
|
forLoop.condition = make_unique<Expression>(parseExpression());
|
2019-04-18 10:51:28 +00:00
|
|
|
m_currentForLoopComponent = ForLoopComponent::ForLoopPost;
|
2017-04-28 13:33:52 +00:00
|
|
|
forLoop.post = parseBlock();
|
2019-04-18 10:51:28 +00:00
|
|
|
m_currentForLoopComponent = ForLoopComponent::ForLoopBody;
|
2017-04-28 13:33:52 +00:00
|
|
|
forLoop.body = parseBlock();
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(forLoop.debugData, nativeLocationOf(forLoop.body));
|
2019-04-18 10:51:28 +00:00
|
|
|
|
|
|
|
m_currentForLoopComponent = outerForLoopComponent;
|
|
|
|
|
2017-04-28 13:33:52 +00:00
|
|
|
return forLoop;
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
Expression Parser::parseExpression()
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
RecursionGuard recursionGuard(*this);
|
2019-05-16 19:19:50 +00:00
|
|
|
|
2020-12-03 16:44:11 +00:00
|
|
|
variant<Literal, Identifier> operation = parseLiteralOrIdentifier();
|
|
|
|
return visit(GenericVisitor{
|
|
|
|
[&](Identifier& _identifier) -> Expression
|
|
|
|
{
|
|
|
|
if (currentToken() == Token::LParen)
|
|
|
|
return parseCall(std::move(operation));
|
|
|
|
if (m_dialect.builtin(_identifier.name))
|
|
|
|
fatalParserError(
|
|
|
|
7104_error,
|
2021-09-20 15:51:01 +00:00
|
|
|
nativeLocationOf(_identifier),
|
2020-12-03 16:44:11 +00:00
|
|
|
"Builtin function \"" + _identifier.name.str() + "\" must be called."
|
|
|
|
);
|
|
|
|
return move(_identifier);
|
|
|
|
},
|
|
|
|
[&](Literal& _literal) -> Expression
|
|
|
|
{
|
|
|
|
return move(_literal);
|
|
|
|
}
|
|
|
|
}, operation);
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 16:44:11 +00:00
|
|
|
variant<Literal, Identifier> Parser::parseLiteralOrIdentifier()
|
2017-01-25 10:33:09 +00:00
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
RecursionGuard recursionGuard(*this);
|
2017-05-24 23:13:51 +00:00
|
|
|
switch (currentToken())
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
case Token::Identifier:
|
|
|
|
{
|
2021-07-08 14:38:16 +00:00
|
|
|
Identifier identifier{createDebugData(), YulString{currentLiteral()}};
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2020-12-03 16:44:11 +00:00
|
|
|
return identifier;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
case Token::StringLiteral:
|
2021-03-25 16:00:05 +00:00
|
|
|
case Token::HexStringLiteral:
|
2016-02-22 01:13:41 +00:00
|
|
|
case Token::Number:
|
2017-05-02 17:26:33 +00:00
|
|
|
case Token::TrueLiteral:
|
|
|
|
case Token::FalseLiteral:
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-05-02 17:26:33 +00:00
|
|
|
LiteralKind kind = LiteralKind::Number;
|
2017-05-24 23:13:51 +00:00
|
|
|
switch (currentToken())
|
2017-05-02 17:26:33 +00:00
|
|
|
{
|
|
|
|
case Token::StringLiteral:
|
2021-03-25 16:00:05 +00:00
|
|
|
case Token::HexStringLiteral:
|
2017-05-02 17:26:33 +00:00
|
|
|
kind = LiteralKind::String;
|
|
|
|
break;
|
|
|
|
case Token::Number:
|
2017-08-21 10:08:29 +00:00
|
|
|
if (!isValidNumberLiteral(currentLiteral()))
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(4828_error, "Invalid number literal.");
|
2017-05-02 17:26:33 +00:00
|
|
|
kind = LiteralKind::Number;
|
|
|
|
break;
|
|
|
|
case Token::TrueLiteral:
|
|
|
|
case Token::FalseLiteral:
|
|
|
|
kind = LiteralKind::Boolean;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-04-26 22:58:34 +00:00
|
|
|
Literal literal{
|
2021-07-08 14:38:16 +00:00
|
|
|
createDebugData(),
|
2017-05-02 17:26:33 +00:00
|
|
|
kind,
|
2018-10-29 14:12:02 +00:00
|
|
|
YulString{currentLiteral()},
|
2020-01-16 11:03:19 +00:00
|
|
|
kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType
|
2016-02-22 01:13:41 +00:00
|
|
|
};
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2019-12-19 16:58:20 +00:00
|
|
|
if (currentToken() == Token::Colon)
|
2017-04-26 22:58:34 +00:00
|
|
|
{
|
|
|
|
expectToken(Token::Colon);
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(literal.debugData, currentLocation());
|
2018-12-03 16:47:15 +00:00
|
|
|
literal.type = expectAsmIdentifier();
|
2017-04-26 22:58:34 +00:00
|
|
|
}
|
2019-12-19 16:58:20 +00:00
|
|
|
|
2020-12-03 16:44:11 +00:00
|
|
|
return literal;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2020-12-16 11:17:13 +00:00
|
|
|
case Token::Illegal:
|
|
|
|
fatalParserError(1465_error, "Illegal token: " + to_string(m_scanner->currentError()));
|
|
|
|
break;
|
2016-02-22 01:13:41 +00:00
|
|
|
default:
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(1856_error, "Literal or identifier expected.");
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2020-12-03 16:44:11 +00:00
|
|
|
return {};
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
VariableDeclaration Parser::parseVariableDeclaration()
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
RecursionGuard recursionGuard(*this);
|
2016-04-18 11:47:40 +00:00
|
|
|
VariableDeclaration varDecl = createWithLocation<VariableDeclaration>();
|
2016-02-22 01:13:41 +00:00
|
|
|
expectToken(Token::Let);
|
2017-05-05 17:35:40 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2017-05-26 08:41:53 +00:00
|
|
|
varDecl.variables.emplace_back(parseTypedName());
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::Comma)
|
2017-05-05 17:35:40 +00:00
|
|
|
expectToken(Token::Comma);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2019-02-18 14:07:15 +00:00
|
|
|
if (currentToken() == Token::AssemblyAssign)
|
2017-05-05 15:46:26 +00:00
|
|
|
{
|
2019-02-18 14:07:15 +00:00
|
|
|
expectToken(Token::AssemblyAssign);
|
2019-01-09 13:05:03 +00:00
|
|
|
varDecl.value = make_unique<Expression>(parseExpression());
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(varDecl.debugData, nativeLocationOf(*varDecl.value));
|
2017-05-05 15:46:26 +00:00
|
|
|
}
|
2021-09-20 15:51:01 +00:00
|
|
|
else
|
|
|
|
updateLocationEndFrom(varDecl.debugData, nativeLocationOf(varDecl.variables.back()));
|
2021-04-27 14:53:04 +00:00
|
|
|
|
2016-04-18 11:47:40 +00:00
|
|
|
return varDecl;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
FunctionDefinition Parser::parseFunctionDefinition()
|
2017-01-31 22:59:41 +00:00
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
RecursionGuard recursionGuard(*this);
|
2019-04-18 16:10:45 +00:00
|
|
|
|
|
|
|
if (m_currentForLoopComponent == ForLoopComponent::ForLoopPre)
|
|
|
|
m_errorReporter.syntaxError(
|
2020-05-05 22:38:28 +00:00
|
|
|
3441_error,
|
2020-02-05 23:04:18 +00:00
|
|
|
currentLocation(),
|
2019-04-18 16:10:45 +00:00
|
|
|
"Functions cannot be defined inside a for-loop init block."
|
|
|
|
);
|
|
|
|
|
2019-04-18 10:51:28 +00:00
|
|
|
ForLoopComponent outerForLoopComponent = m_currentForLoopComponent;
|
|
|
|
m_currentForLoopComponent = ForLoopComponent::None;
|
|
|
|
|
2017-01-31 22:59:41 +00:00
|
|
|
FunctionDefinition funDef = createWithLocation<FunctionDefinition>();
|
|
|
|
expectToken(Token::Function);
|
2018-12-03 16:47:15 +00:00
|
|
|
funDef.name = expectAsmIdentifier();
|
2017-01-31 22:59:41 +00:00
|
|
|
expectToken(Token::LParen);
|
2017-05-24 23:13:51 +00:00
|
|
|
while (currentToken() != Token::RParen)
|
2017-01-31 22:59:41 +00:00
|
|
|
{
|
2017-12-01 13:10:49 +00:00
|
|
|
funDef.parameters.emplace_back(parseTypedName());
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::RParen)
|
2017-01-31 22:59:41 +00:00
|
|
|
break;
|
|
|
|
expectToken(Token::Comma);
|
|
|
|
}
|
|
|
|
expectToken(Token::RParen);
|
2020-08-12 17:56:24 +00:00
|
|
|
if (currentToken() == Token::RightArrow)
|
2017-01-31 22:59:41 +00:00
|
|
|
{
|
2020-08-12 17:56:24 +00:00
|
|
|
expectToken(Token::RightArrow);
|
2017-01-31 22:59:41 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2017-12-01 13:10:49 +00:00
|
|
|
funDef.returnVariables.emplace_back(parseTypedName());
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::LBrace)
|
2017-01-31 22:59:41 +00:00
|
|
|
break;
|
|
|
|
expectToken(Token::Comma);
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 14:25:02 +00:00
|
|
|
bool preInsideFunction = m_insideFunction;
|
|
|
|
m_insideFunction = true;
|
2017-01-31 22:59:41 +00:00
|
|
|
funDef.body = parseBlock();
|
2019-10-28 14:25:02 +00:00
|
|
|
m_insideFunction = preInsideFunction;
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(funDef.debugData, nativeLocationOf(funDef.body));
|
2019-04-18 10:51:28 +00:00
|
|
|
|
|
|
|
m_currentForLoopComponent = outerForLoopComponent;
|
2017-01-31 22:59:41 +00:00
|
|
|
return funDef;
|
|
|
|
}
|
|
|
|
|
2020-12-03 16:44:11 +00:00
|
|
|
FunctionCall Parser::parseCall(variant<Literal, Identifier>&& _initialOp)
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
RecursionGuard recursionGuard(*this);
|
2017-05-24 12:13:05 +00:00
|
|
|
|
2020-07-30 11:12:55 +00:00
|
|
|
if (!holds_alternative<Identifier>(_initialOp))
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(9980_error, "Function name expected.");
|
2017-02-01 20:20:21 +00:00
|
|
|
|
2020-07-30 11:12:55 +00:00
|
|
|
FunctionCall ret;
|
|
|
|
ret.functionName = std::move(std::get<Identifier>(_initialOp));
|
2021-04-27 14:53:04 +00:00
|
|
|
ret.debugData = ret.functionName.debugData;
|
2020-07-30 11:12:55 +00:00
|
|
|
|
2019-11-06 16:25:03 +00:00
|
|
|
expectToken(Token::LParen);
|
|
|
|
if (currentToken() != Token::RParen)
|
|
|
|
{
|
|
|
|
ret.arguments.emplace_back(parseExpression());
|
|
|
|
while (currentToken() != Token::RParen)
|
|
|
|
{
|
|
|
|
expectToken(Token::Comma);
|
|
|
|
ret.arguments.emplace_back(parseExpression());
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(ret.debugData, currentLocation());
|
2019-11-06 16:25:03 +00:00
|
|
|
expectToken(Token::RParen);
|
|
|
|
return ret;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2017-01-31 22:59:41 +00:00
|
|
|
|
2017-04-26 22:58:34 +00:00
|
|
|
TypedName Parser::parseTypedName()
|
|
|
|
{
|
2017-08-21 10:33:29 +00:00
|
|
|
RecursionGuard recursionGuard(*this);
|
2017-04-26 22:58:34 +00:00
|
|
|
TypedName typedName = createWithLocation<TypedName>();
|
2018-12-03 16:47:15 +00:00
|
|
|
typedName.name = expectAsmIdentifier();
|
2019-12-19 16:58:20 +00:00
|
|
|
if (currentToken() == Token::Colon)
|
2017-04-26 22:58:34 +00:00
|
|
|
{
|
|
|
|
expectToken(Token::Colon);
|
2021-09-20 15:51:01 +00:00
|
|
|
updateLocationEndFrom(typedName.debugData, currentLocation());
|
2018-12-03 16:47:15 +00:00
|
|
|
typedName.type = expectAsmIdentifier();
|
2017-04-26 22:58:34 +00:00
|
|
|
}
|
2020-01-16 11:03:19 +00:00
|
|
|
else
|
|
|
|
typedName.type = m_dialect.defaultType;
|
|
|
|
|
2017-04-26 22:58:34 +00:00
|
|
|
return typedName;
|
|
|
|
}
|
|
|
|
|
2018-12-03 16:47:15 +00:00
|
|
|
YulString Parser::expectAsmIdentifier()
|
2017-01-31 22:59:41 +00:00
|
|
|
{
|
2019-06-18 16:12:30 +00:00
|
|
|
YulString name{currentLiteral()};
|
2020-07-10 15:20:04 +00:00
|
|
|
if (currentToken() == Token::Identifier && m_dialect.builtin(name))
|
2020-05-08 23:28:55 +00:00
|
|
|
fatalParserError(5568_error, "Cannot use builtin function name \"" + name.str() + "\" as identifier name.");
|
2020-07-10 15:20:04 +00:00
|
|
|
// NOTE: We keep the expectation here to ensure the correct source location for the error above.
|
|
|
|
expectToken(Token::Identifier);
|
2017-01-31 22:59:41 +00:00
|
|
|
return name;
|
|
|
|
}
|
2017-08-21 10:08:29 +00:00
|
|
|
|
2019-04-18 10:51:28 +00:00
|
|
|
void Parser::checkBreakContinuePosition(string const& _which)
|
|
|
|
{
|
|
|
|
switch (m_currentForLoopComponent)
|
|
|
|
{
|
|
|
|
case ForLoopComponent::None:
|
2020-05-05 22:38:28 +00:00
|
|
|
m_errorReporter.syntaxError(2592_error, currentLocation(), "Keyword \"" + _which + "\" needs to be inside a for-loop body.");
|
2019-04-18 10:51:28 +00:00
|
|
|
break;
|
|
|
|
case ForLoopComponent::ForLoopPre:
|
2020-05-05 22:38:28 +00:00
|
|
|
m_errorReporter.syntaxError(9615_error, currentLocation(), "Keyword \"" + _which + "\" in for-loop init block is not allowed.");
|
2019-04-18 10:51:28 +00:00
|
|
|
break;
|
|
|
|
case ForLoopComponent::ForLoopPost:
|
2020-05-05 22:38:28 +00:00
|
|
|
m_errorReporter.syntaxError(2461_error, currentLocation(), "Keyword \"" + _which + "\" in for-loop post block is not allowed.");
|
2019-04-18 10:51:28 +00:00
|
|
|
break;
|
|
|
|
case ForLoopComponent::ForLoopBody:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 10:08:29 +00:00
|
|
|
bool Parser::isValidNumberLiteral(string const& _literal)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-05-17 10:19:29 +00:00
|
|
|
// Try to convert _literal to u256.
|
2020-01-07 14:45:57 +00:00
|
|
|
[[maybe_unused]] auto tmp = u256(_literal);
|
2017-08-21 10:08:29 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (boost::starts_with(_literal, "0x"))
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return _literal.find_first_not_of("0123456789") == string::npos;
|
|
|
|
}
|