token_kind.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_LEXER_TOKEN_KIND_H_
  5. #define CARBON_TOOLCHAIN_LEXER_TOKEN_KIND_H_
  6. #include <cstdint>
  7. #include <initializer_list>
  8. #include <iterator>
  9. #include "common/ostream.h"
  10. #include "llvm/ADT/StringRef.h"
  11. namespace Carbon {
  12. class TokenKind {
  13. // Note that this must be declared earlier in the class so that its type can
  14. // be used, for example in the conversion operator.
  15. enum class KindEnum : int8_t {
  16. #define CARBON_TOKEN(TokenName) TokenName,
  17. #include "toolchain/lexer/token_registry.def"
  18. };
  19. public:
  20. // The formatting for this macro is weird due to a `clang-format` bug. See
  21. // https://bugs.llvm.org/show_bug.cgi?id=48320 for details.
  22. #define CARBON_TOKEN(TokenName) \
  23. static constexpr auto TokenName()->TokenKind { \
  24. return TokenKind(KindEnum::TokenName); \
  25. }
  26. #include "toolchain/lexer/token_registry.def"
  27. // The default constructor is deleted as objects of this type should always be
  28. // constructed using the above factory functions for each unique kind.
  29. TokenKind() = delete;
  30. friend auto operator==(TokenKind lhs, TokenKind rhs) -> bool {
  31. return lhs.kind_value_ == rhs.kind_value_;
  32. }
  33. friend auto operator!=(TokenKind lhs, TokenKind rhs) -> bool {
  34. return lhs.kind_value_ != rhs.kind_value_;
  35. }
  36. // Get a friendly name for the token for logging or debugging.
  37. [[nodiscard]] auto Name() const -> llvm::StringRef;
  38. // Test whether this kind of token is a simple symbol sequence (punctuation,
  39. // not letters) that appears directly in the source text and can be
  40. // unambiguously lexed with `starts_with` logic. While these may appear
  41. // inside of other tokens, outside of the contents of other tokens they
  42. // don't require any specific characters before or after to distinguish them
  43. // in the source. Returns false otherwise.
  44. [[nodiscard]] auto IsSymbol() const -> bool;
  45. // Test whether this kind of token is a grouping symbol (part of an opening
  46. // and closing pair that must always be matched in the token stream).
  47. [[nodiscard]] auto IsGroupingSymbol() const -> bool;
  48. // Test whether this kind of token is an opening symbol for a group.
  49. [[nodiscard]] auto IsOpeningSymbol() const -> bool;
  50. // Returns the associated closing symbol for an opening symbol.
  51. //
  52. // The token kind must be an opening symbol.
  53. [[nodiscard]] auto GetClosingSymbol() const -> TokenKind;
  54. // Test whether this kind of token is a closing symbol for a group.
  55. [[nodiscard]] auto IsClosingSymbol() const -> bool;
  56. // Returns the associated opening symbol for a closing symbol.
  57. //
  58. // The token kind must be a closing symbol.
  59. [[nodiscard]] auto GetOpeningSymbol() const -> TokenKind;
  60. // Test whether this kind of token is a keyword.
  61. [[nodiscard]] auto IsKeyword() const -> bool;
  62. // Test whether this kind of token is a sized type literal.
  63. [[nodiscard]] auto IsSizedTypeLiteral() const -> bool;
  64. // If this token kind has a fixed spelling when in source code, returns it.
  65. // Otherwise returns an empty string.
  66. [[nodiscard]] auto GetFixedSpelling() const -> llvm::StringRef;
  67. // Test whether this token kind is in the provided list.
  68. [[nodiscard]] auto IsOneOf(std::initializer_list<TokenKind> kinds) const
  69. -> bool {
  70. for (TokenKind kind : kinds) {
  71. if (*this == kind) {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. // Enable conversion to our private enum, including in a `constexpr` context,
  78. // to enable usage in `switch` and `case`. The enum remains private and
  79. // nothing else should be using this.
  80. // NOLINTNEXTLINE(google-explicit-constructor)
  81. constexpr operator KindEnum() const { return kind_value_; }
  82. // Prints the TokenKind, typically for diagnostics.
  83. void Print(llvm::raw_ostream& out) const { out << GetFixedSpelling(); }
  84. private:
  85. constexpr explicit TokenKind(KindEnum kind_value) : kind_value_(kind_value) {}
  86. KindEnum kind_value_;
  87. };
  88. } // namespace Carbon
  89. #endif // CARBON_TOOLCHAIN_LEXER_TOKEN_KIND_H_