From 86ce5f1da50e7de9c4d0649273637adda10f46b9 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 29 Nov 2021 21:19:41 +0100 Subject: [PATCH] Use LineColumn type. --- liblangutil/CharStream.cpp | 4 ++-- liblangutil/CharStream.h | 3 ++- liblangutil/SourceLocation.h | 19 +++++++++++++++++++ liblangutil/SourceReferenceExtractor.h | 9 --------- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/liblangutil/CharStream.cpp b/liblangutil/CharStream.cpp index 3b30bad77..96a6aebe2 100644 --- a/liblangutil/CharStream.cpp +++ b/liblangutil/CharStream.cpp @@ -100,7 +100,7 @@ string CharStream::lineAtPosition(int _position) const return line; } -tuple 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 CharStream::translatePositionToLineColumn(int _position) const lineStart = m_source.rfind('\n', searchPosition - 1); lineStart = lineStart == string::npos ? 0 : lineStart + 1; } - return tuple(lineNumber, searchPosition - lineStart); + return LineColumn{lineNumber, static_cast(searchPosition - lineStart)}; } string_view CharStream::text(SourceLocation const& _location) const diff --git a/liblangutil/CharStream.h b/liblangutil/CharStream.h index 761cece31..2e5d79d93 100644 --- a/liblangutil/CharStream.h +++ b/liblangutil/CharStream.h @@ -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 translatePositionToLineColumn(int _position) const; + LineColumn translatePositionToLineColumn(int _position) const; ///@} /// Tests whether or not given octet sequence is present at the current position in stream. diff --git a/liblangutil/SourceLocation.h b/liblangutil/SourceLocation.h index ea9958933..ddb457535 100644 --- a/liblangutil/SourceLocation.h +++ b/liblangutil/SourceLocation.h @@ -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) {} +}; + + } diff --git a/liblangutil/SourceReferenceExtractor.h b/liblangutil/SourceReferenceExtractor.h index 6aeb6065e..3b418fcfa 100644 --- a/liblangutil/SourceReferenceExtractor.h +++ b/liblangutil/SourceReferenceExtractor.h @@ -30,15 +30,6 @@ namespace solidity::langutil class CharStreamProvider; -struct LineColumn -{ - int line = {-1}; - int column = {-1}; - - LineColumn() = default; - LineColumn(std::tuple 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).