forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_info.cc
97 lines (80 loc) · 3.23 KB
/
token_info.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
// 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/text/token_info.h"
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <sstream> // IWYU pragma: keep // for ostringstream
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "common/strings/rebase.h"
#include "common/util/logging.h"
#include "common/util/range.h"
namespace verible {
TokenInfo TokenInfo::EOFToken() {
static constexpr absl::string_view null_text;
return TokenInfo(TK_EOF, null_text);
}
TokenInfo TokenInfo::EOFToken(absl::string_view buffer) {
return TokenInfo(TK_EOF, absl::string_view(buffer.end(), 0));
}
bool TokenInfo::operator==(const TokenInfo& token) const {
return token_enum_ == token.token_enum_ &&
(token_enum_ == TK_EOF || // All EOF tokens considered equal.
BoundsEqual(text_, token.text_));
}
TokenInfo::Context::Context(absl::string_view b)
: base(b),
// By default, just print the enum integer value, un-translated.
token_enum_translator([](std::ostream& stream, int e) { stream << e; }) {}
std::ostream& TokenInfo::ToStream(std::ostream& output_stream,
const Context& context) const {
output_stream << "(#";
context.token_enum_translator(output_stream, token_enum_);
output_stream << " @" << left(context.base) << '-' << right(context.base)
<< ": \"" << text_ << "\")";
const auto dist = std::distance(context.base.end(), text_.end());
CHECK(IsSubRange(text_, context.base)) << "text.end() is off by " << dist;
return output_stream;
}
std::ostream& TokenInfo::ToStream(std::ostream& output_stream) const {
return output_stream << "(#" << token_enum_ << ": \"" << text_ << "\")";
}
std::string TokenInfo::ToString(const Context& context) const {
std::ostringstream output_stream;
ToStream(output_stream, context);
return output_stream.str();
}
std::string TokenInfo::ToString() const {
std::ostringstream output_stream;
ToStream(output_stream);
return output_stream.str();
}
void TokenInfo::RebaseStringView(absl::string_view new_text) {
verible::RebaseStringView(&text_, new_text);
}
void TokenInfo::Concatenate(std::string* out, std::vector<TokenInfo>* tokens) {
ConcatenateTokenInfos(out, tokens->begin(), tokens->end());
}
// Print human-readable token information.
std::ostream& operator<<(std::ostream& stream, const TokenInfo& token) {
// This will exclude any byte offset information because the base address
// of the enclosing stream is not known to this function.
return token.ToStream(stream);
}
std::ostream& operator<<(std::ostream& stream, const TokenWithContext& t) {
return t.token.ToStream(stream, t.context);
}
} // namespace verible