forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_token.cc
301 lines (274 loc) · 10.4 KB
/
format_token.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/formatting/format_token.h"
#include <iostream>
#include <sstream> // pragma IWYU: keep // for ostringstream
#include <string>
#include "absl/base/macros.h"
#include "absl/strings/string_view.h"
#include "common/strings/display_utils.h"
#include "common/strings/range.h"
#include "common/text/token_info.h"
#include "common/util/iterator_adaptors.h"
#include "common/util/logging.h"
#include "common/util/spacer.h"
namespace verible {
std::ostream& operator<<(std::ostream& stream, SpacingOptions b) {
switch (b) {
case SpacingOptions::Undecided:
stream << "undecided";
break;
case SpacingOptions::MustAppend:
stream << "must-append";
break;
case SpacingOptions::MustWrap:
stream << "must-wrap";
break;
case SpacingOptions::AppendAligned:
stream << "append-aligned";
break;
case SpacingOptions::Preserve:
stream << "preserve";
break;
}
return stream;
}
std::ostream& operator<<(std::ostream& stream, GroupBalancing b) {
switch (b) {
case GroupBalancing::None:
stream << "none";
break;
case GroupBalancing::Open:
stream << "open";
break;
case GroupBalancing::Close:
stream << "close";
break;
}
return stream;
}
std::ostream& operator<<(std::ostream& stream, const InterTokenInfo& t) {
stream << "{\n spaces_required: " << t.spaces_required
<< "\n break_penalty: " << t.break_penalty
<< "\n break_decision: " << t.break_decision
<< "\n preserve_space?: " << (t.preserved_space_start != nullptr)
<< "\n}";
return stream;
}
std::ostream& InterTokenInfo::CompactNotation(std::ostream& stream) const {
stream << '<';
// break_penalty is irrelevant when the options are constrained,
// so don't bother showing it in those cases.
switch (break_decision) {
case SpacingOptions::Undecided:
stream << '_' << spaces_required << ',' << break_penalty;
break;
case SpacingOptions::MustAppend:
stream << "+_" << spaces_required;
break;
case SpacingOptions::MustWrap:
// spaces_required is irrelevant
stream << "\\n";
break;
case SpacingOptions::AppendAligned:
stream << "|_" << spaces_required;
break;
case SpacingOptions::Preserve:
stream << "pre";
break;
}
return stream << '>';
}
std::ostream& operator<<(std::ostream& stream, SpacingDecision d) {
switch (d) {
case SpacingDecision::Append:
stream << "append";
break;
case SpacingDecision::Wrap:
stream << "wrap";
break;
case SpacingDecision::Align:
stream << "align";
break;
case SpacingDecision::Preserve:
stream << "preserve";
break;
}
return stream;
}
static SpacingDecision ConvertSpacing(SpacingOptions opt) {
switch (opt) {
case SpacingOptions::MustWrap:
return SpacingDecision::Wrap;
case SpacingOptions::MustAppend:
return SpacingDecision::Append;
case SpacingOptions::AppendAligned:
return SpacingDecision::Align;
default: // Undecided, Preserve
return SpacingDecision::Preserve;
}
}
InterTokenDecision::InterTokenDecision(const InterTokenInfo& info)
: spaces(info.spaces_required),
action(ConvertSpacing(info.break_decision)),
preserved_space_start(info.preserved_space_start) {}
static absl::string_view OriginalLeadingSpacesRange(const char* begin,
const char* end) {
if (begin == nullptr) {
VLOG(4) << "no original space range";
return make_string_view_range(end, end); // empty range
}
// The original spacing points into the original string buffer, and may span
// multiple whitespace tokens.
VLOG(4) << "non-null original space range";
return make_string_view_range(begin, end);
}
absl::string_view FormattedToken::OriginalLeadingSpaces() const {
return OriginalLeadingSpacesRange(before.preserved_space_start,
token->text().begin());
}
std::ostream& FormattedToken::FormattedText(std::ostream& stream) const {
switch (before.action) {
case SpacingDecision::Preserve: {
if (before.preserved_space_start != nullptr) {
// Calculate string_view range of pre-existing spaces, and print that.
stream << OriginalLeadingSpaces();
} else {
// During testing, we are less interested in Preserve mode due to lack
// of "original spacing", so fall-back to safe behavior.
stream << Spacer(before.spaces);
}
break;
}
case SpacingDecision::Wrap:
// Never print spaces before a newline.
stream << '\n';
ABSL_FALLTHROUGH_INTENDED;
case SpacingDecision::Align:
case SpacingDecision::Append:
stream << Spacer(before.spaces);
break;
}
return stream << token->text();
}
std::ostream& operator<<(std::ostream& stream, const FormattedToken& token) {
return token.FormattedText(stream);
}
absl::string_view PreFormatToken::OriginalLeadingSpaces() const {
return OriginalLeadingSpacesRange(before.preserved_space_start,
token->text().begin());
}
size_t PreFormatToken::LeadingSpacesLength() const {
if (before.break_decision == SpacingOptions::Preserve &&
before.preserved_space_start != nullptr) {
return OriginalLeadingSpaces().length();
}
// in other cases (append, wrap), take the spaces_required value.
return before.spaces_required;
}
int PreFormatToken::ExcessSpaces() const {
if (before.preserved_space_start == nullptr) return 0;
const absl::string_view leading_spaces = OriginalLeadingSpaces();
int delta = 0;
if (!absl::StrContains(leading_spaces, "\n")) {
delta = static_cast<int>(leading_spaces.length()) - before.spaces_required;
}
return delta;
}
std::string PreFormatToken::ToString() const {
std::ostringstream output_stream;
output_stream << *this;
return output_stream.str();
}
// Human readable token information
std::ostream& operator<<(std::ostream& stream, const PreFormatToken& t) {
// don't care about byte offsets
return t.token->ToStream(stream << "TokenInfo: ")
<< "\nenum: " << t.format_token_enum << "\nbefore: " << t.before
<< "\nbalance: " << t.balancing << std::endl;
}
void ConnectPreFormatTokensPreservedSpaceStarts(
const char* buffer_start, std::vector<PreFormatToken>* format_tokens) {
VLOG(4) << __FUNCTION__;
CHECK(buffer_start != nullptr);
for (auto& ftoken : *format_tokens) {
ftoken.before.preserved_space_start = buffer_start;
VLOG(4) << "space: " << VisualizeWhitespace(ftoken.OriginalLeadingSpaces());
buffer_start = ftoken.Text().end();
}
// This does not cover the spacing between the last token and EOF.
}
// Finds the span of format tokens covered by the 'byte_offset_range'.
// Run-time: O(lg N) due to binary search
static MutableFormatTokenRange FindFormatTokensInByteOffsetRange(
std::vector<PreFormatToken>::iterator begin,
std::vector<PreFormatToken>::iterator end,
const std::pair<int, int>& byte_offset_range, absl::string_view base_text) {
const auto tokens_begin =
std::lower_bound(begin, end, byte_offset_range.first,
[=](const PreFormatToken& t, int position) {
return t.token->left(base_text) < position;
});
const auto tokens_end =
std::upper_bound(tokens_begin, end, byte_offset_range.second,
[=](int position, const PreFormatToken& t) {
return position < t.token->right(base_text);
});
return {tokens_begin, tokens_end};
}
void PreserveSpacesOnDisabledTokenRanges(
std::vector<PreFormatToken>* ftokens,
const ByteOffsetSet& disabled_byte_ranges, absl::string_view base_text) {
VLOG(2) << __FUNCTION__;
// saved_iter: shrink bounds of binary search with every iteration,
// due to monotonic, non-overlapping intervals.
auto saved_iter = ftokens->begin();
for (const auto& byte_range : disabled_byte_ranges) {
// 'disable_range' marks the range of format tokens to be
// marked as preserving original spacing (i.e. not formatted).
VLOG(2) << "disabling bytes: " << AsInterval(byte_range);
const auto disable_range = FindFormatTokensInByteOffsetRange(
saved_iter, ftokens->end(), byte_range, base_text);
const std::pair<int, int> disabled_token_indices(SubRangeIndices(
disable_range, make_range(ftokens->begin(), ftokens->end())));
VLOG(2) << "disabling tokens: " << AsInterval(disabled_token_indices);
// kludge: When the disabled range immediately follows a //-style
// comment, skip past the trailing '\n' (not included in the comment
// token), which will be printed by the Emit() method, and preserve the
// whitespaces *beyond* that point up to the start of the following
// token's text. This way, rendering the start of the format-disabled
// excerpt won't get redundant '\n's.
if (!disable_range.empty()) {
auto& first = disable_range.front();
VLOG(3) << "checking whether first ftoken in range is a must-wrap.";
if (first.before.break_decision == SpacingOptions::MustWrap) {
VLOG(3) << "checking if spaces before first ftoken starts with \\n.";
const absl::string_view leading_space = first.OriginalLeadingSpaces();
// consume the first '\n' from the preceding inter-token spaces
if (absl::StartsWith(leading_space, "\n")) {
VLOG(3) << "consuming leading \\n.";
++first.before.preserved_space_start;
}
}
}
// Mark tokens in the disabled range as preserving original spaces.
for (auto& ft : disable_range) {
VLOG(2) << "disable-format preserve spaces before: " << *ft.token;
ft.before.break_decision = SpacingOptions::Preserve;
}
// start next iteration search from previous iteration's end
saved_iter = disable_range.end();
}
}
} // namespace verible