OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/tokenizer.h" | 5 #include "tools/gn/tokenizer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "tools/gn/input_file.h" | 8 #include "tools/gn/input_file.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 if (type != Token::COMMENT) | 127 if (type != Token::COMMENT) |
128 tokens_.push_back(Token(location, type, token_value)); | 128 tokens_.push_back(Token(location, type, token_value)); |
129 } | 129 } |
130 if (err_->has_error()) | 130 if (err_->has_error()) |
131 tokens_.clear(); | 131 tokens_.clear(); |
132 return tokens_; | 132 return tokens_; |
133 } | 133 } |
134 | 134 |
135 // static | 135 // static |
136 size_t Tokenizer::ByteOffsetOfNthLine(const base::StringPiece& buf, int n) { | 136 size_t Tokenizer::ByteOffsetOfNthLine(const base::StringPiece& buf, int n) { |
137 int cur_line = 1; | 137 DCHECK_GT(n, 0); |
138 size_t cur_byte = 0; | |
139 | |
140 DCHECK(n > 0); | |
141 | 138 |
142 if (n == 1) | 139 if (n == 1) |
143 return 0; | 140 return 0; |
144 | 141 |
| 142 int cur_line = 1; |
| 143 size_t cur_byte = 0; |
145 while (cur_byte < buf.size()) { | 144 while (cur_byte < buf.size()) { |
146 if (IsNewline(buf, cur_byte)) { | 145 if (IsNewline(buf, cur_byte)) { |
147 cur_line++; | 146 cur_line++; |
148 if (cur_line == n) | 147 if (cur_line == n) |
149 return cur_byte + 1; | 148 return cur_byte + 1; |
150 } | 149 } |
151 cur_byte++; | 150 cur_byte++; |
152 } | 151 } |
153 return -1; | 152 return static_cast<size_t>(-1); |
154 } | 153 } |
155 | 154 |
156 // static | 155 // static |
157 bool Tokenizer::IsNewline(const base::StringPiece& buffer, size_t offset) { | 156 bool Tokenizer::IsNewline(const base::StringPiece& buffer, size_t offset) { |
158 DCHECK(offset < buffer.size()); | 157 DCHECK(offset < buffer.size()); |
159 // We may need more logic here to handle different line ending styles. | 158 // We may need more logic here to handle different line ending styles. |
160 return buffer[offset] == '\n'; | 159 return buffer[offset] == '\n'; |
161 } | 160 } |
162 | 161 |
163 | 162 |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 } else if (cur_char() == '/' && cur_ + 1 < input_.size() && | 354 } else if (cur_char() == '/' && cur_ + 1 < input_.size() && |
356 (input_[cur_ + 1] == '/' || input_[cur_ + 1] == '*')) { | 355 (input_[cur_ + 1] == '/' || input_[cur_ + 1] == '*')) { |
357 // Different types of comments. | 356 // Different types of comments. |
358 help = "Comments should start with # instead"; | 357 help = "Comments should start with # instead"; |
359 } else { | 358 } else { |
360 help = "I have no idea what this is."; | 359 help = "I have no idea what this is."; |
361 } | 360 } |
362 | 361 |
363 return Err(location, "Invalid token.", help); | 362 return Err(location, "Invalid token.", help); |
364 } | 363 } |
OLD | NEW |