2016-08-19 17:57:21 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2016-08-19 17:57:21 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2016-08-19 17:57:21 +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-08-19 17:57:21 +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-08-19 17:57:21 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2016-08-19 17:57:21 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <chris@ethereum.org>
|
|
|
|
* @date 2016
|
|
|
|
* Utilities to handle semantic versioning.
|
|
|
|
*/
|
|
|
|
|
2019-04-17 11:02:30 +00:00
|
|
|
#include <liblangutil/SemVerHandler.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
|
2021-08-03 12:41:10 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
|
2016-08-19 17:57:21 +00:00
|
|
|
#include <functional>
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <limits>
|
2016-08-19 17:57:21 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::langutil;
|
2016-08-19 17:57:21 +00:00
|
|
|
|
2021-08-03 12:41:10 +00:00
|
|
|
SemVerMatchExpressionParser::SemVerMatchExpressionParser(vector<Token> _tokens, vector<string> _literals):
|
|
|
|
m_tokens(std::move(_tokens)), m_literals(std::move(_literals))
|
|
|
|
{
|
|
|
|
solAssert(m_tokens.size() == m_literals.size(), "");
|
|
|
|
}
|
|
|
|
|
2016-08-19 17:57:21 +00:00
|
|
|
SemVerVersion::SemVerVersion(string const& _versionString)
|
|
|
|
{
|
|
|
|
auto i = _versionString.begin();
|
|
|
|
auto end = _versionString.end();
|
|
|
|
|
|
|
|
for (unsigned level = 0; level < 3; ++level)
|
|
|
|
{
|
|
|
|
unsigned v = 0;
|
|
|
|
for (; i != end && '0' <= *i && *i <= '9'; ++i)
|
2020-06-02 13:45:03 +00:00
|
|
|
v = v * 10 + unsigned(*i - '0');
|
2016-08-19 17:57:21 +00:00
|
|
|
numbers[level] = v;
|
|
|
|
if (level < 2)
|
|
|
|
{
|
|
|
|
if (i == end || *i != '.')
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(SemVerError());
|
2016-08-19 17:57:21 +00:00
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i != end && *i == '-')
|
|
|
|
{
|
|
|
|
auto prereleaseStart = ++i;
|
|
|
|
while (i != end && *i != '+') ++i;
|
|
|
|
prerelease = string(prereleaseStart, i);
|
|
|
|
}
|
|
|
|
if (i != end && *i == '+')
|
|
|
|
{
|
|
|
|
auto buildStart = ++i;
|
|
|
|
while (i != end) ++i;
|
|
|
|
build = string(buildStart, i);
|
|
|
|
}
|
|
|
|
if (i != end)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(SemVerError());
|
2016-08-19 17:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SemVerMatchExpression::MatchComponent::matches(SemVerVersion const& _version) const
|
|
|
|
{
|
|
|
|
if (prefix == Token::BitNot)
|
|
|
|
{
|
|
|
|
MatchComponent comp = *this;
|
|
|
|
|
|
|
|
comp.prefix = Token::GreaterThanOrEqual;
|
|
|
|
if (!comp.matches(_version))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (levelsPresent >= 2)
|
|
|
|
comp.levelsPresent = 2;
|
|
|
|
else
|
|
|
|
comp.levelsPresent = 1;
|
|
|
|
comp.prefix = Token::LessThanOrEqual;
|
|
|
|
return comp.matches(_version);
|
|
|
|
}
|
|
|
|
else if (prefix == Token::BitXor)
|
|
|
|
{
|
|
|
|
MatchComponent comp = *this;
|
|
|
|
|
|
|
|
comp.prefix = Token::GreaterThanOrEqual;
|
|
|
|
if (!comp.matches(_version))
|
|
|
|
return false;
|
|
|
|
|
2019-12-09 10:58:27 +00:00
|
|
|
if (comp.version.numbers[0] == 0 && comp.levelsPresent != 1)
|
2016-08-19 17:57:21 +00:00
|
|
|
comp.levelsPresent = 2;
|
|
|
|
else
|
|
|
|
comp.levelsPresent = 1;
|
|
|
|
comp.prefix = Token::LessThanOrEqual;
|
|
|
|
return comp.matches(_version);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int cmp = 0;
|
|
|
|
bool didCompare = false;
|
|
|
|
for (unsigned i = 0; i < levelsPresent && cmp == 0; i++)
|
2020-06-02 13:45:03 +00:00
|
|
|
if (version.numbers[i] != std::numeric_limits<unsigned>::max())
|
2016-08-19 17:57:21 +00:00
|
|
|
{
|
|
|
|
didCompare = true;
|
2020-06-02 13:45:03 +00:00
|
|
|
cmp = static_cast<int>(_version.numbers[i] - version.numbers[i]);
|
2016-08-19 17:57:21 +00:00
|
|
|
}
|
2019-10-29 00:46:04 +00:00
|
|
|
|
2016-08-19 17:57:21 +00:00
|
|
|
if (cmp == 0 && !_version.prerelease.empty() && didCompare)
|
|
|
|
cmp = -1;
|
2018-09-23 11:28:51 +00:00
|
|
|
|
|
|
|
switch (prefix)
|
|
|
|
{
|
|
|
|
case Token::Assign:
|
2016-08-19 17:57:21 +00:00
|
|
|
return cmp == 0;
|
2018-09-23 11:28:51 +00:00
|
|
|
case Token::LessThan:
|
2016-08-19 17:57:21 +00:00
|
|
|
return cmp < 0;
|
2018-09-23 11:28:51 +00:00
|
|
|
case Token::LessThanOrEqual:
|
2016-08-19 17:57:21 +00:00
|
|
|
return cmp <= 0;
|
2018-09-23 11:28:51 +00:00
|
|
|
case Token::GreaterThan:
|
2016-08-19 17:57:21 +00:00
|
|
|
return cmp > 0;
|
2018-09-23 11:28:51 +00:00
|
|
|
case Token::GreaterThanOrEqual:
|
2016-08-19 17:57:21 +00:00
|
|
|
return cmp >= 0;
|
2018-09-23 11:28:51 +00:00
|
|
|
default:
|
2016-08-19 17:57:21 +00:00
|
|
|
solAssert(false, "Invalid SemVer expression");
|
2018-09-23 11:28:51 +00:00
|
|
|
}
|
2016-08-19 17:57:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SemVerMatchExpression::Conjunction::matches(SemVerVersion const& _version) const
|
|
|
|
{
|
|
|
|
for (auto const& component: components)
|
|
|
|
if (!component.matches(_version))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SemVerMatchExpression::matches(SemVerVersion const& _version) const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return false;
|
|
|
|
for (auto const& range: m_disjunction)
|
|
|
|
if (range.matches(_version))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-16 22:33:40 +00:00
|
|
|
optional<SemVerMatchExpression> SemVerMatchExpressionParser::parse()
|
2016-08-19 17:57:21 +00:00
|
|
|
{
|
|
|
|
reset();
|
|
|
|
|
2020-12-18 15:43:14 +00:00
|
|
|
if (m_tokens.empty())
|
|
|
|
return nullopt;
|
|
|
|
|
2016-08-19 17:57:21 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
parseMatchExpression();
|
|
|
|
if (m_pos >= m_tokens.size())
|
|
|
|
break;
|
|
|
|
if (currentToken() != Token::Or)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(SemVerError());
|
2016-08-19 17:57:21 +00:00
|
|
|
nextToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (SemVerError const&)
|
|
|
|
{
|
|
|
|
reset();
|
2020-12-16 22:33:40 +00:00
|
|
|
return nullopt;
|
2016-08-19 17:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SemVerMatchExpressionParser::reset()
|
|
|
|
{
|
|
|
|
m_expression = SemVerMatchExpression();
|
|
|
|
m_pos = 0;
|
|
|
|
m_posInside = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SemVerMatchExpressionParser::parseMatchExpression()
|
|
|
|
{
|
|
|
|
// component - component (range)
|
|
|
|
// or component component* (conjunction)
|
|
|
|
|
|
|
|
SemVerMatchExpression::Conjunction range;
|
|
|
|
range.components.push_back(parseMatchComponent());
|
|
|
|
if (currentToken() == Token::Sub)
|
|
|
|
{
|
|
|
|
range.components[0].prefix = Token::GreaterThanOrEqual;
|
|
|
|
nextToken();
|
|
|
|
range.components.push_back(parseMatchComponent());
|
|
|
|
range.components[1].prefix = Token::LessThanOrEqual;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
while (currentToken() != Token::Or && currentToken() != Token::Illegal)
|
|
|
|
range.components.push_back(parseMatchComponent());
|
|
|
|
m_expression.m_disjunction.push_back(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
SemVerMatchExpression::MatchComponent SemVerMatchExpressionParser::parseMatchComponent()
|
|
|
|
{
|
|
|
|
SemVerMatchExpression::MatchComponent component;
|
2018-10-22 14:48:21 +00:00
|
|
|
Token token = currentToken();
|
2018-09-23 11:28:51 +00:00
|
|
|
|
|
|
|
switch (token)
|
2016-08-19 17:57:21 +00:00
|
|
|
{
|
2018-09-23 11:28:51 +00:00
|
|
|
case Token::BitXor:
|
|
|
|
case Token::BitNot:
|
|
|
|
case Token::LessThan:
|
|
|
|
case Token::LessThanOrEqual:
|
|
|
|
case Token::GreaterThan:
|
|
|
|
case Token::GreaterThanOrEqual:
|
|
|
|
case Token::Assign:
|
2016-08-19 17:57:21 +00:00
|
|
|
component.prefix = token;
|
|
|
|
nextToken();
|
2018-09-23 11:28:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-08-19 17:57:21 +00:00
|
|
|
component.prefix = Token::Assign;
|
2018-09-23 11:28:51 +00:00
|
|
|
}
|
2016-08-19 17:57:21 +00:00
|
|
|
|
|
|
|
component.levelsPresent = 0;
|
|
|
|
while (component.levelsPresent < 3)
|
|
|
|
{
|
|
|
|
component.version.numbers[component.levelsPresent] = parseVersionPart();
|
|
|
|
component.levelsPresent++;
|
|
|
|
if (currentChar() == '.')
|
|
|
|
nextChar();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// TODO we do not support pre and build version qualifiers for now in match expressions
|
|
|
|
// (but we do support them in the actual versions)
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned SemVerMatchExpressionParser::parseVersionPart()
|
|
|
|
{
|
|
|
|
auto startPos = m_pos;
|
|
|
|
char c = currentChar();
|
|
|
|
nextChar();
|
|
|
|
if (c == 'x' || c == 'X' || c == '*')
|
|
|
|
return unsigned(-1);
|
|
|
|
else if (c == '0')
|
|
|
|
return 0;
|
|
|
|
else if ('1' <= c && c <= '9')
|
|
|
|
{
|
2020-06-15 10:51:58 +00:00
|
|
|
auto v = static_cast<unsigned>(c - '0');
|
2016-08-19 17:57:21 +00:00
|
|
|
// If we skip to the next token, the current number is terminated.
|
|
|
|
while (m_pos == startPos && '0' <= currentChar() && currentChar() <= '9')
|
|
|
|
{
|
|
|
|
c = currentChar();
|
2020-06-15 10:51:58 +00:00
|
|
|
if (v * 10 < v || v * 10 + static_cast<unsigned>(c - '0') < v * 10)
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(SemVerError());
|
2020-06-15 10:51:58 +00:00
|
|
|
v = v * 10 + static_cast<unsigned>(c - '0');
|
2016-08-19 17:57:21 +00:00
|
|
|
nextChar();
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
else
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(SemVerError());
|
2016-08-19 17:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char SemVerMatchExpressionParser::currentChar() const
|
|
|
|
{
|
|
|
|
if (m_pos >= m_literals.size())
|
|
|
|
return char(-1);
|
|
|
|
if (m_posInside >= m_literals[m_pos].size())
|
|
|
|
return char(-1);
|
|
|
|
return m_literals[m_pos][m_posInside];
|
|
|
|
}
|
|
|
|
|
|
|
|
char SemVerMatchExpressionParser::nextChar()
|
|
|
|
{
|
|
|
|
if (m_pos < m_literals.size())
|
|
|
|
{
|
|
|
|
if (m_posInside + 1 >= m_literals[m_pos].size())
|
|
|
|
nextToken();
|
|
|
|
else
|
|
|
|
++m_posInside;
|
|
|
|
}
|
|
|
|
return currentChar();
|
|
|
|
}
|
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
Token SemVerMatchExpressionParser::currentToken() const
|
2016-08-19 17:57:21 +00:00
|
|
|
{
|
|
|
|
if (m_pos < m_tokens.size())
|
|
|
|
return m_tokens[m_pos];
|
|
|
|
else
|
|
|
|
return Token::Illegal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SemVerMatchExpressionParser::nextToken()
|
|
|
|
{
|
|
|
|
++m_pos;
|
|
|
|
m_posInside = 0;
|
|
|
|
}
|