Use LineColumn type.

This commit is contained in:
chriseth 2021-11-29 21:19:41 +01:00
parent 622e89649f
commit 86ce5f1da5
4 changed files with 23 additions and 12 deletions

View File

@ -100,7 +100,7 @@ string CharStream::lineAtPosition(int _position) const
return line;
}
tuple<int, int> CharStream::translatePositionToLineColumn(int _position) const
LineColumn CharStream::translatePositionToLineColumn(int _position) const
{
using size_type = string::size_type;
using diff_type = string::difference_type;
@ -114,7 +114,7 @@ tuple<int, int> CharStream::translatePositionToLineColumn(int _position) const
lineStart = m_source.rfind('\n', searchPosition - 1);
lineStart = lineStart == string::npos ? 0 : lineStart + 1;
}
return tuple<int, int>(lineNumber, searchPosition - lineStart);
return LineColumn{lineNumber, static_cast<int>(searchPosition - lineStart)};
}
string_view CharStream::text(SourceLocation const& _location) const

View File

@ -59,6 +59,7 @@ namespace solidity::langutil
{
struct SourceLocation;
struct LineColumn;
/**
* Bidirectional stream of characters.
@ -97,7 +98,7 @@ public:
/// Functions that help pretty-printing parse errors
/// Do only use in error cases, they are quite expensive.
std::string lineAtPosition(int _position) const;
std::tuple<int, int> translatePositionToLineColumn(int _position) const;
LineColumn translatePositionToLineColumn(int _position) const;
///@}
/// Tests whether or not given octet sequence is present at the current position in stream.

View File

@ -119,4 +119,23 @@ SourceLocation parseSourceLocation(
/// Stream output for Location (used e.g. in boost exceptions).
std::ostream& operator<<(std::ostream& _out, SourceLocation const& _location);
/**
* Alternative, line-column-based representation for source locations.
* Both line and column are zero-based.
* If used as a range, the second location is considered exclusive.
* Negative values are invalid.
*/
struct LineColumn
{
/// Line value, can be between zero and number of `\n` characters in the source file.
int line = -1;
/// Column value, can be between zero and number of characters in the line (inclusive).
int column = -1;
LineColumn() = default;
explicit LineColumn(int _line, int _column): line(_line), column(_column) {}
};
}

View File

@ -30,15 +30,6 @@ namespace solidity::langutil
class CharStreamProvider;
struct LineColumn
{
int line = {-1};
int column = {-1};
LineColumn() = default;
LineColumn(std::tuple<int, int> const& _t): line{std::get<0>(_t)}, column{std::get<1>(_t)} {}
};
struct SourceReference
{
std::string message; ///< A message that relates to this source reference (such as a warning, info or an error message).