mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-27 07:11:13 +00:00
Previously, the code would try each form of lexing and let that sub-lexing routine reject the code. This was very branch heavy and also hard to optimize -- lots of hard to inline function calls, etc. However, it's really nice to keep the different categories of lexing broken out into their own functions rather than flattening this into a huge state machine. So this creates a miniature explicit state machine by building a table of function pointers that wrap the methods on the lexer. The main lexing loop simply indexes this table with the first byte of the source code, and calls the dispatch function pointer returned. The result is that the main lex loop is *incredibly* tight code, and the only time spent appears to be stalls waiting on memory for the next byte. =] As part of this, optimize symbol lexing specifically by recognizing all the symbols that are exactly one character -- IE, we don't even need to look at the *next* character, there is no max-munch or anything else. For these, we pre-detect the exact token kind and hand that into the symbol lexing routine to avoid re-computing it. The symbols in this category are really frequent symbols in practice like `,` and `;`, so this seems likely worthwhile in practice. The one-byte-dispatch should also be reasonably extendable in the future. For example, I suspect this is the likely hot-path for non-ASCII lexing, where we see the UTF-8 marker at the start of a token and most (if not all) of the token is non-ASCII. We can use this table to dispatch immediately to an optimized routine dedicated to UTF-8 processing, without any slowdown for other inputs. The benchmark results are best for keyword lexing because that is the fastest thing to lex -- it goes form 25 mt/s to 30 mt/s. Other improvements are less dramatic, but I think this is still worthwhile because it gives a really strong basis for both expanded functionality without performance hit and further optimizations. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
124 lines
4.1 KiB
C++
124 lines
4.1 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef CARBON_TOOLCHAIN_LEXER_TOKEN_KIND_H_
|
|
#define CARBON_TOOLCHAIN_LEXER_TOKEN_KIND_H_
|
|
|
|
#include <cstdint>
|
|
|
|
#include "common/enum_base.h"
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "llvm/Support/FormatVariadicDetails.h"
|
|
|
|
namespace Carbon {
|
|
|
|
CARBON_DEFINE_RAW_ENUM_CLASS(TokenKind, uint8_t) {
|
|
#define CARBON_TOKEN(TokenName) CARBON_RAW_ENUM_ENUMERATOR(TokenName)
|
|
#include "toolchain/lexer/token_kind.def"
|
|
};
|
|
|
|
class TokenKind : public CARBON_ENUM_BASE(TokenKind) {
|
|
public:
|
|
#define CARBON_TOKEN(TokenName) CARBON_ENUM_CONSTANT_DECLARATION(TokenName)
|
|
#include "toolchain/lexer/token_kind.def"
|
|
|
|
// An array of all the keyword tokens.
|
|
static const llvm::ArrayRef<TokenKind> KeywordTokens;
|
|
|
|
// Test whether this kind of token is a simple symbol sequence (punctuation,
|
|
// not letters) that appears directly in the source text and can be
|
|
// unambiguously lexed with `starts_with` logic. While these may appear
|
|
// inside of other tokens, outside of the contents of other tokens they
|
|
// don't require any specific characters before or after to distinguish them
|
|
// in the source. Returns false otherwise.
|
|
[[nodiscard]] auto is_symbol() const -> bool;
|
|
|
|
// Test whether this kind of token is a grouping symbol (part of an opening
|
|
// and closing pair that must always be matched in the token stream).
|
|
[[nodiscard]] auto is_grouping_symbol() const -> bool;
|
|
|
|
// Test whether this kind of token is an opening symbol for a group.
|
|
[[nodiscard]] auto is_opening_symbol() const -> bool;
|
|
|
|
// Returns the associated closing symbol for an opening symbol.
|
|
//
|
|
// The token kind must be an opening symbol.
|
|
[[nodiscard]] auto closing_symbol() const -> TokenKind;
|
|
|
|
// Test whether this kind of token is a closing symbol for a group.
|
|
[[nodiscard]] auto is_closing_symbol() const -> bool;
|
|
|
|
// Returns the associated opening symbol for a closing symbol.
|
|
//
|
|
// The token kind must be a closing symbol.
|
|
[[nodiscard]] auto opening_symbol() const -> TokenKind;
|
|
|
|
// Test whether this kind of token is a one-character symbol whose character
|
|
// is not part of any other symbol.
|
|
[[nodiscard]] auto is_one_char_symbol() const -> bool;
|
|
|
|
// Test whether this kind of token is a keyword.
|
|
[[nodiscard]] auto is_keyword() const -> bool;
|
|
|
|
// Test whether this kind of token is a sized type literal.
|
|
[[nodiscard]] auto is_sized_type_literal() const -> bool;
|
|
|
|
// If this token kind has a fixed spelling when in source code, returns it.
|
|
// Otherwise returns an empty string.
|
|
[[nodiscard]] auto fixed_spelling() const -> llvm::StringRef;
|
|
|
|
// Get the expected number of parse tree nodes that will be created for this
|
|
// token.
|
|
[[nodiscard]] auto expected_parse_tree_size() const -> int;
|
|
|
|
// Test whether this token kind is in the provided list.
|
|
[[nodiscard]] auto IsOneOf(std::initializer_list<TokenKind> kinds) const
|
|
-> bool {
|
|
for (TokenKind kind : kinds) {
|
|
if (*this == kind) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private:
|
|
static const TokenKind KeywordTokensStorage[];
|
|
};
|
|
|
|
#define CARBON_TOKEN(TokenName) \
|
|
CARBON_ENUM_CONSTANT_DEFINITION(TokenKind, TokenName)
|
|
#include "toolchain/lexer/token_kind.def"
|
|
|
|
constexpr TokenKind TokenKind::KeywordTokensStorage[] = {
|
|
#define CARBON_KEYWORD_TOKEN(TokenName, Spelling) TokenKind::TokenName,
|
|
#include "toolchain/lexer/token_kind.def"
|
|
};
|
|
constexpr llvm::ArrayRef<TokenKind> TokenKind::KeywordTokens =
|
|
KeywordTokensStorage;
|
|
|
|
} // namespace Carbon
|
|
|
|
namespace llvm {
|
|
|
|
// We use formatv primarily for diagnostics. In these cases, it's expected that
|
|
// the spelling in source code should be used.
|
|
template <>
|
|
struct format_provider<Carbon::TokenKind> {
|
|
static void format(const Carbon::TokenKind& kind, raw_ostream& out,
|
|
StringRef /*style*/) {
|
|
auto spelling = kind.fixed_spelling();
|
|
if (!spelling.empty()) {
|
|
out << spelling;
|
|
} else {
|
|
// Default to the name if there's no fixed spelling.
|
|
out << kind;
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace llvm
|
|
|
|
#endif // CARBON_TOOLCHAIN_LEXER_TOKEN_KIND_H_
|