|
|
@@ -28,54 +28,55 @@ class TokenizedBuffer;
|
|
|
|
|
|
// A lightweight handle to a lexed token in a `TokenizedBuffer`.
|
|
|
//
|
|
|
-// `Token` objects are designed to be passed by value, not reference or
|
|
|
+// `TokenIndex` objects are designed to be passed by value, not reference or
|
|
|
// pointer. They are also designed to be small and efficient to store in data
|
|
|
// structures.
|
|
|
//
|
|
|
-// `Token` objects from the same `TokenizedBuffer` can be compared with each
|
|
|
-// other, both for being the same token within the buffer, and to establish
|
|
|
+// `TokenIndex` objects from the same `TokenizedBuffer` can be compared with
|
|
|
+// each other, both for being the same token within the buffer, and to establish
|
|
|
// relative position within the token stream that has been lexed out of the
|
|
|
-// buffer. `Token` objects from different `TokenizedBuffer`s cannot be
|
|
|
+// buffer. `TokenIndex` objects from different `TokenizedBuffer`s cannot be
|
|
|
// meaningfully compared.
|
|
|
//
|
|
|
-// All other APIs to query a `Token` are on the `TokenizedBuffer`.
|
|
|
-struct Token : public ComparableIndexBase {
|
|
|
- static const Token Invalid;
|
|
|
+// All other APIs to query a `TokenIndex` are on the `TokenizedBuffer`.
|
|
|
+struct TokenIndex : public ComparableIndexBase {
|
|
|
+ static const TokenIndex Invalid;
|
|
|
// Comments aren't tokenized, so this is the first token after FileStart.
|
|
|
- static const Token FirstNonCommentToken;
|
|
|
+ static const TokenIndex FirstNonCommentToken;
|
|
|
using ComparableIndexBase::ComparableIndexBase;
|
|
|
};
|
|
|
|
|
|
-constexpr Token Token::Invalid(Token::InvalidIndex);
|
|
|
-constexpr Token Token::FirstNonCommentToken(1);
|
|
|
+constexpr TokenIndex TokenIndex::Invalid(TokenIndex::InvalidIndex);
|
|
|
+constexpr TokenIndex TokenIndex::FirstNonCommentToken(1);
|
|
|
|
|
|
// A lightweight handle to a lexed line in a `TokenizedBuffer`.
|
|
|
//
|
|
|
-// `Line` objects are designed to be passed by value, not reference or
|
|
|
+// `LineIndex` objects are designed to be passed by value, not reference or
|
|
|
// pointer. They are also designed to be small and efficient to store in data
|
|
|
// structures.
|
|
|
//
|
|
|
-// Each `Line` object refers to a specific line in the source code that was
|
|
|
+// Each `LineIndex` object refers to a specific line in the source code that was
|
|
|
// lexed. They can be compared directly to establish that they refer to the
|
|
|
// same line or the relative position of different lines within the source.
|
|
|
//
|
|
|
-// All other APIs to query a `Line` are on the `TokenizedBuffer`.
|
|
|
-struct Line : public ComparableIndexBase {
|
|
|
- static const Line Invalid;
|
|
|
+// All other APIs to query a `LineIndex` are on the `TokenizedBuffer`.
|
|
|
+struct LineIndex : public ComparableIndexBase {
|
|
|
+ static const LineIndex Invalid;
|
|
|
using ComparableIndexBase::ComparableIndexBase;
|
|
|
};
|
|
|
|
|
|
-constexpr Line Line::Invalid(Line::InvalidIndex);
|
|
|
+constexpr LineIndex LineIndex::Invalid(LineIndex::InvalidIndex);
|
|
|
|
|
|
// Random-access iterator over tokens within the buffer.
|
|
|
class TokenIterator
|
|
|
- : public llvm::iterator_facade_base<
|
|
|
- TokenIterator, std::random_access_iterator_tag, const Token, int>,
|
|
|
+ : public llvm::iterator_facade_base<TokenIterator,
|
|
|
+ std::random_access_iterator_tag,
|
|
|
+ const TokenIndex, int>,
|
|
|
public Printable<TokenIterator> {
|
|
|
public:
|
|
|
TokenIterator() = delete;
|
|
|
|
|
|
- explicit TokenIterator(Token token) : token_(token) {}
|
|
|
+ explicit TokenIterator(TokenIndex token) : token_(token) {}
|
|
|
|
|
|
auto operator==(const TokenIterator& rhs) const -> bool {
|
|
|
return token_ == rhs.token_;
|
|
|
@@ -84,7 +85,7 @@ class TokenIterator
|
|
|
return token_ < rhs.token_;
|
|
|
}
|
|
|
|
|
|
- auto operator*() const -> const Token& { return token_; }
|
|
|
+ auto operator*() const -> const TokenIndex& { return token_; }
|
|
|
|
|
|
using iterator_facade_base::operator-;
|
|
|
auto operator-(const TokenIterator& rhs) const -> int {
|
|
|
@@ -106,18 +107,19 @@ class TokenIterator
|
|
|
private:
|
|
|
friend class TokenizedBuffer;
|
|
|
|
|
|
- Token token_;
|
|
|
+ TokenIndex token_;
|
|
|
};
|
|
|
|
|
|
// A diagnostic location translator that maps token locations into source
|
|
|
// buffer locations.
|
|
|
-class TokenLocationTranslator : public DiagnosticLocationTranslator<Token> {
|
|
|
+class TokenLocationTranslator
|
|
|
+ : public DiagnosticLocationTranslator<TokenIndex> {
|
|
|
public:
|
|
|
explicit TokenLocationTranslator(const TokenizedBuffer* buffer)
|
|
|
: buffer_(buffer) {}
|
|
|
|
|
|
// Map the given token into a diagnostic location.
|
|
|
- auto GetLocation(Token token) -> DiagnosticLocation override;
|
|
|
+ auto GetLocation(TokenIndex token) -> DiagnosticLocation override;
|
|
|
|
|
|
private:
|
|
|
const TokenizedBuffer* buffer_;
|
|
|
@@ -133,65 +135,65 @@ class TokenLocationTranslator : public DiagnosticLocationTranslator<Token> {
|
|
|
// `HasError` returning true.
|
|
|
class TokenizedBuffer : public Printable<TokenizedBuffer> {
|
|
|
public:
|
|
|
- auto GetKind(Token token) const -> TokenKind;
|
|
|
- auto GetLine(Token token) const -> Line;
|
|
|
+ auto GetKind(TokenIndex token) const -> TokenKind;
|
|
|
+ auto GetLine(TokenIndex token) const -> LineIndex;
|
|
|
|
|
|
// Returns the 1-based line number.
|
|
|
- auto GetLineNumber(Token token) const -> int;
|
|
|
+ auto GetLineNumber(TokenIndex token) const -> int;
|
|
|
|
|
|
// Returns the 1-based column number.
|
|
|
- auto GetColumnNumber(Token token) const -> int;
|
|
|
+ auto GetColumnNumber(TokenIndex token) const -> int;
|
|
|
|
|
|
// Returns the source text lexed into this token.
|
|
|
- auto GetTokenText(Token token) const -> llvm::StringRef;
|
|
|
+ auto GetTokenText(TokenIndex token) const -> llvm::StringRef;
|
|
|
|
|
|
// Returns the identifier associated with this token. The token kind must be
|
|
|
// an `Identifier`.
|
|
|
- auto GetIdentifier(Token token) const -> IdentifierId;
|
|
|
+ auto GetIdentifier(TokenIndex token) const -> IdentifierId;
|
|
|
|
|
|
// Returns the value of an `IntegerLiteral()` token.
|
|
|
- auto GetIntegerLiteral(Token token) const -> IntegerId;
|
|
|
+ auto GetIntegerLiteral(TokenIndex token) const -> IntegerId;
|
|
|
|
|
|
// Returns the value of an `RealLiteral()` token.
|
|
|
- auto GetRealLiteral(Token token) const -> RealId;
|
|
|
+ auto GetRealLiteral(TokenIndex token) const -> RealId;
|
|
|
|
|
|
// Returns the value of a `StringLiteral()` token.
|
|
|
- auto GetStringLiteral(Token token) const -> StringLiteralId;
|
|
|
+ auto GetStringLiteral(TokenIndex token) const -> StringLiteralId;
|
|
|
|
|
|
// Returns the size specified in a `*TypeLiteral()` token.
|
|
|
- auto GetTypeLiteralSize(Token token) const -> const llvm::APInt&;
|
|
|
+ auto GetTypeLiteralSize(TokenIndex token) const -> const llvm::APInt&;
|
|
|
|
|
|
// Returns the closing token matched with the given opening token.
|
|
|
//
|
|
|
// The given token must be an opening token kind.
|
|
|
- auto GetMatchedClosingToken(Token opening_token) const -> Token;
|
|
|
+ auto GetMatchedClosingToken(TokenIndex opening_token) const -> TokenIndex;
|
|
|
|
|
|
// Returns the opening token matched with the given closing token.
|
|
|
//
|
|
|
// The given token must be a closing token kind.
|
|
|
- auto GetMatchedOpeningToken(Token closing_token) const -> Token;
|
|
|
+ auto GetMatchedOpeningToken(TokenIndex closing_token) const -> TokenIndex;
|
|
|
|
|
|
// Returns whether the given token has leading whitespace.
|
|
|
- auto HasLeadingWhitespace(Token token) const -> bool;
|
|
|
+ auto HasLeadingWhitespace(TokenIndex token) const -> bool;
|
|
|
// Returns whether the given token has trailing whitespace.
|
|
|
- auto HasTrailingWhitespace(Token token) const -> bool;
|
|
|
+ auto HasTrailingWhitespace(TokenIndex token) const -> bool;
|
|
|
|
|
|
// Returns whether the token was created as part of an error recovery effort.
|
|
|
//
|
|
|
// For example, a closing paren inserted to match an unmatched paren.
|
|
|
- auto IsRecoveryToken(Token token) const -> bool;
|
|
|
+ auto IsRecoveryToken(TokenIndex token) const -> bool;
|
|
|
|
|
|
// Returns the 1-based line number.
|
|
|
- auto GetLineNumber(Line line) const -> int;
|
|
|
+ auto GetLineNumber(LineIndex line) const -> int;
|
|
|
|
|
|
// Returns the 1-based indentation column number.
|
|
|
- auto GetIndentColumnNumber(Line line) const -> int;
|
|
|
+ auto GetIndentColumnNumber(LineIndex line) const -> int;
|
|
|
|
|
|
// Returns the next line handle.
|
|
|
- auto GetNextLine(Line line) const -> Line;
|
|
|
+ auto GetNextLine(LineIndex line) const -> LineIndex;
|
|
|
|
|
|
// Returns the previous line handle.
|
|
|
- auto GetPrevLine(Line line) const -> Line;
|
|
|
+ auto GetPrevLine(LineIndex line) const -> LineIndex;
|
|
|
|
|
|
// Prints a description of the tokenized stream to the provided `raw_ostream`.
|
|
|
//
|
|
|
@@ -216,14 +218,15 @@ class TokenizedBuffer : public Printable<TokenizedBuffer> {
|
|
|
|
|
|
// Prints a description of a single token. See `Print` for details on the
|
|
|
// format.
|
|
|
- auto PrintToken(llvm::raw_ostream& output_stream, Token token) const -> void;
|
|
|
+ auto PrintToken(llvm::raw_ostream& output_stream, TokenIndex token) const
|
|
|
+ -> void;
|
|
|
|
|
|
// Returns true if the buffer has errors that were detected at lexing time.
|
|
|
auto has_errors() const -> bool { return has_errors_; }
|
|
|
|
|
|
auto tokens() const -> llvm::iterator_range<TokenIterator> {
|
|
|
- return llvm::make_range(TokenIterator(Token(0)),
|
|
|
- TokenIterator(Token(token_infos_.size())));
|
|
|
+ return llvm::make_range(TokenIterator(TokenIndex(0)),
|
|
|
+ TokenIterator(TokenIndex(token_infos_.size())));
|
|
|
}
|
|
|
|
|
|
auto size() const -> int { return token_infos_.size(); }
|
|
|
@@ -277,8 +280,8 @@ class TokenizedBuffer : public Printable<TokenizedBuffer> {
|
|
|
// Whether the token was injected artificially during error recovery.
|
|
|
bool is_recovery = false;
|
|
|
|
|
|
- // Line on which the Token starts.
|
|
|
- Line token_line;
|
|
|
+ // LineIndex on which the TokenIndex starts.
|
|
|
+ LineIndex token_line;
|
|
|
|
|
|
// Zero-based byte offset of the token within its line.
|
|
|
int32_t column;
|
|
|
@@ -286,15 +289,15 @@ class TokenizedBuffer : public Printable<TokenizedBuffer> {
|
|
|
// We may have up to 32 bits of payload, based on the kind of token.
|
|
|
union {
|
|
|
static_assert(
|
|
|
- sizeof(Token) <= sizeof(int32_t),
|
|
|
+ sizeof(TokenIndex) <= sizeof(int32_t),
|
|
|
"Unable to pack token and identifier index into the same space!");
|
|
|
|
|
|
IdentifierId ident_id = IdentifierId::Invalid;
|
|
|
StringLiteralId string_literal_id;
|
|
|
IntegerId integer_id;
|
|
|
RealId real_id;
|
|
|
- Token closing_token;
|
|
|
- Token opening_token;
|
|
|
+ TokenIndex closing_token;
|
|
|
+ TokenIndex opening_token;
|
|
|
int32_t error_length;
|
|
|
};
|
|
|
};
|
|
|
@@ -330,14 +333,14 @@ class TokenizedBuffer : public Printable<TokenizedBuffer> {
|
|
|
SourceBuffer& source)
|
|
|
: value_stores_(&value_stores), source_(&source) {}
|
|
|
|
|
|
- auto GetLineInfo(Line line) -> LineInfo&;
|
|
|
- auto GetLineInfo(Line line) const -> const LineInfo&;
|
|
|
- auto AddLine(LineInfo info) -> Line;
|
|
|
- auto GetTokenInfo(Token token) -> TokenInfo&;
|
|
|
- auto GetTokenInfo(Token token) const -> const TokenInfo&;
|
|
|
- auto AddToken(TokenInfo info) -> Token;
|
|
|
- auto GetTokenPrintWidths(Token token) const -> PrintWidths;
|
|
|
- auto PrintToken(llvm::raw_ostream& output_stream, Token token,
|
|
|
+ auto GetLineInfo(LineIndex line) -> LineInfo&;
|
|
|
+ auto GetLineInfo(LineIndex line) const -> const LineInfo&;
|
|
|
+ auto AddLine(LineInfo info) -> LineIndex;
|
|
|
+ auto GetTokenInfo(TokenIndex token) -> TokenInfo&;
|
|
|
+ auto GetTokenInfo(TokenIndex token) const -> const TokenInfo&;
|
|
|
+ auto AddToken(TokenInfo info) -> TokenIndex;
|
|
|
+ auto GetTokenPrintWidths(TokenIndex token) const -> PrintWidths;
|
|
|
+ auto PrintToken(llvm::raw_ostream& output_stream, TokenIndex token,
|
|
|
PrintWidths widths) const -> void;
|
|
|
|
|
|
// Used to allocate computed string literals.
|
|
|
@@ -366,7 +369,7 @@ class TokenizedBuffer : public Printable<TokenizedBuffer> {
|
|
|
using LexerDiagnosticEmitter = DiagnosticEmitter<const char*>;
|
|
|
|
|
|
// A diagnostic emitter that uses tokens as its source of location information.
|
|
|
-using TokenDiagnosticEmitter = DiagnosticEmitter<Token>;
|
|
|
+using TokenDiagnosticEmitter = DiagnosticEmitter<TokenIndex>;
|
|
|
|
|
|
} // namespace Carbon::Lex
|
|
|
|