Commit 21aff0d0 authored by tfarina's avatar tfarina Committed by Commit bot

tools/gn: rename char_offset to column_number

While looking at Location and trying to understand what char offset meant,
I got very confused, because in some languages, like Portuguese, it is
hard to translate offset and thus understand what it means. Actually
char offset is referring to the column number in text buffer, which is much
easier to understand and its meaning is instantly understood, since everyone is
familiar with the concenpt of rows (lines) and columns in a text editor.

BUG=None
TEST=gn gen + gn_unittests
R=brettw@chromium.org

Review URL: https://codereview.chromium.org/1525183002

Cr-Commit-Position: refs/heads/master@{#367883}
parent b79dea66
...@@ -15,8 +15,8 @@ bool RangeIs(const LocationRange& range, ...@@ -15,8 +15,8 @@ bool RangeIs(const LocationRange& range,
int line, int begin_char, int end_char) { int line, int begin_char, int end_char) {
return range.begin().line_number() == line && return range.begin().line_number() == line &&
range.end().line_number() == line && range.end().line_number() == line &&
range.begin().char_offset() == begin_char && range.begin().column_number() == begin_char &&
range.end().char_offset() == end_char; range.end().column_number() == end_char;
} }
} // namespace } // namespace
......
...@@ -39,13 +39,13 @@ void FillRangeOnLine(const LocationRange& range, int line_number, ...@@ -39,13 +39,13 @@ void FillRangeOnLine(const LocationRange& range, int line_number,
if (range.begin().line_number() < line_number) if (range.begin().line_number() < line_number)
begin_char = 0; begin_char = 0;
else else
begin_char = range.begin().char_offset() - 1; begin_char = range.begin().column_number() - 1;
int end_char; int end_char;
if (range.end().line_number() > line_number) if (range.end().line_number() > line_number)
end_char = static_cast<int>(line->size()); // Ending is non-inclusive. end_char = static_cast<int>(line->size()); // Ending is non-inclusive.
else else
end_char = range.end().char_offset() - 1; end_char = range.end().column_number() - 1;
CHECK(end_char >= begin_char); CHECK(end_char >= begin_char);
CHECK(begin_char >= 0 && begin_char <= static_cast<int>(line->size())); CHECK(begin_char >= 0 && begin_char <= static_cast<int>(line->size()));
...@@ -71,9 +71,9 @@ void OutputHighlighedPosition(const Location& location, ...@@ -71,9 +71,9 @@ void OutputHighlighedPosition(const Location& location,
// Allow the marker to be one past the end of the line for marking the end. // Allow the marker to be one past the end of the line for marking the end.
highlight.push_back(' '); highlight.push_back(' ');
CHECK(location.char_offset() - 1 >= 0 && CHECK(location.column_number() - 1 >= 0 &&
location.char_offset() - 1 < static_cast<int>(highlight.size())); location.column_number() - 1 < static_cast<int>(highlight.size()));
highlight[location.char_offset() - 1] = '^'; highlight[location.column_number() - 1] = '^';
// Trim unused spaces from end of line. // Trim unused spaces from end of line.
while (!highlight.empty() && highlight[highlight.size() - 1] == ' ') while (!highlight.empty() && highlight[highlight.size() - 1] == ' ')
......
...@@ -63,11 +63,11 @@ LocationRange CreatePersistentRange(const InputFile& input_file, ...@@ -63,11 +63,11 @@ LocationRange CreatePersistentRange(const InputFile& input_file,
return LocationRange(Location(clone_input_file, return LocationRange(Location(clone_input_file,
range.begin().line_number(), range.begin().line_number(),
range.begin().char_offset(), range.begin().column_number(),
-1 /* TODO(scottmg) */), -1 /* TODO(scottmg) */),
Location(clone_input_file, Location(clone_input_file,
range.end().line_number(), range.end().line_number(),
range.end().char_offset(), range.end().column_number(),
-1 /* TODO(scottmg) */)); -1 /* TODO(scottmg) */));
} }
......
...@@ -126,7 +126,7 @@ TEST_F(InputConversionTest, ValueDict) { ...@@ -126,7 +126,7 @@ TEST_F(InputConversionTest, ValueDict) {
ASSERT_TRUE(a_origin); ASSERT_TRUE(a_origin);
LocationRange a_range = a_origin->GetRange(); LocationRange a_range = a_origin->GetRange();
EXPECT_EQ(2, a_range.begin().line_number()); EXPECT_EQ(2, a_range.begin().line_number());
EXPECT_EQ(6, a_range.begin().char_offset()); EXPECT_EQ(6, a_range.begin().column_number());
const InputFile* a_file = a_range.begin().file(); const InputFile* a_file = a_range.begin().file();
EXPECT_EQ(input, a_file->contents()); EXPECT_EQ(input, a_file->contents());
......
...@@ -13,23 +13,23 @@ ...@@ -13,23 +13,23 @@
Location::Location() Location::Location()
: file_(nullptr), : file_(nullptr),
line_number_(-1), line_number_(-1),
char_offset_(-1) { column_number_(-1) {
} }
Location::Location(const InputFile* file, Location::Location(const InputFile* file,
int line_number, int line_number,
int char_offset, int column_number,
int byte) int byte)
: file_(file), : file_(file),
line_number_(line_number), line_number_(line_number),
char_offset_(char_offset), column_number_(column_number),
byte_(byte) { byte_(byte) {
} }
bool Location::operator==(const Location& other) const { bool Location::operator==(const Location& other) const {
return other.file_ == file_ && return other.file_ == file_ &&
other.line_number_ == line_number_ && other.line_number_ == line_number_ &&
other.char_offset_ == char_offset_; other.column_number_ == column_number_;
} }
bool Location::operator!=(const Location& other) const { bool Location::operator!=(const Location& other) const {
...@@ -38,11 +38,11 @@ bool Location::operator!=(const Location& other) const { ...@@ -38,11 +38,11 @@ bool Location::operator!=(const Location& other) const {
bool Location::operator<(const Location& other) const { bool Location::operator<(const Location& other) const {
DCHECK(file_ == other.file_); DCHECK(file_ == other.file_);
return std::tie(line_number_, char_offset_) < return std::tie(line_number_, column_number_) <
std::tie(other.line_number_, other.char_offset_); std::tie(other.line_number_, other.column_number_);
} }
std::string Location::Describe(bool include_char_offset) const { std::string Location::Describe(bool include_column_number) const {
if (!file_) if (!file_)
return std::string(); return std::string();
...@@ -54,9 +54,9 @@ std::string Location::Describe(bool include_char_offset) const { ...@@ -54,9 +54,9 @@ std::string Location::Describe(bool include_char_offset) const {
ret += ":"; ret += ":";
ret += base::IntToString(line_number_); ret += base::IntToString(line_number_);
if (include_char_offset) { if (include_column_number) {
ret += ":"; ret += ":";
ret += base::IntToString(char_offset_); ret += base::IntToString(column_number_);
} }
return ret; return ret;
} }
......
...@@ -13,11 +13,11 @@ class InputFile; ...@@ -13,11 +13,11 @@ class InputFile;
class Location { class Location {
public: public:
Location(); Location();
Location(const InputFile* file, int line_number, int char_offset, int byte); Location(const InputFile* file, int line_number, int column_number, int byte);
const InputFile* file() const { return file_; } const InputFile* file() const { return file_; }
int line_number() const { return line_number_; } int line_number() const { return line_number_; }
int char_offset() const { return char_offset_; } int column_number() const { return column_number_; }
int byte() const { return byte_; } int byte() const { return byte_; }
bool operator==(const Location& other) const; bool operator==(const Location& other) const;
...@@ -27,12 +27,12 @@ class Location { ...@@ -27,12 +27,12 @@ class Location {
// Returns a string with the file, line, and (optionally) the character // Returns a string with the file, line, and (optionally) the character
// offset for this location. If this location is null, returns an empty // offset for this location. If this location is null, returns an empty
// string. // string.
std::string Describe(bool include_char_offset) const; std::string Describe(bool include_column_number) const;
private: private:
const InputFile* file_; // Null when unset. const InputFile* file_; // Null when unset.
int line_number_; // -1 when unset. 1-based. int line_number_; // -1 when unset. 1-based.
int char_offset_; // -1 when unset. 1-based. int column_number_; // -1 when unset. 1-based.
int byte_; // Index into the buffer, 0-based. int byte_; // Index into the buffer, 0-based.
}; };
......
...@@ -256,7 +256,7 @@ Value AccessorNode::ExecuteScopeAccess(Scope* scope, Err* err) const { ...@@ -256,7 +256,7 @@ Value AccessorNode::ExecuteScopeAccess(Scope* scope, Err* err) const {
void AccessorNode::SetNewLocation(int line_number) { void AccessorNode::SetNewLocation(int line_number) {
Location old = base_.location(); Location old = base_.location();
base_.set_location( base_.set_location(
Location(old.file(), line_number, old.char_offset(), old.byte())); Location(old.file(), line_number, old.column_number(), old.byte()));
} }
// BinaryOpNode --------------------------------------------------------------- // BinaryOpNode ---------------------------------------------------------------
...@@ -483,7 +483,7 @@ void IdentifierNode::Print(std::ostream& out, int indent) const { ...@@ -483,7 +483,7 @@ void IdentifierNode::Print(std::ostream& out, int indent) const {
void IdentifierNode::SetNewLocation(int line_number) { void IdentifierNode::SetNewLocation(int line_number) {
Location old = value_.location(); Location old = value_.location();
value_.set_location( value_.set_location(
Location(old.file(), line_number, old.char_offset(), old.byte())); Location(old.file(), line_number, old.column_number(), old.byte()));
} }
// ListNode ------------------------------------------------------------------- // ListNode -------------------------------------------------------------------
...@@ -742,7 +742,7 @@ void LiteralNode::Print(std::ostream& out, int indent) const { ...@@ -742,7 +742,7 @@ void LiteralNode::Print(std::ostream& out, int indent) const {
void LiteralNode::SetNewLocation(int line_number) { void LiteralNode::SetNewLocation(int line_number) {
Location old = value_.location(); Location old = value_.location();
value_.set_location( value_.set_location(
Location(old.file(), line_number, old.char_offset(), old.byte())); Location(old.file(), line_number, old.column_number(), old.byte()));
} }
// UnaryOpNode ---------------------------------------------------------------- // UnaryOpNode ----------------------------------------------------------------
......
...@@ -94,7 +94,7 @@ TEST(ParseTree, BlockUnusedVars) { ...@@ -94,7 +94,7 @@ TEST(ParseTree, BlockUnusedVars) {
// origin will point to the value assigned to the variable (in this case, the // origin will point to the value assigned to the variable (in this case, the
// "13" assigned to "b". // "13" assigned to "b".
EXPECT_EQ(3, err.location().line_number()); EXPECT_EQ(3, err.location().line_number());
EXPECT_EQ(7, err.location().char_offset()); EXPECT_EQ(7, err.location().column_number());
} }
TEST(ParseTree, OriginForDereference) { TEST(ParseTree, OriginForDereference) {
...@@ -111,7 +111,7 @@ TEST(ParseTree, OriginForDereference) { ...@@ -111,7 +111,7 @@ TEST(ParseTree, OriginForDereference) {
// The origin for the "not a string" error message should be where the value // The origin for the "not a string" error message should be where the value
// was dereferenced (the "a" on the second line). // was dereferenced (the "a" on the second line).
EXPECT_EQ(2, err.location().line_number()); EXPECT_EQ(2, err.location().line_number());
EXPECT_EQ(20, err.location().char_offset()); EXPECT_EQ(20, err.location().column_number());
} }
TEST(ParseTree, SortRangeExtraction) { TEST(ParseTree, SortRangeExtraction) {
......
...@@ -68,7 +68,7 @@ void DoParserErrorTest(const char* input, int err_line, int err_char) { ...@@ -68,7 +68,7 @@ void DoParserErrorTest(const char* input, int err_line, int err_char) {
} }
EXPECT_EQ(err_line, err.location().line_number()); EXPECT_EQ(err_line, err.location().line_number());
EXPECT_EQ(err_char, err.location().char_offset()); EXPECT_EQ(err_char, err.location().column_number());
} }
// Expects the tokenizer or parser to identify an error at the given line and // Expects the tokenizer or parser to identify an error at the given line and
...@@ -86,7 +86,7 @@ void DoExpressionErrorTest(const char* input, int err_line, int err_char) { ...@@ -86,7 +86,7 @@ void DoExpressionErrorTest(const char* input, int err_line, int err_char) {
} }
EXPECT_EQ(err_line, err.location().line_number()); EXPECT_EQ(err_line, err.location().line_number());
EXPECT_EQ(err_char, err.location().char_offset()); EXPECT_EQ(err_char, err.location().column_number());
} }
} // namespace } // namespace
......
...@@ -27,12 +27,13 @@ Err ErrInsideStringToken(const Token& token, size_t offset, size_t size, ...@@ -27,12 +27,13 @@ Err ErrInsideStringToken(const Token& token, size_t offset, size_t size,
int int_offset = static_cast<int>(offset); int int_offset = static_cast<int>(offset);
Location begin_loc(token.location().file(), Location begin_loc(token.location().file(),
token.location().line_number(), token.location().line_number(),
token.location().char_offset() + int_offset + 1, token.location().column_number() + int_offset + 1,
token.location().byte() + int_offset + 1); token.location().byte() + int_offset + 1);
Location end_loc( Location end_loc(
token.location().file(), token.location().file(),
token.location().line_number(), token.location().line_number(),
token.location().char_offset() + int_offset + 1 + static_cast<int>(size), token.location().column_number() + int_offset + 1 +
static_cast<int>(size),
token.location().byte() + int_offset + 1 + static_cast<int>(size)); token.location().byte() + int_offset + 1 + static_cast<int>(size));
return Err(LocationRange(begin_loc, end_loc), msg, help); return Err(LocationRange(begin_loc, end_loc), msg, help);
} }
......
...@@ -68,7 +68,7 @@ class Token { ...@@ -68,7 +68,7 @@ class Token {
location_, location_,
Location(location_.file(), Location(location_.file(),
location_.line_number(), location_.line_number(),
location_.char_offset() + static_cast<int>(value_.size()), location_.column_number() + static_cast<int>(value_.size()),
location_.byte() + static_cast<int>(value_.size()))); location_.byte() + static_cast<int>(value_.size())));
} }
......
...@@ -74,7 +74,7 @@ Tokenizer::Tokenizer(const InputFile* input_file, Err* err) ...@@ -74,7 +74,7 @@ Tokenizer::Tokenizer(const InputFile* input_file, Err* err)
err_(err), err_(err),
cur_(0), cur_(0),
line_number_(1), line_number_(1),
char_in_line_(1) { column_number_(1) {
} }
Tokenizer::~Tokenizer() { Tokenizer::~Tokenizer() {
...@@ -126,7 +126,8 @@ std::vector<Token> Tokenizer::Run() { ...@@ -126,7 +126,8 @@ std::vector<Token> Tokenizer::Run() {
(tokens_.empty() || tokens_.back().type() != Token::SUFFIX_COMMENT || (tokens_.empty() || tokens_.back().type() != Token::SUFFIX_COMMENT ||
tokens_.back().location().line_number() + 1 != tokens_.back().location().line_number() + 1 !=
location.line_number() || location.line_number() ||
tokens_.back().location().char_offset() != location.char_offset())) { tokens_.back().location().column_number() !=
location.column_number())) {
type = Token::LINE_COMMENT; type = Token::LINE_COMMENT;
if (!at_end()) // Could be EOF. if (!at_end()) // Could be EOF.
Advance(); // The current \n. Advance(); // The current \n.
...@@ -374,16 +375,16 @@ void Tokenizer::Advance() { ...@@ -374,16 +375,16 @@ void Tokenizer::Advance() {
DCHECK(cur_ < input_.size()); DCHECK(cur_ < input_.size());
if (IsCurrentNewline()) { if (IsCurrentNewline()) {
line_number_++; line_number_++;
char_in_line_ = 1; column_number_ = 1;
} else { } else {
char_in_line_++; column_number_++;
} }
cur_++; cur_++;
} }
Location Tokenizer::GetCurrentLocation() const { Location Tokenizer::GetCurrentLocation() const {
return Location( return Location(
input_file_, line_number_, char_in_line_, static_cast<int>(cur_)); input_file_, line_number_, column_number_, static_cast<int>(cur_));
} }
Err Tokenizer::GetErrorForInvalidToken(const Location& location) const { Err Tokenizer::GetErrorForInvalidToken(const Location& location) const {
......
...@@ -82,7 +82,7 @@ class Tokenizer { ...@@ -82,7 +82,7 @@ class Tokenizer {
size_t cur_; // Byte offset into input buffer. size_t cur_; // Byte offset into input buffer.
int line_number_; int line_number_;
int char_in_line_; int column_number_;
DISALLOW_COPY_AND_ASSIGN(Tokenizer); DISALLOW_COPY_AND_ASSIGN(Tokenizer);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment