Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use LineColumn type. #12345

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions liblangutil/CharStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion liblangutil/CharStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace solidity::langutil
{

struct SourceLocation;
struct LineColumn;

/**
* Bidirectional stream of characters.
Expand Down Expand Up @@ -97,7 +98,7 @@ class CharStream
/// 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.
Expand Down
19 changes: 19 additions & 0 deletions liblangutil/SourceLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Comment on lines +136 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it even need a constructor now that arguments match struct fields and are no longer a tuple?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but can we have default values and prevent construction from just a single value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, that's a good argument for keeping the constructor.

};


}
9 changes: 0 additions & 9 deletions liblangutil/SourceReferenceExtractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down