72 using Index = unsigned;
73 constexpr static Index
None = -1;
75 enum BracketKind :
char { Paren, Brace, Square }
Kind;
76 enum Direction :
bool { Open, Close }
Dir;
84std::vector<Bracket> findBrackets(
const TokenStream &Stream) {
85 std::vector<Bracket> Brackets;
86 auto Add = [&](
const Token &Tok, Bracket::BracketKind
K,
87 Bracket::Direction D) {
89 {
K, D, Tok.
Line, Tok.
Indent, Stream.index(Tok), Bracket::None});
91 for (
const auto &Tok : Stream.tokens()) {
93 case clang::tok::l_paren:
94 Add(Tok, Bracket::Paren, Bracket::Open);
96 case clang::tok::r_paren:
97 Add(Tok, Bracket::Paren, Bracket::Close);
99 case clang::tok::l_brace:
100 Add(Tok, Bracket::Brace, Bracket::Open);
102 case clang::tok::r_brace:
103 Add(Tok, Bracket::Brace, Bracket::Close);
105 case clang::tok::l_square:
106 Add(Tok, Bracket::Square, Bracket::Open);
108 case clang::tok::r_square:
109 Add(Tok, Bracket::Square, Bracket::Close);
119void applyPairings(ArrayRef<Bracket> Brackets, TokenStream &Tokens) {
120 for (
const auto &B : Brackets)
121 Tokens.tokens()[
B.Tok].Pair =
122 (
B.Pair == Bracket::None) ? 0 : (int32_t)Brackets[
B.Pair].Tok -
B.Tok;
128void simplePairBrackets(MutableArrayRef<Bracket> Brackets) {
129 std::vector<unsigned> Stack;
130 for (
unsigned I = 0; I < Brackets.size(); ++I) {
131 if (Brackets[I].
Dir == Bracket::Open) {
133 }
else if (!Stack.empty() &&
134 Brackets[Stack.back()].Kind == Brackets[I].Kind) {
135 Brackets[Stack.back()].Pair = I;
136 Brackets[I].Pair = Stack.back();
149 auto Brackets = findBrackets(Stream);
150 simplePairBrackets(Brackets);
151 applyPairings(Brackets, Stream);
enum clang::clangd::@1062::Bracket::Direction Dir
static constexpr Index None
A complete sequence of Tokens representing a source file.
void pairBrackets(TokenStream &Stream)
Identifies bracket token in the stream which should be paired.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
uint8_t Indent
Width of whitespace before the first token on this line.
uint32_t Index
An Index identifies a token within a stream.
uint32_t Line
Zero-based line number for the start of the token.
clang::tok::TokenKind Kind
The type of token as determined by clang's lexer.