solidity/liblangutil/SourceLocation.cpp

53 lines
1.6 KiB
C++
Raw Normal View History

2019-09-11 19:16:35 +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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2019-09-11 19:16:35 +00:00
#include <liblangutil/Exceptions.h>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
using namespace solidity;
namespace solidity::langutil
{
SourceLocation const parseSourceLocation(std::string const& _input, std::string const& _sourceName, size_t _maxIndex)
{
// Expected input: "start:length:sourceindex"
enum SrcElem : size_t { Start, Length, Index };
std::vector<std::string> pos;
boost::algorithm::split(pos, _input, boost::is_any_of(":"));
2021-06-29 12:38:59 +00:00
// TODO What to do with sourceIndex?
solAssert(pos.size() == 3, "SourceLocation string must have 3 colon separated numeric fields.");
auto const sourceIndex = stoi(pos[Index]);
2019-09-11 19:16:35 +00:00
astAssert(
sourceIndex == -1 || _maxIndex >= static_cast<size_t>(sourceIndex),
2019-09-11 19:16:35 +00:00
"'src'-field ill-formatted or src-index too high"
);
int start = stoi(pos[Start]);
int end = start + stoi(pos[Length]);
2021-06-29 12:38:59 +00:00
return SourceLocation{start, end, std::make_shared<std::string>(_sourceName)};
2019-09-11 19:16:35 +00:00
}
}