28#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_TOKEN_H
29#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_TOKEN_H
31#include "clang/Basic/LLVM.h"
32#include "clang/Basic/LangStandard.h"
33#include "clang/Basic/TokenKinds.h"
34#include "llvm/ADT/ArrayRef.h"
35#include "llvm/ADT/STLForwardCompat.h"
36#include "llvm/Support/raw_ostream.h"
54 constexpr static Index Invalid = std::numeric_limits<Index>::max();
61 const char *
Data =
nullptr;
74 template <
class T>
bool flag(T Mask)
const {
75 return Flags & uint8_t{llvm::to_underlying(Mask)};
78 Flags |= uint8_t{llvm::to_underlying(Mask)};
83 assert(
Kind != tok::eof);
88 const Token *T =
this;
91 while (T->Kind == tok::comment);
96 assert(
Kind != tok::eof);
103 clang::tok::TokenKind
Kind = clang::tok::unknown;
107static_assert(
sizeof(
Token) <=
sizeof(
char *) + 24,
"Careful with layout!");
139 explicit TokenStream(std::shared_ptr<void> Payload =
nullptr);
144 Storage.push_back(std::move(T));
157 assert(&T >= Storage.data() && &T < Storage.data() + Storage.size());
158 assert(&T != Storage.data() &&
"start sentinel");
159 return &T - Tokens.data();
186 Payload = std::move(P);
188 Payload = std::make_shared<
189 std::pair<std::shared_ptr<void>, std::shared_ptr<void>>>(
190 std::move(P), std::move(Payload));
196 void print(llvm::raw_ostream &)
const;
199 std::shared_ptr<void> Payload;
201 MutableArrayRef<Token> Tokens;
202 std::vector<Token> Storage;
204llvm::raw_ostream &
operator<<(llvm::raw_ostream &,
const TokenStream &);
210TokenStream
lex(
const std::string &,
const clang::LangOptions &);
228 clang::Language = clang::Language::CXX,
229 clang::LangStandard::Kind = clang::LangStandard::lang_unspecified);
A complete sequence of Tokens representing a source file.
ArrayRef< Token > tokens() const
std::shared_ptr< void > getPayload() const
Returns the shared payload.
Token::Index index(const Token &T) const
Returns the index of T within the stream.
void print(llvm::raw_ostream &) const
Print the tokens in this stream to the output stream.
void finalize()
Finalize the token stream, allowing tokens to be accessed.
void push(Token T)
Append a token to the stream, which must not be finalized.
const Token & front() const
May return the end sentinel if the stream is empty.
MutableArrayRef< Token > tokens()
void addPayload(std::shared_ptr< void > P)
Adds the given payload to the stream.
ArrayRef< Token > tokens(Token::Range R) const
static void lex(llvm::StringRef Code, const LangOptions &LangOpts, llvm::function_ref< void(const syntax::Token &, const SourceManager &SM)> Action)
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
TokenStream stripComments(const TokenStream &Input)
Drops comment tokens.
clang::LangOptions genericLangOpts(clang::Language Lang, clang::LangStandard::Kind Standard)
A generic lang options suitable for lexing/parsing a langage.
TokenStream cook(const TokenStream &Code, const LangOptions &LangOpts)
@ NeedsCleaning
Marks tokens containing trigraphs, escaped newlines, UCNs etc.
@ StartsPPLine
Marks the token at the start of a logical preprocessor line.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
A half-open range of tokens within a stream.
static Range emptyAt(Index Index)
A single C++ or preprocessor token.
uint8_t Indent
Width of whitespace before the first token on this line.
Index OriginalIndex
Index into the original token stream (as raw-lexed from the source code).
const Token * pair() const
Returns the bracket paired with this one, if any.
int32_t Pair
If this token is a paired bracket, the offset of the pair in the stream.
StringRef text() const
The token text.
uint32_t Index
An Index identifies a token within a stream.
static constexpr Index Invalid
A sentinel Index indicating no token.
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.
const Token & next() const
Returns the next token in the stream. this may not be a sentinel.
const Token & nextNC() const
Returns the next token in the stream, skipping over comments.
const Token & prev() const
Returns the previous token in the stream. this may not be a sentinel.
uint8_t Flags
Flags have some meaning defined by the function that produced this stream.